1*7c3d14c8STreehugger Robot /* ===-- enable_execute_stack.c - Implement __enable_execute_stack ---------=== 2*7c3d14c8STreehugger Robot * 3*7c3d14c8STreehugger Robot * The LLVM Compiler Infrastructure 4*7c3d14c8STreehugger Robot * 5*7c3d14c8STreehugger Robot * This file is dual licensed under the MIT and the University of Illinois Open 6*7c3d14c8STreehugger Robot * Source Licenses. See LICENSE.TXT for details. 7*7c3d14c8STreehugger Robot * 8*7c3d14c8STreehugger Robot * ===----------------------------------------------------------------------=== 9*7c3d14c8STreehugger Robot */ 10*7c3d14c8STreehugger Robot 11*7c3d14c8STreehugger Robot #include "int_lib.h" 12*7c3d14c8STreehugger Robot 13*7c3d14c8STreehugger Robot #ifndef _WIN32 14*7c3d14c8STreehugger Robot #include <sys/mman.h> 15*7c3d14c8STreehugger Robot #endif 16*7c3d14c8STreehugger Robot 17*7c3d14c8STreehugger Robot /* #include "config.h" 18*7c3d14c8STreehugger Robot * FIXME: CMake - include when cmake system is ready. 19*7c3d14c8STreehugger Robot * Remove #define HAVE_SYSCONF 1 line. 20*7c3d14c8STreehugger Robot */ 21*7c3d14c8STreehugger Robot #define HAVE_SYSCONF 1 22*7c3d14c8STreehugger Robot 23*7c3d14c8STreehugger Robot #ifdef _WIN32 24*7c3d14c8STreehugger Robot #define WIN32_LEAN_AND_MEAN 25*7c3d14c8STreehugger Robot #include <Windows.h> 26*7c3d14c8STreehugger Robot #else 27*7c3d14c8STreehugger Robot #ifndef __APPLE__ 28*7c3d14c8STreehugger Robot #include <unistd.h> 29*7c3d14c8STreehugger Robot #endif /* __APPLE__ */ 30*7c3d14c8STreehugger Robot #endif /* _WIN32 */ 31*7c3d14c8STreehugger Robot 32*7c3d14c8STreehugger Robot #if __LP64__ 33*7c3d14c8STreehugger Robot #define TRAMPOLINE_SIZE 48 34*7c3d14c8STreehugger Robot #else 35*7c3d14c8STreehugger Robot #define TRAMPOLINE_SIZE 40 36*7c3d14c8STreehugger Robot #endif 37*7c3d14c8STreehugger Robot 38*7c3d14c8STreehugger Robot /* 39*7c3d14c8STreehugger Robot * The compiler generates calls to __enable_execute_stack() when creating 40*7c3d14c8STreehugger Robot * trampoline functions on the stack for use with nested functions. 41*7c3d14c8STreehugger Robot * It is expected to mark the page(s) containing the address 42*7c3d14c8STreehugger Robot * and the next 48 bytes as executable. Since the stack is normally rw- 43*7c3d14c8STreehugger Robot * that means changing the protection on those page(s) to rwx. 44*7c3d14c8STreehugger Robot */ 45*7c3d14c8STreehugger Robot 46*7c3d14c8STreehugger Robot COMPILER_RT_ABI void __enable_execute_stack(void * addr)47*7c3d14c8STreehugger Robot__enable_execute_stack(void* addr) 48*7c3d14c8STreehugger Robot { 49*7c3d14c8STreehugger Robot 50*7c3d14c8STreehugger Robot #if _WIN32 51*7c3d14c8STreehugger Robot MEMORY_BASIC_INFORMATION mbi; 52*7c3d14c8STreehugger Robot if (!VirtualQuery (addr, &mbi, sizeof(mbi))) 53*7c3d14c8STreehugger Robot return; /* We should probably assert here because there is no return value */ 54*7c3d14c8STreehugger Robot VirtualProtect (mbi.BaseAddress, mbi.RegionSize, PAGE_EXECUTE_READWRITE, &mbi.Protect); 55*7c3d14c8STreehugger Robot #else 56*7c3d14c8STreehugger Robot #if __APPLE__ 57*7c3d14c8STreehugger Robot /* On Darwin, pagesize is always 4096 bytes */ 58*7c3d14c8STreehugger Robot const uintptr_t pageSize = 4096; 59*7c3d14c8STreehugger Robot #elif !defined(HAVE_SYSCONF) 60*7c3d14c8STreehugger Robot #error "HAVE_SYSCONF not defined! See enable_execute_stack.c" 61*7c3d14c8STreehugger Robot #else 62*7c3d14c8STreehugger Robot const uintptr_t pageSize = sysconf(_SC_PAGESIZE); 63*7c3d14c8STreehugger Robot #endif /* __APPLE__ */ 64*7c3d14c8STreehugger Robot 65*7c3d14c8STreehugger Robot const uintptr_t pageAlignMask = ~(pageSize-1); 66*7c3d14c8STreehugger Robot uintptr_t p = (uintptr_t)addr; 67*7c3d14c8STreehugger Robot unsigned char* startPage = (unsigned char*)(p & pageAlignMask); 68*7c3d14c8STreehugger Robot unsigned char* endPage = (unsigned char*)((p+TRAMPOLINE_SIZE+pageSize) & pageAlignMask); 69*7c3d14c8STreehugger Robot size_t length = endPage - startPage; 70*7c3d14c8STreehugger Robot (void) mprotect((void *)startPage, length, PROT_READ | PROT_WRITE | PROT_EXEC); 71*7c3d14c8STreehugger Robot #endif 72*7c3d14c8STreehugger Robot } 73