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")))
14static void __shadowcallstack_init() {
15 void *stack = mmap(NULL, len: 8192, PROT_READ | PROT_WRITE,
16 MAP_PRIVATE | MAP_ANONYMOUS, fd: -1, offset: 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
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 our
33 // return address for testing purposes (see overflow.c), so we need to exit
34 // ourselves.
35 exit(status: scs_main());
36}
37

source code of compiler-rt/test/shadowcallstack/minimal_runtime.h