2009-06-27 00:47:03 +08:00
|
|
|
//===-- enable_execute_stack_test.c - Test __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-06-27 00:47:03 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdint.h>
|
2012-01-13 05:13:48 +08:00
|
|
|
#if defined(_WIN32)
|
|
|
|
#include <windows.h>
|
|
|
|
void __clear_cache(void* start, void* end)
|
|
|
|
{
|
|
|
|
if (!FlushInstructionCache(GetCurrentProcess(), start, end-start))
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
void __enable_execute_stack(void *addr)
|
|
|
|
{
|
|
|
|
MEMORY_BASIC_INFORMATION b;
|
2009-06-27 00:47:03 +08:00
|
|
|
|
2012-01-13 05:13:48 +08:00
|
|
|
if (!VirtualQuery(addr, &b, sizeof(b)))
|
|
|
|
exit(1);
|
|
|
|
if (!VirtualProtect(b.BaseAddress, b.RegionSize, PAGE_EXECUTE_READWRITE, &b.Protect))
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#include <sys/mman.h>
|
2009-06-27 00:47:03 +08:00
|
|
|
extern void __clear_cache(void* start, void* end);
|
|
|
|
extern void __enable_execute_stack(void* addr);
|
2012-01-13 05:13:48 +08:00
|
|
|
#endif
|
2009-06-27 00:47:03 +08:00
|
|
|
|
|
|
|
typedef int (*pfunc)(void);
|
|
|
|
|
|
|
|
int func1()
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int func2()
|
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2015-01-06 05:54:50 +08:00
|
|
|
void *__attribute__((noinline))
|
|
|
|
memcpy_f(void *dst, const void *src, size_t n) {
|
|
|
|
// ARM and MIPS nartually align functions, but use the LSB for ISA selection
|
|
|
|
// (THUMB, MIPS16/uMIPS respectively). Ensure that the ISA bit is ignored in
|
|
|
|
// the memcpy
|
|
|
|
#if defined(__arm__) || defined(__mips__)
|
|
|
|
return (void *)((uintptr_t)memcpy(dst, (void *)((uintptr_t)src & ~1), n) |
|
|
|
|
((uintptr_t)src & 1));
|
|
|
|
#else
|
|
|
|
return memcpy(dst, (void *)((uintptr_t)src), n);
|
|
|
|
#endif
|
|
|
|
}
|
2009-06-27 00:47:03 +08:00
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2009-10-28 23:54:04 +08:00
|
|
|
unsigned char execution_buffer[128];
|
|
|
|
// mark stack page containing execution_buffer to be executable
|
|
|
|
__enable_execute_stack(execution_buffer);
|
2009-06-27 00:47:03 +08:00
|
|
|
|
|
|
|
// verify you can copy and execute a function
|
2015-01-06 05:54:50 +08:00
|
|
|
pfunc f1 = (pfunc)memcpy_f(execution_buffer, func1, 128);
|
2009-06-27 00:47:03 +08:00
|
|
|
__clear_cache(execution_buffer, &execution_buffer[128]);
|
2012-01-13 05:13:48 +08:00
|
|
|
if ((*f1)() != 1)
|
2009-06-27 00:47:03 +08:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
// verify you can overwrite a function with another
|
2015-01-06 05:54:50 +08:00
|
|
|
pfunc f2 = (pfunc)memcpy_f(execution_buffer, func2, 128);
|
2009-06-27 00:47:03 +08:00
|
|
|
__clear_cache(execution_buffer, &execution_buffer[128]);
|
2012-01-13 05:13:48 +08:00
|
|
|
if ((*f2)() != 2)
|
2009-06-27 00:47:03 +08:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|