| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "base/allocator/partition_allocator/address_space_randomization.h" | 5 #include "base/allocator/partition_allocator/address_space_randomization.h" |
| 6 | 6 |
| 7 #include "base/allocator/partition_allocator/page_allocator.h" | 7 #include "base/allocator/partition_allocator/page_allocator.h" |
| 8 #include "base/allocator/partition_allocator/spin_lock.h" | 8 #include "base/allocator/partition_allocator/spin_lock.h" |
| 9 #include "base/win/windows_version.h" | 9 #include "base/win/windows_version.h" |
| 10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| 11 | 11 |
| 12 #if defined(OS_WIN) | 12 #if defined(OS_WIN) |
| 13 #include <windows.h> | 13 #include <windows.h> |
| 14 #else | 14 #else |
| 15 #include <sys/time.h> | 15 #include <sys/time.h> |
| 16 #include <unistd.h> | 16 #include <unistd.h> |
| 17 #endif | 17 #endif |
| 18 | 18 |
| 19 // VersionHelpers.h must be included after windows.h. |
| 20 #if defined(OS_WIN) |
| 21 #include <VersionHelpers.h> |
| 22 #endif |
| 23 |
| 19 namespace base { | 24 namespace base { |
| 20 | 25 |
| 21 namespace { | 26 namespace { |
| 22 | 27 |
| 23 // This is the same PRNG as used by tcmalloc for mapping address randomness; | 28 // This is the same PRNG as used by tcmalloc for mapping address randomness; |
| 24 // see http://burtleburtle.net/bob/rand/smallprng.html | 29 // see http://burtleburtle.net/bob/rand/smallprng.html |
| 25 struct ranctx { | 30 struct ranctx { |
| 26 subtle::SpinLock lock; | 31 subtle::SpinLock lock; |
| 27 bool initialized; | 32 bool initialized; |
| 28 uint32_t a; | 33 uint32_t a; |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 uintptr_t random; | 90 uintptr_t random; |
| 86 random = static_cast<uintptr_t>(ranval(&s_ranctx)); | 91 random = static_cast<uintptr_t>(ranval(&s_ranctx)); |
| 87 #if defined(ARCH_CPU_X86_64) | 92 #if defined(ARCH_CPU_X86_64) |
| 88 random <<= 32UL; | 93 random <<= 32UL; |
| 89 random |= static_cast<uintptr_t>(ranval(&s_ranctx)); | 94 random |= static_cast<uintptr_t>(ranval(&s_ranctx)); |
| 90 // This address mask gives a low likelihood of address space collisions. We | 95 // This address mask gives a low likelihood of address space collisions. We |
| 91 // handle the situation gracefully if there is a collision. | 96 // handle the situation gracefully if there is a collision. |
| 92 #if defined(OS_WIN) | 97 #if defined(OS_WIN) |
| 93 random &= 0x3ffffffffffUL; | 98 random &= 0x3ffffffffffUL; |
| 94 // Windows >= 8.1 has the full 47 bits. Use them where available. | 99 // Windows >= 8.1 has the full 47 bits. Use them where available. |
| 95 if (base::win::GetVersion() < base::win::Version::VERSION_WIN8_1) { | 100 static bool windows_81 = false; |
| 101 static bool windows_81_initialized = false; |
| 102 if (!windows_81_initialized) { |
| 103 windows_81 = IsWindows8Point1OrGreater(); |
| 104 windows_81_initialized = true; |
| 105 } |
| 106 if (!windows_81) { |
| 96 random += 0x10000000000UL; | 107 random += 0x10000000000UL; |
| 97 } | 108 } |
| 98 #elif defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | 109 #elif defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
| 99 // This range is copied from the TSan source, but works for all tools. | 110 // This range is copied from the TSan source, but works for all tools. |
| 100 random &= 0x007fffffffffUL; | 111 random &= 0x007fffffffffUL; |
| 101 random += 0x7e8000000000UL; | 112 random += 0x7e8000000000UL; |
| 102 #else | 113 #else |
| 103 // Linux and OS X support the full 47-bit user space of x64 processors. | 114 // Linux and OS X support the full 47-bit user space of x64 processors. |
| 104 random &= 0x3fffffffffffUL; | 115 random &= 0x3fffffffffffUL; |
| 105 #endif | 116 #endif |
| (...skipping 16 matching lines...) Expand all Loading... |
| 122 // This is a good range on Windows, Linux and Mac. | 133 // This is a good range on Windows, Linux and Mac. |
| 123 // Allocates in the 0.5-1.5GB region. | 134 // Allocates in the 0.5-1.5GB region. |
| 124 random &= 0x3fffffff; | 135 random &= 0x3fffffff; |
| 125 random += 0x20000000; | 136 random += 0x20000000; |
| 126 #endif // defined(ARCH_CPU_X86_64) | 137 #endif // defined(ARCH_CPU_X86_64) |
| 127 random &= kPageAllocationGranularityBaseMask; | 138 random &= kPageAllocationGranularityBaseMask; |
| 128 return reinterpret_cast<void*>(random); | 139 return reinterpret_cast<void*>(random); |
| 129 } | 140 } |
| 130 | 141 |
| 131 } // namespace base | 142 } // namespace base |
| OLD | NEW |