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 "wtf/allocator/AddressSpaceRandomization.h" | 5 #include "base/allocator/partition_allocator/address_space_randomization.h" |
| 6 | 6 |
| 7 #include "wtf/SpinLock.h" | 7 #include "base/allocator/partition_allocator/page_allocator.h" |
| 8 #include "wtf/allocator/PageAllocator.h" | 8 #include "base/synchronization/spin_lock.h" |
| 9 | 9 |
| 10 #if OS(WIN) | 10 #if defined(OS_WIN) |
|
Primiano Tucci (use gerrit)
2016/11/22 14:28:32
IWYU: #include "build/build_config.h"
palmer
2016/11/24 01:05:55
Done.
| |
| 11 #include <windows.h> | 11 #include <windows.h> |
| 12 #else | 12 #else |
| 13 #include <sys/time.h> | 13 #include <sys/time.h> |
| 14 #include <unistd.h> | 14 #include <unistd.h> |
| 15 #endif | 15 #endif |
| 16 | 16 |
| 17 namespace WTF { | 17 namespace base { |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 // This is the same PRNG as used by tcmalloc for mapping address randomness; | 21 // This is the same PRNG as used by tcmalloc for mapping address randomness; |
| 22 // see http://burtleburtle.net/bob/rand/smallprng.html | 22 // see http://burtleburtle.net/bob/rand/smallprng.html |
| 23 struct ranctx { | 23 struct ranctx { |
| 24 SpinLock lock; | 24 subtle::SpinLock lock; |
| 25 bool initialized; | 25 bool initialized; |
| 26 uint32_t a; | 26 uint32_t a; |
| 27 uint32_t b; | 27 uint32_t b; |
| 28 uint32_t c; | 28 uint32_t c; |
| 29 uint32_t d; | 29 uint32_t d; |
| 30 }; | 30 }; |
| 31 | 31 |
| 32 #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k)))) | 32 #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k)))) |
| 33 | 33 |
| 34 uint32_t ranvalInternal(ranctx* x) { | 34 uint32_t ranvalInternal(ranctx* x) { |
| 35 uint32_t e = x->a - rot(x->b, 27); | 35 uint32_t e = x->a - rot(x->b, 27); |
| 36 x->a = x->b ^ rot(x->c, 17); | 36 x->a = x->b ^ rot(x->c, 17); |
| 37 x->b = x->c + x->d; | 37 x->b = x->c + x->d; |
| 38 x->c = x->d + e; | 38 x->c = x->d + e; |
| 39 x->d = e + x->a; | 39 x->d = e + x->a; |
| 40 return x->d; | 40 return x->d; |
| 41 } | 41 } |
| 42 | 42 |
| 43 #undef rot | 43 #undef rot |
| 44 | 44 |
| 45 uint32_t ranval(ranctx* x) { | 45 uint32_t ranval(ranctx* x) { |
| 46 SpinLock::Guard guard(x->lock); | 46 subtle::SpinLock::Guard guard(x->lock); |
| 47 if (UNLIKELY(!x->initialized)) { | 47 if (UNLIKELY(!x->initialized)) { |
| 48 x->initialized = true; | 48 x->initialized = true; |
| 49 char c; | 49 char c; |
| 50 uint32_t seed = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(&c)); | 50 uint32_t seed = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(&c)); |
| 51 uint32_t pid; | 51 uint32_t pid; |
| 52 uint32_t usec; | 52 uint32_t usec; |
| 53 #if OS(WIN) | 53 #if defined(OS_WIN) |
| 54 pid = GetCurrentProcessId(); | 54 pid = GetCurrentProcessId(); |
| 55 SYSTEMTIME st; | 55 SYSTEMTIME st; |
| 56 GetSystemTime(&st); | 56 GetSystemTime(&st); |
| 57 usec = static_cast<uint32_t>(st.wMilliseconds * 1000); | 57 usec = static_cast<uint32_t>(st.wMilliseconds * 1000); |
| 58 #else | 58 #else |
| 59 pid = static_cast<uint32_t>(getpid()); | 59 pid = static_cast<uint32_t>(getpid()); |
| 60 struct timeval tv; | 60 struct timeval tv; |
| 61 gettimeofday(&tv, 0); | 61 gettimeofday(&tv, 0); |
| 62 usec = static_cast<uint32_t>(tv.tv_usec); | 62 usec = static_cast<uint32_t>(tv.tv_usec); |
| 63 #endif | 63 #endif |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 81 // address, we balance good ASLR against not fragmenting the address | 81 // address, we balance good ASLR against not fragmenting the address |
| 82 // space too badly. | 82 // space too badly. |
| 83 void* getRandomPageBase() { | 83 void* getRandomPageBase() { |
| 84 uintptr_t random; | 84 uintptr_t random; |
| 85 random = static_cast<uintptr_t>(ranval(&s_ranctx)); | 85 random = static_cast<uintptr_t>(ranval(&s_ranctx)); |
| 86 #if CPU(X86_64) | 86 #if CPU(X86_64) |
| 87 random <<= 32UL; | 87 random <<= 32UL; |
| 88 random |= static_cast<uintptr_t>(ranval(&s_ranctx)); | 88 random |= static_cast<uintptr_t>(ranval(&s_ranctx)); |
| 89 // This address mask gives a low liklihood of address space collisions. | 89 // This address mask gives a low liklihood of address space collisions. |
| 90 // We handle the situation gracefully if there is a collision. | 90 // We handle the situation gracefully if there is a collision. |
| 91 #if OS(WIN) | 91 #if defined(OS_WIN) |
| 92 // 64-bit Windows has a bizarrely small 8TB user address space. | 92 // 64-bit Windows has a bizarrely small 8TB user address space. |
| 93 // Allocates in the 1-5TB region. | 93 // Allocates in the 1-5TB region. |
| 94 // TODO(cevans): I think Win 8.1 has 47-bits like Linux. | 94 // TODO(cevans): I think Win 8.1 has 47-bits like Linux. |
| 95 random &= 0x3ffffffffffUL; | 95 random &= 0x3ffffffffffUL; |
| 96 random += 0x10000000000UL; | 96 random += 0x10000000000UL; |
| 97 #elif defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | 97 #elif defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
| 98 // This range is copied from the TSan source, but works for all tools. | 98 // This range is copied from the TSan source, but works for all tools. |
| 99 random &= 0x007fffffffffUL; | 99 random &= 0x007fffffffffUL; |
| 100 random += 0x7e8000000000UL; | 100 random += 0x7e8000000000UL; |
| 101 #else | 101 #else |
| 102 // Linux and OS X support the full 47-bit user space of x64 processors. | 102 // Linux and OS X support the full 47-bit user space of x64 processors. |
| 103 random &= 0x3fffffffffffUL; | 103 random &= 0x3fffffffffffUL; |
| 104 #endif | 104 #endif |
| 105 #elif CPU(ARM64) | 105 #elif CPU(ARM64) |
| 106 // ARM64 on Linux has 39-bit user space. | 106 // ARM64 on Linux has 39-bit user space. |
| 107 random &= 0x3fffffffffUL; | 107 random &= 0x3fffffffffUL; |
| 108 random += 0x1000000000UL; | 108 random += 0x1000000000UL; |
| 109 #else // !CPU(X86_64) && !CPU(ARM64) | 109 #else // !CPU(X86_64) && !CPU(ARM64) |
| 110 #if OS(WIN) | 110 #if defined(OS_WIN) |
| 111 // On win32 host systems the randomization plus huge alignment causes | 111 // On win32 host systems the randomization plus huge alignment causes |
| 112 // excessive fragmentation. Plus most of these systems lack ASLR, so the | 112 // excessive fragmentation. Plus most of these systems lack ASLR, so the |
| 113 // randomization isn't buying anything. In that case we just skip it. | 113 // randomization isn't buying anything. In that case we just skip it. |
| 114 // TODO(jschuh): Just dump the randomization when HE-ASLR is present. | 114 // TODO(jschuh): Just dump the randomization when HE-ASLR is present. |
| 115 static BOOL isWow64 = -1; | 115 static BOOL isWow64 = -1; |
| 116 if (isWow64 == -1 && !IsWow64Process(GetCurrentProcess(), &isWow64)) | 116 if (isWow64 == -1 && !IsWow64Process(GetCurrentProcess(), &isWow64)) |
| 117 isWow64 = FALSE; | 117 isWow64 = FALSE; |
| 118 if (!isWow64) | 118 if (!isWow64) |
| 119 return nullptr; | 119 return nullptr; |
| 120 #endif // OS(WIN) | 120 #endif // defined(OS_WIN) |
| 121 // This is a good range on Windows, Linux and Mac. | 121 // This is a good range on Windows, Linux and Mac. |
| 122 // Allocates in the 0.5-1.5GB region. | 122 // Allocates in the 0.5-1.5GB region. |
| 123 random &= 0x3fffffff; | 123 random &= 0x3fffffff; |
| 124 random += 0x20000000; | 124 random += 0x20000000; |
| 125 #endif // CPU(X86_64) | 125 #endif // CPU(X86_64) |
| 126 random &= kPageAllocationGranularityBaseMask; | 126 random &= kPageAllocationGranularityBaseMask; |
| 127 return reinterpret_cast<void*>(random); | 127 return reinterpret_cast<void*>(random); |
| 128 } | 128 } |
| 129 | 129 |
| 130 } // namespace WTF | 130 } // namespace base |
| OLD | NEW |