brintos

brintos / llvm-project-archived public Read only

0
0
Text · 982 B · fab4fdf Raw
37 lines · c
1// A shadow call stack runtime is not yet included with compiler-rt, provide a2// minimal runtime to allocate a shadow call stack and assign an3// architecture-specific register to point at it.4 5#pragma once6 7#include <stdlib.h>8#include <sys/mman.h>9#include <sys/prctl.h>10 11#include "libc_support.h"12 13__attribute__((no_sanitize("shadow-call-stack")))14static void __shadowcallstack_init() {15  void *stack = mmap(NULL, 8192, PROT_READ | PROT_WRITE,16                     MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);17  if (stack == MAP_FAILED)18    abort();19 20#if defined(__aarch64__)21  __asm__ __volatile__("mov x18, %0" ::"r"(stack));22#else23#error Unsupported platform24#endif25}26 27int scs_main(void);28 29__attribute__((no_sanitize("shadow-call-stack"))) int main(void) {30  __shadowcallstack_init();31 32  // We can't simply return scs_main() because scs_main might have corrupted our33  // return address for testing purposes (see overflow.c), so we need to exit34  // ourselves.35  exit(scs_main());36}37