SafeStackLayout.cpp 5.3 KB

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