Edward O'Callaghan | 2bf6272 | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 1 | /* ===-- enable_execute_stack.c - Implement __enable_execute_stack ---------=== |
| 2 | * |
| 3 | * The LLVM Compiler Infrastructure |
| 4 | * |
Howard Hinnant | 9ad441f | 2010-11-16 22:13:33 +0000 | [diff] [blame] | 5 | * This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | * Source Licenses. See LICENSE.TXT for details. |
Edward O'Callaghan | 2bf6272 | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 7 | * |
| 8 | * ===----------------------------------------------------------------------=== |
| 9 | */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 10 | |
Daniel Dunbar | 23d1542 | 2011-11-15 19:02:22 +0000 | [diff] [blame] | 11 | #include "int_lib.h" |
| 12 | |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 13 | #include <sys/mman.h> |
Edward O'Callaghan | de1c6cf | 2009-08-10 00:56:46 +0000 | [diff] [blame] | 14 | |
| 15 | /* #include "config.h" |
| 16 | * FIXME: CMake - include when cmake system is ready. |
Edward O'Callaghan | b72794d | 2009-08-10 01:02:16 +0000 | [diff] [blame] | 17 | * Remove #define HAVE_SYSCONF 1 line. |
Edward O'Callaghan | de1c6cf | 2009-08-10 00:56:46 +0000 | [diff] [blame] | 18 | */ |
Edward O'Callaghan | b72794d | 2009-08-10 01:02:16 +0000 | [diff] [blame] | 19 | #define HAVE_SYSCONF 1 |
Edward O'Callaghan | de1c6cf | 2009-08-10 00:56:46 +0000 | [diff] [blame] | 20 | |
Daniel Dunbar | f639213 | 2009-07-01 06:06:42 +0000 | [diff] [blame] | 21 | #ifndef __APPLE__ |
| 22 | #include <unistd.h> |
Edward O'Callaghan | de1c6cf | 2009-08-10 00:56:46 +0000 | [diff] [blame] | 23 | #endif /* __APPLE__ */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 24 | |
Nick Kledzik | e7d0a5c | 2010-03-31 19:52:01 +0000 | [diff] [blame] | 25 | #if __LP64__ |
| 26 | #define TRAMPOLINE_SIZE 48 |
| 27 | #else |
| 28 | #define TRAMPOLINE_SIZE 40 |
| 29 | #endif |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 30 | |
Edward O'Callaghan | 2bf6272 | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 31 | /* |
| 32 | * The compiler generates calls to __enable_execute_stack() when creating |
| 33 | * trampoline functions on the stack for use with nested functions. |
| 34 | * It is expected to mark the page(s) containing the address |
| 35 | * and the next 48 bytes as executable. Since the stack is normally rw- |
| 36 | * that means changing the protection on those page(s) to rwx. |
| 37 | */ |
| 38 | |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 39 | void __enable_execute_stack(void* addr) |
| 40 | { |
Edward O'Callaghan | de1c6cf | 2009-08-10 00:56:46 +0000 | [diff] [blame] | 41 | |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 42 | #if __APPLE__ |
Edward O'Callaghan | 2bf6272 | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 43 | /* On Darwin, pagesize is always 4096 bytes */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 44 | const uintptr_t pageSize = 4096; |
Edward O'Callaghan | de1c6cf | 2009-08-10 00:56:46 +0000 | [diff] [blame] | 45 | #elif !defined(HAVE_SYSCONF) |
| 46 | #error "HAVE_SYSCONF not defined! See enable_execute_stack.c" |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 47 | #else |
Nuno Lopes | 166b783 | 2009-08-09 18:59:21 +0000 | [diff] [blame] | 48 | const uintptr_t pageSize = sysconf(_SC_PAGESIZE); |
Edward O'Callaghan | de1c6cf | 2009-08-10 00:56:46 +0000 | [diff] [blame] | 49 | #endif /* __APPLE__ */ |
| 50 | |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 51 | const uintptr_t pageAlignMask = ~(pageSize-1); |
| 52 | uintptr_t p = (uintptr_t)addr; |
| 53 | unsigned char* startPage = (unsigned char*)(p & pageAlignMask); |
Nick Kledzik | e7d0a5c | 2010-03-31 19:52:01 +0000 | [diff] [blame] | 54 | unsigned char* endPage = (unsigned char*)((p+TRAMPOLINE_SIZE+pageSize) & pageAlignMask); |
Edward O'Callaghan | bb119a4 | 2009-08-08 02:31:50 +0000 | [diff] [blame] | 55 | size_t length = endPage - startPage; |
| 56 | (void) mprotect((void *)startPage, length, PROT_READ | PROT_WRITE | PROT_EXEC); |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | |