brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.1 KiB · b3cb4df Raw
68 lines · c
1// REQUIRES: native-run2// RUN: %clang_builtins %s %librt -o %t && %run_nomprotect %t3// REQUIRES: librt_has_enable_execute_stack4 5#include <stdio.h>6#include <string.h>7#include <stdint.h>8extern void __clear_cache(void* start, void* end);9extern void __enable_execute_stack(void* addr);10 11typedef int (*pfunc)(void);12 13#ifdef __arm64ec__14// On ARM64EC, we need the x86_64 version of this function, but the compiler15// would normally generate the AArch64 variant, so we hardcode it here.16static char func1[] = {17    0xb8, 0x01, 0x00, 0x00, 0x00, // movl    $0x1, %eax18    0xc3                          // retq19};20static char func2[] = {21    0xb8, 0x02, 0x00, 0x00, 0x00, // movl    $0x2, %eax22    0xc3                          // retq23};24#else25// Make these static to avoid ILT jumps for incremental linking on Windows.26static int func1() { return 1; }27static int func2() { return 2; }28#endif29 30void *__attribute__((noinline))31memcpy_f(void *dst, const void *src, size_t n) {32// ARM and MIPS naturally align functions, but use the LSB for ISA selection33// (THUMB, MIPS16/uMIPS respectively).  Ensure that the ISA bit is ignored in34// the memcpy35#if defined(__arm__) || defined(__mips__)36  return (void *)((uintptr_t)memcpy(dst, (void *)((uintptr_t)src & ~1), n) |37                  ((uintptr_t)src & 1));38#else39  return memcpy(dst, (void *)((uintptr_t)src), n);40#endif41}42 43int main()44{45#if defined(__ve__)46    unsigned char execution_buffer[128] __attribute__((__aligned__(8)));47#else48    unsigned char execution_buffer[128];49#endif50    // mark stack page containing execution_buffer to be executable51    __enable_execute_stack(execution_buffer);52	53    // verify you can copy and execute a function54    pfunc f1 = (pfunc)memcpy_f(execution_buffer, func1, 128);55    __clear_cache(execution_buffer, &execution_buffer[128]);56    printf("f1: %p\n", f1);57    if ((*f1)() != 1)58        return 1;59 60    // verify you can overwrite a function with another61    pfunc f2 = (pfunc)memcpy_f(execution_buffer, func2, 128);62    __clear_cache(execution_buffer, &execution_buffer[128]);63    if ((*f2)() != 2)64        return 1;65 66    return 0;67}68