Chromium Code Reviews| 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 "build/build_config.h" | 9 #include "build/build_config.h" |
| 10 | 10 |
| 11 #if defined(OS_WIN) | 11 #if defined(OS_WIN) |
| 12 #include <windows.h> | 12 #include <windows.h> |
| 13 #include <VersionHelpers.h> | |
| 13 #else | 14 #else |
| 14 #include <sys/time.h> | 15 #include <sys/time.h> |
| 15 #include <unistd.h> | 16 #include <unistd.h> |
| 16 #endif | 17 #endif |
| 17 | 18 |
| 18 namespace base { | 19 namespace base { |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 // This is the same PRNG as used by tcmalloc for mapping address randomness; | 23 // This is the same PRNG as used by tcmalloc for mapping address randomness; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 82 // balance good ASLR against not fragmenting the address space too badly. | 83 // balance good ASLR against not fragmenting the address space too badly. |
| 83 void* GetRandomPageBase() { | 84 void* GetRandomPageBase() { |
| 84 uintptr_t random; | 85 uintptr_t random; |
| 85 random = static_cast<uintptr_t>(ranval(&s_ranctx)); | 86 random = static_cast<uintptr_t>(ranval(&s_ranctx)); |
| 86 #if defined(ARCH_CPU_X86_64) | 87 #if defined(ARCH_CPU_X86_64) |
| 87 random <<= 32UL; | 88 random <<= 32UL; |
| 88 random |= static_cast<uintptr_t>(ranval(&s_ranctx)); | 89 random |= static_cast<uintptr_t>(ranval(&s_ranctx)); |
| 89 // This address mask gives a low likelihood of address space collisions. We | 90 // This address mask gives a low likelihood of address space collisions. We |
| 90 // handle the situation gracefully if there is a collision. | 91 // handle the situation gracefully if there is a collision. |
| 91 #if defined(OS_WIN) | 92 #if defined(OS_WIN) |
| 92 // 64-bit Windows has a bizarrely small 8TB user address space. Allocates in | |
| 93 // the 1-5TB region. TODO(palmer): See if Windows >= 8.1 has the full 47 bits, | |
| 94 // and use it if so. crbug.com/672219 | |
| 95 random &= 0x3ffffffffffUL; | 93 random &= 0x3ffffffffffUL; |
| 96 random += 0x10000000000UL; | 94 // Windows >= 8.1 has the full 47 bits. Use them where available. |
| 95 static bool windows_81 = false; | |
| 96 static bool windows_81_initialized = false; | |
| 97 if (!windows_81_initialized) { | |
| 98 windows_81 = IsWindows8Point1OrGreater(); | |
|
Will Harris
2017/03/28 16:42:16
can this code use base::win::GetVersion - by conve
palmer
2017/03/28 19:05:35
Thanks. Using it now, although I might have to go
| |
| 99 windows_81_initialized = true; | |
| 100 } | |
| 101 if (!windows_81) { | |
| 102 random += 0x10000000000UL; | |
| 103 } | |
| 97 #elif defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | 104 #elif defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
| 98 // This range is copied from the TSan source, but works for all tools. | 105 // This range is copied from the TSan source, but works for all tools. |
| 99 random &= 0x007fffffffffUL; | 106 random &= 0x007fffffffffUL; |
| 100 random += 0x7e8000000000UL; | 107 random += 0x7e8000000000UL; |
| 101 #else | 108 #else |
| 102 // Linux and OS X support the full 47-bit user space of x64 processors. | 109 // Linux and OS X support the full 47-bit user space of x64 processors. |
| 103 random &= 0x3fffffffffffUL; | 110 random &= 0x3fffffffffffUL; |
| 104 #endif | 111 #endif |
| 105 #elif defined(ARCH_CPU_ARM64) | 112 #elif defined(ARCH_CPU_ARM64) |
| 106 // ARM64 on Linux has 39-bit user space. | 113 // ARM64 on Linux has 39-bit user space. |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 121 // This is a good range on Windows, Linux and Mac. | 128 // This is a good range on Windows, Linux and Mac. |
| 122 // Allocates in the 0.5-1.5GB region. | 129 // Allocates in the 0.5-1.5GB region. |
| 123 random &= 0x3fffffff; | 130 random &= 0x3fffffff; |
| 124 random += 0x20000000; | 131 random += 0x20000000; |
| 125 #endif // defined(ARCH_CPU_X86_64) | 132 #endif // defined(ARCH_CPU_X86_64) |
| 126 random &= kPageAllocationGranularityBaseMask; | 133 random &= kPageAllocationGranularityBaseMask; |
| 127 return reinterpret_cast<void*>(random); | 134 return reinterpret_cast<void*>(random); |
| 128 } | 135 } |
| 129 | 136 |
| 130 } // namespace base | 137 } // namespace base |
| OLD | NEW |