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) { |
| 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, AlignedMem) { |
| 229 ASSERT_DEATH({ |
| 230 SetUpInDeathAssert(); |
| 231 value_ = base::AlignedAlloc(test_size_, 8); |
| 232 }, kOomRegex); |
| 233 } |
| 234 |
222 // OS X has no 2Gb allocation limit. | 235 // OS X has no 2Gb allocation limit. |
223 // See https://crbug.com/169327. | 236 // See https://crbug.com/169327. |
224 #if !defined(OS_MACOSX) | 237 #if !defined(OS_MACOSX) |
225 TEST_F(OutOfMemoryDeathTest, SecurityNew) { | 238 TEST_F(OutOfMemoryDeathTest, SecurityNew) { |
226 ASSERT_DEATH({ | 239 ASSERT_DEATH({ |
227 SetUpInDeathAssert(); | 240 SetUpInDeathAssert(); |
228 value_ = operator new(insecure_test_size_); | 241 value_ = operator new(insecure_test_size_); |
229 }, kOomRegex); | 242 }, kOomRegex); |
230 } | 243 } |
231 | 244 |
(...skipping 17 matching lines...) Expand all Loading... |
249 value_ = realloc(NULL, insecure_test_size_); | 262 value_ = realloc(NULL, insecure_test_size_); |
250 }, kOomRegex); | 263 }, kOomRegex); |
251 } | 264 } |
252 | 265 |
253 TEST_F(OutOfMemoryDeathTest, SecurityCalloc) { | 266 TEST_F(OutOfMemoryDeathTest, SecurityCalloc) { |
254 ASSERT_DEATH({ | 267 ASSERT_DEATH({ |
255 SetUpInDeathAssert(); | 268 SetUpInDeathAssert(); |
256 value_ = calloc(1024, insecure_test_size_ / 1024L); | 269 value_ = calloc(1024, insecure_test_size_ / 1024L); |
257 }, kOomRegex); | 270 }, kOomRegex); |
258 } | 271 } |
| 272 |
| 273 TEST_F(OutOfMemoryDeathTest, SecurityAlignedMem) { |
| 274 ASSERT_DEATH({ |
| 275 SetUpInDeathAssert(); |
| 276 value_ = base::AlignedAlloc(insecure_test_size_, 8); |
| 277 }, kOomRegex); |
| 278 } |
| 279 |
259 #endif // !defined(OS_MACOSX) | 280 #endif // !defined(OS_MACOSX) |
260 | 281 |
261 #if defined(OS_LINUX) | 282 #if defined(OS_LINUX) |
262 | 283 |
263 TEST_F(OutOfMemoryDeathTest, Valloc) { | 284 TEST_F(OutOfMemoryDeathTest, Valloc) { |
264 ASSERT_DEATH({ | 285 ASSERT_DEATH({ |
265 SetUpInDeathAssert(); | 286 SetUpInDeathAssert(); |
266 value_ = valloc(test_size_); | 287 value_ = valloc(test_size_); |
267 }, kOomRegex); | 288 }, kOomRegex); |
268 } | 289 } |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
469 for (size_t i = 0; i < (kSafeCallocItems * kSafeCallocSize); ++i) | 490 for (size_t i = 0; i < (kSafeCallocItems * kSafeCallocSize); ++i) |
470 EXPECT_EQ(0, bytes[i]); | 491 EXPECT_EQ(0, bytes[i]); |
471 free(value_); | 492 free(value_); |
472 | 493 |
473 EXPECT_FALSE(base::UncheckedCalloc(1, test_size_, &value_)); | 494 EXPECT_FALSE(base::UncheckedCalloc(1, test_size_, &value_)); |
474 EXPECT_TRUE(value_ == NULL); | 495 EXPECT_TRUE(value_ == NULL); |
475 } | 496 } |
476 #endif // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | 497 #endif // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
477 #endif // !defined(OS_ANDROID) && !defined(OS_OPENBSD) && !(defined(OS_WIN) && | 498 #endif // !defined(OS_ANDROID) && !defined(OS_OPENBSD) && !(defined(OS_WIN) && |
478 // !defined(ALLOCATOR_SHIM)) && !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | 499 // !defined(ALLOCATOR_SHIM)) && !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
OLD | NEW |