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 |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
79 | 79 |
80 static struct ranctx s_ranctx; | 80 static struct ranctx s_ranctx; |
81 | 81 |
82 } | 82 } |
83 | 83 |
84 // Calculates a random preferred mapping address. In calculating an | 84 // Calculates a random preferred mapping address. In calculating an |
85 // address, we balance good ASLR against not fragmenting the address | 85 // address, we balance good ASLR against not fragmenting the address |
86 // space too badly. | 86 // space too badly. |
87 void* getRandomPageBase() | 87 void* getRandomPageBase() |
88 { | 88 { |
89 #if defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | |
Alexander Potapenko
2015/10/13 16:02:42
You also need "if !defined(MEMORY_TOOL_REPLACES_AL
jschuh
2015/10/13 17:44:13
I dropped this approach and just added a custom ra
| |
90 return nullptr; | |
91 #else | |
89 uintptr_t random; | 92 uintptr_t random; |
90 random = static_cast<uintptr_t>(ranval(&s_ranctx)); | 93 random = static_cast<uintptr_t>(ranval(&s_ranctx)); |
91 #if CPU(X86_64) | 94 #if CPU(X86_64) |
92 random <<= 32UL; | 95 random <<= 32UL; |
93 random |= static_cast<uintptr_t>(ranval(&s_ranctx)); | 96 random |= static_cast<uintptr_t>(ranval(&s_ranctx)); |
94 // This address mask gives a low liklihood of address space collisions. | 97 // This address mask gives a low liklihood of address space collisions. |
95 // We handle the situation gracefully if there is a collision. | 98 // We handle the situation gracefully if there is a collision. |
96 #if OS(WIN) | 99 #if OS(WIN) |
97 // 64-bit Windows has a bizarrely small 8TB user address space. | 100 // 64-bit Windows has a bizarrely small 8TB user address space. |
98 // Allocates in the 1-5TB region. | 101 // Allocates in the 1-5TB region. |
99 // TODO(cevans): I think Win 8.1 has 47-bits like Linux. | 102 // TODO(cevans): I think Win 8.1 has 47-bits like Linux. |
100 random &= 0x3ffffffffffUL; | 103 random &= 0x3ffffffffffUL; |
101 random += 0x10000000000UL; | 104 random += 0x10000000000UL; |
102 #else | 105 #else |
103 // Linux and OS X support the full 47-bit user space of x64 processors. | 106 // Linux and OS X support the full 47-bit user space of x64 processors. |
104 random &= 0x3fffffffffffUL; | 107 random &= 0x3fffffffffffUL; |
105 #endif | 108 #endif |
106 #elif CPU(ARM64) | 109 #elif CPU(ARM64) |
107 // ARM64 on Linux has 39-bit user space. | 110 // ARM64 on Linux has 39-bit user space. |
108 random &= 0x3fffffffffUL; | 111 random &= 0x3fffffffffUL; |
109 random += 0x1000000000UL; | 112 random += 0x1000000000UL; |
110 #else // !CPU(X86_64) && !CPU(ARM64) | 113 #else // !CPU(X86_64) && !CPU(ARM64) |
114 #if OS(WIN) | |
115 // On win32 host systems the randomization plus huge alignment causes | |
116 // excessive fragmentation. Plus most of these systems lack ASLR, so the | |
117 // randomization isn't buying anything. In that case we just skip it. | |
118 // TODO(jschuh): Just dump the randomization when HE-ASLR is present. | |
119 static BOOL isWow64 = -1; | |
120 if (isWow64 == -1 && !IsWow64Process(GetCurrentProcess(), &isWow64)) | |
121 isWow64 = FALSE; | |
122 if (!isWow64) | |
123 return nullptr; | |
124 #endif // OS(WIN) | |
111 // This is a good range on Windows, Linux and Mac. | 125 // This is a good range on Windows, Linux and Mac. |
112 // Allocates in the 0.5-1.5GB region. | 126 // Allocates in the 0.5-1.5GB region. |
113 random &= 0x3fffffff; | 127 random &= 0x3fffffff; |
114 random += 0x20000000; | 128 random += 0x20000000; |
115 #endif // CPU(X86_64) | 129 #endif // CPU(X86_64) |
116 random &= kPageAllocationGranularityBaseMask; | 130 random &= kPageAllocationGranularityBaseMask; |
117 return reinterpret_cast<void*>(random); | 131 return reinterpret_cast<void*>(random); |
132 #endif // !MEMORY_TOOL_REPLACES_ALLOCATOR | |
118 } | 133 } |
119 | 134 |
120 } | 135 } |
OLD | NEW |