Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 #include <fcntl.h> | |
| 5 #include <stdio.h> | 6 #include <stdio.h> |
| 6 #include <stdlib.h> | 7 #include <stdlib.h> |
| 7 #include <string.h> | 8 #include <string.h> |
| 9 #include <sys/stat.h> | |
| 10 #include <sys/types.h> | |
| 8 | 11 |
| 9 #include <algorithm> | 12 #include <algorithm> |
| 10 #include <limits> | 13 #include <limits> |
| 11 | 14 |
| 12 #include "base/logging.h" | 15 #include "base/logging.h" |
| 13 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 18 |
| 16 using std::nothrow; | 19 using std::nothrow; |
| 17 | 20 |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 93 } | 96 } |
| 94 } | 97 } |
| 95 | 98 |
| 96 TEST(SecurityTest, ALLOC_TEST(MemoryAllocationRestrictionsNewArray)) { | 99 TEST(SecurityTest, ALLOC_TEST(MemoryAllocationRestrictionsNewArray)) { |
| 97 if (!IsTcMallocBypassed()) { | 100 if (!IsTcMallocBypassed()) { |
| 98 scoped_ptr<char[]> ptr(new (nothrow) char[kTooBigAllocSize]); | 101 scoped_ptr<char[]> ptr(new (nothrow) char[kTooBigAllocSize]); |
| 99 ASSERT_TRUE(ptr == NULL); | 102 ASSERT_TRUE(ptr == NULL); |
| 100 } | 103 } |
| 101 } | 104 } |
| 102 | 105 |
| 106 #if (defined(OS_LINUX) || defined(OS_CHROMEOS)) && defined(__x86_64__) | |
| 107 // Useful for debugging. | |
| 108 void PrintProcSelfMaps() { | |
| 109 int fd = open("/proc/self/maps", O_RDONLY); | |
| 110 ASSERT_GE(fd, 0); | |
| 111 char buffer[1<<13]; | |
| 112 int ret; | |
| 113 ret = read(fd, buffer, sizeof(buffer) - 1); | |
| 114 ASSERT_GT(ret, 0); | |
| 115 buffer[ret - 1] = 0; | |
| 116 fprintf(stdout, "%s\n", buffer); | |
| 117 } | |
| 118 | |
| 119 // Check if TCMalloc uses an underlying random memory allocator. | |
| 120 TEST(SecurityTest, ALLOC_TEST(RandomMemoryAllocations)) { | |
|
Chris Evans
2013/01/29 06:10:58
This test seems complicated to me.
Can you just d
jln (very slow on Chromium)
2013/01/29 06:23:46
1GB is a bit special. That's where we would magica
| |
| 121 if (IsTcMallocBypassed()) | |
| 122 return; | |
| 123 // Two successive calls to mmap() have roughly one chance out of 2^6 to be | |
| 124 // detected as having the same order. With 32 allocations, we see ~16 that | |
| 125 // trigger a call to mmap, so the chances of this test flaking is roughly | |
| 126 // 2^-(6*15), i.e. virtually impossible. | |
| 127 const int kAllocNumber = 32; | |
| 128 bool is_contiguous = true; | |
| 129 // Make kAllocNumber successive allocations of growing size and compare the | |
| 130 // successive pointers to detect adjacent mappings. We grow the size because | |
| 131 // TCMalloc can sometimes over-allocate. | |
| 132 scoped_ptr<char, base::FreeDeleter> ptr[kAllocNumber]; | |
| 133 for (int i = 0; i < kAllocNumber; ++i) { | |
| 134 // Grow the Malloc size slightly sub-exponentially. | |
| 135 const size_t kMallocSize = 1 << (12 + (i>>1)); | |
| 136 ptr[i].reset(static_cast<char*>(malloc(kMallocSize))); | |
| 137 ASSERT_TRUE(ptr[i] != NULL); | |
| 138 if (i > 0) { | |
| 139 // Without mmap randomization, the two high order nibbles | |
| 140 // of a 47 bits userland address address will be identical. | |
| 141 const size_t kHighOrderMask = 0xff0000000000; | |
| 142 bool pointer_have_same_high_order = | |
| 143 (reinterpret_cast<size_t>(ptr[i].get()) & kHighOrderMask) == | |
| 144 (reinterpret_cast<size_t>(ptr[i - 1].get()) & kHighOrderMask); | |
| 145 if (!pointer_have_same_high_order) { | |
| 146 // PrintProcSelfMaps(); | |
| 147 is_contiguous = false; | |
| 148 break; | |
| 149 } | |
| 150 } | |
| 151 } | |
| 152 ASSERT_FALSE(is_contiguous); | |
| 153 } | |
| 154 | |
| 155 #endif // (defined(OS_LINUX) || defined(OS_CHROMEOS)) && defined(__x86_64__) | |
| 156 | |
| 103 } // namespace | 157 } // namespace |
| OLD | NEW |