Vlad Tsyrklevich | 1781d10 | 2018-04-04 17:53:33 +0000 | [diff] [blame] | 1 | // A shadow call stack runtime is not yet included with compiler-rt, provide a |
Peter Collingbourne | 6662e98 | 2018-04-09 20:18:10 +0000 | [diff] [blame] | 2 | // minimal runtime to allocate a shadow call stack and assign an |
| 3 | // architecture-specific register to point at it. |
Vlad Tsyrklevich | 1781d10 | 2018-04-04 17:53:33 +0000 | [diff] [blame] | 4 | |
| 5 | #pragma once |
| 6 | |
Peter Collingbourne | 6662e98 | 2018-04-09 20:18:10 +0000 | [diff] [blame] | 7 | #ifdef __x86_64__ |
Vlad Tsyrklevich | 1781d10 | 2018-04-04 17:53:33 +0000 | [diff] [blame] | 8 | #include <asm/prctl.h> |
Peter Collingbourne | 6662e98 | 2018-04-09 20:18:10 +0000 | [diff] [blame] | 9 | int arch_prctl(int code, void *addr); |
| 10 | #endif |
Vlad Tsyrklevich | 1781d10 | 2018-04-04 17:53:33 +0000 | [diff] [blame] | 11 | #include <stdlib.h> |
| 12 | #include <sys/mman.h> |
| 13 | #include <sys/prctl.h> |
| 14 | |
Peter Collingbourne | 6662e98 | 2018-04-09 20:18:10 +0000 | [diff] [blame] | 15 | #include "libc_support.h" |
Vlad Tsyrklevich | 1781d10 | 2018-04-04 17:53:33 +0000 | [diff] [blame] | 16 | |
| 17 | __attribute__((no_sanitize("shadow-call-stack"))) |
| 18 | static 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 Collingbourne | 6662e98 | 2018-04-09 20:18:10 +0000 | [diff] [blame] | 24 | #if defined(__x86_64__) |
Vlad Tsyrklevich | 1781d10 | 2018-04-04 17:53:33 +0000 | [diff] [blame] | 25 | if (arch_prctl(ARCH_SET_GS, stack)) |
| 26 | abort(); |
Peter Collingbourne | 6662e98 | 2018-04-09 20:18:10 +0000 | [diff] [blame] | 27 | #elif defined(__aarch64__) |
| 28 | __asm__ __volatile__("mov x18, %0" ::"r"(stack)); |
| 29 | #else |
| 30 | #error Unsupported platform |
| 31 | #endif |
Vlad Tsyrklevich | 1781d10 | 2018-04-04 17:53:33 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Peter Collingbourne | 6662e98 | 2018-04-09 20:18:10 +0000 | [diff] [blame] | 34 | int 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 | } |