clang 20.0.0git
Stack.cpp
Go to the documentation of this file.
1//===--- Stack.cpp - Utilities for dealing with stack space ---------------===//
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/// \file
10/// Defines utilities for dealing with stack allocation and stack space.
11///
12//===----------------------------------------------------------------------===//
13
14#include "clang/Basic/Stack.h"
15#include "llvm/Support/CrashRecoveryContext.h"
16
17#ifdef _MSC_VER
18#include <intrin.h> // for _AddressOfReturnAddress
19#endif
20
21static LLVM_THREAD_LOCAL void *BottomOfStack = nullptr;
22
23static void *getStackPointer() {
24#if __GNUC__ || __has_builtin(__builtin_frame_address)
25 return __builtin_frame_address(0);
26#elif defined(_MSC_VER)
27 return _AddressOfReturnAddress();
28#else
29 char CharOnStack = 0;
30 // The volatile store here is intended to escape the local variable, to
31 // prevent the compiler from optimizing CharOnStack into anything other
32 // than a char on the stack.
33 //
34 // Tested on: MSVC 2015 - 2019, GCC 4.9 - 9, Clang 3.2 - 9, ICC 13 - 19.
35 char *volatile Ptr = &CharOnStack;
36 return Ptr;
37#endif
38}
39
41 if (!BottomOfStack)
43}
44
46 // We consider 256 KiB to be sufficient for any code that runs between checks
47 // for stack size.
48 constexpr size_t SufficientStack = 256 << 10;
49
50 // If we don't know where the bottom of the stack is, hope for the best.
51 if (!BottomOfStack)
52 return false;
53
55 size_t StackUsage = (size_t)std::abs(StackDiff);
56
57 // If the stack pointer has a surprising value, we do not understand this
58 // stack usage scheme. (Perhaps the target allocates new stack regions on
59 // demand for us.) Don't try to guess what's going on.
60 if (StackUsage > DesiredStackSize)
61 return false;
62
63 return StackUsage >= DesiredStackSize - SufficientStack;
64}
65
66void clang::runWithSufficientStackSpaceSlow(llvm::function_ref<void()> Diag,
67 llvm::function_ref<void()> Fn) {
68 llvm::CrashRecoveryContext CRC;
69 CRC.RunSafelyOnThread([&] {
71 Diag();
72 Fn();
74}
static DiagnosticBuilder Diag(DiagnosticsEngine *Diags, const LangOptions &Features, FullSourceLoc TokLoc, const char *TokBegin, const char *TokRangeBegin, const char *TokRangeEnd, unsigned DiagID)
Produce a diagnostic highlighting some portion of a literal.
static LLVM_THREAD_LOCAL void * BottomOfStack
Definition: Stack.cpp:21
static void * getStackPointer()
Definition: Stack.cpp:23
Defines utilities for dealing with stack allocation and stack space.
__SIZE_TYPE__ size_t
void runWithSufficientStackSpaceSlow(llvm::function_ref< void()> Diag, llvm::function_ref< void()> Fn)
Definition: Stack.cpp:66
void noteBottomOfStack()
Call this once on each thread, as soon after starting the thread as feasible, to note the approximate...
Definition: Stack.cpp:40
bool isStackNearlyExhausted()
Determine whether the stack is nearly exhausted.
Definition: Stack.cpp:45
constexpr size_t DesiredStackSize
The amount of stack space that Clang would like to be provided with.
Definition: Stack.h:26
__DEVICE__ _Tp abs(const std::complex< _Tp > &__c)
Definition: complex_cmath.h:34
__INTPTR_TYPE__ intptr_t
A signed integer type with the property that any valid pointer to void can be converted to this type,...