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> |
11 #include <sys/types.h> | 11 #include <sys/types.h> |
12 | 12 |
13 #include <algorithm> | 13 #include <algorithm> |
14 #include <limits> | 14 #include <limits> |
| 15 #include <memory> |
15 | 16 |
16 #include "base/files/file_util.h" | 17 #include "base/files/file_util.h" |
17 #include "base/logging.h" | 18 #include "base/logging.h" |
18 #include "base/memory/free_deleter.h" | 19 #include "base/memory/free_deleter.h" |
19 #include "base/memory/scoped_ptr.h" | |
20 #include "build/build_config.h" | 20 #include "build/build_config.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
22 | 22 |
23 #if defined(OS_POSIX) | 23 #if defined(OS_POSIX) |
24 #include <sys/mman.h> | 24 #include <sys/mman.h> |
25 #include <unistd.h> | 25 #include <unistd.h> |
26 #endif | 26 #endif |
27 | 27 |
28 using std::nothrow; | 28 using std::nothrow; |
29 using std::numeric_limits; | 29 using std::numeric_limits; |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 // We want something "dynamic" here, so that the compiler doesn't | 103 // We want something "dynamic" here, so that the compiler doesn't |
104 // immediately reject crazy arrays. | 104 // immediately reject crazy arrays. |
105 const size_t kDynamicArraySize = HideValueFromCompiler(kArraySize); | 105 const size_t kDynamicArraySize = HideValueFromCompiler(kArraySize); |
106 // numeric_limits are still not constexpr until we switch to C++11, so we | 106 // numeric_limits are still not constexpr until we switch to C++11, so we |
107 // use an ugly cast. | 107 // use an ugly cast. |
108 const size_t kMaxSizeT = ~static_cast<size_t>(0); | 108 const size_t kMaxSizeT = ~static_cast<size_t>(0); |
109 ASSERT_EQ(numeric_limits<size_t>::max(), kMaxSizeT); | 109 ASSERT_EQ(numeric_limits<size_t>::max(), kMaxSizeT); |
110 const size_t kArraySize2 = kMaxSizeT / kArraySize + 10; | 110 const size_t kArraySize2 = kMaxSizeT / kArraySize + 10; |
111 const size_t kDynamicArraySize2 = HideValueFromCompiler(kArraySize2); | 111 const size_t kDynamicArraySize2 = HideValueFromCompiler(kArraySize2); |
112 { | 112 { |
113 scoped_ptr<char[][kArraySize]> array_pointer(new (nothrow) | 113 std::unique_ptr<char[][kArraySize]> array_pointer( |
114 char[kDynamicArraySize2][kArraySize]); | 114 new (nothrow) char[kDynamicArraySize2][kArraySize]); |
115 OverflowTestsSoftExpectTrue(!array_pointer); | 115 OverflowTestsSoftExpectTrue(!array_pointer); |
116 } | 116 } |
117 // On windows, the compiler prevents static array sizes of more than | 117 // On windows, the compiler prevents static array sizes of more than |
118 // 0x7fffffff (error C2148). | 118 // 0x7fffffff (error C2148). |
119 #if defined(OS_WIN) && defined(ARCH_CPU_64_BITS) | 119 #if defined(OS_WIN) && defined(ARCH_CPU_64_BITS) |
120 ALLOW_UNUSED_LOCAL(kDynamicArraySize); | 120 ALLOW_UNUSED_LOCAL(kDynamicArraySize); |
121 #else | 121 #else |
122 { | 122 { |
123 scoped_ptr<char[][kArraySize2]> array_pointer(new (nothrow) | 123 std::unique_ptr<char[][kArraySize2]> array_pointer( |
124 char[kDynamicArraySize][kArraySize2]); | 124 new (nothrow) char[kDynamicArraySize][kArraySize2]); |
125 OverflowTestsSoftExpectTrue(!array_pointer); | 125 OverflowTestsSoftExpectTrue(!array_pointer); |
126 } | 126 } |
127 #endif // !defined(OS_WIN) || !defined(ARCH_CPU_64_BITS) | 127 #endif // !defined(OS_WIN) || !defined(ARCH_CPU_64_BITS) |
128 } | 128 } |
129 | 129 |
130 #if defined(OS_LINUX) && defined(__x86_64__) | 130 #if defined(OS_LINUX) && defined(__x86_64__) |
131 // Check if ptr1 and ptr2 are separated by less than size chars. | 131 // Check if ptr1 and ptr2 are separated by less than size chars. |
132 bool ArePointersToSameArea(void* ptr1, void* ptr2, size_t size) { | 132 bool ArePointersToSameArea(void* ptr1, void* ptr2, size_t size) { |
133 ptrdiff_t ptr_diff = reinterpret_cast<char*>(std::max(ptr1, ptr2)) - | 133 ptrdiff_t ptr_diff = reinterpret_cast<char*>(std::max(ptr1, ptr2)) - |
134 reinterpret_cast<char*>(std::min(ptr1, ptr2)); | 134 reinterpret_cast<char*>(std::min(ptr1, ptr2)); |
(...skipping 15 matching lines...) Expand all Loading... |
150 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); | 150 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
151 ASSERT_NE(default_mmap_heap_address, | 151 ASSERT_NE(default_mmap_heap_address, |
152 static_cast<void*>(MAP_FAILED)); | 152 static_cast<void*>(MAP_FAILED)); |
153 ASSERT_EQ(munmap(default_mmap_heap_address, kPageSize), 0); | 153 ASSERT_EQ(munmap(default_mmap_heap_address, kPageSize), 0); |
154 void* brk_heap_address = sbrk(0); | 154 void* brk_heap_address = sbrk(0); |
155 ASSERT_NE(brk_heap_address, reinterpret_cast<void*>(-1)); | 155 ASSERT_NE(brk_heap_address, reinterpret_cast<void*>(-1)); |
156 ASSERT_TRUE(brk_heap_address != NULL); | 156 ASSERT_TRUE(brk_heap_address != NULL); |
157 // 1 MB should get us past what TCMalloc pre-allocated before initializing | 157 // 1 MB should get us past what TCMalloc pre-allocated before initializing |
158 // the sophisticated allocators. | 158 // the sophisticated allocators. |
159 size_t kAllocSize = 1<<20; | 159 size_t kAllocSize = 1<<20; |
160 scoped_ptr<char, base::FreeDeleter> ptr( | 160 std::unique_ptr<char, base::FreeDeleter> ptr( |
161 static_cast<char*>(malloc(kAllocSize))); | 161 static_cast<char*>(malloc(kAllocSize))); |
162 ASSERT_TRUE(ptr != NULL); | 162 ASSERT_TRUE(ptr != NULL); |
163 // If two pointers are separated by less than 512MB, they are considered | 163 // If two pointers are separated by less than 512MB, they are considered |
164 // to be in the same area. | 164 // to be in the same area. |
165 // Our random pointer could be anywhere within 0x3fffffffffff (46bits), | 165 // Our random pointer could be anywhere within 0x3fffffffffff (46bits), |
166 // and we are checking that it's not withing 1GB (30 bits) from two | 166 // and we are checking that it's not withing 1GB (30 bits) from two |
167 // addresses (brk and mmap heap). We have roughly one chance out of | 167 // addresses (brk and mmap heap). We have roughly one chance out of |
168 // 2^15 to flake. | 168 // 2^15 to flake. |
169 const size_t kAreaRadius = 1<<29; | 169 const size_t kAreaRadius = 1<<29; |
170 bool in_default_mmap_heap = ArePointersToSameArea( | 170 bool in_default_mmap_heap = ArePointersToSameArea( |
171 ptr.get(), default_mmap_heap_address, kAreaRadius); | 171 ptr.get(), default_mmap_heap_address, kAreaRadius); |
172 EXPECT_FALSE(in_default_mmap_heap); | 172 EXPECT_FALSE(in_default_mmap_heap); |
173 | 173 |
174 bool in_default_brk_heap = ArePointersToSameArea( | 174 bool in_default_brk_heap = ArePointersToSameArea( |
175 ptr.get(), brk_heap_address, kAreaRadius); | 175 ptr.get(), brk_heap_address, kAreaRadius); |
176 EXPECT_FALSE(in_default_brk_heap); | 176 EXPECT_FALSE(in_default_brk_heap); |
177 | 177 |
178 // In the implementation, we always mask our random addresses with | 178 // In the implementation, we always mask our random addresses with |
179 // kRandomMask, so we use it as an additional detection mechanism. | 179 // kRandomMask, so we use it as an additional detection mechanism. |
180 const uintptr_t kRandomMask = 0x3fffffffffffULL; | 180 const uintptr_t kRandomMask = 0x3fffffffffffULL; |
181 bool impossible_random_address = | 181 bool impossible_random_address = |
182 reinterpret_cast<uintptr_t>(ptr.get()) & ~kRandomMask; | 182 reinterpret_cast<uintptr_t>(ptr.get()) & ~kRandomMask; |
183 EXPECT_FALSE(impossible_random_address); | 183 EXPECT_FALSE(impossible_random_address); |
184 } | 184 } |
185 | 185 |
186 #endif // defined(OS_LINUX) && defined(__x86_64__) | 186 #endif // defined(OS_LINUX) && defined(__x86_64__) |
187 | 187 |
188 } // namespace | 188 } // namespace |
OLD | NEW |