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 %gs to point at
3 // it.
4 
5 #pragma once
6 
7 #include <asm/prctl.h>
8 #include <stdlib.h>
9 #include <sys/mman.h>
10 #include <sys/prctl.h>
11 
12 int arch_prctl(int code, void *addr);
13 
14 __attribute__((no_sanitize("shadow-call-stack")))
15 static void __shadowcallstack_init() {
16   void *stack = mmap(NULL, 8192, PROT_READ | PROT_WRITE,
17                      MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
18   if (stack == MAP_FAILED)
19     abort();
20 
21   if (arch_prctl(ARCH_SET_GS, stack))
22     abort();
23 }
24 
25 __attribute__((section(".preinit_array"), used))
26     void (*__shadowcallstack_preinit)(void) = __shadowcallstack_init;
27