blob: 7ab8e8d4155c0cb544a6057aee0f0bac5cc730c8 [file] [log] [blame]
Edward O'Callaghan2bf62722009-08-05 04:02:56 +00001/* ===-- enable_execute_stack.c - Implement __enable_execute_stack ---------===
2 *
3 * The LLVM Compiler Infrastructure
4 *
Howard Hinnant9ad441f2010-11-16 22:13:33 +00005 * This file is dual licensed under the MIT and the University of Illinois Open
6 * Source Licenses. See LICENSE.TXT for details.
Edward O'Callaghan2bf62722009-08-05 04:02:56 +00007 *
8 * ===----------------------------------------------------------------------===
9 */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000010
11#include <stdint.h>
12#include <sys/mman.h>
Edward O'Callaghande1c6cf2009-08-10 00:56:46 +000013
14/* #include "config.h"
15 * FIXME: CMake - include when cmake system is ready.
Edward O'Callaghanb72794d2009-08-10 01:02:16 +000016 * Remove #define HAVE_SYSCONF 1 line.
Edward O'Callaghande1c6cf2009-08-10 00:56:46 +000017 */
Edward O'Callaghanb72794d2009-08-10 01:02:16 +000018#define HAVE_SYSCONF 1
Edward O'Callaghande1c6cf2009-08-10 00:56:46 +000019
Daniel Dunbarf6392132009-07-01 06:06:42 +000020#ifndef __APPLE__
21#include <unistd.h>
Edward O'Callaghande1c6cf2009-08-10 00:56:46 +000022#endif /* __APPLE__ */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000023
Nick Kledzike7d0a5c2010-03-31 19:52:01 +000024#if __LP64__
25 #define TRAMPOLINE_SIZE 48
26#else
27 #define TRAMPOLINE_SIZE 40
28#endif
Daniel Dunbarb3a69012009-06-26 16:47:03 +000029
Edward O'Callaghan2bf62722009-08-05 04:02:56 +000030/*
31 * The compiler generates calls to __enable_execute_stack() when creating
32 * trampoline functions on the stack for use with nested functions.
33 * It is expected to mark the page(s) containing the address
34 * and the next 48 bytes as executable. Since the stack is normally rw-
35 * that means changing the protection on those page(s) to rwx.
36 */
37
Daniel Dunbarb3a69012009-06-26 16:47:03 +000038void __enable_execute_stack(void* addr)
39{
Edward O'Callaghande1c6cf2009-08-10 00:56:46 +000040
Daniel Dunbarb3a69012009-06-26 16:47:03 +000041#if __APPLE__
Edward O'Callaghan2bf62722009-08-05 04:02:56 +000042 /* On Darwin, pagesize is always 4096 bytes */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000043 const uintptr_t pageSize = 4096;
Edward O'Callaghande1c6cf2009-08-10 00:56:46 +000044#elif !defined(HAVE_SYSCONF)
45#error "HAVE_SYSCONF not defined! See enable_execute_stack.c"
Daniel Dunbarb3a69012009-06-26 16:47:03 +000046#else
Nuno Lopes166b7832009-08-09 18:59:21 +000047 const uintptr_t pageSize = sysconf(_SC_PAGESIZE);
Edward O'Callaghande1c6cf2009-08-10 00:56:46 +000048#endif /* __APPLE__ */
49
Daniel Dunbarb3a69012009-06-26 16:47:03 +000050 const uintptr_t pageAlignMask = ~(pageSize-1);
51 uintptr_t p = (uintptr_t)addr;
52 unsigned char* startPage = (unsigned char*)(p & pageAlignMask);
Nick Kledzike7d0a5c2010-03-31 19:52:01 +000053 unsigned char* endPage = (unsigned char*)((p+TRAMPOLINE_SIZE+pageSize) & pageAlignMask);
Edward O'Callaghanbb119a42009-08-08 02:31:50 +000054 size_t length = endPage - startPage;
55 (void) mprotect((void *)startPage, length, PROT_READ | PROT_WRITE | PROT_EXEC);
Daniel Dunbarb3a69012009-06-26 16:47:03 +000056}
57
58