• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // A shadow call stack runtime is not yet included with compiler-rt, provide a
2 // minimal runtime to allocate a shadow call stack and assign an
3 // architecture-specific register to point at it.
4 
5 #pragma once
6 
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")))
__shadowcallstack_init()14 static 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 #else
23 #error Unsupported platform
24 #endif
25 }
26 
27 int scs_main(void);
28 
main(void)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 our
33   // return address for testing purposes (see overflow.c), so we need to exit
34   // ourselves.
35   exit(scs_main());
36 }
37