Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(15)

Side by Side Diff: third_party/WebKit/Source/wtf/AddressSpaceRandomization.cpp

Issue 1436153002: Apply clang-format with Chromium-style without column limit. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 int 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 uint32_t e = x->a - rot(x->b, 27);
37 uint32_t e = x->a - rot(x->b, 27); 37 x->a = x->b ^ rot(x->c, 17);
38 x->a = x->b ^ rot(x->c, 17); 38 x->b = x->c + x->d;
39 x->b = x->c + x->d; 39 x->c = x->d + e;
40 x->c = x->d + e; 40 x->d = e + x->a;
41 x->d = e + x->a; 41 return x->d;
42 return x->d;
43 } 42 }
44 43
45 #undef rot 44 #undef rot
46 45
47 uint32_t ranval(ranctx* x) 46 uint32_t ranval(ranctx* x) {
48 { 47 spinLockLock(&x->lock);
49 spinLockLock(&x->lock); 48 if (UNLIKELY(!x->initialized)) {
50 if (UNLIKELY(!x->initialized)) { 49 x->initialized = true;
51 x->initialized = true; 50 char c;
52 char c; 51 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)); 52 uint32_t pid;
54 uint32_t pid; 53 uint32_t usec;
55 uint32_t usec;
56 #if OS(WIN) 54 #if OS(WIN)
57 pid = GetCurrentProcessId(); 55 pid = GetCurrentProcessId();
58 SYSTEMTIME st; 56 SYSTEMTIME st;
59 GetSystemTime(&st); 57 GetSystemTime(&st);
60 usec = static_cast<uint32_t>(st.wMilliseconds * 1000); 58 usec = static_cast<uint32_t>(st.wMilliseconds * 1000);
61 #else 59 #else
62 pid = static_cast<uint32_t>(getpid()); 60 pid = static_cast<uint32_t>(getpid());
63 struct timeval tv; 61 struct timeval tv;
64 gettimeofday(&tv, 0); 62 gettimeofday(&tv, 0);
65 usec = static_cast<uint32_t>(tv.tv_usec); 63 usec = static_cast<uint32_t>(tv.tv_usec);
66 #endif 64 #endif
67 seed ^= pid; 65 seed ^= pid;
68 seed ^= usec; 66 seed ^= usec;
69 x->a = 0xf1ea5eed; 67 x->a = 0xf1ea5eed;
70 x->b = x->c = x->d = seed; 68 x->b = x->c = x->d = seed;
71 for (int i = 0; i < 20; ++i) { 69 for (int i = 0; i < 20; ++i) {
72 (void) ranvalInternal(x); 70 (void)ranvalInternal(x);
73 }
74 } 71 }
75 uint32_t ret = ranvalInternal(x); 72 }
76 spinLockUnlock(&x->lock); 73 uint32_t ret = ranvalInternal(x);
77 return ret; 74 spinLockUnlock(&x->lock);
75 return ret;
78 } 76 }
79 77
80 static struct ranctx s_ranctx; 78 static struct ranctx s_ranctx;
81
82 } 79 }
83 80
84 // Calculates a random preferred mapping address. In calculating an 81 // Calculates a random preferred mapping address. In calculating an
85 // address, we balance good ASLR against not fragmenting the address 82 // address, we balance good ASLR against not fragmenting the address
86 // space too badly. 83 // space too badly.
87 void* getRandomPageBase() 84 void* getRandomPageBase() {
88 { 85 uintptr_t random;
89 uintptr_t random; 86 random = static_cast<uintptr_t>(ranval(&s_ranctx));
90 random = static_cast<uintptr_t>(ranval(&s_ranctx));
91 #if CPU(X86_64) 87 #if CPU(X86_64)
92 random <<= 32UL; 88 random <<= 32UL;
93 random |= static_cast<uintptr_t>(ranval(&s_ranctx)); 89 random |= static_cast<uintptr_t>(ranval(&s_ranctx));
94 // This address mask gives a low liklihood of address space collisions. 90 // This address mask gives a low liklihood of address space collisions.
95 // We handle the situation gracefully if there is a collision. 91 // We handle the situation gracefully if there is a collision.
96 #if OS(WIN) 92 #if OS(WIN)
97 // 64-bit Windows has a bizarrely small 8TB user address space. 93 // 64-bit Windows has a bizarrely small 8TB user address space.
98 // Allocates in the 1-5TB region. 94 // Allocates in the 1-5TB region.
99 // TODO(cevans): I think Win 8.1 has 47-bits like Linux. 95 // TODO(cevans): I think Win 8.1 has 47-bits like Linux.
100 random &= 0x3ffffffffffUL; 96 random &= 0x3ffffffffffUL;
101 random += 0x10000000000UL; 97 random += 0x10000000000UL;
102 #elif defined(MEMORY_TOOL_REPLACES_ALLOCATOR) 98 #elif defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
103 // This range is copied from the TSan source, but works for all tools. 99 // This range is copied from the TSan source, but works for all tools.
104 random &= 0x007fffffffffUL; 100 random &= 0x007fffffffffUL;
105 random += 0x7e8000000000UL; 101 random += 0x7e8000000000UL;
106 #else 102 #else
107 // 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.
108 random &= 0x3fffffffffffUL; 104 random &= 0x3fffffffffffUL;
109 #endif 105 #endif
110 #elif CPU(ARM64) 106 #elif CPU(ARM64)
111 // ARM64 on Linux has 39-bit user space. 107 // ARM64 on Linux has 39-bit user space.
112 random &= 0x3fffffffffUL; 108 random &= 0x3fffffffffUL;
113 random += 0x1000000000UL; 109 random += 0x1000000000UL;
114 #else // !CPU(X86_64) && !CPU(ARM64) 110 #else // !CPU(X86_64) && !CPU(ARM64)
115 #if OS(WIN) 111 #if OS(WIN)
116 // On win32 host systems the randomization plus huge alignment causes 112 // On win32 host systems the randomization plus huge alignment causes
117 // excessive fragmentation. Plus most of these systems lack ASLR, so the 113 // excessive fragmentation. Plus most of these systems lack ASLR, so the
118 // randomization isn't buying anything. In that case we just skip it. 114 // randomization isn't buying anything. In that case we just skip it.
119 // TODO(jschuh): Just dump the randomization when HE-ASLR is present. 115 // TODO(jschuh): Just dump the randomization when HE-ASLR is present.
120 static BOOL isWow64 = -1; 116 static BOOL isWow64 = -1;
121 if (isWow64 == -1 && !IsWow64Process(GetCurrentProcess(), &isWow64)) 117 if (isWow64 == -1 && !IsWow64Process(GetCurrentProcess(), &isWow64))
122 isWow64 = FALSE; 118 isWow64 = FALSE;
123 if (!isWow64) 119 if (!isWow64)
124 return nullptr; 120 return nullptr;
125 #endif // OS(WIN) 121 #endif // OS(WIN)
126 // This is a good range on Windows, Linux and Mac. 122 // This is a good range on Windows, Linux and Mac.
127 // Allocates in the 0.5-1.5GB region. 123 // Allocates in the 0.5-1.5GB region.
128 random &= 0x3fffffff; 124 random &= 0x3fffffff;
129 random += 0x20000000; 125 random += 0x20000000;
130 #endif // CPU(X86_64) 126 #endif // CPU(X86_64)
131 random &= kPageAllocationGranularityBaseMask; 127 random &= kPageAllocationGranularityBaseMask;
132 return reinterpret_cast<void*>(random); 128 return reinterpret_cast<void*>(random);
133 } 129 }
134
135 } 130 }
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/AddressSpaceRandomization.h ('k') | third_party/WebKit/Source/wtf/Alias.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698