SafeStackLayout.cpp 5.2 KB

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