xref: /freebsd/contrib/llvm-project/clang/lib/Basic/Stack.cpp (revision a7dea1671b87c07d2d266f836bfa8b58efc7c134)
1*a7dea167SDimitry Andric //===--- Stack.cpp - Utilities for dealing with stack space ---------------===//
2*a7dea167SDimitry Andric //
3*a7dea167SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*a7dea167SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*a7dea167SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*a7dea167SDimitry Andric //
7*a7dea167SDimitry Andric //===----------------------------------------------------------------------===//
8*a7dea167SDimitry Andric ///
9*a7dea167SDimitry Andric /// \file
10*a7dea167SDimitry Andric /// Defines utilities for dealing with stack allocation and stack space.
11*a7dea167SDimitry Andric ///
12*a7dea167SDimitry Andric //===----------------------------------------------------------------------===//
13*a7dea167SDimitry Andric 
14*a7dea167SDimitry Andric #include "clang/Basic/Stack.h"
15*a7dea167SDimitry Andric #include "llvm/ADT/Optional.h"
16*a7dea167SDimitry Andric #include "llvm/Support/CrashRecoveryContext.h"
17*a7dea167SDimitry Andric 
18*a7dea167SDimitry Andric #ifdef _MSC_VER
19*a7dea167SDimitry Andric #include <intrin.h>  // for _AddressOfReturnAddress
20*a7dea167SDimitry Andric #endif
21*a7dea167SDimitry Andric 
22*a7dea167SDimitry Andric static LLVM_THREAD_LOCAL void *BottomOfStack = nullptr;
23*a7dea167SDimitry Andric 
24*a7dea167SDimitry Andric static void *getStackPointer() {
25*a7dea167SDimitry Andric #if __GNUC__ || __has_builtin(__builtin_frame_address)
26*a7dea167SDimitry Andric   return __builtin_frame_address(0);
27*a7dea167SDimitry Andric #elif defined(_MSC_VER)
28*a7dea167SDimitry Andric   return _AddressOfReturnAddress();
29*a7dea167SDimitry Andric #else
30*a7dea167SDimitry Andric   char CharOnStack = 0;
31*a7dea167SDimitry Andric   // The volatile store here is intended to escape the local variable, to
32*a7dea167SDimitry Andric   // prevent the compiler from optimizing CharOnStack into anything other
33*a7dea167SDimitry Andric   // than a char on the stack.
34*a7dea167SDimitry Andric   //
35*a7dea167SDimitry Andric   // Tested on: MSVC 2015 - 2019, GCC 4.9 - 9, Clang 3.2 - 9, ICC 13 - 19.
36*a7dea167SDimitry Andric   char *volatile Ptr = &CharOnStack;
37*a7dea167SDimitry Andric   return Ptr;
38*a7dea167SDimitry Andric #endif
39*a7dea167SDimitry Andric }
40*a7dea167SDimitry Andric 
41*a7dea167SDimitry Andric void clang::noteBottomOfStack() {
42*a7dea167SDimitry Andric   if (!BottomOfStack)
43*a7dea167SDimitry Andric     BottomOfStack = getStackPointer();
44*a7dea167SDimitry Andric }
45*a7dea167SDimitry Andric 
46*a7dea167SDimitry Andric bool clang::isStackNearlyExhausted() {
47*a7dea167SDimitry Andric   // We consider 256 KiB to be sufficient for any code that runs between checks
48*a7dea167SDimitry Andric   // for stack size.
49*a7dea167SDimitry Andric   constexpr size_t SufficientStack = 256 << 10;
50*a7dea167SDimitry Andric 
51*a7dea167SDimitry Andric   // If we don't know where the bottom of the stack is, hope for the best.
52*a7dea167SDimitry Andric   if (!BottomOfStack)
53*a7dea167SDimitry Andric     return false;
54*a7dea167SDimitry Andric 
55*a7dea167SDimitry Andric   intptr_t StackDiff = (intptr_t)getStackPointer() - (intptr_t)BottomOfStack;
56*a7dea167SDimitry Andric   size_t StackUsage = (size_t)std::abs(StackDiff);
57*a7dea167SDimitry Andric 
58*a7dea167SDimitry Andric   // If the stack pointer has a surprising value, we do not understand this
59*a7dea167SDimitry Andric   // stack usage scheme. (Perhaps the target allocates new stack regions on
60*a7dea167SDimitry Andric   // demand for us.) Don't try to guess what's going on.
61*a7dea167SDimitry Andric   if (StackUsage > DesiredStackSize)
62*a7dea167SDimitry Andric     return false;
63*a7dea167SDimitry Andric 
64*a7dea167SDimitry Andric   return StackUsage >= DesiredStackSize - SufficientStack;
65*a7dea167SDimitry Andric }
66*a7dea167SDimitry Andric 
67*a7dea167SDimitry Andric void clang::runWithSufficientStackSpaceSlow(llvm::function_ref<void()> Diag,
68*a7dea167SDimitry Andric                                             llvm::function_ref<void()> Fn) {
69*a7dea167SDimitry Andric   llvm::CrashRecoveryContext CRC;
70*a7dea167SDimitry Andric   CRC.RunSafelyOnThread([&] {
71*a7dea167SDimitry Andric     noteBottomOfStack();
72*a7dea167SDimitry Andric     Diag();
73*a7dea167SDimitry Andric     Fn();
74*a7dea167SDimitry Andric   }, DesiredStackSize);
75*a7dea167SDimitry Andric }
76