| OLD | NEW |
| 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 Loading... |
| 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_LINUX) | 90 #if defined(OS_IOS) || defined(OS_WIN) || defined(OS_LINUX) |
| 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. |
| 97 // Disabled on Linux because failing Linux Valgrind bot, and Valgrind exclusions | 98 // Disabled on Linux because failing Linux Valgrind bot, and Valgrind exclusions |
| 98 // are not currently read. See http://crbug.com/582398 | 99 // are not currently read. See http://crbug.com/582398 |
| 99 TEST(SecurityTest, MAYBE_NewOverflow) { | 100 TEST(SecurityTest, MAYBE_NewOverflow) { |
| 100 const size_t kArraySize = 4096; | 101 const size_t kArraySize = 4096; |
| 101 // We want something "dynamic" here, so that the compiler doesn't | 102 // We want something "dynamic" here, so that the compiler doesn't |
| 102 // immediately reject crazy arrays. | 103 // immediately reject crazy arrays. |
| 103 const size_t kDynamicArraySize = HideValueFromCompiler(kArraySize); | 104 const size_t kDynamicArraySize = HideValueFromCompiler(kArraySize); |
| 104 const size_t kMaxSizeT = std::numeric_limits<size_t>::max(); | 105 // numeric_limits are still not constexpr until we switch to C++11, so we |
| 106 // use an ugly cast. |
| 107 const size_t kMaxSizeT = ~static_cast<size_t>(0); |
| 108 ASSERT_EQ(numeric_limits<size_t>::max(), kMaxSizeT); |
| 105 const size_t kArraySize2 = kMaxSizeT / kArraySize + 10; | 109 const size_t kArraySize2 = kMaxSizeT / kArraySize + 10; |
| 106 const size_t kDynamicArraySize2 = HideValueFromCompiler(kArraySize2); | 110 const size_t kDynamicArraySize2 = HideValueFromCompiler(kArraySize2); |
| 107 { | 111 { |
| 108 std::unique_ptr<char[][kArraySize]> array_pointer( | 112 std::unique_ptr<char[][kArraySize]> array_pointer( |
| 109 new (nothrow) char[kDynamicArraySize2][kArraySize]); | 113 new (nothrow) char[kDynamicArraySize2][kArraySize]); |
| 110 // Prevent clang from optimizing away the whole test. | 114 OverflowTestsSoftExpectTrue(!array_pointer); |
| 111 char* volatile p = reinterpret_cast<char*>(array_pointer.get()); | |
| 112 OverflowTestsSoftExpectTrue(!p); | |
| 113 } | 115 } |
| 114 // On windows, the compiler prevents static array sizes of more than | 116 // On windows, the compiler prevents static array sizes of more than |
| 115 // 0x7fffffff (error C2148). | 117 // 0x7fffffff (error C2148). |
| 116 #if defined(OS_WIN) && defined(ARCH_CPU_64_BITS) | 118 #if defined(OS_WIN) && defined(ARCH_CPU_64_BITS) |
| 117 ALLOW_UNUSED_LOCAL(kDynamicArraySize); | 119 ALLOW_UNUSED_LOCAL(kDynamicArraySize); |
| 118 #else | 120 #else |
| 119 { | 121 { |
| 120 std::unique_ptr<char[][kArraySize2]> array_pointer( | 122 std::unique_ptr<char[][kArraySize2]> array_pointer( |
| 121 new (nothrow) char[kDynamicArraySize][kArraySize2]); | 123 new (nothrow) char[kDynamicArraySize][kArraySize2]); |
| 122 // Prevent clang from optimizing away the whole test. | 124 OverflowTestsSoftExpectTrue(!array_pointer); |
| 123 char* volatile p = reinterpret_cast<char*>(array_pointer.get()); | |
| 124 OverflowTestsSoftExpectTrue(!p); | |
| 125 } | 125 } |
| 126 #endif // !defined(OS_WIN) || !defined(ARCH_CPU_64_BITS) | 126 #endif // !defined(OS_WIN) || !defined(ARCH_CPU_64_BITS) |
| 127 } | 127 } |
| 128 | 128 |
| 129 #if defined(OS_LINUX) && defined(__x86_64__) | 129 #if defined(OS_LINUX) && defined(__x86_64__) |
| 130 // Check if ptr1 and ptr2 are separated by less than size chars. | 130 // Check if ptr1 and ptr2 are separated by less than size chars. |
| 131 bool ArePointersToSameArea(void* ptr1, void* ptr2, size_t size) { | 131 bool ArePointersToSameArea(void* ptr1, void* ptr2, size_t size) { |
| 132 ptrdiff_t ptr_diff = reinterpret_cast<char*>(std::max(ptr1, ptr2)) - | 132 ptrdiff_t ptr_diff = reinterpret_cast<char*>(std::max(ptr1, ptr2)) - |
| 133 reinterpret_cast<char*>(std::min(ptr1, ptr2)); | 133 reinterpret_cast<char*>(std::min(ptr1, ptr2)); |
| 134 return static_cast<size_t>(ptr_diff) <= size; | 134 return static_cast<size_t>(ptr_diff) <= size; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 // kRandomMask, so we use it as an additional detection mechanism. | 178 // kRandomMask, so we use it as an additional detection mechanism. |
| 179 const uintptr_t kRandomMask = 0x3fffffffffffULL; | 179 const uintptr_t kRandomMask = 0x3fffffffffffULL; |
| 180 bool impossible_random_address = | 180 bool impossible_random_address = |
| 181 reinterpret_cast<uintptr_t>(ptr.get()) & ~kRandomMask; | 181 reinterpret_cast<uintptr_t>(ptr.get()) & ~kRandomMask; |
| 182 EXPECT_FALSE(impossible_random_address); | 182 EXPECT_FALSE(impossible_random_address); |
| 183 } | 183 } |
| 184 | 184 |
| 185 #endif // defined(OS_LINUX) && defined(__x86_64__) | 185 #endif // defined(OS_LINUX) && defined(__x86_64__) |
| 186 | 186 |
| 187 } // namespace | 187 } // namespace |
| OLD | NEW |