2009-08-05 12:02:56 +08:00
|
|
|
/* ===-- enable_execute_stack.c - Implement __enable_execute_stack ---------===
|
|
|
|
*
|
|
|
|
* The LLVM Compiler Infrastructure
|
|
|
|
*
|
|
|
|
* This file is distributed under the University of Illinois Open Source
|
|
|
|
* License. See LICENSE.TXT for details.
|
|
|
|
*
|
|
|
|
* ===----------------------------------------------------------------------===
|
|
|
|
*/
|
2009-06-27 00:47:03 +08:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <sys/mman.h>
|
2009-07-01 14:06:42 +08:00
|
|
|
#ifndef __APPLE__
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2009-06-27 00:47:03 +08:00
|
|
|
|
|
|
|
|
2009-08-05 12:02:56 +08:00
|
|
|
/*
|
|
|
|
* The compiler generates calls to __enable_execute_stack() when creating
|
|
|
|
* trampoline functions on the stack for use with nested functions.
|
|
|
|
* It is expected to mark the page(s) containing the address
|
|
|
|
* and the next 48 bytes as executable. Since the stack is normally rw-
|
|
|
|
* that means changing the protection on those page(s) to rwx.
|
|
|
|
*/
|
|
|
|
|
2009-06-27 00:47:03 +08:00
|
|
|
void __enable_execute_stack(void* addr)
|
|
|
|
{
|
|
|
|
#if __APPLE__
|
2009-08-05 12:02:56 +08:00
|
|
|
/* On Darwin, pagesize is always 4096 bytes */
|
2009-06-27 00:47:03 +08:00
|
|
|
const uintptr_t pageSize = 4096;
|
|
|
|
#else
|
2009-08-05 12:02:56 +08:00
|
|
|
/* FIXME: We should have a configure check for this. */
|
2009-07-01 14:06:42 +08:00
|
|
|
const uintptr_t pageSize = getpagesize();
|
2009-06-27 00:47:03 +08:00
|
|
|
#endif
|
|
|
|
const uintptr_t pageAlignMask = ~(pageSize-1);
|
|
|
|
uintptr_t p = (uintptr_t)addr;
|
|
|
|
unsigned char* startPage = (unsigned char*)(p & pageAlignMask);
|
|
|
|
unsigned char* endPage = (unsigned char*)((p+48+pageSize) & pageAlignMask);
|
2009-08-08 10:31:50 +08:00
|
|
|
size_t length = endPage - startPage;
|
|
|
|
(void) mprotect((void *)startPage, length, PROT_READ | PROT_WRITE | PROT_EXEC);
|
2009-06-27 00:47:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|