19b7bbecaSWeiming Zhao // REQUIRES: native-run
2190b9110SMichał Górny // RUN: %clang_builtins %s %librt -o %t && %run_nomprotect %t
35be7eb3aSDan Liew // REQUIRES: librt_has_enable_execute_stack
48c1441f8SAlexey Samsonov
58c1441f8SAlexey Samsonov #include <stdio.h>
68c1441f8SAlexey Samsonov #include <string.h>
78c1441f8SAlexey Samsonov #include <stdint.h>
88c1441f8SAlexey Samsonov extern void __clear_cache(void* start, void* end);
98c1441f8SAlexey Samsonov extern void __enable_execute_stack(void* addr);
108c1441f8SAlexey Samsonov
118c1441f8SAlexey Samsonov typedef int (*pfunc)(void);
128c1441f8SAlexey Samsonov
138c78ca2eSReid Kleckner // Make these static to avoid ILT jumps for incremental linking on Windows.
func1()148c78ca2eSReid Kleckner static int func1() { return 1; }
func2()158c78ca2eSReid Kleckner static int func2() { return 2; }
168c1441f8SAlexey Samsonov
176e6c1c3eSSaleem Abdulrasool void *__attribute__((noinline))
memcpy_f(void * dst,const void * src,size_t n)186e6c1c3eSSaleem Abdulrasool memcpy_f(void *dst, const void *src, size_t n) {
19a1e7e401SKazuaki Ishizaki // ARM and MIPS naturally align functions, but use the LSB for ISA selection
206e6c1c3eSSaleem Abdulrasool // (THUMB, MIPS16/uMIPS respectively). Ensure that the ISA bit is ignored in
216e6c1c3eSSaleem Abdulrasool // the memcpy
226e6c1c3eSSaleem Abdulrasool #if defined(__arm__) || defined(__mips__)
236e6c1c3eSSaleem Abdulrasool return (void *)((uintptr_t)memcpy(dst, (void *)((uintptr_t)src & ~1), n) |
246e6c1c3eSSaleem Abdulrasool ((uintptr_t)src & 1));
256e6c1c3eSSaleem Abdulrasool #else
266e6c1c3eSSaleem Abdulrasool return memcpy(dst, (void *)((uintptr_t)src), n);
276e6c1c3eSSaleem Abdulrasool #endif
286e6c1c3eSSaleem Abdulrasool }
298c1441f8SAlexey Samsonov
main()308c1441f8SAlexey Samsonov int main()
318c1441f8SAlexey Samsonov {
32*496e7f33SSimon Moll #if defined(__ve__)
33*496e7f33SSimon Moll unsigned char execution_buffer[128] __attribute__((__aligned__(8)));
34*496e7f33SSimon Moll #else
358c1441f8SAlexey Samsonov unsigned char execution_buffer[128];
36*496e7f33SSimon Moll #endif
378c1441f8SAlexey Samsonov // mark stack page containing execution_buffer to be executable
388c1441f8SAlexey Samsonov __enable_execute_stack(execution_buffer);
398c1441f8SAlexey Samsonov
408c1441f8SAlexey Samsonov // verify you can copy and execute a function
416e6c1c3eSSaleem Abdulrasool pfunc f1 = (pfunc)memcpy_f(execution_buffer, func1, 128);
428c1441f8SAlexey Samsonov __clear_cache(execution_buffer, &execution_buffer[128]);
438c78ca2eSReid Kleckner printf("f1: %p\n", f1);
448c1441f8SAlexey Samsonov if ((*f1)() != 1)
458c1441f8SAlexey Samsonov return 1;
468c1441f8SAlexey Samsonov
478c1441f8SAlexey Samsonov // verify you can overwrite a function with another
486e6c1c3eSSaleem Abdulrasool pfunc f2 = (pfunc)memcpy_f(execution_buffer, func2, 128);
498c1441f8SAlexey Samsonov __clear_cache(execution_buffer, &execution_buffer[128]);
508c1441f8SAlexey Samsonov if ((*f2)() != 2)
518c1441f8SAlexey Samsonov return 1;
528c1441f8SAlexey Samsonov
538c1441f8SAlexey Samsonov return 0;
548c1441f8SAlexey Samsonov }
55