| 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 "build/build_config.h" | 10 #include "build/build_config.h" |
| 10 | 11 |
| 11 #if defined(OS_WIN) | 12 #if defined(OS_WIN) |
| 12 #include <windows.h> | 13 #include <windows.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 { |
| (...skipping 63 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 if (base::win::GetVersion() < base::win::Version::VERSION_WIN8_1) { |
| 96 random += 0x10000000000UL; |
| 97 } |
| 97 #elif defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | 98 #elif defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
| 98 // This range is copied from the TSan source, but works for all tools. | 99 // This range is copied from the TSan source, but works for all tools. |
| 99 random &= 0x007fffffffffUL; | 100 random &= 0x007fffffffffUL; |
| 100 random += 0x7e8000000000UL; | 101 random += 0x7e8000000000UL; |
| 101 #else | 102 #else |
| 102 // Linux and OS X support the full 47-bit user space of x64 processors. | 103 // Linux and OS X support the full 47-bit user space of x64 processors. |
| 103 random &= 0x3fffffffffffUL; | 104 random &= 0x3fffffffffffUL; |
| 104 #endif | 105 #endif |
| 105 #elif defined(ARCH_CPU_ARM64) | 106 #elif defined(ARCH_CPU_ARM64) |
| 106 // ARM64 on Linux has 39-bit user space. | 107 // 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. | 122 // This is a good range on Windows, Linux and Mac. |
| 122 // Allocates in the 0.5-1.5GB region. | 123 // Allocates in the 0.5-1.5GB region. |
| 123 random &= 0x3fffffff; | 124 random &= 0x3fffffff; |
| 124 random += 0x20000000; | 125 random += 0x20000000; |
| 125 #endif // defined(ARCH_CPU_X86_64) | 126 #endif // defined(ARCH_CPU_X86_64) |
| 126 random &= kPageAllocationGranularityBaseMask; | 127 random &= kPageAllocationGranularityBaseMask; |
| 127 return reinterpret_cast<void*>(random); | 128 return reinterpret_cast<void*>(random); |
| 128 } | 129 } |
| 129 | 130 |
| 130 } // namespace base | 131 } // namespace base |
| OLD | NEW |