ASanStackFrameLayout.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //===-- ASanStackFrameLayout.cpp - helper for AddressSanitizer ------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // Definition of ComputeASanStackFrameLayout (see ASanStackFrameLayout.h).
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Transforms/Utils/ASanStackFrameLayout.h"
  14. #include "llvm/ADT/SmallString.h"
  15. #include "llvm/Support/raw_ostream.h"
  16. #include "llvm/Support/MathExtras.h"
  17. #include <algorithm>
  18. // //
  19. ///////////////////////////////////////////////////////////////////////////////
  20. namespace llvm {
  21. // We sort the stack variables by alignment (largest first) to minimize
  22. // unnecessary large gaps due to alignment.
  23. // It is tempting to also sort variables by size so that larger variables
  24. // have larger redzones at both ends. But reordering will make report analysis
  25. // harder, especially when temporary unnamed variables are present.
  26. // So, until we can provide more information (type, line number, etc)
  27. // for the stack variables we avoid reordering them too much.
  28. static inline bool CompareVars(const ASanStackVariableDescription &a,
  29. const ASanStackVariableDescription &b) {
  30. return a.Alignment > b.Alignment;
  31. }
  32. // We also force minimal alignment for all vars to kMinAlignment so that vars
  33. // with e.g. alignment 1 and alignment 16 do not get reordered by CompareVars.
  34. static const size_t kMinAlignment = 16;
  35. // The larger the variable Size the larger is the redzone.
  36. // The resulting frame size is a multiple of Alignment.
  37. static size_t VarAndRedzoneSize(size_t Size, size_t Alignment) {
  38. size_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 RoundUpToAlignment(Res, Alignment);
  46. }
  47. void
  48. ComputeASanStackFrameLayout(SmallVectorImpl<ASanStackVariableDescription> &Vars,
  49. size_t Granularity, size_t MinHeaderSize,
  50. ASanStackFrameLayout *Layout) {
  51. assert(Granularity >= 8 && Granularity <= 64 &&
  52. (Granularity & (Granularity - 1)) == 0);
  53. assert(MinHeaderSize >= 16 && (MinHeaderSize & (MinHeaderSize - 1)) == 0 &&
  54. MinHeaderSize >= Granularity);
  55. 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. std::stable_sort(Vars.begin(), Vars.end(), CompareVars);
  60. SmallString<2048> StackDescriptionStorage;
  61. raw_svector_ostream StackDescription(StackDescriptionStorage);
  62. StackDescription << NumVars;
  63. Layout->FrameAlignment = std::max(Granularity, Vars[0].Alignment);
  64. SmallVector<uint8_t, 64> &SB(Layout->ShadowBytes);
  65. SB.clear();
  66. size_t Offset = std::max(std::max(MinHeaderSize, Granularity),
  67. Vars[0].Alignment);
  68. assert((Offset % Granularity) == 0);
  69. SB.insert(SB.end(), Offset / Granularity, kAsanStackLeftRedzoneMagic);
  70. for (size_t i = 0; i < NumVars; i++) {
  71. bool IsLast = i == NumVars - 1;
  72. size_t Alignment = std::max(Granularity, Vars[i].Alignment);
  73. (void)Alignment; // Used only in asserts.
  74. size_t Size = Vars[i].Size;
  75. const char *Name = Vars[i].Name;
  76. assert((Alignment & (Alignment - 1)) == 0);
  77. assert(Layout->FrameAlignment >= Alignment);
  78. assert((Offset % Alignment) == 0);
  79. assert(Size > 0);
  80. StackDescription << " " << Offset << " " << Size << " " << strlen(Name)
  81. << " " << Name;
  82. size_t NextAlignment = IsLast ? Granularity
  83. : std::max(Granularity, Vars[i + 1].Alignment);
  84. size_t SizeWithRedzone = VarAndRedzoneSize(Vars[i].Size, NextAlignment);
  85. SB.insert(SB.end(), Size / Granularity, 0);
  86. if (Size % Granularity)
  87. SB.insert(SB.end(), Size % Granularity);
  88. SB.insert(SB.end(), (SizeWithRedzone - Size) / Granularity,
  89. IsLast ? kAsanStackRightRedzoneMagic
  90. : kAsanStackMidRedzoneMagic);
  91. Vars[i].Offset = Offset;
  92. Offset += SizeWithRedzone;
  93. }
  94. if (Offset % MinHeaderSize) {
  95. size_t ExtraRedzone = MinHeaderSize - (Offset % MinHeaderSize);
  96. SB.insert(SB.end(), ExtraRedzone / Granularity,
  97. kAsanStackRightRedzoneMagic);
  98. Offset += ExtraRedzone;
  99. }
  100. Layout->DescriptionString = StackDescription.str();
  101. Layout->FrameSize = Offset;
  102. assert((Layout->FrameSize % MinHeaderSize) == 0);
  103. assert(Layout->FrameSize / Granularity == Layout->ShadowBytes.size());
  104. }
  105. } // llvm namespace