| 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 "config.h" | 5 #include "config.h" |
| 6 #include "wtf/AddressSpaceRandomization.h" | 6 #include "wtf/AddressSpaceRandomization.h" |
| 7 | 7 |
| 8 #include "wtf/PageAllocator.h" | 8 #include "wtf/PageAllocator.h" |
| 9 #include "wtf/ProcessID.h" | |
| 10 #include "wtf/SpinLock.h" | 9 #include "wtf/SpinLock.h" |
| 11 | 10 |
| 11 #if OS(WIN) |
| 12 #include <windows.h> |
| 13 #else |
| 14 #include <sys/time.h> |
| 15 #include <unistd.h> |
| 16 #endif |
| 17 |
| 12 namespace WTF { | 18 namespace WTF { |
| 13 | 19 |
| 14 namespace { | 20 namespace { |
| 15 | 21 |
| 16 // This is the same PRNG as used by tcmalloc for mapping address randomness; | 22 // This is the same PRNG as used by tcmalloc for mapping address randomness; |
| 17 // see http://burtleburtle.net/bob/rand/smallprng.html | 23 // see http://burtleburtle.net/bob/rand/smallprng.html |
| 18 struct ranctx { | 24 struct ranctx { |
| 19 int lock; | 25 int lock; |
| 20 bool initialized; | 26 bool initialized; |
| 21 uint32_t a; | 27 uint32_t a; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 38 | 44 |
| 39 #undef rot | 45 #undef rot |
| 40 | 46 |
| 41 uint32_t ranval(ranctx* x) | 47 uint32_t ranval(ranctx* x) |
| 42 { | 48 { |
| 43 spinLockLock(&x->lock); | 49 spinLockLock(&x->lock); |
| 44 if (UNLIKELY(!x->initialized)) { | 50 if (UNLIKELY(!x->initialized)) { |
| 45 x->initialized = true; | 51 x->initialized = true; |
| 46 char c; | 52 char c; |
| 47 uint32_t seed = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(&c)); | 53 uint32_t seed = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(&c)); |
| 48 seed ^= static_cast<uint32_t>(getCurrentProcessID()); | 54 uint32_t pid; |
| 55 uint32_t usec; |
| 56 #if OS(WIN) |
| 57 pid = GetCurrentProcessId(); |
| 58 SYSTEMTIME st; |
| 59 GetSystemTime(&st); |
| 60 usec = static_cast<uint32_t>(st.wMilliseconds * 1000); |
| 61 #else |
| 62 pid = static_cast<uint32_t>(getpid()); |
| 63 struct timeval tv; |
| 64 gettimeofday(&tv, 0); |
| 65 usec = static_cast<uint32_t>(tv.tv_usec); |
| 66 #endif |
| 67 seed ^= pid; |
| 68 seed ^= usec; |
| 49 x->a = 0xf1ea5eed; | 69 x->a = 0xf1ea5eed; |
| 50 x->b = x->c = x->d = seed; | 70 x->b = x->c = x->d = seed; |
| 51 for (int i = 0; i < 20; ++i) { | 71 for (int i = 0; i < 20; ++i) { |
| 52 (void) ranvalInternal(x); | 72 (void) ranvalInternal(x); |
| 53 } | 73 } |
| 54 } | 74 } |
| 55 uint32_t ret = ranvalInternal(x); | 75 uint32_t ret = ranvalInternal(x); |
| 56 spinLockUnlock(&x->lock); | 76 spinLockUnlock(&x->lock); |
| 57 return ret; | 77 return ret; |
| 58 } | 78 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 69 uintptr_t random; | 89 uintptr_t random; |
| 70 random = static_cast<uintptr_t>(ranval(&s_ranctx)); | 90 random = static_cast<uintptr_t>(ranval(&s_ranctx)); |
| 71 #if CPU(X86_64) | 91 #if CPU(X86_64) |
| 72 random <<= 32UL; | 92 random <<= 32UL; |
| 73 random |= static_cast<uintptr_t>(ranval(&s_ranctx)); | 93 random |= static_cast<uintptr_t>(ranval(&s_ranctx)); |
| 74 // This address mask gives a low liklihood of address space collisions. | 94 // This address mask gives a low liklihood of address space collisions. |
| 75 // We handle the situation gracefully if there is a collision. | 95 // We handle the situation gracefully if there is a collision. |
| 76 #if OS(WIN) | 96 #if OS(WIN) |
| 77 // 64-bit Windows has a bizarrely small 8TB user address space. | 97 // 64-bit Windows has a bizarrely small 8TB user address space. |
| 78 // Allocates in the 1-5TB region. | 98 // Allocates in the 1-5TB region. |
| 99 // TODO(cevans): I think Win 8.1 has 47-bits like Linux. |
| 79 random &= 0x3ffffffffffUL; | 100 random &= 0x3ffffffffffUL; |
| 80 random += 0x10000000000UL; | 101 random += 0x10000000000UL; |
| 81 #else | 102 #else |
| 82 // 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. |
| 83 random &= 0x3fffffffffffUL; | 104 random &= 0x3fffffffffffUL; |
| 84 #endif | 105 #endif |
| 85 #elif CPU(ARM64) | 106 #elif CPU(ARM64) |
| 86 // ARM64 on Linux has 39-bit user space. | 107 // ARM64 on Linux has 39-bit user space. |
| 87 random &= 0x3fffffffffUL; | 108 random &= 0x3fffffffffUL; |
| 88 random += 0x1000000000UL; | 109 random += 0x1000000000UL; |
| 89 #else // !CPU(X86_64) && !CPU(ARM64) | 110 #else // !CPU(X86_64) && !CPU(ARM64) |
| 90 // This is a good range on Windows, Linux and Mac. | 111 // This is a good range on Windows, Linux and Mac. |
| 91 // Allocates in the 0.5-1.5GB region. | 112 // Allocates in the 0.5-1.5GB region. |
| 92 random &= 0x3fffffff; | 113 random &= 0x3fffffff; |
| 93 random += 0x20000000; | 114 random += 0x20000000; |
| 94 #endif // CPU(X86_64) | 115 #endif // CPU(X86_64) |
| 95 random &= kPageAllocationGranularityBaseMask; | 116 random &= kPageAllocationGranularityBaseMask; |
| 96 return reinterpret_cast<void*>(random); | 117 return reinterpret_cast<void*>(random); |
| 97 } | 118 } |
| 98 | 119 |
| 99 } | 120 } |
| OLD | NEW |