blob: 278ca246f537e16924d570d4e489b27d23c40c03 [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
Daniel Dunbar23d15422011-11-15 19:02:22 +000011#include "int_lib.h"
12
Daniel Dunbarb3a69012009-06-26 16:47:03 +000013#include <sys/mman.h>
Edward O'Callaghande1c6cf2009-08-10 00:56:46 +000014
15/* #include "config.h"
16 * FIXME: CMake - include when cmake system is ready.
Edward O'Callaghanb72794d2009-08-10 01:02:16 +000017 * Remove #define HAVE_SYSCONF 1 line.
Edward O'Callaghande1c6cf2009-08-10 00:56:46 +000018 */
Edward O'Callaghanb72794d2009-08-10 01:02:16 +000019#define HAVE_SYSCONF 1
Edward O'Callaghande1c6cf2009-08-10 00:56:46 +000020
Daniel Dunbarf6392132009-07-01 06:06:42 +000021#ifndef __APPLE__
22#include <unistd.h>
Edward O'Callaghande1c6cf2009-08-10 00:56:46 +000023#endif /* __APPLE__ */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000024
Nick Kledzike7d0a5c2010-03-31 19:52:01 +000025#if __LP64__
26 #define TRAMPOLINE_SIZE 48
27#else
28 #define TRAMPOLINE_SIZE 40
29#endif
Daniel Dunbarb3a69012009-06-26 16:47:03 +000030
Edward O'Callaghan2bf62722009-08-05 04:02:56 +000031/*
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 Dunbarb3a69012009-06-26 16:47:03 +000039void __enable_execute_stack(void* addr)
40{
Edward O'Callaghande1c6cf2009-08-10 00:56:46 +000041
Daniel Dunbarb3a69012009-06-26 16:47:03 +000042#if __APPLE__
Edward O'Callaghan2bf62722009-08-05 04:02:56 +000043 /* On Darwin, pagesize is always 4096 bytes */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000044 const uintptr_t pageSize = 4096;
Edward O'Callaghande1c6cf2009-08-10 00:56:46 +000045#elif !defined(HAVE_SYSCONF)
46#error "HAVE_SYSCONF not defined! See enable_execute_stack.c"
Daniel Dunbarb3a69012009-06-26 16:47:03 +000047#else
Nuno Lopes166b7832009-08-09 18:59:21 +000048 const uintptr_t pageSize = sysconf(_SC_PAGESIZE);
Edward O'Callaghande1c6cf2009-08-10 00:56:46 +000049#endif /* __APPLE__ */
50
Daniel Dunbarb3a69012009-06-26 16:47:03 +000051 const uintptr_t pageAlignMask = ~(pageSize-1);
52 uintptr_t p = (uintptr_t)addr;
53 unsigned char* startPage = (unsigned char*)(p & pageAlignMask);
Nick Kledzike7d0a5c2010-03-31 19:52:01 +000054 unsigned char* endPage = (unsigned char*)((p+TRAMPOLINE_SIZE+pageSize) & pageAlignMask);
Edward O'Callaghanbb119a42009-08-08 02:31:50 +000055 size_t length = endPage - startPage;
56 (void) mprotect((void *)startPage, length, PROT_READ | PROT_WRITE | PROT_EXEC);
Daniel Dunbarb3a69012009-06-26 16:47:03 +000057}
58
59