1*9b7bbecaSWeiming Zhao // REQUIRES: native-run 2*9b7bbecaSWeiming Zhao // RUN: %clang_builtins %s %librt -o %t && %run %t 38c1441f8SAlexey Samsonov //===-- enable_execute_stack_test.c - Test __enable_execute_stack ----------===// 48c1441f8SAlexey Samsonov // 58c1441f8SAlexey Samsonov // The LLVM Compiler Infrastructure 68c1441f8SAlexey Samsonov // 78c1441f8SAlexey Samsonov // This file is dual licensed under the MIT and the University of Illinois Open 88c1441f8SAlexey Samsonov // Source Licenses. See LICENSE.TXT for details. 98c1441f8SAlexey Samsonov // 108c1441f8SAlexey Samsonov //===----------------------------------------------------------------------===// 118c1441f8SAlexey Samsonov 128c1441f8SAlexey Samsonov 138c1441f8SAlexey Samsonov #include <stdio.h> 148c1441f8SAlexey Samsonov #include <string.h> 158c1441f8SAlexey Samsonov #include <stdint.h> 168c1441f8SAlexey Samsonov #if defined(_WIN32) 178c1441f8SAlexey Samsonov #include <windows.h> 188c1441f8SAlexey Samsonov void __clear_cache(void* start, void* end) 198c1441f8SAlexey Samsonov { 208c1441f8SAlexey Samsonov if (!FlushInstructionCache(GetCurrentProcess(), start, end-start)) 218c1441f8SAlexey Samsonov exit(1); 228c1441f8SAlexey Samsonov } 238c1441f8SAlexey Samsonov void __enable_execute_stack(void *addr) 248c1441f8SAlexey Samsonov { 258c1441f8SAlexey Samsonov MEMORY_BASIC_INFORMATION b; 268c1441f8SAlexey Samsonov 278c1441f8SAlexey Samsonov if (!VirtualQuery(addr, &b, sizeof(b))) 288c1441f8SAlexey Samsonov exit(1); 298c1441f8SAlexey Samsonov if (!VirtualProtect(b.BaseAddress, b.RegionSize, PAGE_EXECUTE_READWRITE, &b.Protect)) 308c1441f8SAlexey Samsonov exit(1); 318c1441f8SAlexey Samsonov } 328c1441f8SAlexey Samsonov #else 338c1441f8SAlexey Samsonov #include <sys/mman.h> 348c1441f8SAlexey Samsonov extern void __clear_cache(void* start, void* end); 358c1441f8SAlexey Samsonov extern void __enable_execute_stack(void* addr); 368c1441f8SAlexey Samsonov #endif 378c1441f8SAlexey Samsonov 388c1441f8SAlexey Samsonov typedef int (*pfunc)(void); 398c1441f8SAlexey Samsonov 408c1441f8SAlexey Samsonov int func1() 418c1441f8SAlexey Samsonov { 428c1441f8SAlexey Samsonov return 1; 438c1441f8SAlexey Samsonov } 448c1441f8SAlexey Samsonov 458c1441f8SAlexey Samsonov int func2() 468c1441f8SAlexey Samsonov { 478c1441f8SAlexey Samsonov return 2; 488c1441f8SAlexey Samsonov } 498c1441f8SAlexey Samsonov 506e6c1c3eSSaleem Abdulrasool void *__attribute__((noinline)) 516e6c1c3eSSaleem Abdulrasool memcpy_f(void *dst, const void *src, size_t n) { 526e6c1c3eSSaleem Abdulrasool // ARM and MIPS nartually align functions, but use the LSB for ISA selection 536e6c1c3eSSaleem Abdulrasool // (THUMB, MIPS16/uMIPS respectively). Ensure that the ISA bit is ignored in 546e6c1c3eSSaleem Abdulrasool // the memcpy 556e6c1c3eSSaleem Abdulrasool #if defined(__arm__) || defined(__mips__) 566e6c1c3eSSaleem Abdulrasool return (void *)((uintptr_t)memcpy(dst, (void *)((uintptr_t)src & ~1), n) | 576e6c1c3eSSaleem Abdulrasool ((uintptr_t)src & 1)); 586e6c1c3eSSaleem Abdulrasool #else 596e6c1c3eSSaleem Abdulrasool return memcpy(dst, (void *)((uintptr_t)src), n); 606e6c1c3eSSaleem Abdulrasool #endif 616e6c1c3eSSaleem Abdulrasool } 628c1441f8SAlexey Samsonov 638c1441f8SAlexey Samsonov int main() 648c1441f8SAlexey Samsonov { 658c1441f8SAlexey Samsonov unsigned char execution_buffer[128]; 668c1441f8SAlexey Samsonov // mark stack page containing execution_buffer to be executable 678c1441f8SAlexey Samsonov __enable_execute_stack(execution_buffer); 688c1441f8SAlexey Samsonov 698c1441f8SAlexey Samsonov // verify you can copy and execute a function 706e6c1c3eSSaleem Abdulrasool pfunc f1 = (pfunc)memcpy_f(execution_buffer, func1, 128); 718c1441f8SAlexey Samsonov __clear_cache(execution_buffer, &execution_buffer[128]); 728c1441f8SAlexey Samsonov if ((*f1)() != 1) 738c1441f8SAlexey Samsonov return 1; 748c1441f8SAlexey Samsonov 758c1441f8SAlexey Samsonov // verify you can overwrite a function with another 766e6c1c3eSSaleem Abdulrasool pfunc f2 = (pfunc)memcpy_f(execution_buffer, func2, 128); 778c1441f8SAlexey Samsonov __clear_cache(execution_buffer, &execution_buffer[128]); 788c1441f8SAlexey Samsonov if ((*f2)() != 2) 798c1441f8SAlexey Samsonov return 1; 808c1441f8SAlexey Samsonov 818c1441f8SAlexey Samsonov return 0; 828c1441f8SAlexey Samsonov } 83