Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(32)

Side by Side Diff: base/security_unittest.cc

Issue 2692313002: Reland of nable SecurityTest.NewOverflow on Windows, with clang fix (Closed)
Patch Set: no asan Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <fcntl.h> 5 #include <fcntl.h>
6 #include <stddef.h> 6 #include <stddef.h>
7 #include <stdio.h> 7 #include <stdio.h>
8 #include <stdlib.h> 8 #include <stdlib.h>
9 #include <string.h> 9 #include <string.h>
10 #include <sys/stat.h> 10 #include <sys/stat.h>
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 printf("Platform has overflow: %s\n", 80 printf("Platform has overflow: %s\n",
81 !overflow_detected ? "yes." : "no."); 81 !overflow_detected ? "yes." : "no.");
82 #else 82 #else
83 // Otherwise, fail the test. (Note: EXPECT are ok in subfunctions, ASSERT 83 // Otherwise, fail the test. (Note: EXPECT are ok in subfunctions, ASSERT
84 // aren't). 84 // aren't).
85 EXPECT_TRUE(overflow_detected); 85 EXPECT_TRUE(overflow_detected);
86 #endif 86 #endif
87 } 87 }
88 } 88 }
89 89
90 #if defined(OS_IOS) || defined(OS_WIN) || defined(OS_LINUX) 90 #if defined(OS_IOS) || defined(OS_LINUX) || defined(ADDRESS_SANITIZER)
91 #define MAYBE_NewOverflow DISABLED_NewOverflow 91 #define MAYBE_NewOverflow DISABLED_NewOverflow
92 #else 92 #else
93 #define MAYBE_NewOverflow NewOverflow 93 #define MAYBE_NewOverflow NewOverflow
94 #endif 94 #endif
95 // Test array[TooBig][X] and array[X][TooBig] allocations for int overflows. 95 // Test array[TooBig][X] and array[X][TooBig] allocations for int overflows.
96 // IOS doesn't honor nothrow, so disable the test there. 96 // IOS doesn't honor nothrow, so disable the test there.
97 // Crashes on Windows Dbg builds, disable there as well.
98 // Disabled on Linux because failing Linux Valgrind bot, and Valgrind exclusions 97 // Disabled on Linux because failing Linux Valgrind bot, and Valgrind exclusions
99 // are not currently read. See http://crbug.com/582398 98 // are not currently read. See http://crbug.com/582398
99 // Disabled under ASan because asan aborts when new returns nullptr,
100 // https://bugs.chromium.org/p/chromium/issues/detail?id=690271#c15
100 TEST(SecurityTest, MAYBE_NewOverflow) { 101 TEST(SecurityTest, MAYBE_NewOverflow) {
101 const size_t kArraySize = 4096; 102 const size_t kArraySize = 4096;
102 // We want something "dynamic" here, so that the compiler doesn't 103 // We want something "dynamic" here, so that the compiler doesn't
103 // immediately reject crazy arrays. 104 // immediately reject crazy arrays.
104 const size_t kDynamicArraySize = HideValueFromCompiler(kArraySize); 105 const size_t kDynamicArraySize = HideValueFromCompiler(kArraySize);
105 // numeric_limits are still not constexpr until we switch to C++11, so we 106 const size_t kMaxSizeT = std::numeric_limits<size_t>::max();
106 // use an ugly cast.
107 const size_t kMaxSizeT = ~static_cast<size_t>(0);
108 ASSERT_EQ(numeric_limits<size_t>::max(), kMaxSizeT);
109 const size_t kArraySize2 = kMaxSizeT / kArraySize + 10; 107 const size_t kArraySize2 = kMaxSizeT / kArraySize + 10;
110 const size_t kDynamicArraySize2 = HideValueFromCompiler(kArraySize2); 108 const size_t kDynamicArraySize2 = HideValueFromCompiler(kArraySize2);
111 { 109 {
112 std::unique_ptr<char[][kArraySize]> array_pointer( 110 std::unique_ptr<char[][kArraySize]> array_pointer(
113 new (nothrow) char[kDynamicArraySize2][kArraySize]); 111 new (nothrow) char[kDynamicArraySize2][kArraySize]);
114 OverflowTestsSoftExpectTrue(!array_pointer); 112 // Prevent clang from optimizing away the whole test.
113 char* volatile p = reinterpret_cast<char*>(array_pointer.get());
114 OverflowTestsSoftExpectTrue(!p);
115 } 115 }
116 // On windows, the compiler prevents static array sizes of more than 116 // On windows, the compiler prevents static array sizes of more than
117 // 0x7fffffff (error C2148). 117 // 0x7fffffff (error C2148).
118 #if defined(OS_WIN) && defined(ARCH_CPU_64_BITS) 118 #if defined(OS_WIN) && defined(ARCH_CPU_64_BITS)
119 ALLOW_UNUSED_LOCAL(kDynamicArraySize); 119 ALLOW_UNUSED_LOCAL(kDynamicArraySize);
120 #else 120 #else
121 { 121 {
122 std::unique_ptr<char[][kArraySize2]> array_pointer( 122 std::unique_ptr<char[][kArraySize2]> array_pointer(
123 new (nothrow) char[kDynamicArraySize][kArraySize2]); 123 new (nothrow) char[kDynamicArraySize][kArraySize2]);
124 OverflowTestsSoftExpectTrue(!array_pointer); 124 // Prevent clang from optimizing away the whole test.
125 char* volatile p = reinterpret_cast<char*>(array_pointer.get());
126 OverflowTestsSoftExpectTrue(!p);
125 } 127 }
126 #endif // !defined(OS_WIN) || !defined(ARCH_CPU_64_BITS) 128 #endif // !defined(OS_WIN) || !defined(ARCH_CPU_64_BITS)
127 } 129 }
128 130
129 #if defined(OS_LINUX) && defined(__x86_64__) 131 #if defined(OS_LINUX) && defined(__x86_64__)
130 // Check if ptr1 and ptr2 are separated by less than size chars. 132 // Check if ptr1 and ptr2 are separated by less than size chars.
131 bool ArePointersToSameArea(void* ptr1, void* ptr2, size_t size) { 133 bool ArePointersToSameArea(void* ptr1, void* ptr2, size_t size) {
132 ptrdiff_t ptr_diff = reinterpret_cast<char*>(std::max(ptr1, ptr2)) - 134 ptrdiff_t ptr_diff = reinterpret_cast<char*>(std::max(ptr1, ptr2)) -
133 reinterpret_cast<char*>(std::min(ptr1, ptr2)); 135 reinterpret_cast<char*>(std::min(ptr1, ptr2));
134 return static_cast<size_t>(ptr_diff) <= size; 136 return static_cast<size_t>(ptr_diff) <= size;
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 // kRandomMask, so we use it as an additional detection mechanism. 180 // kRandomMask, so we use it as an additional detection mechanism.
179 const uintptr_t kRandomMask = 0x3fffffffffffULL; 181 const uintptr_t kRandomMask = 0x3fffffffffffULL;
180 bool impossible_random_address = 182 bool impossible_random_address =
181 reinterpret_cast<uintptr_t>(ptr.get()) & ~kRandomMask; 183 reinterpret_cast<uintptr_t>(ptr.get()) & ~kRandomMask;
182 EXPECT_FALSE(impossible_random_address); 184 EXPECT_FALSE(impossible_random_address);
183 } 185 }
184 186
185 #endif // defined(OS_LINUX) && defined(__x86_64__) 187 #endif // defined(OS_LINUX) && defined(__x86_64__)
186 188
187 } // namespace 189 } // namespace
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698