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 using std::numeric_limits; | 20 using std::numeric_limits; |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 185 { | 188 { |
| 186 scoped_ptr<char> array_pointer( | 189 scoped_ptr<char> array_pointer( |
| 187 static_cast<char*>(calloc(kArraySize2, kArraySize))); | 190 static_cast<char*>(calloc(kArraySize2, kArraySize))); |
| 188 // We need the call to HideValueFromCompiler(): we have seen LLVM | 191 // We need the call to HideValueFromCompiler(): we have seen LLVM |
| 189 // optimize away the call to calloc() entirely and assume | 192 // optimize away the call to calloc() entirely and assume |
| 190 // the pointer to not be NULL. | 193 // the pointer to not be NULL. |
| 191 EXPECT_TRUE(HideValueFromCompiler(array_pointer.get()) == NULL); | 194 EXPECT_TRUE(HideValueFromCompiler(array_pointer.get()) == NULL); |
| 192 } | 195 } |
| 193 } | 196 } |
| 194 | 197 |
| 198 #if (defined(OS_LINUX) || defined(OS_CHROMEOS)) && defined(__x86_64__) | |
| 199 // Useful for debugging. | |
| 200 void PrintProcSelfMaps() { | |
| 201 int fd = open("/proc/self/maps", O_RDONLY); | |
| 202 ASSERT_GE(fd, 0); | |
| 203 char buffer[1<<13]; | |
| 204 int ret; | |
| 205 ret = read(fd, buffer, sizeof(buffer) - 1); | |
| 206 ASSERT_GT(ret, 0); | |
| 207 buffer[ret - 1] = 0; | |
| 208 fprintf(stdout, "%s\n", buffer); | |
|
Marius
2013/01/29 06:45:21
close(fd)?
jln (very slow on Chromium)
2013/01/29 07:02:16
Arg. Thanks. Done.
| |
| 209 } | |
| 210 | |
| 211 // Check if TCMalloc uses an underlying random memory allocator. | |
| 212 TEST(SecurityTest, ALLOC_TEST(RandomMemoryAllocations)) { | |
| 213 if (IsTcMallocBypassed()) | |
| 214 return; | |
| 215 // Two successive calls to mmap() have roughly one chance out of 2^7 to be | |
|
Marius
2013/01/29 06:45:21
With the 0x6f mask, there's only 6 bits of random
jln (very slow on Chromium)
2013/01/29 07:02:16
This had been patched in a previous revision alrea
| |
| 216 // detected as having the same order. With 32 allocations, we see ~16 that | |
| 217 // trigger a call to mmap, so the chances of this test flaking is roughly | |
| 218 // 2^-(7*15), i.e. virtually impossible. | |
| 219 const int kAllocNumber = 32; | |
| 220 bool is_contiguous = true; | |
| 221 // Make kAllocNumber successive allocations of growing size and compare the | |
| 222 // successive pointers to detect adjacent mappings. We grow the size because | |
| 223 // TCMalloc can sometimes over-allocate. | |
| 224 scoped_ptr<char, base::FreeDeleter> ptr[kAllocNumber]; | |
| 225 for (int i = 0; i < kAllocNumber; ++i) { | |
| 226 // Grow the Malloc size slightly sub-exponentially. | |
| 227 const size_t kMallocSize = 1 << (12 + (i>>1)); | |
| 228 ptr[i].reset(static_cast<char*>(malloc(kMallocSize))); | |
| 229 ASSERT_TRUE(ptr[i] != NULL); | |
| 230 if (i > 0) { | |
| 231 // Without mmap randomization, the two high order nibbles | |
| 232 // of a 47 bits userland address address will be identical. | |
| 233 const size_t kHighOrderMask = 0xff0000000000; | |
| 234 bool pointer_have_same_high_order = | |
| 235 (reinterpret_cast<size_t>(ptr[i].get()) & kHighOrderMask) == | |
| 236 (reinterpret_cast<size_t>(ptr[i - 1].get()) & kHighOrderMask); | |
| 237 if (!pointer_have_same_high_order) { | |
| 238 // PrintProcSelfMaps(); | |
| 239 is_contiguous = false; | |
| 240 break; | |
| 241 } | |
| 242 } | |
| 243 } | |
| 244 ASSERT_FALSE(is_contiguous); | |
| 245 } | |
| 246 | |
| 247 #endif // (defined(OS_LINUX) || defined(OS_CHROMEOS)) && defined(__x86_64__) | |
| 248 | |
| 195 } // namespace | 249 } // namespace |
| OLD | NEW |