Thu, 15 Jan 2015 15:55:04 +0100
Back out 97036ab72558 which inappropriately compared turds to third parties.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright (C) 2008 Apple Inc. All rights reserved. |
michael@0 | 3 | * |
michael@0 | 4 | * Redistribution and use in source and binary forms, with or without |
michael@0 | 5 | * modification, are permitted provided that the following conditions |
michael@0 | 6 | * are met: |
michael@0 | 7 | * 1. Redistributions of source code must retain the above copyright |
michael@0 | 8 | * notice, this list of conditions and the following disclaimer. |
michael@0 | 9 | * 2. Redistributions in binary form must reproduce the above copyright |
michael@0 | 10 | * notice, this list of conditions and the following disclaimer in the |
michael@0 | 11 | * documentation and/or other materials provided with the distribution. |
michael@0 | 12 | * |
michael@0 | 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
michael@0 | 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
michael@0 | 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
michael@0 | 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
michael@0 | 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
michael@0 | 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
michael@0 | 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
michael@0 | 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
michael@0 | 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 24 | */ |
michael@0 | 25 | |
michael@0 | 26 | #include "assembler/jit/ExecutableAllocator.h" |
michael@0 | 27 | |
michael@0 | 28 | #if ENABLE_ASSEMBLER && WTF_OS_UNIX && !WTF_OS_SYMBIAN |
michael@0 | 29 | |
michael@0 | 30 | #include <sys/mman.h> |
michael@0 | 31 | #include <unistd.h> |
michael@0 | 32 | |
michael@0 | 33 | #include "assembler/wtf/Assertions.h" |
michael@0 | 34 | #include "assembler/wtf/VMTags.h" |
michael@0 | 35 | #include "js/Utility.h" |
michael@0 | 36 | |
michael@0 | 37 | namespace JSC { |
michael@0 | 38 | |
michael@0 | 39 | size_t ExecutableAllocator::determinePageSize() |
michael@0 | 40 | { |
michael@0 | 41 | return getpagesize(); |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | ExecutablePool::Allocation ExecutableAllocator::systemAlloc(size_t n) |
michael@0 | 45 | { |
michael@0 | 46 | void *allocation = mmap(NULL, n, INITIAL_PROTECTION_FLAGS, MAP_PRIVATE | MAP_ANON, VM_TAG_FOR_EXECUTABLEALLOCATOR_MEMORY, 0); |
michael@0 | 47 | if (allocation == MAP_FAILED) |
michael@0 | 48 | allocation = NULL; |
michael@0 | 49 | ExecutablePool::Allocation alloc = { reinterpret_cast<char*>(allocation), n }; |
michael@0 | 50 | return alloc; |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | void ExecutableAllocator::systemRelease(const ExecutablePool::Allocation& alloc) |
michael@0 | 54 | { |
michael@0 | 55 | int result = munmap(alloc.pages, alloc.size); |
michael@0 | 56 | ASSERT_UNUSED(result, !result); |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | #if WTF_ENABLE_ASSEMBLER_WX_EXCLUSIVE |
michael@0 | 60 | void ExecutableAllocator::reprotectRegion(void* start, size_t size, ProtectionSetting setting) |
michael@0 | 61 | { |
michael@0 | 62 | if (!pageSize) |
michael@0 | 63 | intializePageSize(); |
michael@0 | 64 | |
michael@0 | 65 | // Calculate the start of the page containing this region, |
michael@0 | 66 | // and account for this extra memory within size. |
michael@0 | 67 | intptr_t startPtr = reinterpret_cast<intptr_t>(start); |
michael@0 | 68 | intptr_t pageStartPtr = startPtr & ~(pageSize - 1); |
michael@0 | 69 | void* pageStart = reinterpret_cast<void*>(pageStartPtr); |
michael@0 | 70 | size += (startPtr - pageStartPtr); |
michael@0 | 71 | |
michael@0 | 72 | // Round size up |
michael@0 | 73 | size += (pageSize - 1); |
michael@0 | 74 | size &= ~(pageSize - 1); |
michael@0 | 75 | |
michael@0 | 76 | mprotect(pageStart, size, (setting == Writable) ? PROTECTION_FLAGS_RW : PROTECTION_FLAGS_RX); |
michael@0 | 77 | } |
michael@0 | 78 | #endif |
michael@0 | 79 | |
michael@0 | 80 | #if WTF_CPU_ARM_TRADITIONAL && WTF_OS_LINUX && WTF_COMPILER_RVCT |
michael@0 | 81 | __asm void ExecutableAllocator::cacheFlush(void* code, size_t size) |
michael@0 | 82 | { |
michael@0 | 83 | ARM |
michael@0 | 84 | push {r7} |
michael@0 | 85 | add r1, r1, r0 |
michael@0 | 86 | mov r7, #0xf0000 |
michael@0 | 87 | add r7, r7, #0x2 |
michael@0 | 88 | mov r2, #0x0 |
michael@0 | 89 | svc #0x0 |
michael@0 | 90 | pop {r7} |
michael@0 | 91 | bx lr |
michael@0 | 92 | } |
michael@0 | 93 | #endif |
michael@0 | 94 | |
michael@0 | 95 | void |
michael@0 | 96 | ExecutablePool::toggleAllCodeAsAccessible(bool accessible) |
michael@0 | 97 | { |
michael@0 | 98 | char* begin = m_allocation.pages; |
michael@0 | 99 | size_t size = m_freePtr - begin; |
michael@0 | 100 | |
michael@0 | 101 | if (size) { |
michael@0 | 102 | // N.B. Some systems, like 32bit Mac OS 10.6, implicitly add PROT_EXEC |
michael@0 | 103 | // when mprotect'ing memory with any flag other than PROT_NONE. Be |
michael@0 | 104 | // sure to use PROT_NONE when making inaccessible. |
michael@0 | 105 | int flags = accessible ? PROT_READ | PROT_WRITE | PROT_EXEC : PROT_NONE; |
michael@0 | 106 | if (mprotect(begin, size, flags)) |
michael@0 | 107 | MOZ_CRASH(); |
michael@0 | 108 | } |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | #endif // HAVE(ASSEMBLER) |