michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * vim: set ts=8 sts=4 et sw=4 tw=99: michael@0: * michael@0: * ***** BEGIN LICENSE BLOCK ***** michael@0: * Copyright (C) 2010 Apple Inc. All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' michael@0: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, michael@0: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR michael@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS michael@0: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR michael@0: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF michael@0: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS michael@0: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN michael@0: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) michael@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF michael@0: * THE POSSIBILITY OF SUCH DAMAGE. michael@0: * michael@0: * ***** END LICENSE BLOCK ***** */ michael@0: michael@0: #include "assembler/wtf/Platform.h" michael@0: michael@0: #if WTF_OS_UNIX && !WTF_OS_SYMBIAN michael@0: michael@0: #include "yarr/OSAllocator.h" michael@0: michael@0: #include michael@0: #include michael@0: #include "assembler/wtf/Assertions.h" michael@0: michael@0: namespace WTF { michael@0: michael@0: void* OSAllocator::reserveUncommitted(size_t bytes, Usage usage, bool writable, bool executable) michael@0: { michael@0: void* result = reserveAndCommit(bytes, usage, writable, executable); michael@0: #if HAVE_MADV_FREE_REUSE michael@0: // To support the "reserve then commit" model, we have to initially decommit. michael@0: while (madvise(result, bytes, MADV_FREE_REUSABLE) == -1 && errno == EAGAIN) { } michael@0: #endif michael@0: return result; michael@0: } michael@0: michael@0: void* OSAllocator::reserveAndCommit(size_t bytes, Usage usage, bool writable, bool executable) michael@0: { michael@0: // All POSIX reservations start out logically committed. michael@0: int protection = PROT_READ; michael@0: if (writable) michael@0: protection |= PROT_WRITE; michael@0: if (executable) michael@0: protection |= PROT_EXEC; michael@0: michael@0: int flags = MAP_PRIVATE | MAP_ANON; michael@0: michael@0: #if WTF_OS_DARWIN && !defined(BUILDING_ON_TIGER) michael@0: int fd = usage; michael@0: #else michael@0: int fd = -1; michael@0: #endif michael@0: michael@0: void* result = 0; michael@0: #if (WTF_OS_DARWIN && WTF_CPU_X86_64) michael@0: if (executable) { michael@0: // Cook up an address to allocate at, using the following recipe: michael@0: // 17 bits of zero, stay in userspace kids. michael@0: // 26 bits of randomness for ASLR. michael@0: // 21 bits of zero, at least stay aligned within one level of the pagetables. michael@0: // michael@0: // But! - as a temporary workaround for some plugin problems (rdar://problem/6812854), michael@0: // for now instead of 2^26 bits of ASLR lets stick with 25 bits of randomization plus michael@0: // 2^24, which should put up somewhere in the middle of userspace (in the address range michael@0: // 0x200000000000 .. 0x5fffffffffff). michael@0: intptr_t randomLocation = 0; michael@0: randomLocation = arc4random() & ((1 << 25) - 1); michael@0: randomLocation += (1 << 24); michael@0: randomLocation <<= 21; michael@0: result = reinterpret_cast(randomLocation); michael@0: } michael@0: #endif michael@0: michael@0: result = mmap(result, bytes, protection, flags, fd, 0); michael@0: if (result == MAP_FAILED) michael@0: CRASH(); michael@0: return result; michael@0: } michael@0: michael@0: void OSAllocator::commit(void* address, size_t bytes, bool, bool) michael@0: { michael@0: #if HAVE_MADV_FREE_REUSE michael@0: while (madvise(address, bytes, MADV_FREE_REUSE) == -1 && errno == EAGAIN) { } michael@0: #else michael@0: // Non-MADV_FREE_REUSE reservations automatically commit on demand. michael@0: UNUSED_PARAM(address); michael@0: UNUSED_PARAM(bytes); michael@0: #endif michael@0: } michael@0: michael@0: void OSAllocator::decommit(void* address, size_t bytes) michael@0: { michael@0: #if HAVE_MADV_FREE_REUSE michael@0: while (madvise(address, bytes, MADV_FREE_REUSABLE) == -1 && errno == EAGAIN) { } michael@0: #elif HAVE_MADV_FREE michael@0: while (madvise(address, bytes, MADV_FREE) == -1 && errno == EAGAIN) { } michael@0: #elif HAVE_MADV_DONTNEED michael@0: while (madvise(address, bytes, MADV_DONTNEED) == -1 && errno == EAGAIN) { } michael@0: #else michael@0: UNUSED_PARAM(address); michael@0: UNUSED_PARAM(bytes); michael@0: #endif michael@0: } michael@0: michael@0: void OSAllocator::releaseDecommitted(void* address, size_t bytes) michael@0: { michael@0: int result = munmap(address, bytes); michael@0: if (result == -1) michael@0: CRASH(); michael@0: } michael@0: michael@0: } // namespace WTF michael@0: michael@0: #endif