ASanStackFrameLayout.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //===-- ASanStackFrameLayout.cpp - helper for AddressSanitizer ------------===//
  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. // Definition of ComputeASanStackFrameLayout (see ASanStackFrameLayout.h).
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Transforms/Utils/ASanStackFrameLayout.h"
  13. #include "llvm/ADT/SmallString.h"
  14. #include "llvm/IR/DebugInfo.h"
  15. #include "llvm/Support/MathExtras.h"
  16. #include "llvm/Support/ScopedPrinter.h"
  17. #include "llvm/Support/raw_ostream.h"
  18. #include <algorithm>
  19. namespace llvm {
  20. // We sort the stack variables by alignment (largest first) to minimize
  21. // unnecessary large gaps due to alignment.
  22. // It is tempting to also sort variables by size so that larger variables
  23. // have larger redzones at both ends. But reordering will make report analysis
  24. // harder, especially when temporary unnamed variables are present.
  25. // So, until we can provide more information (type, line number, etc)
  26. // for the stack variables we avoid reordering them too much.
  27. static inline bool CompareVars(const ASanStackVariableDescription &a,
  28. const ASanStackVariableDescription &b) {
  29. return a.Alignment > b.Alignment;
  30. }
  31. // We also force minimal alignment for all vars to kMinAlignment so that vars
  32. // with e.g. alignment 1 and alignment 16 do not get reordered by CompareVars.
  33. static const uint64_t kMinAlignment = 16;
  34. // We want to add a full redzone after every variable.
  35. // The larger the variable Size the larger is the redzone.
  36. // The resulting frame size is a multiple of Alignment.
  37. static uint64_t VarAndRedzoneSize(uint64_t Size, uint64_t Granularity,
  38. uint64_t Alignment) {
  39. uint64_t Res = 0;
  40. if (Size <= 4) Res = 16;
  41. else if (Size <= 16) Res = 32;
  42. else if (Size <= 128) Res = Size + 32;
  43. else if (Size <= 512) Res = Size + 64;
  44. else if (Size <= 4096) Res = Size + 128;
  45. else Res = Size + 256;
  46. return alignTo(std::max(Res, 2 * Granularity), Alignment);
  47. }
  48. ASanStackFrameLayout
  49. ComputeASanStackFrameLayout(SmallVectorImpl<ASanStackVariableDescription> &Vars,
  50. uint64_t Granularity, uint64_t MinHeaderSize) {
  51. assert(Granularity >= 8 && Granularity <= 64 &&
  52. (Granularity & (Granularity - 1)) == 0);
  53. assert(MinHeaderSize >= 16 && (MinHeaderSize & (MinHeaderSize - 1)) == 0 &&
  54. MinHeaderSize >= Granularity);
  55. const size_t NumVars = Vars.size();
  56. assert(NumVars > 0);
  57. for (size_t i = 0; i < NumVars; i++)
  58. Vars[i].Alignment = std::max(Vars[i].Alignment, kMinAlignment);
  59. llvm::stable_sort(Vars, CompareVars);
  60. ASanStackFrameLayout Layout;
  61. Layout.Granularity = Granularity;
  62. Layout.FrameAlignment = std::max(Granularity, Vars[0].Alignment);
  63. uint64_t Offset =
  64. std::max(std::max(MinHeaderSize, Granularity), Vars[0].Alignment);
  65. assert((Offset % Granularity) == 0);
  66. for (size_t i = 0; i < NumVars; i++) {
  67. bool IsLast = i == NumVars - 1;
  68. uint64_t Alignment = std::max(Granularity, Vars[i].Alignment);
  69. (void)Alignment; // Used only in asserts.
  70. uint64_t Size = Vars[i].Size;
  71. assert((Alignment & (Alignment - 1)) == 0);
  72. assert(Layout.FrameAlignment >= Alignment);
  73. assert((Offset % Alignment) == 0);
  74. assert(Size > 0);
  75. uint64_t NextAlignment =
  76. IsLast ? Granularity : std::max(Granularity, Vars[i + 1].Alignment);
  77. uint64_t SizeWithRedzone =
  78. VarAndRedzoneSize(Size, Granularity, NextAlignment);
  79. Vars[i].Offset = Offset;
  80. Offset += SizeWithRedzone;
  81. }
  82. if (Offset % MinHeaderSize) {
  83. Offset += MinHeaderSize - (Offset % MinHeaderSize);
  84. }
  85. Layout.FrameSize = Offset;
  86. assert((Layout.FrameSize % MinHeaderSize) == 0);
  87. return Layout;
  88. }
  89. SmallString<64> ComputeASanStackFrameDescription(
  90. const SmallVectorImpl<ASanStackVariableDescription> &Vars) {
  91. SmallString<2048> StackDescriptionStorage;
  92. raw_svector_ostream StackDescription(StackDescriptionStorage);
  93. StackDescription << Vars.size();
  94. for (const auto &Var : Vars) {
  95. std::string Name = Var.Name;
  96. if (Var.Line) {
  97. Name += ":";
  98. Name += to_string(Var.Line);
  99. }
  100. StackDescription << " " << Var.Offset << " " << Var.Size << " "
  101. << Name.size() << " " << Name;
  102. }
  103. return StackDescription.str();
  104. }
  105. SmallVector<uint8_t, 64>
  106. GetShadowBytes(const SmallVectorImpl<ASanStackVariableDescription> &Vars,
  107. const ASanStackFrameLayout &Layout) {
  108. assert(Vars.size() > 0);
  109. SmallVector<uint8_t, 64> SB;
  110. SB.clear();
  111. const uint64_t Granularity = Layout.Granularity;
  112. SB.resize(Vars[0].Offset / Granularity, kAsanStackLeftRedzoneMagic);
  113. for (const auto &Var : Vars) {
  114. SB.resize(Var.Offset / Granularity, kAsanStackMidRedzoneMagic);
  115. SB.resize(SB.size() + Var.Size / Granularity, 0);
  116. if (Var.Size % Granularity)
  117. SB.push_back(Var.Size % Granularity);
  118. }
  119. SB.resize(Layout.FrameSize / Granularity, kAsanStackRightRedzoneMagic);
  120. return SB;
  121. }
  122. SmallVector<uint8_t, 64> GetShadowBytesAfterScope(
  123. const SmallVectorImpl<ASanStackVariableDescription> &Vars,
  124. const ASanStackFrameLayout &Layout) {
  125. SmallVector<uint8_t, 64> SB = GetShadowBytes(Vars, Layout);
  126. const uint64_t Granularity = Layout.Granularity;
  127. for (const auto &Var : Vars) {
  128. assert(Var.LifetimeSize <= Var.Size);
  129. const uint64_t LifetimeShadowSize =
  130. (Var.LifetimeSize + Granularity - 1) / Granularity;
  131. const uint64_t Offset = Var.Offset / Granularity;
  132. std::fill(SB.begin() + Offset, SB.begin() + Offset + LifetimeShadowSize,
  133. kAsanStackUseAfterScopeMagic);
  134. }
  135. return SB;
  136. }
  137. } // llvm namespace