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