| 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 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 // heap corruption. | 91 // heap corruption. |
| 90 ASSERT_DEATH(free(buf), "attempting free on address which " | 92 ASSERT_DEATH(free(buf), "attempting free on address which " |
| 91 "was not malloc\\(\\)-ed"); | 93 "was not malloc\\(\\)-ed"); |
| 92 #else | 94 #else |
| 93 ADD_FAILURE() << "This test is not supported in this build configuration."; | 95 ADD_FAILURE() << "This test is not supported in this build configuration."; |
| 94 #endif | 96 #endif |
| 95 } | 97 } |
| 96 | 98 |
| 97 #endif // defined(OS_MACOSX) | 99 #endif // defined(OS_MACOSX) |
| 98 | 100 |
| 101 TEST(MemoryTest, AllocatorShimWorking) { |
| 102 ASSERT_TRUE(base::allocator::IsAllocatorInitialized()); |
| 103 } |
| 104 |
| 99 // Android doesn't implement set_new_handler, so we can't use the | 105 // Android doesn't implement set_new_handler, so we can't use the |
| 100 // OutOfMemoryTest cases. OpenBSD does not support these tests either. | 106 // OutOfMemoryTest cases. OpenBSD does not support these tests either. |
| 101 // Don't test these on ASan/TSan/MSan configurations: only test the real | 107 // Don't test these on ASan/TSan/MSan configurations: only test the real |
| 102 // allocator. | 108 // allocator. |
| 103 // Windows only supports these tests with the allocator shim in place. | 109 // Windows only supports these tests with the allocator shim in place. |
| 104 #if !defined(OS_ANDROID) && !defined(OS_OPENBSD) && \ | 110 #if !defined(OS_ANDROID) && !defined(OS_OPENBSD) && \ |
| 105 !(defined(OS_WIN) && !defined(ALLOCATOR_SHIM)) && \ | 111 !(defined(OS_WIN) && !defined(ALLOCATOR_SHIM)) && \ |
| 106 !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | 112 !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
| 107 | 113 |
| 108 namespace { | 114 namespace { |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 }, kOomRegex); | 175 }, kOomRegex); |
| 170 } | 176 } |
| 171 | 177 |
| 172 TEST_F(OutOfMemoryDeathTest, Calloc) { | 178 TEST_F(OutOfMemoryDeathTest, Calloc) { |
| 173 ASSERT_DEATH({ | 179 ASSERT_DEATH({ |
| 174 SetUpInDeathAssert(); | 180 SetUpInDeathAssert(); |
| 175 value_ = calloc(1024, test_size_ / 1024L); | 181 value_ = calloc(1024, test_size_ / 1024L); |
| 176 }, kOomRegex); | 182 }, kOomRegex); |
| 177 } | 183 } |
| 178 | 184 |
| 185 TEST_F(OutOfMemoryDeathTest, AlignedAlloc) { |
| 186 ASSERT_DEATH({ |
| 187 SetUpInDeathAssert(); |
| 188 value_ = base::AlignedAlloc(test_size_, 8); |
| 189 }, kOomRegex); |
| 190 } |
| 191 |
| 192 // POSIX does not define an aligned realloc function. |
| 193 #if defined(OS_WIN) |
| 194 TEST_F(OutOfMemoryDeathTest, AlignedRealloc) { |
| 195 ASSERT_DEATH({ |
| 196 SetUpInDeathAssert(); |
| 197 value_ = _aligned_realloc(NULL, test_size_, 8); |
| 198 }, kOomRegex); |
| 199 } |
| 200 #endif // defined(OS_WIN) |
| 201 |
| 179 // OS X has no 2Gb allocation limit. | 202 // OS X has no 2Gb allocation limit. |
| 180 // See https://crbug.com/169327. | 203 // See https://crbug.com/169327. |
| 181 #if !defined(OS_MACOSX) | 204 #if !defined(OS_MACOSX) |
| 182 TEST_F(OutOfMemoryDeathTest, SecurityNew) { | 205 TEST_F(OutOfMemoryDeathTest, SecurityNew) { |
| 183 ASSERT_DEATH({ | 206 ASSERT_DEATH({ |
| 184 SetUpInDeathAssert(); | 207 SetUpInDeathAssert(); |
| 185 value_ = operator new(insecure_test_size_); | 208 value_ = operator new(insecure_test_size_); |
| 186 }, kOomRegex); | 209 }, kOomRegex); |
| 187 } | 210 } |
| 188 | 211 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 206 value_ = realloc(NULL, insecure_test_size_); | 229 value_ = realloc(NULL, insecure_test_size_); |
| 207 }, kOomRegex); | 230 }, kOomRegex); |
| 208 } | 231 } |
| 209 | 232 |
| 210 TEST_F(OutOfMemoryDeathTest, SecurityCalloc) { | 233 TEST_F(OutOfMemoryDeathTest, SecurityCalloc) { |
| 211 ASSERT_DEATH({ | 234 ASSERT_DEATH({ |
| 212 SetUpInDeathAssert(); | 235 SetUpInDeathAssert(); |
| 213 value_ = calloc(1024, insecure_test_size_ / 1024L); | 236 value_ = calloc(1024, insecure_test_size_ / 1024L); |
| 214 }, kOomRegex); | 237 }, kOomRegex); |
| 215 } | 238 } |
| 239 |
| 240 TEST_F(OutOfMemoryDeathTest, SecurityAlignedAlloc) { |
| 241 ASSERT_DEATH({ |
| 242 SetUpInDeathAssert(); |
| 243 value_ = base::AlignedAlloc(insecure_test_size_, 8); |
| 244 }, kOomRegex); |
| 245 } |
| 246 |
| 247 // POSIX does not define an aligned realloc function. |
| 248 #if defined(OS_WIN) |
| 249 TEST_F(OutOfMemoryDeathTest, SecurityAlignedRealloc) { |
| 250 ASSERT_DEATH({ |
| 251 SetUpInDeathAssert(); |
| 252 value_ = _aligned_realloc(NULL, insecure_test_size_, 8); |
| 253 }, kOomRegex); |
| 254 } |
| 255 #endif // defined(OS_WIN) |
| 216 #endif // !defined(OS_MACOSX) | 256 #endif // !defined(OS_MACOSX) |
| 217 | 257 |
| 218 #if defined(OS_LINUX) | 258 #if defined(OS_LINUX) |
| 219 | 259 |
| 220 TEST_F(OutOfMemoryDeathTest, Valloc) { | 260 TEST_F(OutOfMemoryDeathTest, Valloc) { |
| 221 ASSERT_DEATH({ | 261 ASSERT_DEATH({ |
| 222 SetUpInDeathAssert(); | 262 SetUpInDeathAssert(); |
| 223 value_ = valloc(test_size_); | 263 value_ = valloc(test_size_); |
| 224 }, kOomRegex); | 264 }, kOomRegex); |
| 225 } | 265 } |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 426 for (size_t i = 0; i < (kSafeCallocItems * kSafeCallocSize); ++i) | 466 for (size_t i = 0; i < (kSafeCallocItems * kSafeCallocSize); ++i) |
| 427 EXPECT_EQ(0, bytes[i]); | 467 EXPECT_EQ(0, bytes[i]); |
| 428 free(value_); | 468 free(value_); |
| 429 | 469 |
| 430 EXPECT_FALSE(base::UncheckedCalloc(1, test_size_, &value_)); | 470 EXPECT_FALSE(base::UncheckedCalloc(1, test_size_, &value_)); |
| 431 EXPECT_TRUE(value_ == NULL); | 471 EXPECT_TRUE(value_ == NULL); |
| 432 } | 472 } |
| 433 #endif // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | 473 #endif // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
| 434 #endif // !defined(OS_ANDROID) && !defined(OS_OPENBSD) && !(defined(OS_WIN) && | 474 #endif // !defined(OS_ANDROID) && !defined(OS_OPENBSD) && !(defined(OS_WIN) && |
| 435 // !defined(ALLOCATOR_SHIM)) && !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | 475 // !defined(ALLOCATOR_SHIM)) && !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
| OLD | NEW |