ASanStackFrameLayout.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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/MathExtras.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 size_t kMinAlignment = 16;
  33. // The larger the variable Size the larger is the redzone.
  34. // The resulting frame size is a multiple of Alignment.
  35. static size_t VarAndRedzoneSize(size_t Size, size_t Alignment) {
  36. size_t Res = 0;
  37. if (Size <= 4) Res = 16;
  38. else if (Size <= 16) Res = 32;
  39. else if (Size <= 128) Res = Size + 32;
  40. else if (Size <= 512) Res = Size + 64;
  41. else if (Size <= 4096) Res = Size + 128;
  42. else Res = Size + 256;
  43. return alignTo(Res, Alignment);
  44. }
  45. void
  46. ComputeASanStackFrameLayout(SmallVectorImpl<ASanStackVariableDescription> &Vars,
  47. size_t Granularity, size_t MinHeaderSize,
  48. ASanStackFrameLayout *Layout) {
  49. assert(Granularity >= 8 && Granularity <= 64 &&
  50. (Granularity & (Granularity - 1)) == 0);
  51. assert(MinHeaderSize >= 16 && (MinHeaderSize & (MinHeaderSize - 1)) == 0 &&
  52. MinHeaderSize >= Granularity);
  53. size_t NumVars = Vars.size();
  54. assert(NumVars > 0);
  55. for (size_t i = 0; i < NumVars; i++)
  56. Vars[i].Alignment = std::max(Vars[i].Alignment, kMinAlignment);
  57. std::stable_sort(Vars.begin(), Vars.end(), CompareVars);
  58. SmallString<2048> StackDescriptionStorage;
  59. raw_svector_ostream StackDescription(StackDescriptionStorage);
  60. StackDescription << NumVars;
  61. Layout->FrameAlignment = std::max(Granularity, Vars[0].Alignment);
  62. SmallVector<uint8_t, 64> &SB(Layout->ShadowBytes);
  63. SB.clear();
  64. size_t Offset = std::max(std::max(MinHeaderSize, Granularity),
  65. Vars[0].Alignment);
  66. assert((Offset % Granularity) == 0);
  67. SB.insert(SB.end(), Offset / Granularity, kAsanStackLeftRedzoneMagic);
  68. for (size_t i = 0; i < NumVars; i++) {
  69. bool IsLast = i == NumVars - 1;
  70. size_t Alignment = std::max(Granularity, Vars[i].Alignment);
  71. (void)Alignment; // Used only in asserts.
  72. size_t Size = Vars[i].Size;
  73. const char *Name = Vars[i].Name;
  74. assert((Alignment & (Alignment - 1)) == 0);
  75. assert(Layout->FrameAlignment >= Alignment);
  76. assert((Offset % Alignment) == 0);
  77. assert(Size > 0);
  78. StackDescription << " " << Offset << " " << Size << " " << strlen(Name)
  79. << " " << Name;
  80. size_t NextAlignment = IsLast ? Granularity
  81. : std::max(Granularity, Vars[i + 1].Alignment);
  82. size_t SizeWithRedzone = VarAndRedzoneSize(Vars[i].Size, NextAlignment);
  83. SB.insert(SB.end(), Size / Granularity, 0);
  84. if (Size % Granularity)
  85. SB.insert(SB.end(), Size % Granularity);
  86. SB.insert(SB.end(), (SizeWithRedzone - Size) / Granularity,
  87. IsLast ? kAsanStackRightRedzoneMagic
  88. : kAsanStackMidRedzoneMagic);
  89. Vars[i].Offset = Offset;
  90. Offset += SizeWithRedzone;
  91. }
  92. if (Offset % MinHeaderSize) {
  93. size_t ExtraRedzone = MinHeaderSize - (Offset % MinHeaderSize);
  94. SB.insert(SB.end(), ExtraRedzone / Granularity,
  95. kAsanStackRightRedzoneMagic);
  96. Offset += ExtraRedzone;
  97. }
  98. Layout->DescriptionString = StackDescription.str();
  99. Layout->FrameSize = Offset;
  100. assert((Layout->FrameSize % MinHeaderSize) == 0);
  101. assert(Layout->FrameSize / Granularity == Layout->ShadowBytes.size());
  102. }
  103. } // llvm namespace