Stack.cpp 2.4 KB

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