ASanStackFrameLayout.cpp 5.7 KB

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