xref: /src/contrib/llvm-project/clang/lib/Basic/Stack.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
1519fc96cSDimitry Andric //===--- Stack.cpp - Utilities for dealing with stack space ---------------===//
2519fc96cSDimitry Andric //
3519fc96cSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4519fc96cSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5519fc96cSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6519fc96cSDimitry Andric //
7519fc96cSDimitry Andric //===----------------------------------------------------------------------===//
8519fc96cSDimitry Andric ///
9519fc96cSDimitry Andric /// \file
10519fc96cSDimitry Andric /// Defines utilities for dealing with stack allocation and stack space.
11519fc96cSDimitry Andric ///
12519fc96cSDimitry Andric //===----------------------------------------------------------------------===//
13519fc96cSDimitry Andric 
14519fc96cSDimitry Andric #include "clang/Basic/Stack.h"
15519fc96cSDimitry Andric #include "llvm/Support/CrashRecoveryContext.h"
16519fc96cSDimitry Andric 
17519fc96cSDimitry Andric #ifdef _MSC_VER
18519fc96cSDimitry Andric #include <intrin.h>  // for _AddressOfReturnAddress
19519fc96cSDimitry Andric #endif
20519fc96cSDimitry Andric 
21519fc96cSDimitry Andric static LLVM_THREAD_LOCAL void *BottomOfStack = nullptr;
22519fc96cSDimitry Andric 
getStackPointer()23519fc96cSDimitry Andric static void *getStackPointer() {
24519fc96cSDimitry Andric #if __GNUC__ || __has_builtin(__builtin_frame_address)
25519fc96cSDimitry Andric   return __builtin_frame_address(0);
26519fc96cSDimitry Andric #elif defined(_MSC_VER)
27519fc96cSDimitry Andric   return _AddressOfReturnAddress();
28519fc96cSDimitry Andric #else
29519fc96cSDimitry Andric   char CharOnStack = 0;
30519fc96cSDimitry Andric   // The volatile store here is intended to escape the local variable, to
31519fc96cSDimitry Andric   // prevent the compiler from optimizing CharOnStack into anything other
32519fc96cSDimitry Andric   // than a char on the stack.
33519fc96cSDimitry Andric   //
34519fc96cSDimitry Andric   // Tested on: MSVC 2015 - 2019, GCC 4.9 - 9, Clang 3.2 - 9, ICC 13 - 19.
35519fc96cSDimitry Andric   char *volatile Ptr = &CharOnStack;
36519fc96cSDimitry Andric   return Ptr;
37519fc96cSDimitry Andric #endif
38519fc96cSDimitry Andric }
39519fc96cSDimitry Andric 
noteBottomOfStack()40519fc96cSDimitry Andric void clang::noteBottomOfStack() {
41519fc96cSDimitry Andric   if (!BottomOfStack)
42519fc96cSDimitry Andric     BottomOfStack = getStackPointer();
43519fc96cSDimitry Andric }
44519fc96cSDimitry Andric 
isStackNearlyExhausted()45519fc96cSDimitry Andric bool clang::isStackNearlyExhausted() {
46519fc96cSDimitry Andric   // We consider 256 KiB to be sufficient for any code that runs between checks
47519fc96cSDimitry Andric   // for stack size.
48519fc96cSDimitry Andric   constexpr size_t SufficientStack = 256 << 10;
49519fc96cSDimitry Andric 
50519fc96cSDimitry Andric   // If we don't know where the bottom of the stack is, hope for the best.
51519fc96cSDimitry Andric   if (!BottomOfStack)
52519fc96cSDimitry Andric     return false;
53519fc96cSDimitry Andric 
54519fc96cSDimitry Andric   intptr_t StackDiff = (intptr_t)getStackPointer() - (intptr_t)BottomOfStack;
55519fc96cSDimitry Andric   size_t StackUsage = (size_t)std::abs(StackDiff);
56519fc96cSDimitry Andric 
57519fc96cSDimitry Andric   // If the stack pointer has a surprising value, we do not understand this
58519fc96cSDimitry Andric   // stack usage scheme. (Perhaps the target allocates new stack regions on
59519fc96cSDimitry Andric   // demand for us.) Don't try to guess what's going on.
60519fc96cSDimitry Andric   if (StackUsage > DesiredStackSize)
61519fc96cSDimitry Andric     return false;
62519fc96cSDimitry Andric 
63519fc96cSDimitry Andric   return StackUsage >= DesiredStackSize - SufficientStack;
64519fc96cSDimitry Andric }
65519fc96cSDimitry Andric 
runWithSufficientStackSpaceSlow(llvm::function_ref<void ()> Diag,llvm::function_ref<void ()> Fn)66519fc96cSDimitry Andric void clang::runWithSufficientStackSpaceSlow(llvm::function_ref<void()> Diag,
67519fc96cSDimitry Andric                                             llvm::function_ref<void()> Fn) {
68519fc96cSDimitry Andric   llvm::CrashRecoveryContext CRC;
69519fc96cSDimitry Andric   CRC.RunSafelyOnThread([&] {
70519fc96cSDimitry Andric     noteBottomOfStack();
71519fc96cSDimitry Andric     Diag();
72519fc96cSDimitry Andric     Fn();
73519fc96cSDimitry Andric   }, DesiredStackSize);
74519fc96cSDimitry Andric }
75