OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #define _CRT_SECURE_NO_WARNINGS | 5 #define _CRT_SECURE_NO_WARNINGS |
6 | 6 |
7 #include "base/process/memory.h" | 7 #include "base/process/memory.h" |
8 | 8 |
9 #include <stddef.h> | 9 #include <stddef.h> |
10 | 10 |
11 #include <limits> | 11 #include <limits> |
12 | 12 |
13 #include "base/allocator/allocator_check.h" | |
14 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
15 #include "base/debug/alias.h" | 14 #include "base/debug/alias.h" |
16 #include "base/memory/aligned_memory.h" | |
17 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
18 #include "build/build_config.h" | 16 #include "build/build_config.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
20 | 18 |
21 #if defined(OS_WIN) | 19 #if defined(OS_WIN) |
22 #include <windows.h> | 20 #include <windows.h> |
23 #endif | 21 #endif |
24 #if defined(OS_POSIX) | 22 #if defined(OS_POSIX) |
25 #include <errno.h> | 23 #include <errno.h> |
26 #endif | 24 #endif |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 // heap corruption. | 132 // heap corruption. |
135 ASSERT_DEATH(free(buf), "attempting free on address which " | 133 ASSERT_DEATH(free(buf), "attempting free on address which " |
136 "was not malloc\\(\\)-ed"); | 134 "was not malloc\\(\\)-ed"); |
137 #else | 135 #else |
138 ADD_FAILURE() << "This test is not supported in this build configuration."; | 136 ADD_FAILURE() << "This test is not supported in this build configuration."; |
139 #endif | 137 #endif |
140 } | 138 } |
141 | 139 |
142 #endif // defined(OS_MACOSX) | 140 #endif // defined(OS_MACOSX) |
143 | 141 |
144 TEST(MemoryTest, AllocatorShimWorking) { | |
145 ASSERT_TRUE(base::allocator::IsAllocatorInitialized()); | |
146 } | |
147 | |
148 // Android doesn't implement set_new_handler, so we can't use the | 142 // Android doesn't implement set_new_handler, so we can't use the |
149 // OutOfMemoryTest cases. OpenBSD does not support these tests either. | 143 // OutOfMemoryTest cases. OpenBSD does not support these tests either. |
150 // Don't test these on ASan/TSan/MSan configurations: only test the real | 144 // Don't test these on ASan/TSan/MSan configurations: only test the real |
151 // allocator. | 145 // allocator. |
152 // Windows only supports these tests with the allocator shim in place. | 146 // Windows only supports these tests with the allocator shim in place. |
153 #if !defined(OS_ANDROID) && !defined(OS_OPENBSD) && \ | 147 #if !defined(OS_ANDROID) && !defined(OS_OPENBSD) && \ |
154 !(defined(OS_WIN) && !defined(ALLOCATOR_SHIM)) && \ | 148 !(defined(OS_WIN) && !defined(ALLOCATOR_SHIM)) && \ |
155 !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | 149 !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
156 | 150 |
157 namespace { | 151 namespace { |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 }, kOomRegex); | 212 }, kOomRegex); |
219 } | 213 } |
220 | 214 |
221 TEST_F(OutOfMemoryDeathTest, Calloc) { | 215 TEST_F(OutOfMemoryDeathTest, Calloc) { |
222 ASSERT_DEATH({ | 216 ASSERT_DEATH({ |
223 SetUpInDeathAssert(); | 217 SetUpInDeathAssert(); |
224 value_ = calloc(1024, test_size_ / 1024L); | 218 value_ = calloc(1024, test_size_ / 1024L); |
225 }, kOomRegex); | 219 }, kOomRegex); |
226 } | 220 } |
227 | 221 |
228 TEST_F(OutOfMemoryDeathTest, AlignedAlloc) { | |
229 ASSERT_DEATH({ | |
230 SetUpInDeathAssert(); | |
231 value_ = base::AlignedAlloc(test_size_, 8); | |
232 }, kOomRegex); | |
233 } | |
234 | |
235 // POSIX does not define an aligned realloc function. | |
236 #if defined(OS_WIN) | |
237 TEST_F(OutOfMemoryDeathTest, AlignedRealloc) { | |
238 ASSERT_DEATH({ | |
239 SetUpInDeathAssert(); | |
240 value_ = _aligned_realloc(NULL, test_size_, 8); | |
241 }, kOomRegex); | |
242 } | |
243 #endif // defined(OS_WIN) | |
244 | |
245 // OS X has no 2Gb allocation limit. | 222 // OS X has no 2Gb allocation limit. |
246 // See https://crbug.com/169327. | 223 // See https://crbug.com/169327. |
247 #if !defined(OS_MACOSX) | 224 #if !defined(OS_MACOSX) |
248 TEST_F(OutOfMemoryDeathTest, SecurityNew) { | 225 TEST_F(OutOfMemoryDeathTest, SecurityNew) { |
249 ASSERT_DEATH({ | 226 ASSERT_DEATH({ |
250 SetUpInDeathAssert(); | 227 SetUpInDeathAssert(); |
251 value_ = operator new(insecure_test_size_); | 228 value_ = operator new(insecure_test_size_); |
252 }, kOomRegex); | 229 }, kOomRegex); |
253 } | 230 } |
254 | 231 |
(...skipping 17 matching lines...) Expand all Loading... |
272 value_ = realloc(NULL, insecure_test_size_); | 249 value_ = realloc(NULL, insecure_test_size_); |
273 }, kOomRegex); | 250 }, kOomRegex); |
274 } | 251 } |
275 | 252 |
276 TEST_F(OutOfMemoryDeathTest, SecurityCalloc) { | 253 TEST_F(OutOfMemoryDeathTest, SecurityCalloc) { |
277 ASSERT_DEATH({ | 254 ASSERT_DEATH({ |
278 SetUpInDeathAssert(); | 255 SetUpInDeathAssert(); |
279 value_ = calloc(1024, insecure_test_size_ / 1024L); | 256 value_ = calloc(1024, insecure_test_size_ / 1024L); |
280 }, kOomRegex); | 257 }, kOomRegex); |
281 } | 258 } |
282 | |
283 TEST_F(OutOfMemoryDeathTest, SecurityAlignedAlloc) { | |
284 ASSERT_DEATH({ | |
285 SetUpInDeathAssert(); | |
286 value_ = base::AlignedAlloc(insecure_test_size_, 8); | |
287 }, kOomRegex); | |
288 } | |
289 | |
290 // POSIX does not define an aligned realloc function. | |
291 #if defined(OS_WIN) | |
292 TEST_F(OutOfMemoryDeathTest, SecurityAlignedRealloc) { | |
293 ASSERT_DEATH({ | |
294 SetUpInDeathAssert(); | |
295 value_ = _aligned_realloc(NULL, insecure_test_size_, 8); | |
296 }, kOomRegex); | |
297 } | |
298 #endif // defined(OS_WIN) | |
299 #endif // !defined(OS_MACOSX) | 259 #endif // !defined(OS_MACOSX) |
300 | 260 |
301 #if defined(OS_LINUX) | 261 #if defined(OS_LINUX) |
302 | 262 |
303 TEST_F(OutOfMemoryDeathTest, Valloc) { | 263 TEST_F(OutOfMemoryDeathTest, Valloc) { |
304 ASSERT_DEATH({ | 264 ASSERT_DEATH({ |
305 SetUpInDeathAssert(); | 265 SetUpInDeathAssert(); |
306 value_ = valloc(test_size_); | 266 value_ = valloc(test_size_); |
307 }, kOomRegex); | 267 }, kOomRegex); |
308 } | 268 } |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
509 for (size_t i = 0; i < (kSafeCallocItems * kSafeCallocSize); ++i) | 469 for (size_t i = 0; i < (kSafeCallocItems * kSafeCallocSize); ++i) |
510 EXPECT_EQ(0, bytes[i]); | 470 EXPECT_EQ(0, bytes[i]); |
511 free(value_); | 471 free(value_); |
512 | 472 |
513 EXPECT_FALSE(base::UncheckedCalloc(1, test_size_, &value_)); | 473 EXPECT_FALSE(base::UncheckedCalloc(1, test_size_, &value_)); |
514 EXPECT_TRUE(value_ == NULL); | 474 EXPECT_TRUE(value_ == NULL); |
515 } | 475 } |
516 #endif // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | 476 #endif // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
517 #endif // !defined(OS_ANDROID) && !defined(OS_OPENBSD) && !(defined(OS_WIN) && | 477 #endif // !defined(OS_ANDROID) && !defined(OS_OPENBSD) && !(defined(OS_WIN) && |
518 // !defined(ALLOCATOR_SHIM)) && !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | 478 // !defined(ALLOCATOR_SHIM)) && !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
OLD | NEW |