19b7bbecaSWeiming Zhao // REQUIRES: native-run 29b7bbecaSWeiming Zhao // RUN: %clang_builtins %s %librt -o %t && %run %t 38c1441f8SAlexey Samsonov //===-- enable_execute_stack_test.c - Test __enable_execute_stack ----------===// 48c1441f8SAlexey Samsonov // 5*57b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 6*57b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 7*57b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 88c1441f8SAlexey Samsonov // 98c1441f8SAlexey Samsonov //===----------------------------------------------------------------------===// 108c1441f8SAlexey Samsonov 118c1441f8SAlexey Samsonov 128c1441f8SAlexey Samsonov #include <stdio.h> 138c1441f8SAlexey Samsonov #include <string.h> 148c1441f8SAlexey Samsonov #include <stdint.h> 158c1441f8SAlexey Samsonov extern void __clear_cache(void* start, void* end); 168c1441f8SAlexey Samsonov extern void __enable_execute_stack(void* addr); 178c1441f8SAlexey Samsonov 188c1441f8SAlexey Samsonov typedef int (*pfunc)(void); 198c1441f8SAlexey Samsonov 208c78ca2eSReid Kleckner // Make these static to avoid ILT jumps for incremental linking on Windows. 218c78ca2eSReid Kleckner static int func1() { return 1; } 228c78ca2eSReid Kleckner static int func2() { return 2; } 238c1441f8SAlexey Samsonov 246e6c1c3eSSaleem Abdulrasool void *__attribute__((noinline)) 256e6c1c3eSSaleem Abdulrasool memcpy_f(void *dst, const void *src, size_t n) { 266e6c1c3eSSaleem Abdulrasool // ARM and MIPS nartually align functions, but use the LSB for ISA selection 276e6c1c3eSSaleem Abdulrasool // (THUMB, MIPS16/uMIPS respectively). Ensure that the ISA bit is ignored in 286e6c1c3eSSaleem Abdulrasool // the memcpy 296e6c1c3eSSaleem Abdulrasool #if defined(__arm__) || defined(__mips__) 306e6c1c3eSSaleem Abdulrasool return (void *)((uintptr_t)memcpy(dst, (void *)((uintptr_t)src & ~1), n) | 316e6c1c3eSSaleem Abdulrasool ((uintptr_t)src & 1)); 326e6c1c3eSSaleem Abdulrasool #else 336e6c1c3eSSaleem Abdulrasool return memcpy(dst, (void *)((uintptr_t)src), n); 346e6c1c3eSSaleem Abdulrasool #endif 356e6c1c3eSSaleem Abdulrasool } 368c1441f8SAlexey Samsonov 378c1441f8SAlexey Samsonov int main() 388c1441f8SAlexey Samsonov { 398c1441f8SAlexey Samsonov unsigned char execution_buffer[128]; 408c1441f8SAlexey Samsonov // mark stack page containing execution_buffer to be executable 418c1441f8SAlexey Samsonov __enable_execute_stack(execution_buffer); 428c1441f8SAlexey Samsonov 438c1441f8SAlexey Samsonov // verify you can copy and execute a function 446e6c1c3eSSaleem Abdulrasool pfunc f1 = (pfunc)memcpy_f(execution_buffer, func1, 128); 458c1441f8SAlexey Samsonov __clear_cache(execution_buffer, &execution_buffer[128]); 468c78ca2eSReid Kleckner printf("f1: %p\n", f1); 478c1441f8SAlexey Samsonov if ((*f1)() != 1) 488c1441f8SAlexey Samsonov return 1; 498c1441f8SAlexey Samsonov 508c1441f8SAlexey Samsonov // verify you can overwrite a function with another 516e6c1c3eSSaleem Abdulrasool pfunc f2 = (pfunc)memcpy_f(execution_buffer, func2, 128); 528c1441f8SAlexey Samsonov __clear_cache(execution_buffer, &execution_buffer[128]); 538c1441f8SAlexey Samsonov if ((*f2)() != 2) 548c1441f8SAlexey Samsonov return 1; 558c1441f8SAlexey Samsonov 568c1441f8SAlexey Samsonov return 0; 578c1441f8SAlexey Samsonov } 58