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 //
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 extern void __clear_cache(void* start, void* end);
178c1441f8SAlexey Samsonov extern void __enable_execute_stack(void* addr);
188c1441f8SAlexey Samsonov 
198c1441f8SAlexey Samsonov typedef int (*pfunc)(void);
208c1441f8SAlexey Samsonov 
21*8c78ca2eSReid Kleckner // Make these static to avoid ILT jumps for incremental linking on Windows.
22*8c78ca2eSReid Kleckner static int func1() { return 1; }
23*8c78ca2eSReid Kleckner static int func2() { return 2; }
248c1441f8SAlexey Samsonov 
256e6c1c3eSSaleem Abdulrasool void *__attribute__((noinline))
266e6c1c3eSSaleem Abdulrasool memcpy_f(void *dst, const void *src, size_t n) {
276e6c1c3eSSaleem Abdulrasool // ARM and MIPS nartually align functions, but use the LSB for ISA selection
286e6c1c3eSSaleem Abdulrasool // (THUMB, MIPS16/uMIPS respectively).  Ensure that the ISA bit is ignored in
296e6c1c3eSSaleem Abdulrasool // the memcpy
306e6c1c3eSSaleem Abdulrasool #if defined(__arm__) || defined(__mips__)
316e6c1c3eSSaleem Abdulrasool   return (void *)((uintptr_t)memcpy(dst, (void *)((uintptr_t)src & ~1), n) |
326e6c1c3eSSaleem Abdulrasool                   ((uintptr_t)src & 1));
336e6c1c3eSSaleem Abdulrasool #else
346e6c1c3eSSaleem Abdulrasool   return memcpy(dst, (void *)((uintptr_t)src), n);
356e6c1c3eSSaleem Abdulrasool #endif
366e6c1c3eSSaleem Abdulrasool }
378c1441f8SAlexey Samsonov 
388c1441f8SAlexey Samsonov int main()
398c1441f8SAlexey Samsonov {
408c1441f8SAlexey Samsonov     unsigned char execution_buffer[128];
418c1441f8SAlexey Samsonov     // mark stack page containing execution_buffer to be executable
428c1441f8SAlexey Samsonov     __enable_execute_stack(execution_buffer);
438c1441f8SAlexey Samsonov 
448c1441f8SAlexey Samsonov     // verify you can copy and execute a function
456e6c1c3eSSaleem Abdulrasool     pfunc f1 = (pfunc)memcpy_f(execution_buffer, func1, 128);
468c1441f8SAlexey Samsonov     __clear_cache(execution_buffer, &execution_buffer[128]);
47*8c78ca2eSReid Kleckner     printf("f1: %p\n", f1);
488c1441f8SAlexey Samsonov     if ((*f1)() != 1)
498c1441f8SAlexey Samsonov         return 1;
508c1441f8SAlexey Samsonov 
518c1441f8SAlexey Samsonov     // verify you can overwrite a function with another
526e6c1c3eSSaleem Abdulrasool     pfunc f2 = (pfunc)memcpy_f(execution_buffer, func2, 128);
538c1441f8SAlexey Samsonov     __clear_cache(execution_buffer, &execution_buffer[128]);
548c1441f8SAlexey Samsonov     if ((*f2)() != 2)
558c1441f8SAlexey Samsonov         return 1;
568c1441f8SAlexey Samsonov 
578c1441f8SAlexey Samsonov     return 0;
588c1441f8SAlexey Samsonov }
59