Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | |
|
Tom Sepez
2014/06/26 17:06:54
nit: no (C), year 2014.
Mads Ager (chromium)
2014/06/27 06:16:36
Done.
| |
| 3 * | |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions are | |
| 6 * met: | |
| 7 * | |
| 8 * * Redistributions of source code must retain the above copyright | |
| 9 * notice, this list of conditions and the following disclaimer. | |
| 10 * * Redistributions in binary form must reproduce the above | |
| 11 * copyright notice, this list of conditions and the following disclaimer | |
| 12 * in the documentation and/or other materials provided with the | |
| 13 * distribution. | |
| 14 * * Neither the name of Google Inc. nor the names of its | |
| 15 * contributors may be used to endorse or promote products derived from | |
| 16 * this software without specific prior written permission. | |
| 17 * | |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 29 */ | |
| 30 | |
| 31 #include "config.h" | |
| 32 #include "wtf/AddressSpaceRandomization.h" | |
| 33 | |
| 34 #include "wtf/PageAllocator.h" | |
| 35 #include "wtf/ProcessID.h" | |
| 36 #include "wtf/SpinLock.h" | |
| 37 | |
| 38 namespace WTF { | |
| 39 | |
| 40 // This is the same PRNG as used by tcmalloc for mapping address randomness; | |
| 41 // see http://burtleburtle.net/bob/rand/smallprng.html | |
| 42 struct ranctx { | |
| 43 int lock; | |
| 44 bool initialized; | |
| 45 uint32_t a; | |
| 46 uint32_t b; | |
| 47 uint32_t c; | |
| 48 uint32_t d; | |
| 49 }; | |
| 50 | |
| 51 #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k)))) | |
| 52 | |
| 53 uint32_t ranvalInternal(ranctx* x) | |
|
Tom Sepez
2014/06/26 17:06:54
nit: can this be static or empty namespace'd? I kn
Mads Ager (chromium)
2014/06/27 06:16:36
Yes, it definitely can and I agree that there is n
| |
| 54 { | |
| 55 uint32_t e = x->a - rot(x->b, 27); | |
| 56 x->a = x->b ^ rot(x->c, 17); | |
| 57 x->b = x->c + x->d; | |
| 58 x->c = x->d + e; | |
| 59 x->d = e + x->a; | |
| 60 return x->d; | |
| 61 } | |
| 62 | |
| 63 #undef rot | |
| 64 | |
| 65 uint32_t ranval(ranctx* x) | |
|
Tom Sepez
2014/06/26 17:06:54
ditto
Mads Ager (chromium)
2014/06/27 06:16:36
Done.
| |
| 66 { | |
| 67 spinLockLock(&x->lock); | |
| 68 if (UNLIKELY(!x->initialized)) { | |
| 69 x->initialized = true; | |
| 70 char c; | |
| 71 uint32_t seed = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(&c)); | |
| 72 seed ^= static_cast<uint32_t>(getCurrentProcessID()); | |
| 73 x->a = 0xf1ea5eed; | |
| 74 x->b = x->c = x->d = seed; | |
| 75 for (int i = 0; i < 20; ++i) { | |
| 76 (void) ranvalInternal(x); | |
| 77 } | |
| 78 } | |
| 79 uint32_t ret = ranvalInternal(x); | |
| 80 spinLockUnlock(&x->lock); | |
| 81 return ret; | |
| 82 } | |
| 83 | |
| 84 static struct ranctx s_ranctx; | |
| 85 | |
| 86 // Calculates a random preferred mapping address. In calculating an | |
| 87 // address, we balance good ASLR against not fragmenting the address | |
| 88 // space too badly. | |
| 89 void* getRandomPageBase() | |
| 90 { | |
| 91 uintptr_t random; | |
| 92 random = static_cast<uintptr_t>(ranval(&s_ranctx)); | |
| 93 #if CPU(X86_64) | |
| 94 random <<= 32UL; | |
| 95 random |= static_cast<uintptr_t>(ranval(&s_ranctx)); | |
| 96 // This address mask gives a low liklihood of address space collisions. | |
| 97 // We handle the situation gracefully if there is a collision. | |
| 98 #if OS(WIN) | |
| 99 // 64-bit Windows has a bizarrely small 8TB user address space. | |
| 100 // Allocates in the 1-5TB region. | |
| 101 random &= 0x3ffffffffffUL; | |
| 102 random += 0x10000000000UL; | |
| 103 #else | |
| 104 // Linux and OS X support the full 47-bit user space of x64 processors. | |
| 105 random &= 0x3fffffffffffUL; | |
| 106 #endif | |
| 107 #elif CPU(ARM64) | |
| 108 // ARM64 on Linux has 39-bit user space. | |
| 109 random &= 0x3fffffffffUL; | |
| 110 random += 0x1000000000UL; | |
| 111 #else // !CPU(X86_64) && !CPU(ARM64) | |
| 112 // This is a good range on Windows, Linux and Mac. | |
| 113 // Allocates in the 0.5-1.5GB region. | |
| 114 random &= 0x3fffffff; | |
| 115 random += 0x20000000; | |
| 116 #endif // CPU(X86_64) | |
| 117 random &= kPageAllocationGranularityBaseMask; | |
| 118 return reinterpret_cast<void*>(random); | |
| 119 } | |
| 120 | |
| 121 } | |
| OLD | NEW |