SafeStackLayout.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //===- SafeStackLayout.cpp - SafeStack frame layout -----------------------===//
  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. #include "SafeStackLayout.h"
  10. #include "SafeStackColoring.h"
  11. #include "llvm/IR/Value.h"
  12. #include "llvm/Support/CommandLine.h"
  13. #include "llvm/Support/Compiler.h"
  14. #include "llvm/Support/Debug.h"
  15. #include "llvm/Support/MathExtras.h"
  16. #include "llvm/Support/raw_ostream.h"
  17. #include <algorithm>
  18. #include <cassert>
  19. using namespace llvm;
  20. using namespace llvm::safestack;
  21. #define DEBUG_TYPE "safestacklayout"
  22. static cl::opt<bool> ClLayout("safe-stack-layout",
  23. cl::desc("enable safe stack layout"), cl::Hidden,
  24. cl::init(true));
  25. LLVM_DUMP_METHOD void StackLayout::print(raw_ostream &OS) {
  26. OS << "Stack regions:\n";
  27. for (unsigned i = 0; i < Regions.size(); ++i) {
  28. OS << " " << i << ": [" << Regions[i].Start << ", " << Regions[i].End
  29. << "), range " << Regions[i].Range << "\n";
  30. }
  31. OS << "Stack objects:\n";
  32. for (auto &IT : ObjectOffsets) {
  33. OS << " at " << IT.getSecond() << ": " << *IT.getFirst() << "\n";
  34. }
  35. }
  36. void StackLayout::addObject(const Value *V, unsigned Size, unsigned Alignment,
  37. const StackColoring::LiveRange &Range) {
  38. StackObjects.push_back({V, Size, Alignment, Range});
  39. ObjectAlignments[V] = Alignment;
  40. MaxAlignment = std::max(MaxAlignment, Alignment);
  41. }
  42. static unsigned AdjustStackOffset(unsigned Offset, unsigned Size,
  43. unsigned Alignment) {
  44. return alignTo(Offset + Size, Alignment) - Size;
  45. }
  46. void StackLayout::layoutObject(StackObject &Obj) {
  47. if (!ClLayout) {
  48. // If layout is disabled, just grab the next aligned address.
  49. // This effectively disables stack coloring as well.
  50. unsigned LastRegionEnd = Regions.empty() ? 0 : Regions.back().End;
  51. unsigned Start = AdjustStackOffset(LastRegionEnd, Obj.Size, Obj.Alignment);
  52. unsigned End = Start + Obj.Size;
  53. Regions.emplace_back(Start, End, Obj.Range);
  54. ObjectOffsets[Obj.Handle] = End;
  55. return;
  56. }
  57. DEBUG(dbgs() << "Layout: size " << Obj.Size << ", align " << Obj.Alignment
  58. << ", range " << Obj.Range << "\n");
  59. assert(Obj.Alignment <= MaxAlignment);
  60. unsigned Start = AdjustStackOffset(0, Obj.Size, Obj.Alignment);
  61. unsigned End = Start + Obj.Size;
  62. DEBUG(dbgs() << " First candidate: " << Start << " .. " << End << "\n");
  63. for (const StackRegion &R : Regions) {
  64. DEBUG(dbgs() << " Examining region: " << R.Start << " .. " << R.End
  65. << ", range " << R.Range << "\n");
  66. assert(End >= R.Start);
  67. if (Start >= R.End) {
  68. DEBUG(dbgs() << " Does not intersect, skip.\n");
  69. continue;
  70. }
  71. if (Obj.Range.Overlaps(R.Range)) {
  72. // Find the next appropriate location.
  73. Start = AdjustStackOffset(R.End, Obj.Size, Obj.Alignment);
  74. End = Start + Obj.Size;
  75. DEBUG(dbgs() << " Overlaps. Next candidate: " << Start << " .. " << End
  76. << "\n");
  77. continue;
  78. }
  79. if (End <= R.End) {
  80. DEBUG(dbgs() << " Reusing region(s).\n");
  81. break;
  82. }
  83. }
  84. unsigned LastRegionEnd = Regions.empty() ? 0 : Regions.back().End;
  85. if (End > LastRegionEnd) {
  86. // Insert a new region at the end. Maybe two.
  87. if (Start > LastRegionEnd) {
  88. DEBUG(dbgs() << " Creating gap region: " << LastRegionEnd << " .. "
  89. << Start << "\n");
  90. Regions.emplace_back(LastRegionEnd, Start, StackColoring::LiveRange());
  91. LastRegionEnd = Start;
  92. }
  93. DEBUG(dbgs() << " Creating new region: " << LastRegionEnd << " .. " << End
  94. << ", range " << Obj.Range << "\n");
  95. Regions.emplace_back(LastRegionEnd, End, Obj.Range);
  96. LastRegionEnd = End;
  97. }
  98. // Split starting and ending regions if necessary.
  99. for (unsigned i = 0; i < Regions.size(); ++i) {
  100. StackRegion &R = Regions[i];
  101. if (Start > R.Start && Start < R.End) {
  102. StackRegion R0 = R;
  103. R.Start = R0.End = Start;
  104. Regions.insert(&R, R0);
  105. continue;
  106. }
  107. if (End > R.Start && End < R.End) {
  108. StackRegion R0 = R;
  109. R0.End = R.Start = End;
  110. Regions.insert(&R, R0);
  111. break;
  112. }
  113. }
  114. // Update live ranges for all affected regions.
  115. for (StackRegion &R : Regions) {
  116. if (Start < R.End && End > R.Start)
  117. R.Range.Join(Obj.Range);
  118. if (End <= R.End)
  119. break;
  120. }
  121. ObjectOffsets[Obj.Handle] = End;
  122. }
  123. void StackLayout::computeLayout() {
  124. // Simple greedy algorithm.
  125. // If this is replaced with something smarter, it must preserve the property
  126. // that the first object is always at the offset 0 in the stack frame (for
  127. // StackProtectorSlot), or handle stack protector in some other way.
  128. // Sort objects by size (largest first) to reduce fragmentation.
  129. if (StackObjects.size() > 2)
  130. std::stable_sort(StackObjects.begin() + 1, StackObjects.end(),
  131. [](const StackObject &a, const StackObject &b) {
  132. return a.Size > b.Size;
  133. });
  134. for (auto &Obj : StackObjects)
  135. layoutObject(Obj);
  136. DEBUG(print(dbgs()));
  137. }