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/Support/CrashRecoveryContext.h"
16*a7dea167SDimitry Andric
17*a7dea167SDimitry Andric #ifdef _MSC_VER
18*a7dea167SDimitry Andric #include <intrin.h> // for _AddressOfReturnAddress
19*a7dea167SDimitry Andric #endif
20*a7dea167SDimitry Andric
21*a7dea167SDimitry Andric static LLVM_THREAD_LOCAL void *BottomOfStack = nullptr;
22*a7dea167SDimitry Andric
getStackPointer()23*a7dea167SDimitry Andric static void *getStackPointer() {
24*a7dea167SDimitry Andric #if __GNUC__ || __has_builtin(__builtin_frame_address)
25*a7dea167SDimitry Andric return __builtin_frame_address(0);
26*a7dea167SDimitry Andric #elif defined(_MSC_VER)
27*a7dea167SDimitry Andric return _AddressOfReturnAddress();
28*a7dea167SDimitry Andric #else
29*a7dea167SDimitry Andric char CharOnStack = 0;
30*a7dea167SDimitry Andric // The volatile store here is intended to escape the local variable, to
31*a7dea167SDimitry Andric // prevent the compiler from optimizing CharOnStack into anything other
32*a7dea167SDimitry Andric // than a char on the stack.
33*a7dea167SDimitry Andric //
34*a7dea167SDimitry Andric // Tested on: MSVC 2015 - 2019, GCC 4.9 - 9, Clang 3.2 - 9, ICC 13 - 19.
35*a7dea167SDimitry Andric char *volatile Ptr = &CharOnStack;
36*a7dea167SDimitry Andric return Ptr;
37*a7dea167SDimitry Andric #endif
38*a7dea167SDimitry Andric }
39*a7dea167SDimitry Andric
noteBottomOfStack()40*a7dea167SDimitry Andric void clang::noteBottomOfStack() {
41*a7dea167SDimitry Andric if (!BottomOfStack)
42*a7dea167SDimitry Andric BottomOfStack = getStackPointer();
43*a7dea167SDimitry Andric }
44*a7dea167SDimitry Andric
isStackNearlyExhausted()45*a7dea167SDimitry Andric bool clang::isStackNearlyExhausted() {
46*a7dea167SDimitry Andric // We consider 256 KiB to be sufficient for any code that runs between checks
47*a7dea167SDimitry Andric // for stack size.
48*a7dea167SDimitry Andric constexpr size_t SufficientStack = 256 << 10;
49*a7dea167SDimitry Andric
50*a7dea167SDimitry Andric // If we don't know where the bottom of the stack is, hope for the best.
51*a7dea167SDimitry Andric if (!BottomOfStack)
52*a7dea167SDimitry Andric return false;
53*a7dea167SDimitry Andric
54*a7dea167SDimitry Andric intptr_t StackDiff = (intptr_t)getStackPointer() - (intptr_t)BottomOfStack;
55*a7dea167SDimitry Andric size_t StackUsage = (size_t)std::abs(StackDiff);
56*a7dea167SDimitry Andric
57*a7dea167SDimitry Andric // If the stack pointer has a surprising value, we do not understand this
58*a7dea167SDimitry Andric // stack usage scheme. (Perhaps the target allocates new stack regions on
59*a7dea167SDimitry Andric // demand for us.) Don't try to guess what's going on.
60*a7dea167SDimitry Andric if (StackUsage > DesiredStackSize)
61*a7dea167SDimitry Andric return false;
62*a7dea167SDimitry Andric
63*a7dea167SDimitry Andric return StackUsage >= DesiredStackSize - SufficientStack;
64*a7dea167SDimitry Andric }
65*a7dea167SDimitry Andric
runWithSufficientStackSpaceSlow(llvm::function_ref<void ()> Diag,llvm::function_ref<void ()> Fn)66*a7dea167SDimitry Andric void clang::runWithSufficientStackSpaceSlow(llvm::function_ref<void()> Diag,
67*a7dea167SDimitry Andric llvm::function_ref<void()> Fn) {
68*a7dea167SDimitry Andric llvm::CrashRecoveryContext CRC;
69*a7dea167SDimitry Andric CRC.RunSafelyOnThread([&] {
70*a7dea167SDimitry Andric noteBottomOfStack();
71*a7dea167SDimitry Andric Diag();
72*a7dea167SDimitry Andric Fn();
73*a7dea167SDimitry Andric }, DesiredStackSize);
74*a7dea167SDimitry Andric }
75