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