From ce1a63509d2750d5e647d6378d99c1457374eba4 Mon Sep 17 00:00:00 2001 From: Chandler Carruth Date: Wed, 24 May 2023 08:27:47 -0700 Subject: [PATCH] Make stack detection more reliable. (#2845) The old version wasn't compatible with ASan's instrumentation. Instead, use a builtin when available, and even in the fallback disable inlining and sanitizing as they can't do anything useful in this routine. This fixes a confusing internal ASan error seen by some folks since the stack detection was added. --- explorer/interpreter/stack_space.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/explorer/interpreter/stack_space.cpp b/explorer/interpreter/stack_space.cpp index a1597012f3e2..3b87f54c378d 100644 --- a/explorer/interpreter/stack_space.cpp +++ b/explorer/interpreter/stack_space.cpp @@ -16,10 +16,15 @@ static constexpr int64_t DesiredStackSpace = 8 << 20; static LLVM_THREAD_LOCAL intptr_t bottom_of_stack = 0; // Returns the current bottom of stack. -static auto GetStackPointer() -> intptr_t { +LLVM_NO_SANITIZE("address") +LLVM_ATTRIBUTE_NOINLINE static auto GetStackPointer() -> intptr_t { +#if __GNUC__ || __has_builtin(__builtin_frame_address) + return reinterpret_cast(__builtin_frame_address(0)); +#else char char_on_stack = 0; char* volatile ptr = &char_on_stack; return reinterpret_cast(ptr); +#endif } auto IsStackSpaceNearlyExhausted() -> bool {