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" | |
13 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
14 #include "base/debug/alias.h" | 15 #include "base/debug/alias.h" |
16 #include "base/memory/aligned_memory.h" | |
15 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
16 #include "build/build_config.h" | 18 #include "build/build_config.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
18 | 20 |
19 #if defined(OS_WIN) | 21 #if defined(OS_WIN) |
20 #include <windows.h> | 22 #include <windows.h> |
21 #endif | 23 #endif |
22 #if defined(OS_POSIX) | 24 #if defined(OS_POSIX) |
23 #include <errno.h> | 25 #include <errno.h> |
24 #endif | 26 #endif |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
132 // heap corruption. | 134 // heap corruption. |
133 ASSERT_DEATH(free(buf), "attempting free on address which " | 135 ASSERT_DEATH(free(buf), "attempting free on address which " |
134 "was not malloc\\(\\)-ed"); | 136 "was not malloc\\(\\)-ed"); |
135 #else | 137 #else |
136 ADD_FAILURE() << "This test is not supported in this build configuration."; | 138 ADD_FAILURE() << "This test is not supported in this build configuration."; |
137 #endif | 139 #endif |
138 } | 140 } |
139 | 141 |
140 #endif // defined(OS_MACOSX) | 142 #endif // defined(OS_MACOSX) |
141 | 143 |
144 TEST(MemoryTest, AllocatorShimWorking) { | |
Primiano Tucci (use gerrit)
2016/03/29 18:46:07
Not sure how much this will add. It's very easy to
Will Harris
2016/03/29 18:56:20
Ack. I added it after I tripped it while developin
Primiano Tucci (use gerrit)
2016/03/29 19:30:03
Acknowledged.
| |
145 ASSERT_TRUE(base::allocator::IsAllocatorInitialized()); | |
146 } | |
147 | |
142 // Android doesn't implement set_new_handler, so we can't use the | 148 // Android doesn't implement set_new_handler, so we can't use the |
143 // OutOfMemoryTest cases. OpenBSD does not support these tests either. | 149 // OutOfMemoryTest cases. OpenBSD does not support these tests either. |
144 // Don't test these on ASan/TSan/MSan configurations: only test the real | 150 // Don't test these on ASan/TSan/MSan configurations: only test the real |
145 // allocator. | 151 // allocator. |
146 // Windows only supports these tests with the allocator shim in place. | 152 // Windows only supports these tests with the allocator shim in place. |
147 #if !defined(OS_ANDROID) && !defined(OS_OPENBSD) && \ | 153 #if !defined(OS_ANDROID) && !defined(OS_OPENBSD) && \ |
148 !(defined(OS_WIN) && !defined(ALLOCATOR_SHIM)) && \ | 154 !(defined(OS_WIN) && !defined(ALLOCATOR_SHIM)) && \ |
149 !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | 155 !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
150 | 156 |
151 namespace { | 157 namespace { |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
212 }, kOomRegex); | 218 }, kOomRegex); |
213 } | 219 } |
214 | 220 |
215 TEST_F(OutOfMemoryDeathTest, Calloc) { | 221 TEST_F(OutOfMemoryDeathTest, Calloc) { |
216 ASSERT_DEATH({ | 222 ASSERT_DEATH({ |
217 SetUpInDeathAssert(); | 223 SetUpInDeathAssert(); |
218 value_ = calloc(1024, test_size_ / 1024L); | 224 value_ = calloc(1024, test_size_ / 1024L); |
219 }, kOomRegex); | 225 }, kOomRegex); |
220 } | 226 } |
221 | 227 |
228 TEST_F(OutOfMemoryDeathTest, AlignedAlloc) { | |
229 ASSERT_DEATH({ | |
230 SetUpInDeathAssert(); | |
231 value_ = base::AlignedAlloc(test_size_, 8); | |
232 }, kOomRegex); | |
233 } | |
234 | |
235 TEST_F(OutOfMemoryDeathTest, AlignedMalloc) { | |
236 ASSERT_DEATH({ | |
237 SetUpInDeathAssert(); | |
238 value_ = _aligned_malloc(test_size_, 8); | |
239 }, kOomRegex); | |
240 } | |
241 | |
242 TEST_F(OutOfMemoryDeathTest, AlignedRealloc) { | |
243 ASSERT_DEATH({ | |
244 SetUpInDeathAssert(); | |
245 value_ = _aligned_realloc(NULL, test_size_, 8); | |
246 }, kOomRegex); | |
247 } | |
248 | |
222 // OS X has no 2Gb allocation limit. | 249 // OS X has no 2Gb allocation limit. |
223 // See https://crbug.com/169327. | 250 // See https://crbug.com/169327. |
224 #if !defined(OS_MACOSX) | 251 #if !defined(OS_MACOSX) |
225 TEST_F(OutOfMemoryDeathTest, SecurityNew) { | 252 TEST_F(OutOfMemoryDeathTest, SecurityNew) { |
226 ASSERT_DEATH({ | 253 ASSERT_DEATH({ |
227 SetUpInDeathAssert(); | 254 SetUpInDeathAssert(); |
228 value_ = operator new(insecure_test_size_); | 255 value_ = operator new(insecure_test_size_); |
229 }, kOomRegex); | 256 }, kOomRegex); |
230 } | 257 } |
231 | 258 |
(...skipping 17 matching lines...) Expand all Loading... | |
249 value_ = realloc(NULL, insecure_test_size_); | 276 value_ = realloc(NULL, insecure_test_size_); |
250 }, kOomRegex); | 277 }, kOomRegex); |
251 } | 278 } |
252 | 279 |
253 TEST_F(OutOfMemoryDeathTest, SecurityCalloc) { | 280 TEST_F(OutOfMemoryDeathTest, SecurityCalloc) { |
254 ASSERT_DEATH({ | 281 ASSERT_DEATH({ |
255 SetUpInDeathAssert(); | 282 SetUpInDeathAssert(); |
256 value_ = calloc(1024, insecure_test_size_ / 1024L); | 283 value_ = calloc(1024, insecure_test_size_ / 1024L); |
257 }, kOomRegex); | 284 }, kOomRegex); |
258 } | 285 } |
286 | |
287 TEST_F(OutOfMemoryDeathTest, SecurityAlignedAlloc) { | |
288 ASSERT_DEATH({ | |
289 SetUpInDeathAssert(); | |
290 value_ = base::AlignedAlloc(insecure_test_size_, 8); | |
291 }, kOomRegex); | |
292 } | |
293 | |
294 TEST_F(OutOfMemoryDeathTest, SecurityAlignedMalloc) { | |
295 ASSERT_DEATH({ | |
296 SetUpInDeathAssert(); | |
297 value_ = _aligned_malloc(insecure_test_size_, 8); | |
298 }, kOomRegex); | |
299 } | |
300 | |
301 TEST_F(OutOfMemoryDeathTest, SecurityAlignedRealloc) { | |
302 ASSERT_DEATH({ | |
303 SetUpInDeathAssert(); | |
304 value_ = _aligned_realloc(NULL, insecure_test_size_, 8); | |
305 }, kOomRegex); | |
306 } | |
307 | |
259 #endif // !defined(OS_MACOSX) | 308 #endif // !defined(OS_MACOSX) |
260 | 309 |
261 #if defined(OS_LINUX) | 310 #if defined(OS_LINUX) |
262 | 311 |
263 TEST_F(OutOfMemoryDeathTest, Valloc) { | 312 TEST_F(OutOfMemoryDeathTest, Valloc) { |
264 ASSERT_DEATH({ | 313 ASSERT_DEATH({ |
265 SetUpInDeathAssert(); | 314 SetUpInDeathAssert(); |
266 value_ = valloc(test_size_); | 315 value_ = valloc(test_size_); |
267 }, kOomRegex); | 316 }, kOomRegex); |
268 } | 317 } |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
469 for (size_t i = 0; i < (kSafeCallocItems * kSafeCallocSize); ++i) | 518 for (size_t i = 0; i < (kSafeCallocItems * kSafeCallocSize); ++i) |
470 EXPECT_EQ(0, bytes[i]); | 519 EXPECT_EQ(0, bytes[i]); |
471 free(value_); | 520 free(value_); |
472 | 521 |
473 EXPECT_FALSE(base::UncheckedCalloc(1, test_size_, &value_)); | 522 EXPECT_FALSE(base::UncheckedCalloc(1, test_size_, &value_)); |
474 EXPECT_TRUE(value_ == NULL); | 523 EXPECT_TRUE(value_ == NULL); |
475 } | 524 } |
476 #endif // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | 525 #endif // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
477 #endif // !defined(OS_ANDROID) && !defined(OS_OPENBSD) && !(defined(OS_WIN) && | 526 #endif // !defined(OS_ANDROID) && !defined(OS_OPENBSD) && !(defined(OS_WIN) && |
478 // !defined(ALLOCATOR_SHIM)) && !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | 527 // !defined(ALLOCATOR_SHIM)) && !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
OLD | NEW |