|
| 1 | +//===--- RunOnNewStack.cpp - Crash Recovery -------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "llvm/Support/ProgramStack.h" |
| 10 | +#include "llvm/Config/config.h" |
| 11 | +#include "llvm/Support/Compiler.h" |
| 12 | + |
| 13 | +#ifdef LLVM_ON_UNIX |
| 14 | +# include <sys/resource.h> // for getrlimit |
| 15 | +#endif |
| 16 | + |
| 17 | +#ifdef _MSC_VER |
| 18 | +# include <intrin.h> // for _AddressOfReturnAddress |
| 19 | +#endif |
| 20 | + |
| 21 | +// Currently only Apple AArch64 is known to support split stacks in the debugger |
| 22 | +// and other tooling. |
| 23 | +#if defined(__APPLE__) && defined(__aarch64__) && \ |
| 24 | + LLVM_HAS_CPP_ATTRIBUTE(gnu::naked) && __has_extension(gnu_asm) |
| 25 | +# define LLVM_HAS_SPLIT_STACKS |
| 26 | +# define LLVM_HAS_SPLIT_STACKS_AARCH64 |
| 27 | +#include <sys/mman.h> |
| 28 | +#endif |
| 29 | + |
| 30 | +#ifndef LLVM_HAS_SPLIT_STACKS |
| 31 | +# include "llvm/Support/thread.h" |
| 32 | +#endif |
| 33 | + |
| 34 | +using namespace llvm; |
| 35 | + |
| 36 | +uintptr_t llvm::getStackPointer() { |
| 37 | +#if __GNUC__ || __has_builtin(__builtin_frame_address) |
| 38 | + return (uintptr_t)__builtin_frame_address(0); |
| 39 | +#elif defined(_MSC_VER) |
| 40 | + return (uintptr_t)_AddressOfReturnAddress(); |
| 41 | +#else |
| 42 | + volatile char CharOnStack = 0; |
| 43 | + // The volatile store here is intended to escape the local variable, to |
| 44 | + // prevent the compiler from optimizing CharOnStack into anything other |
| 45 | + // than a char on the stack. |
| 46 | + // |
| 47 | + // Tested on: MSVC 2015 - 2019, GCC 4.9 - 9, Clang 3.2 - 9, ICC 13 - 19. |
| 48 | + char *volatile Ptr = &CharOnStack; |
| 49 | + return (uintptr_t)Ptr; |
| 50 | +#endif |
| 51 | +} |
| 52 | + |
| 53 | +unsigned llvm::getDefaultStackSize() { |
| 54 | +#ifdef LLVM_ON_UNIX |
| 55 | + rlimit RL; |
| 56 | + getrlimit(RLIMIT_STACK, &RL); |
| 57 | + return RL.rlim_cur; |
| 58 | +#else |
| 59 | + // Clang recursively parses, instantiates templates, and evaluates constant |
| 60 | + // expressions. We've found 8MiB to be a reasonable stack size given the way |
| 61 | + // Clang works and the way C++ is commonly written. |
| 62 | + return 8 << 20; |
| 63 | +#endif |
| 64 | +} |
| 65 | + |
| 66 | +namespace { |
| 67 | +#ifdef LLVM_HAS_SPLIT_STACKS_AARCH64 |
| 68 | +[[gnu::naked]] void runOnNewStackImpl(void *Stack, void (*Fn)(void *), |
| 69 | + void *Ctx) { |
| 70 | + __asm__ volatile( |
| 71 | + "mov x16, sp\n\t" |
| 72 | + "sub x0, x0, #0x20\n\t" // subtract space from stack |
| 73 | + "stp xzr, x16, [x0, #0x00]\n\t" // save old sp |
| 74 | + "stp x29, x30, [x0, #0x10]\n\t" // save fp, lr |
| 75 | + "mov sp, x0\n\t" // switch to new stack |
| 76 | + "add x29, x0, #0x10\n\t" // switch to new frame |
| 77 | + ".cfi_def_cfa w29, 16\n\t" |
| 78 | + ".cfi_offset w30, -8\n\t" // lr |
| 79 | + ".cfi_offset w29, -16\n\t" // fp |
| 80 | + |
| 81 | + "mov x0, x2\n\t" // Ctx is the only argument |
| 82 | + "blr x1\n\t" // call Fn |
| 83 | + |
| 84 | + "ldp x29, x30, [sp, #0x10]\n\t" // restore fp, lr |
| 85 | + "ldp xzr, x16, [sp, #0x00]\n\t" // load old sp |
| 86 | + "mov sp, x16\n\t" |
| 87 | + "ret" |
| 88 | + ); |
| 89 | +} |
| 90 | +#endif |
| 91 | + |
| 92 | +#ifdef LLVM_HAS_SPLIT_STACKS |
| 93 | +void callback(void *Ctx) { |
| 94 | + (*reinterpret_cast<function_ref<void()> *>(Ctx))(); |
| 95 | +} |
| 96 | +#endif |
| 97 | +} // namespace |
| 98 | + |
| 99 | +#ifdef LLVM_HAS_SPLIT_STACKS |
| 100 | +void llvm::runOnNewStack(unsigned StackSize, function_ref<void()> Fn) { |
| 101 | + if (StackSize == 0) |
| 102 | + StackSize = getDefaultStackSize(); |
| 103 | + |
| 104 | + // We use malloc here instead of mmap because: |
| 105 | + // - it's simpler, |
| 106 | + // - many malloc implementations will reuse the allocation in cases where |
| 107 | + // we're bouncing accross the edge of a stack boundry, and |
| 108 | + // - many malloc implemenations will already provide guard pages for |
| 109 | + // allocations this large. |
| 110 | + void *Stack = malloc(StackSize); |
| 111 | + void *BottomOfStack = (char *)Stack + StackSize; |
| 112 | + |
| 113 | + runOnNewStackImpl(BottomOfStack, callback, &Fn); |
| 114 | + |
| 115 | + free(Stack); |
| 116 | +} |
| 117 | +#else |
| 118 | +void llvm::runOnNewStack(unsigned StackSize, function_ref<void()> Fn) { |
| 119 | + llvm::thread Thread( |
| 120 | + StackSize == 0 ? std::nullopt : std::optional<unsigned>(StackSize), Fn); |
| 121 | + Thread.join(); |
| 122 | +} |
| 123 | +#endif |
0 commit comments