blob: f36fa5a7d245aa02b7377cf71008350fb9936bff [file] [log] [blame]
Vlad Tsyrklevich1781d102018-04-04 17:53:33 +00001// A shadow call stack runtime is not yet included with compiler-rt, provide a
Peter Collingbourne6662e982018-04-09 20:18:10 +00002// minimal runtime to allocate a shadow call stack and assign an
3// architecture-specific register to point at it.
Vlad Tsyrklevich1781d102018-04-04 17:53:33 +00004
5#pragma once
6
Peter Collingbourne6662e982018-04-09 20:18:10 +00007#ifdef __x86_64__
Vlad Tsyrklevich1781d102018-04-04 17:53:33 +00008#include <asm/prctl.h>
Peter Collingbourne6662e982018-04-09 20:18:10 +00009int arch_prctl(int code, void *addr);
10#endif
Vlad Tsyrklevich1781d102018-04-04 17:53:33 +000011#include <stdlib.h>
12#include <sys/mman.h>
13#include <sys/prctl.h>
14
Peter Collingbourne6662e982018-04-09 20:18:10 +000015#include "libc_support.h"
Vlad Tsyrklevich1781d102018-04-04 17:53:33 +000016
17__attribute__((no_sanitize("shadow-call-stack")))
18static void __shadowcallstack_init() {
19 void *stack = mmap(NULL, 8192, PROT_READ | PROT_WRITE,
20 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
21 if (stack == MAP_FAILED)
22 abort();
23
Peter Collingbourne6662e982018-04-09 20:18:10 +000024#if defined(__x86_64__)
Vlad Tsyrklevich1781d102018-04-04 17:53:33 +000025 if (arch_prctl(ARCH_SET_GS, stack))
26 abort();
Peter Collingbourne6662e982018-04-09 20:18:10 +000027#elif defined(__aarch64__)
28 __asm__ __volatile__("mov x18, %0" ::"r"(stack));
29#else
30#error Unsupported platform
31#endif
Vlad Tsyrklevich1781d102018-04-04 17:53:33 +000032}
33
Peter Collingbourne6662e982018-04-09 20:18:10 +000034int scs_main(void);
35
36__attribute__((no_sanitize("shadow-call-stack"))) int main(void) {
37 __shadowcallstack_init();
38
39 // We can't simply return scs_main() because scs_main might have corrupted our
40 // return address for testing purposes (see overflow.c), so we need to exit
41 // ourselves.
42 exit(scs_main());
43}