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