OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 13 matching lines...) Expand all Loading... |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #include "wtf/allocator/PageAllocator.h" | 31 #include "wtf/allocator/PageAllocator.h" |
32 | 32 |
33 #include "wtf/Assertions.h" | 33 #include "wtf/Assertions.h" |
| 34 #include "wtf/Atomics.h" |
34 #include "wtf/allocator/AddressSpaceRandomization.h" | 35 #include "wtf/allocator/AddressSpaceRandomization.h" |
35 | 36 |
36 #include <limits.h> | 37 #include <limits.h> |
37 | 38 |
38 #if OS(POSIX) | 39 #if OS(POSIX) |
39 | 40 |
40 #include <errno.h> | 41 #include <errno.h> |
41 #include <sys/mman.h> | 42 #include <sys/mman.h> |
42 | 43 |
43 #ifndef MADV_FREE | 44 #ifndef MADV_FREE |
44 #define MADV_FREE MADV_DONTNEED | 45 #define MADV_FREE MADV_DONTNEED |
45 #endif | 46 #endif |
46 | 47 |
47 #ifndef MAP_ANONYMOUS | 48 #ifndef MAP_ANONYMOUS |
48 #define MAP_ANONYMOUS MAP_ANON | 49 #define MAP_ANONYMOUS MAP_ANON |
49 #endif | 50 #endif |
50 | 51 |
51 // On POSIX memmap uses a nearby address if the hint address is blocked. | 52 // On POSIX memmap uses a nearby address if the hint address is blocked. |
52 static const bool kHintIsAdvisory = true; | 53 static const bool kHintIsAdvisory = true; |
53 static uint32_t allocPageErrorCode = 0; | 54 static uint32_t s_allocPageErrorCode = 0; |
54 | 55 |
55 #elif OS(WIN) | 56 #elif OS(WIN) |
56 | 57 |
57 #include <windows.h> | 58 #include <windows.h> |
58 | 59 |
59 // VirtualAlloc will fail if allocation at the hint address is blocked. | 60 // VirtualAlloc will fail if allocation at the hint address is blocked. |
60 static const bool kHintIsAdvisory = false; | 61 static const bool kHintIsAdvisory = false; |
61 static uint32_t allocPageErrorCode = ERROR_SUCCESS; | 62 static uint32_t s_allocPageErrorCode = ERROR_SUCCESS; |
62 | 63 |
63 #else | 64 #else |
64 #error Unknown OS | 65 #error Unknown OS |
65 #endif // OS(POSIX) | 66 #endif // OS(POSIX) |
66 | 67 |
67 namespace WTF { | 68 namespace WTF { |
68 | 69 |
69 // This internal function wraps the OS-specific page allocation call. The | 70 // This internal function wraps the OS-specific page allocation call. The |
70 // behavior of the hint address is determined by the kHintIsAdvisory constant. | 71 // behavior of the hint address is determined by the kHintIsAdvisory constant. |
71 // If true, a non-zero hint is advisory and the returned address may differ from | 72 // If true, a non-zero hint is advisory and the returned address may differ from |
72 // the hint. If false, the hint is mandatory and a successful allocation will | 73 // the hint. If false, the hint is mandatory and a successful allocation will |
73 // not differ from the hint. | 74 // not differ from the hint. |
74 static void* systemAllocPages(void* hint, size_t len, PageAccessibilityConfigura
tion pageAccessibility) | 75 static void* systemAllocPages(void* hint, size_t len, PageAccessibilityConfigura
tion pageAccessibility) |
75 { | 76 { |
76 ASSERT(!(len & kPageAllocationGranularityOffsetMask)); | 77 ASSERT(!(len & kPageAllocationGranularityOffsetMask)); |
77 ASSERT(!(reinterpret_cast<uintptr_t>(hint) & kPageAllocationGranularityOffse
tMask)); | 78 ASSERT(!(reinterpret_cast<uintptr_t>(hint) & kPageAllocationGranularityOffse
tMask)); |
78 void* ret; | 79 void* ret; |
79 #if OS(WIN) | 80 #if OS(WIN) |
80 DWORD accessFlag = pageAccessibility == PageAccessible ? PAGE_READWRITE : PA
GE_NOACCESS; | 81 DWORD accessFlag = pageAccessibility == PageAccessible ? PAGE_READWRITE : PA
GE_NOACCESS; |
81 ret = VirtualAlloc(hint, len, MEM_RESERVE | MEM_COMMIT, accessFlag); | 82 ret = VirtualAlloc(hint, len, MEM_RESERVE | MEM_COMMIT, accessFlag); |
82 if (!ret) | 83 if (!ret) |
83 allocPageErrorCode = GetLastError(); | 84 releaseStore(&s_allocPageErrorCode, GetLastError()); |
84 #else | 85 #else |
85 int accessFlag = pageAccessibility == PageAccessible ? (PROT_READ | PROT_WRI
TE) : PROT_NONE; | 86 int accessFlag = pageAccessibility == PageAccessible ? (PROT_READ | PROT_WRI
TE) : PROT_NONE; |
86 ret = mmap(hint, len, accessFlag, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); | 87 ret = mmap(hint, len, accessFlag, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); |
87 if (ret == MAP_FAILED) { | 88 if (ret == MAP_FAILED) { |
88 allocPageErrorCode = errno; | 89 releaseStore(&s_allocPageErrorCode, errno); |
89 ret = 0; | 90 ret = 0; |
90 } | 91 } |
91 #endif | 92 #endif |
92 return ret; | 93 return ret; |
93 } | 94 } |
94 | 95 |
95 // Trims base to given length and alignment. Windows returns null on failure and
frees base. | 96 // Trims base to given length and alignment. Windows returns null on failure and
frees base. |
96 static void* trimMapping(void *base, size_t baseLen, size_t trimLen, uintptr_t a
lign, PageAccessibilityConfiguration pageAccessibility) | 97 static void* trimMapping(void *base, size_t baseLen, size_t trimLen, uintptr_t a
lign, PageAccessibilityConfiguration pageAccessibility) |
97 { | 98 { |
98 size_t preSlack = reinterpret_cast<uintptr_t>(base) & (align - 1); | 99 size_t preSlack = reinterpret_cast<uintptr_t>(base) & (align - 1); |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 // DiscardVirtualMemory is buggy in Win10 SP0, so fall back to MEM_RESET on
failure. | 264 // DiscardVirtualMemory is buggy in Win10 SP0, so fall back to MEM_RESET on
failure. |
264 if (ret) { | 265 if (ret) { |
265 void* ret = VirtualAlloc(addr, len, MEM_RESET, PAGE_READWRITE); | 266 void* ret = VirtualAlloc(addr, len, MEM_RESET, PAGE_READWRITE); |
266 RELEASE_ASSERT(ret); | 267 RELEASE_ASSERT(ret); |
267 } | 268 } |
268 #endif | 269 #endif |
269 } | 270 } |
270 | 271 |
271 uint32_t getAllocPageErrorCode() | 272 uint32_t getAllocPageErrorCode() |
272 { | 273 { |
273 return allocPageErrorCode; | 274 return acquireLoad(&s_allocPageErrorCode); |
274 } | 275 } |
275 | 276 |
276 } // namespace WTF | 277 } // namespace WTF |
277 | 278 |
OLD | NEW |