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 17 matching lines...) Expand all Loading... |
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 "config.h" | 31 #include "config.h" |
32 #include "wtf/PageAllocator.h" | 32 #include "wtf/PageAllocator.h" |
33 | 33 |
34 #include "wtf/Assertions.h" | 34 #include "wtf/Assertions.h" |
35 #include "wtf/CPU.h" | 35 #include "wtf/CPU.h" |
36 #include "wtf/CryptographicallyRandomNumber.h" | 36 #include "wtf/CryptographicallyRandomNumber.h" |
37 | 37 |
38 #if OS(UNIX) | 38 #if OS(POSIX) |
39 | 39 |
40 #include <sys/mman.h> | 40 #include <sys/mman.h> |
41 | 41 |
42 #ifndef MADV_FREE | 42 #ifndef MADV_FREE |
43 #define MADV_FREE MADV_DONTNEED | 43 #define MADV_FREE MADV_DONTNEED |
44 #endif | 44 #endif |
45 | 45 |
46 #ifndef MAP_ANONYMOUS | 46 #ifndef MAP_ANONYMOUS |
47 #define MAP_ANONYMOUS MAP_ANON | 47 #define MAP_ANONYMOUS MAP_ANON |
48 #endif | 48 #endif |
49 | 49 |
50 #elif OS(WIN) | 50 #elif OS(WIN) |
51 | 51 |
52 #include <windows.h> | 52 #include <windows.h> |
53 | 53 |
54 #else | 54 #else |
55 #error Unknown OS | 55 #error Unknown OS |
56 #endif // OS(UNIX) | 56 #endif // OS(POSIX) |
57 | 57 |
58 namespace WTF { | 58 namespace WTF { |
59 | 59 |
60 void* allocSuperPages(void* addr, size_t len) | 60 void* allocSuperPages(void* addr, size_t len) |
61 { | 61 { |
62 ASSERT(!(len & kSuperPageOffsetMask)); | 62 ASSERT(!(len & kSuperPageOffsetMask)); |
63 ASSERT(!(reinterpret_cast<uintptr_t>(addr) & kSuperPageOffsetMask)); | 63 ASSERT(!(reinterpret_cast<uintptr_t>(addr) & kSuperPageOffsetMask)); |
64 #if OS(UNIX) | 64 #if OS(POSIX) |
65 char* ptr = reinterpret_cast<char*>(mmap(addr, len, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0)); | 65 char* ptr = reinterpret_cast<char*>(mmap(addr, len, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0)); |
66 RELEASE_ASSERT(ptr != MAP_FAILED); | 66 RELEASE_ASSERT(ptr != MAP_FAILED); |
67 // If our requested address collided with another mapping, there's a | 67 // If our requested address collided with another mapping, there's a |
68 // chance we'll get back an unaligned address. We fix this by attempting | 68 // chance we'll get back an unaligned address. We fix this by attempting |
69 // the allocation again, but with enough slack pages that we can find | 69 // the allocation again, but with enough slack pages that we can find |
70 // correct alignment within the allocation. | 70 // correct alignment within the allocation. |
71 if (UNLIKELY(reinterpret_cast<uintptr_t>(ptr) & kSuperPageOffsetMask)) { | 71 if (UNLIKELY(reinterpret_cast<uintptr_t>(ptr) & kSuperPageOffsetMask)) { |
72 int ret = munmap(ptr, len); | 72 int ret = munmap(ptr, len); |
73 ASSERT(!ret); | 73 ASSERT(!ret); |
74 ptr = reinterpret_cast<char*>(mmap(0, len + kSuperPageSize - kSystemPage
Size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0)); | 74 ptr = reinterpret_cast<char*>(mmap(0, len + kSuperPageSize - kSystemPage
Size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0)); |
(...skipping 14 matching lines...) Expand all Loading... |
89 } | 89 } |
90 } | 90 } |
91 void* ret = ptr; | 91 void* ret = ptr; |
92 #else | 92 #else |
93 // Windows is a lot simpler because we've designed around its | 93 // Windows is a lot simpler because we've designed around its |
94 // coarser-grained alignement. | 94 // coarser-grained alignement. |
95 void* ret = VirtualAlloc(addr, len, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE
); | 95 void* ret = VirtualAlloc(addr, len, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE
); |
96 if (!ret) | 96 if (!ret) |
97 ret = VirtualAlloc(0, len, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); | 97 ret = VirtualAlloc(0, len, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); |
98 RELEASE_ASSERT(ret); | 98 RELEASE_ASSERT(ret); |
99 #endif // OS(UNIX) | 99 #endif // OS(POSIX) |
100 return ret; | 100 return ret; |
101 } | 101 } |
102 | 102 |
103 void freeSuperPages(void* addr, size_t len) | 103 void freeSuperPages(void* addr, size_t len) |
104 { | 104 { |
105 ASSERT(!(reinterpret_cast<uintptr_t>(addr) & kSuperPageOffsetMask)); | 105 ASSERT(!(reinterpret_cast<uintptr_t>(addr) & kSuperPageOffsetMask)); |
106 ASSERT(!(len & kSuperPageOffsetMask)); | 106 ASSERT(!(len & kSuperPageOffsetMask)); |
107 #if OS(UNIX) | 107 #if OS(POSIX) |
108 int ret = munmap(addr, len); | 108 int ret = munmap(addr, len); |
109 ASSERT(!ret); | 109 ASSERT(!ret); |
110 #else | 110 #else |
111 BOOL ret = VirtualFree(addr, 0, MEM_RELEASE); | 111 BOOL ret = VirtualFree(addr, 0, MEM_RELEASE); |
112 ASSERT(ret); | 112 ASSERT(ret); |
113 #endif | 113 #endif |
114 } | 114 } |
115 | 115 |
116 void setSystemPagesInaccessible(void* addr, size_t len) | 116 void setSystemPagesInaccessible(void* addr, size_t len) |
117 { | 117 { |
118 ASSERT(!(len & kSystemPageOffsetMask)); | 118 ASSERT(!(len & kSystemPageOffsetMask)); |
119 #if OS(UNIX) | 119 #if OS(POSIX) |
120 int ret = mprotect(addr, len, PROT_NONE); | 120 int ret = mprotect(addr, len, PROT_NONE); |
121 ASSERT(!ret); | 121 ASSERT(!ret); |
122 #else | 122 #else |
123 BOOL ret = VirtualFree(addr, len, MEM_DECOMMIT); | 123 BOOL ret = VirtualFree(addr, len, MEM_DECOMMIT); |
124 ASSERT(ret); | 124 ASSERT(ret); |
125 #endif | 125 #endif |
126 } | 126 } |
127 | 127 |
128 void decommitSystemPages(void* addr, size_t len) | 128 void decommitSystemPages(void* addr, size_t len) |
129 { | 129 { |
130 ASSERT(!(len & kSystemPageOffsetMask)); | 130 ASSERT(!(len & kSystemPageOffsetMask)); |
131 #if OS(UNIX) | 131 #if OS(POSIX) |
132 int ret = madvise(addr, len, MADV_FREE); | 132 int ret = madvise(addr, len, MADV_FREE); |
133 ASSERT(!ret); | 133 ASSERT(!ret); |
134 #else | 134 #else |
135 void* ret = VirtualAlloc(addr, len, MEM_RESET, PAGE_READWRITE); | 135 void* ret = VirtualAlloc(addr, len, MEM_RESET, PAGE_READWRITE); |
136 ASSERT(ret); | 136 ASSERT(ret); |
137 #endif | 137 #endif |
138 } | 138 } |
139 | 139 |
140 char* getRandomSuperPageBase() | 140 char* getRandomSuperPageBase() |
141 { | 141 { |
(...skipping 16 matching lines...) Expand all Loading... |
158 // This is a good range on Windows, Linux and Mac. | 158 // This is a good range on Windows, Linux and Mac. |
159 // Allocates in the 0.5-1.5GB region. | 159 // Allocates in the 0.5-1.5GB region. |
160 random &= (0x3fffffff & kSuperPageBaseMask); | 160 random &= (0x3fffffff & kSuperPageBaseMask); |
161 random += 0x20000000; | 161 random += 0x20000000; |
162 #endif // CPU(X86_64) | 162 #endif // CPU(X86_64) |
163 return reinterpret_cast<char*>(random); | 163 return reinterpret_cast<char*>(random); |
164 } | 164 } |
165 | 165 |
166 } // namespace WTF | 166 } // namespace WTF |
167 | 167 |
OLD | NEW |