OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | |
Tom Sepez
2014/06/26 17:06:54
Nit: no (C), 2014.
| |
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 "wtf/PageAllocator.h" | |
32 #include "wtf/ProcessID.h" | |
33 #include "wtf/SpinLock.h" | |
34 | |
35 namespace WTF { | |
36 | |
37 // This is the same PRNG as used by tcmalloc for mapping address randomness; | |
38 // see http://burtleburtle.net/bob/rand/smallprng.html | |
39 struct ranctx { | |
40 int lock; | |
41 bool initialized; | |
42 uint32_t a; | |
43 uint32_t b; | |
44 uint32_t c; | |
45 uint32_t d; | |
46 }; | |
47 | |
48 #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k)))) | |
49 | |
50 uint32_t ranvalInternal(ranctx* x) | |
51 { | |
52 uint32_t e = x->a - rot(x->b, 27); | |
53 x->a = x->b ^ rot(x->c, 17); | |
54 x->b = x->c + x->d; | |
55 x->c = x->d + e; | |
56 x->d = e + x->a; | |
57 return x->d; | |
58 } | |
59 | |
60 #undef rot | |
61 | |
62 uint32_t ranval(ranctx* x) | |
63 { | |
64 spinLockLock(&x->lock); | |
65 if (UNLIKELY(!x->initialized)) { | |
66 x->initialized = true; | |
67 char c; | |
68 uint32_t seed = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(&c)); | |
69 seed ^= static_cast<uint32_t>(getCurrentProcessID()); | |
70 x->a = 0xf1ea5eed; | |
71 x->b = x->c = x->d = seed; | |
72 for (int i = 0; i < 20; ++i) { | |
73 (void) ranvalInternal(x); | |
74 } | |
75 } | |
76 uint32_t ret = ranvalInternal(x); | |
77 spinLockUnlock(&x->lock); | |
78 return ret; | |
79 } | |
80 | |
81 // Calculates a random preferred mapping address. In calculating an | |
82 // address, we balance good ASLR against not fragmenting the address | |
83 // space too badly. | |
84 void* getRandomPageBase() | |
85 { | |
86 static struct ranctx s_ranctx; | |
87 uintptr_t random; | |
88 random = static_cast<uintptr_t>(ranval(&s_ranctx)); | |
89 #if CPU(X86_64) | |
90 random <<= 32UL; | |
91 random |= static_cast<uintptr_t>(ranval(&s_ranctx)); | |
92 // This address mask gives a low liklihood of address space collisions. | |
93 // We handle the situation gracefully if there is a collision. | |
94 #if OS(WIN) | |
95 // 64-bit Windows has a bizarrely small 8TB user address space. | |
96 // Allocates in the 1-5TB region. | |
97 random &= 0x3ffffffffffUL; | |
98 random += 0x10000000000UL; | |
99 #else | |
100 // Linux and OS X support the full 47-bit user space of x64 processors. | |
101 random &= 0x3fffffffffffUL; | |
102 #endif | |
103 #elif CPU(ARM64) | |
104 // ARM64 on Linux has 39-bit user space. | |
105 random &= 0x3fffffffffUL; | |
106 random += 0x1000000000UL; | |
107 #else // !CPU(X86_64) && !CPU(ARM64) | |
108 // This is a good range on Windows, Linux and Mac. | |
109 // Allocates in the 0.5-1.5GB region. | |
110 random &= 0x3fffffff; | |
111 random += 0x20000000; | |
112 #endif // CPU(X86_64) | |
113 random &= kPageAllocationGranularityBaseMask; | |
114 return reinterpret_cast<void*>(random); | |
115 } | |
116 | |
117 } | |
OLD | NEW |