2009-08-05 12:02:56 +08:00
|
|
|
/* ===-- enable_execute_stack.c - Implement __enable_execute_stack ---------===
|
|
|
|
*
|
|
|
|
* The LLVM Compiler Infrastructure
|
|
|
|
*
|
2010-11-17 06:13:33 +08:00
|
|
|
* This file is dual licensed under the MIT and the University of Illinois Open
|
|
|
|
* Source Licenses. See LICENSE.TXT for details.
|
2009-08-05 12:02:56 +08:00
|
|
|
*
|
|
|
|
* ===----------------------------------------------------------------------===
|
|
|
|
*/
|
2009-06-27 00:47:03 +08:00
|
|
|
|
2011-11-16 03:02:22 +08:00
|
|
|
#include "int_lib.h"
|
|
|
|
|
2015-07-18 00:23:05 +08:00
|
|
|
#ifndef _WIN32
|
2009-06-27 00:47:03 +08:00
|
|
|
#include <sys/mman.h>
|
2015-07-18 00:23:05 +08:00
|
|
|
#endif
|
2009-08-10 08:56:46 +08:00
|
|
|
|
|
|
|
/* #include "config.h"
|
|
|
|
* FIXME: CMake - include when cmake system is ready.
|
2009-08-10 09:02:16 +08:00
|
|
|
* Remove #define HAVE_SYSCONF 1 line.
|
2009-08-10 08:56:46 +08:00
|
|
|
*/
|
2009-08-10 09:02:16 +08:00
|
|
|
#define HAVE_SYSCONF 1
|
2009-08-10 08:56:46 +08:00
|
|
|
|
2015-07-18 00:23:05 +08:00
|
|
|
#ifdef _WIN32
|
2015-10-06 12:32:59 +08:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
2017-07-31 14:01:39 +08:00
|
|
|
#include <windows.h>
|
2015-07-18 00:23:05 +08:00
|
|
|
#else
|
2009-07-01 14:06:42 +08:00
|
|
|
#ifndef __APPLE__
|
|
|
|
#include <unistd.h>
|
2009-08-10 08:56:46 +08:00
|
|
|
#endif /* __APPLE__ */
|
2015-07-18 00:23:05 +08:00
|
|
|
#endif /* _WIN32 */
|
2009-06-27 00:47:03 +08:00
|
|
|
|
2010-04-01 03:52:01 +08:00
|
|
|
#if __LP64__
|
|
|
|
#define TRAMPOLINE_SIZE 48
|
|
|
|
#else
|
|
|
|
#define TRAMPOLINE_SIZE 40
|
|
|
|
#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.
|
|
|
|
*/
|
|
|
|
|
2014-03-01 23:30:50 +08:00
|
|
|
COMPILER_RT_ABI void
|
|
|
|
__enable_execute_stack(void* addr)
|
2009-06-27 00:47:03 +08:00
|
|
|
{
|
2009-08-10 08:56:46 +08:00
|
|
|
|
2015-07-18 00:23:05 +08:00
|
|
|
#if _WIN32
|
|
|
|
MEMORY_BASIC_INFORMATION mbi;
|
|
|
|
if (!VirtualQuery (addr, &mbi, sizeof(mbi)))
|
|
|
|
return; /* We should probably assert here because there is no return value */
|
|
|
|
VirtualProtect (mbi.BaseAddress, mbi.RegionSize, PAGE_EXECUTE_READWRITE, &mbi.Protect);
|
|
|
|
#else
|
2009-06-27 00:47:03 +08:00
|
|
|
#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;
|
2009-08-10 08:56:46 +08:00
|
|
|
#elif !defined(HAVE_SYSCONF)
|
|
|
|
#error "HAVE_SYSCONF not defined! See enable_execute_stack.c"
|
2009-06-27 00:47:03 +08:00
|
|
|
#else
|
2009-08-10 02:59:21 +08:00
|
|
|
const uintptr_t pageSize = sysconf(_SC_PAGESIZE);
|
2009-08-10 08:56:46 +08:00
|
|
|
#endif /* __APPLE__ */
|
|
|
|
|
2009-06-27 00:47:03 +08:00
|
|
|
const uintptr_t pageAlignMask = ~(pageSize-1);
|
|
|
|
uintptr_t p = (uintptr_t)addr;
|
|
|
|
unsigned char* startPage = (unsigned char*)(p & pageAlignMask);
|
2010-04-01 03:52:01 +08:00
|
|
|
unsigned char* endPage = (unsigned char*)((p+TRAMPOLINE_SIZE+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);
|
2015-07-18 00:23:05 +08:00
|
|
|
#endif
|
2009-06-27 00:47:03 +08:00
|
|
|
}
|