Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 1 | //===-- enable_execute_stack.c - Implement __enable_execute_stack ---------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include <stdint.h> |
| 11 | #include <sys/mman.h> |
| 12 | |
| 13 | |
| 14 | // |
| 15 | // The compiler generates calls to __enable_execute_stack() when creating |
| 16 | // trampoline functions on the stack for use with nested functions. |
| 17 | // It is expected to mark the page(s) containing the address |
| 18 | // and the next 48 bytes as executable. Since the stack is normally rw- |
| 19 | // that means changing the protection on those page(s) to rwx. |
| 20 | // |
| 21 | void __enable_execute_stack(void* addr) |
| 22 | { |
| 23 | #if __APPLE__ |
| 24 | // On Darwin, pagesize is always 4096 bytes |
| 25 | const uintptr_t pageSize = 4096; |
| 26 | #else |
Daniel Dunbar | f118402 | 2009-07-01 06:02:53 +0000 | [diff] [blame^] | 27 | // FIXME: We should have a configure check for this. |
| 28 | const uintptr_t pagesize = getpagesize(); |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 29 | #endif |
| 30 | const uintptr_t pageAlignMask = ~(pageSize-1); |
| 31 | uintptr_t p = (uintptr_t)addr; |
| 32 | unsigned char* startPage = (unsigned char*)(p & pageAlignMask); |
| 33 | unsigned char* endPage = (unsigned char*)((p+48+pageSize) & pageAlignMask); |
| 34 | mprotect(startPage, endPage-startPage, PROT_READ | PROT_WRITE | PROT_EXEC); |
| 35 | } |
| 36 | |
| 37 | |