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> |
Daniel Dunbar | c6cd62b | 2009-07-01 06:06:42 +0000 | [diff] [blame^] | 12 | #ifndef __APPLE__ |
| 13 | #include <unistd.h> |
| 14 | #endif |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 15 | |
| 16 | |
| 17 | // |
| 18 | // The compiler generates calls to __enable_execute_stack() when creating |
| 19 | // trampoline functions on the stack for use with nested functions. |
| 20 | // It is expected to mark the page(s) containing the address |
| 21 | // and the next 48 bytes as executable. Since the stack is normally rw- |
| 22 | // that means changing the protection on those page(s) to rwx. |
| 23 | // |
| 24 | void __enable_execute_stack(void* addr) |
| 25 | { |
| 26 | #if __APPLE__ |
| 27 | // On Darwin, pagesize is always 4096 bytes |
| 28 | const uintptr_t pageSize = 4096; |
| 29 | #else |
Daniel Dunbar | f118402 | 2009-07-01 06:02:53 +0000 | [diff] [blame] | 30 | // FIXME: We should have a configure check for this. |
Daniel Dunbar | c6cd62b | 2009-07-01 06:06:42 +0000 | [diff] [blame^] | 31 | const uintptr_t pageSize = getpagesize(); |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 32 | #endif |
| 33 | const uintptr_t pageAlignMask = ~(pageSize-1); |
| 34 | uintptr_t p = (uintptr_t)addr; |
| 35 | unsigned char* startPage = (unsigned char*)(p & pageAlignMask); |
| 36 | unsigned char* endPage = (unsigned char*)((p+48+pageSize) & pageAlignMask); |
| 37 | mprotect(startPage, endPage-startPage, PROT_READ | PROT_WRITE | PROT_EXEC); |
| 38 | } |
| 39 | |
| 40 | |