js/src/assembler/jit/ExecutableAllocatorWin.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/assembler/jit/ExecutableAllocatorWin.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,134 @@
     1.4 +/*
     1.5 + * Copyright (C) 2008 Apple Inc. All rights reserved.
     1.6 + *
     1.7 + * Redistribution and use in source and binary forms, with or without
     1.8 + * modification, are permitted provided that the following conditions
     1.9 + * are met:
    1.10 + * 1. Redistributions of source code must retain the above copyright
    1.11 + *    notice, this list of conditions and the following disclaimer.
    1.12 + * 2. Redistributions in binary form must reproduce the above copyright
    1.13 + *    notice, this list of conditions and the following disclaimer in the
    1.14 + *    documentation and/or other materials provided with the distribution.
    1.15 + *
    1.16 + * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
    1.17 + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.18 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    1.19 + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
    1.20 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    1.21 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    1.22 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    1.23 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
    1.24 + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.25 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    1.26 + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.27 + */
    1.28 +
    1.29 +#include "assembler/jit/ExecutableAllocator.h"
    1.30 +
    1.31 +#if ENABLE_ASSEMBLER && WTF_OS_WINDOWS
    1.32 +
    1.33 +#include "jswin.h"
    1.34 +#include "mozilla/WindowsVersion.h"
    1.35 +
    1.36 +extern uint64_t random_next(uint64_t *, int);
    1.37 +
    1.38 +namespace JSC {
    1.39 +
    1.40 +uint64_t ExecutableAllocator::rngSeed;
    1.41 +
    1.42 +size_t ExecutableAllocator::determinePageSize()
    1.43 +{
    1.44 +    SYSTEM_INFO system_info;
    1.45 +    GetSystemInfo(&system_info);
    1.46 +    return system_info.dwPageSize;
    1.47 +}
    1.48 +
    1.49 +void *ExecutableAllocator::computeRandomAllocationAddress()
    1.50 +{
    1.51 +    /*
    1.52 +     * Inspiration is V8's OS::Allocate in platform-win32.cc.
    1.53 +     *
    1.54 +     * VirtualAlloc takes 64K chunks out of the virtual address space, so we
    1.55 +     * keep 16b alignment.
    1.56 +     *
    1.57 +     * x86: V8 comments say that keeping addresses in the [64MiB, 1GiB) range
    1.58 +     * tries to avoid system default DLL mapping space. In the end, we get 13
    1.59 +     * bits of randomness in our selection.
    1.60 +     * x64: [2GiB, 4TiB), with 25 bits of randomness.
    1.61 +     */
    1.62 +    static const unsigned chunkBits = 16;
    1.63 +#if WTF_CPU_X86_64
    1.64 +    static const uintptr_t base = 0x0000000080000000;
    1.65 +    static const uintptr_t mask = 0x000003ffffff0000;
    1.66 +#elif WTF_CPU_X86
    1.67 +    static const uintptr_t base = 0x04000000;
    1.68 +    static const uintptr_t mask = 0x3fff0000;
    1.69 +#else
    1.70 +# error "Unsupported architecture"
    1.71 +#endif
    1.72 +    uint64_t rand = random_next(&rngSeed, 32) << chunkBits;
    1.73 +    return (void *) (base | rand & mask);
    1.74 +}
    1.75 +
    1.76 +static bool
    1.77 +RandomizeIsBrokenImpl()
    1.78 +{
    1.79 +    // We disable everything before Vista, for now.
    1.80 +    return !mozilla::IsVistaOrLater();
    1.81 +}
    1.82 +
    1.83 +static bool
    1.84 +RandomizeIsBroken()
    1.85 +{
    1.86 +    // Use the compiler's intrinsic guards for |static type value = expr| to avoid some potential
    1.87 +    // races if runtimes are created from multiple threads.
    1.88 +    static int result = RandomizeIsBrokenImpl();
    1.89 +    return !!result;
    1.90 +}
    1.91 +
    1.92 +ExecutablePool::Allocation ExecutableAllocator::systemAlloc(size_t n)
    1.93 +{
    1.94 +    void *allocation = NULL;
    1.95 +    // Randomization disabled to avoid a performance fault on x64 builds.
    1.96 +    // See bug 728623.
    1.97 +#ifndef JS_CPU_X64
    1.98 +    if (!RandomizeIsBroken()) {
    1.99 +        void *randomAddress = computeRandomAllocationAddress();
   1.100 +        allocation = VirtualAlloc(randomAddress, n, MEM_COMMIT | MEM_RESERVE,
   1.101 +                                  PAGE_EXECUTE_READWRITE);
   1.102 +    }
   1.103 +#endif
   1.104 +    if (!allocation)
   1.105 +        allocation = VirtualAlloc(0, n, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
   1.106 +    ExecutablePool::Allocation alloc = { reinterpret_cast<char*>(allocation), n };
   1.107 +    return alloc;
   1.108 +}
   1.109 +
   1.110 +void ExecutableAllocator::systemRelease(const ExecutablePool::Allocation& alloc)
   1.111 +{
   1.112 +    VirtualFree(alloc.pages, 0, MEM_RELEASE);
   1.113 +}
   1.114 +
   1.115 +void
   1.116 +ExecutablePool::toggleAllCodeAsAccessible(bool accessible)
   1.117 +{
   1.118 +    char* begin = m_allocation.pages;
   1.119 +    size_t size = m_freePtr - begin;
   1.120 +
   1.121 +    if (size) {
   1.122 +        // N.B. DEP is not on automatically in Windows XP, so be sure to use
   1.123 +        // PAGE_NOACCESS instead of PAGE_READWRITE when making inaccessible.
   1.124 +        DWORD oldProtect;
   1.125 +        int flags = accessible ? PAGE_EXECUTE_READWRITE : PAGE_NOACCESS;
   1.126 +        if (!VirtualProtect(begin, size, flags, &oldProtect))
   1.127 +            MOZ_CRASH();
   1.128 +    }
   1.129 +}
   1.130 +
   1.131 +#if ENABLE_ASSEMBLER_WX_EXCLUSIVE
   1.132 +#error "ASSEMBLER_WX_EXCLUSIVE not yet suported on this platform."
   1.133 +#endif
   1.134 +
   1.135 +}
   1.136 +
   1.137 +#endif // HAVE(ASSEMBLER)

mercurial