| 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/SpinLock.h" | 9 #include "wtf/SpinLock.h" |
| 10 | 10 |
| 11 #if OS(WIN) | 11 #if OS(WIN) |
| 12 #include <windows.h> | 12 #include <windows.h> |
| 13 #else | 13 #else |
| 14 #include <sys/time.h> | 14 #include <sys/time.h> |
| 15 #include <unistd.h> | 15 #include <unistd.h> |
| 16 #endif | 16 #endif |
| 17 | 17 |
| 18 namespace WTF { | 18 namespace WTF { |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 // 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; |
| 23 // see http://burtleburtle.net/bob/rand/smallprng.html | 23 // see http://burtleburtle.net/bob/rand/smallprng.html |
| 24 struct ranctx { | 24 struct ranctx { |
| 25 int lock; | 25 SpinLock lock; |
| 26 bool initialized; | 26 bool initialized; |
| 27 uint32_t a; | 27 uint32_t a; |
| 28 uint32_t b; | 28 uint32_t b; |
| 29 uint32_t c; | 29 uint32_t c; |
| 30 uint32_t d; | 30 uint32_t d; |
| 31 }; | 31 }; |
| 32 | 32 |
| 33 #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k)))) | 33 #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k)))) |
| 34 | 34 |
| 35 uint32_t ranvalInternal(ranctx* x) | 35 uint32_t ranvalInternal(ranctx* x) |
| 36 { | 36 { |
| 37 uint32_t e = x->a - rot(x->b, 27); | 37 uint32_t e = x->a - rot(x->b, 27); |
| 38 x->a = x->b ^ rot(x->c, 17); | 38 x->a = x->b ^ rot(x->c, 17); |
| 39 x->b = x->c + x->d; | 39 x->b = x->c + x->d; |
| 40 x->c = x->d + e; | 40 x->c = x->d + e; |
| 41 x->d = e + x->a; | 41 x->d = e + x->a; |
| 42 return x->d; | 42 return x->d; |
| 43 } | 43 } |
| 44 | 44 |
| 45 #undef rot | 45 #undef rot |
| 46 | 46 |
| 47 uint32_t ranval(ranctx* x) | 47 uint32_t ranval(ranctx* x) |
| 48 { | 48 { |
| 49 spinLockLock(&x->lock); | 49 SpinLock::Guard guard(x->lock); |
| 50 if (UNLIKELY(!x->initialized)) { | 50 if (UNLIKELY(!x->initialized)) { |
| 51 x->initialized = true; | 51 x->initialized = true; |
| 52 char c; | 52 char c; |
| 53 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)); |
| 54 uint32_t pid; | 54 uint32_t pid; |
| 55 uint32_t usec; | 55 uint32_t usec; |
| 56 #if OS(WIN) | 56 #if OS(WIN) |
| 57 pid = GetCurrentProcessId(); | 57 pid = GetCurrentProcessId(); |
| 58 SYSTEMTIME st; | 58 SYSTEMTIME st; |
| 59 GetSystemTime(&st); | 59 GetSystemTime(&st); |
| 60 usec = static_cast<uint32_t>(st.wMilliseconds * 1000); | 60 usec = static_cast<uint32_t>(st.wMilliseconds * 1000); |
| 61 #else | 61 #else |
| 62 pid = static_cast<uint32_t>(getpid()); | 62 pid = static_cast<uint32_t>(getpid()); |
| 63 struct timeval tv; | 63 struct timeval tv; |
| 64 gettimeofday(&tv, 0); | 64 gettimeofday(&tv, 0); |
| 65 usec = static_cast<uint32_t>(tv.tv_usec); | 65 usec = static_cast<uint32_t>(tv.tv_usec); |
| 66 #endif | 66 #endif |
| 67 seed ^= pid; | 67 seed ^= pid; |
| 68 seed ^= usec; | 68 seed ^= usec; |
| 69 x->a = 0xf1ea5eed; | 69 x->a = 0xf1ea5eed; |
| 70 x->b = x->c = x->d = seed; | 70 x->b = x->c = x->d = seed; |
| 71 for (int i = 0; i < 20; ++i) { | 71 for (int i = 0; i < 20; ++i) { |
| 72 (void) ranvalInternal(x); | 72 (void) ranvalInternal(x); |
| 73 } | 73 } |
| 74 } | 74 } |
| 75 uint32_t ret = ranvalInternal(x); | 75 uint32_t ret = ranvalInternal(x); |
| 76 spinLockUnlock(&x->lock); | |
| 77 return ret; | 76 return ret; |
| 78 } | 77 } |
| 79 | 78 |
| 80 static struct ranctx s_ranctx; | 79 static struct ranctx s_ranctx; |
| 81 | 80 |
| 82 } | 81 } |
| 83 | 82 |
| 84 // Calculates a random preferred mapping address. In calculating an | 83 // Calculates a random preferred mapping address. In calculating an |
| 85 // address, we balance good ASLR against not fragmenting the address | 84 // address, we balance good ASLR against not fragmenting the address |
| 86 // space too badly. | 85 // space too badly. |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 // This is a good range on Windows, Linux and Mac. | 125 // This is a good range on Windows, Linux and Mac. |
| 127 // Allocates in the 0.5-1.5GB region. | 126 // Allocates in the 0.5-1.5GB region. |
| 128 random &= 0x3fffffff; | 127 random &= 0x3fffffff; |
| 129 random += 0x20000000; | 128 random += 0x20000000; |
| 130 #endif // CPU(X86_64) | 129 #endif // CPU(X86_64) |
| 131 random &= kPageAllocationGranularityBaseMask; | 130 random &= kPageAllocationGranularityBaseMask; |
| 132 return reinterpret_cast<void*>(random); | 131 return reinterpret_cast<void*>(random); |
| 133 } | 132 } |
| 134 | 133 |
| 135 } | 134 } |
| OLD | NEW |