Home | History | Annotate | Download | only in wtf
      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 <errno.h>
     30 #include <sys/mman.h>
     31 #include <wtf/Assertions.h>
     32 #include <wtf/UnusedParam.h>
     33 
     34 namespace WTF {
     35 
     36 void* OSAllocator::reserveUncommitted(size_t bytes, Usage usage, bool writable, bool executable)
     37 {
     38     void* result = reserveAndCommit(bytes, usage, writable, executable);
     39 #if HAVE(MADV_FREE_REUSE)
     40     // To support the "reserve then commit" model, we have to initially decommit.
     41     while (madvise(result, bytes, MADV_FREE_REUSABLE) == -1 && errno == EAGAIN) { }
     42 #endif
     43     return result;
     44 }
     45 
     46 void* OSAllocator::reserveAndCommit(size_t bytes, Usage usage, bool writable, bool executable)
     47 {
     48     // All POSIX reservations start out logically committed.
     49     int protection = PROT_READ;
     50     if (writable)
     51         protection |= PROT_WRITE;
     52     if (executable)
     53         protection |= PROT_EXEC;
     54 
     55     int flags = MAP_PRIVATE | MAP_ANON;
     56 
     57 #if OS(DARWIN) && !defined(BUILDING_ON_TIGER)
     58     int fd = usage;
     59 #else
     60     int fd = -1;
     61 #endif
     62 
     63     void* result = 0;
     64 #if (OS(DARWIN) && CPU(X86_64))
     65     if (executable) {
     66         // Cook up an address to allocate at, using the following recipe:
     67         //   17 bits of zero, stay in userspace kids.
     68         //   26 bits of randomness for ASLR.
     69         //   21 bits of zero, at least stay aligned within one level of the pagetables.
     70         //
     71         // But! - as a temporary workaround for some plugin problems (rdar://problem/6812854),
     72         // for now instead of 2^26 bits of ASLR lets stick with 25 bits of randomization plus
     73         // 2^24, which should put up somewhere in the middle of userspace (in the address range
     74         // 0x200000000000 .. 0x5fffffffffff).
     75         intptr_t randomLocation = 0;
     76         randomLocation = arc4random() & ((1 << 25) - 1);
     77         randomLocation += (1 << 24);
     78         randomLocation <<= 21;
     79         result = reinterpret_cast<void*>(randomLocation);
     80     }
     81 #endif
     82 
     83     result = mmap(result, bytes, protection, flags, fd, 0);
     84     if (result == MAP_FAILED)
     85         CRASH();
     86     return result;
     87 }
     88 
     89 void OSAllocator::commit(void* address, size_t bytes, bool, bool)
     90 {
     91 #if HAVE(MADV_FREE_REUSE)
     92     while (madvise(address, bytes, MADV_FREE_REUSE) == -1 && errno == EAGAIN) { }
     93 #else
     94     // Non-MADV_FREE_REUSE reservations automatically commit on demand.
     95     UNUSED_PARAM(address);
     96     UNUSED_PARAM(bytes);
     97 #endif
     98 }
     99 
    100 void OSAllocator::decommit(void* address, size_t bytes)
    101 {
    102 #if HAVE(MADV_FREE_REUSE)
    103     while (madvise(address, bytes, MADV_FREE_REUSABLE) == -1 && errno == EAGAIN) { }
    104 #elif HAVE(MADV_FREE)
    105     while (madvise(address, bytes, MADV_FREE) == -1 && errno == EAGAIN) { }
    106 #elif HAVE(MADV_DONTNEED)
    107     while (madvise(address, bytes, MADV_DONTNEED) == -1 && errno == EAGAIN) { }
    108 #else
    109     UNUSED_PARAM(address);
    110     UNUSED_PARAM(bytes);
    111 #endif
    112 }
    113 
    114 void OSAllocator::releaseDecommitted(void* address, size_t bytes)
    115 {
    116     int result = munmap(address, bytes);
    117     if (result == -1)
    118         CRASH();
    119 }
    120 
    121 } // namespace WTF
    122