]> git.saurik.com Git - apple/javascriptcore.git/blob - wtf/OSAllocatorPosix.cpp
JavaScriptCore-903.5.tar.gz
[apple/javascriptcore.git] / wtf / OSAllocatorPosix.cpp
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 "OSAllocator.h"
28
29 #include "PageAllocation.h"
30 #include <errno.h>
31 #include <sys/mman.h>
32 #include <wtf/Assertions.h>
33 #include <wtf/UnusedParam.h>
34
35 namespace WTF {
36
37 void* OSAllocator::reserveUncommitted(size_t bytes, Usage usage, bool writable, bool executable, bool includesGuardPages)
38 {
39 void* result = reserveAndCommit(bytes, usage, writable, executable, includesGuardPages);
40 #if HAVE(MADV_FREE_REUSE)
41 // To support the "reserve then commit" model, we have to initially decommit.
42 while (madvise(result, bytes, MADV_FREE_REUSABLE) == -1 && errno == EAGAIN) { }
43 #endif
44 return result;
45 }
46
47 void* OSAllocator::reserveAndCommit(size_t bytes, Usage usage, bool writable, bool executable, bool includesGuardPages)
48 {
49 // All POSIX reservations start out logically committed.
50 int protection = PROT_READ;
51 if (writable)
52 protection |= PROT_WRITE;
53 if (executable)
54 protection |= PROT_EXEC;
55
56 int flags = MAP_PRIVATE | MAP_ANON;
57 #if defined MAP_JIT
58 if (executable)
59 flags |= MAP_JIT;
60 #endif
61
62 #if OS(DARWIN)
63 int fd = usage;
64 #else
65 int fd = -1;
66 #endif
67
68 void* result = 0;
69 #if (OS(DARWIN) && CPU(X86_64))
70 if (executable) {
71 ASSERT(includesGuardPages);
72 // Cook up an address to allocate at, using the following recipe:
73 // 17 bits of zero, stay in userspace kids.
74 // 26 bits of randomness for ASLR.
75 // 21 bits of zero, at least stay aligned within one level of the pagetables.
76 //
77 // But! - as a temporary workaround for some plugin problems (rdar://problem/6812854),
78 // for now instead of 2^26 bits of ASLR lets stick with 25 bits of randomization plus
79 // 2^24, which should put up somewhere in the middle of userspace (in the address range
80 // 0x200000000000 .. 0x5fffffffffff).
81 intptr_t randomLocation = 0;
82 randomLocation = arc4random() & ((1 << 25) - 1);
83 randomLocation += (1 << 24);
84 randomLocation <<= 21;
85 result = reinterpret_cast<void*>(randomLocation);
86 }
87 #endif
88
89 result = mmap(result, bytes, protection, flags, fd, 0);
90 if (result == MAP_FAILED) {
91 #if ENABLE(INTERPRETER)
92 if (executable)
93 result = 0;
94 else
95 #endif
96 CRASH();
97 }
98 if (result && includesGuardPages) {
99 // We use mmap to remap the guardpages rather than using mprotect as
100 // mprotect results in multiple references to the code region. This
101 // breaks the madvise based mechanism we use to return physical memory
102 // to the OS.
103 mmap(result, pageSize(), PROT_NONE, MAP_FIXED | MAP_PRIVATE | MAP_ANON, fd, 0);
104 mmap(static_cast<char*>(result) + bytes - pageSize(), pageSize(), PROT_NONE, MAP_FIXED | MAP_PRIVATE | MAP_ANON, fd, 0);
105 }
106
107 return result;
108 }
109
110 void OSAllocator::commit(void* address, size_t bytes, bool, bool)
111 {
112 #if HAVE(MADV_FREE_REUSE)
113 while (madvise(address, bytes, MADV_FREE_REUSE) == -1 && errno == EAGAIN) { }
114 #else
115 // Non-MADV_FREE_REUSE reservations automatically commit on demand.
116 UNUSED_PARAM(address);
117 UNUSED_PARAM(bytes);
118 #endif
119 }
120
121 void OSAllocator::decommit(void* address, size_t bytes)
122 {
123 #if HAVE(MADV_FREE_REUSE)
124 while (madvise(address, bytes, MADV_FREE_REUSABLE) == -1 && errno == EAGAIN) { }
125 #elif HAVE(MADV_FREE)
126 while (madvise(address, bytes, MADV_FREE) == -1 && errno == EAGAIN) { }
127 #elif HAVE(MADV_DONTNEED)
128 while (madvise(address, bytes, MADV_DONTNEED) == -1 && errno == EAGAIN) { }
129 #else
130 UNUSED_PARAM(address);
131 UNUSED_PARAM(bytes);
132 #endif
133 }
134
135 void OSAllocator::releaseDecommitted(void* address, size_t bytes)
136 {
137 int result = munmap(address, bytes);
138 if (result == -1)
139 CRASH();
140 }
141
142 } // namespace WTF