1 | /* |
2 | * Copyright (C) 2010 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
23 | * THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #include "config.h" |
27 | #include <wtf/OSAllocator.h> |
28 | |
29 | #include <errno.h> |
30 | #include <sys/mman.h> |
31 | #include <wtf/Assertions.h> |
32 | #include <wtf/PageAllocation.h> |
33 | |
34 | namespace WTF { |
35 | |
36 | void* OSAllocator::reserveUncommitted(size_t bytes, Usage usage, bool writable, bool executable, bool includesGuardPages) |
37 | { |
38 | #if OS(LINUX) |
39 | UNUSED_PARAM(usage); |
40 | UNUSED_PARAM(writable); |
41 | UNUSED_PARAM(executable); |
42 | UNUSED_PARAM(includesGuardPages); |
43 | |
44 | void* result = mmap(0, bytes, PROT_NONE, MAP_NORESERVE | MAP_PRIVATE | MAP_ANON, -1, 0); |
45 | if (result == MAP_FAILED) |
46 | CRASH(); |
47 | madvise(result, bytes, MADV_DONTNEED); |
48 | #else |
49 | void* result = reserveAndCommit(bytes, usage, writable, executable, includesGuardPages); |
50 | #if HAVE(MADV_FREE_REUSE) |
51 | if (result) { |
52 | // To support the "reserve then commit" model, we have to initially decommit. |
53 | while (madvise(result, bytes, MADV_FREE_REUSABLE) == -1 && errno == EAGAIN) { } |
54 | } |
55 | #endif |
56 | |
57 | #endif |
58 | |
59 | return result; |
60 | } |
61 | |
62 | void* OSAllocator::reserveAndCommit(size_t bytes, Usage usage, bool writable, bool executable, bool includesGuardPages) |
63 | { |
64 | // All POSIX reservations start out logically committed. |
65 | int protection = PROT_READ; |
66 | if (writable) |
67 | protection |= PROT_WRITE; |
68 | if (executable) |
69 | protection |= PROT_EXEC; |
70 | |
71 | int flags = MAP_PRIVATE | MAP_ANON; |
72 | #if OS(DARWIN) |
73 | if (executable) |
74 | flags |= MAP_JIT; |
75 | #endif |
76 | |
77 | #if OS(DARWIN) |
78 | int fd = usage; |
79 | #else |
80 | UNUSED_PARAM(usage); |
81 | int fd = -1; |
82 | #endif |
83 | |
84 | void* result = 0; |
85 | #if (OS(DARWIN) && CPU(X86_64)) |
86 | if (executable) { |
87 | ASSERT(includesGuardPages); |
88 | // Cook up an address to allocate at, using the following recipe: |
89 | // 17 bits of zero, stay in userspace kids. |
90 | // 26 bits of randomness for ASLR. |
91 | // 21 bits of zero, at least stay aligned within one level of the pagetables. |
92 | // |
93 | // But! - as a temporary workaround for some plugin problems (rdar://problem/6812854), |
94 | // for now instead of 2^26 bits of ASLR lets stick with 25 bits of randomization plus |
95 | // 2^24, which should put up somewhere in the middle of userspace (in the address range |
96 | // 0x200000000000 .. 0x5fffffffffff). |
97 | intptr_t randomLocation = 0; |
98 | randomLocation = arc4random() & ((1 << 25) - 1); |
99 | randomLocation += (1 << 24); |
100 | randomLocation <<= 21; |
101 | result = reinterpret_cast<void*>(randomLocation); |
102 | } |
103 | #endif |
104 | |
105 | result = mmap(result, bytes, protection, flags, fd, 0); |
106 | if (result == MAP_FAILED) { |
107 | if (executable) |
108 | result = 0; |
109 | else |
110 | CRASH(); |
111 | } |
112 | if (result && includesGuardPages) { |
113 | // We use mmap to remap the guardpages rather than using mprotect as |
114 | // mprotect results in multiple references to the code region. This |
115 | // breaks the madvise based mechanism we use to return physical memory |
116 | // to the OS. |
117 | mmap(result, pageSize(), PROT_NONE, MAP_FIXED | MAP_PRIVATE | MAP_ANON, fd, 0); |
118 | mmap(static_cast<char*>(result) + bytes - pageSize(), pageSize(), PROT_NONE, MAP_FIXED | MAP_PRIVATE | MAP_ANON, fd, 0); |
119 | } |
120 | return result; |
121 | } |
122 | |
123 | void OSAllocator::commit(void* address, size_t bytes, bool writable, bool executable) |
124 | { |
125 | #if OS(LINUX) |
126 | int protection = PROT_READ; |
127 | if (writable) |
128 | protection |= PROT_WRITE; |
129 | if (executable) |
130 | protection |= PROT_EXEC; |
131 | if (mprotect(address, bytes, protection)) |
132 | CRASH(); |
133 | madvise(address, bytes, MADV_WILLNEED); |
134 | #elif HAVE(MADV_FREE_REUSE) |
135 | UNUSED_PARAM(writable); |
136 | UNUSED_PARAM(executable); |
137 | while (madvise(address, bytes, MADV_FREE_REUSE) == -1 && errno == EAGAIN) { } |
138 | #else |
139 | // Non-MADV_FREE_REUSE reservations automatically commit on demand. |
140 | UNUSED_PARAM(address); |
141 | UNUSED_PARAM(bytes); |
142 | UNUSED_PARAM(writable); |
143 | UNUSED_PARAM(executable); |
144 | #endif |
145 | } |
146 | |
147 | void OSAllocator::decommit(void* address, size_t bytes) |
148 | { |
149 | #if OS(LINUX) |
150 | madvise(address, bytes, MADV_DONTNEED); |
151 | if (mprotect(address, bytes, PROT_NONE)) |
152 | CRASH(); |
153 | #elif HAVE(MADV_FREE_REUSE) |
154 | while (madvise(address, bytes, MADV_FREE_REUSABLE) == -1 && errno == EAGAIN) { } |
155 | #elif HAVE(MADV_FREE) |
156 | while (madvise(address, bytes, MADV_FREE) == -1 && errno == EAGAIN) { } |
157 | #elif HAVE(MADV_DONTNEED) |
158 | while (madvise(address, bytes, MADV_DONTNEED) == -1 && errno == EAGAIN) { } |
159 | #else |
160 | UNUSED_PARAM(address); |
161 | UNUSED_PARAM(bytes); |
162 | #endif |
163 | } |
164 | |
165 | void OSAllocator::hintMemoryNotNeededSoon(void* address, size_t bytes) |
166 | { |
167 | #if HAVE(MADV_DONTNEED) |
168 | while (madvise(address, bytes, MADV_DONTNEED) == -1 && errno == EAGAIN) { } |
169 | #else |
170 | UNUSED_PARAM(address); |
171 | UNUSED_PARAM(bytes); |
172 | #endif |
173 | } |
174 | |
175 | void OSAllocator::releaseDecommitted(void* address, size_t bytes) |
176 | { |
177 | int result = munmap(address, bytes); |
178 | if (result == -1) |
179 | CRASH(); |
180 | } |
181 | |
182 | } // namespace WTF |
183 | |