xref: /src/contrib/llvm-project/llvm/lib/CodeGen/SafeStackLayout.cpp (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
1044eb2f6SDimitry Andric //===- SafeStackLayout.cpp - SafeStack frame layout -----------------------===//
201095a5dSDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
601095a5dSDimitry Andric //
701095a5dSDimitry Andric //===----------------------------------------------------------------------===//
801095a5dSDimitry Andric 
901095a5dSDimitry Andric #include "SafeStackLayout.h"
10044eb2f6SDimitry Andric #include "llvm/IR/Value.h"
11044eb2f6SDimitry Andric #include "llvm/Support/CommandLine.h"
12044eb2f6SDimitry Andric #include "llvm/Support/Compiler.h"
1301095a5dSDimitry Andric #include "llvm/Support/Debug.h"
14044eb2f6SDimitry Andric #include "llvm/Support/raw_ostream.h"
15044eb2f6SDimitry Andric #include <algorithm>
16044eb2f6SDimitry Andric #include <cassert>
1701095a5dSDimitry Andric 
1801095a5dSDimitry Andric using namespace llvm;
1901095a5dSDimitry Andric using namespace llvm::safestack;
2001095a5dSDimitry Andric 
2101095a5dSDimitry Andric #define DEBUG_TYPE "safestacklayout"
2201095a5dSDimitry Andric 
2301095a5dSDimitry Andric static cl::opt<bool> ClLayout("safe-stack-layout",
2401095a5dSDimitry Andric                               cl::desc("enable safe stack layout"), cl::Hidden,
2501095a5dSDimitry Andric                               cl::init(true));
2601095a5dSDimitry Andric 
print(raw_ostream & OS)2701095a5dSDimitry Andric LLVM_DUMP_METHOD void StackLayout::print(raw_ostream &OS) {
2801095a5dSDimitry Andric   OS << "Stack regions:\n";
2901095a5dSDimitry Andric   for (unsigned i = 0; i < Regions.size(); ++i) {
3001095a5dSDimitry Andric     OS << "  " << i << ": [" << Regions[i].Start << ", " << Regions[i].End
3101095a5dSDimitry Andric        << "), range " << Regions[i].Range << "\n";
3201095a5dSDimitry Andric   }
3301095a5dSDimitry Andric   OS << "Stack objects:\n";
3401095a5dSDimitry Andric   for (auto &IT : ObjectOffsets) {
3501095a5dSDimitry Andric     OS << "  at " << IT.getSecond() << ": " << *IT.getFirst() << "\n";
3601095a5dSDimitry Andric   }
3701095a5dSDimitry Andric }
3801095a5dSDimitry Andric 
addObject(const Value * V,unsigned Size,Align Alignment,const StackLifetime::LiveRange & Range)3977fc4c14SDimitry Andric void StackLayout::addObject(const Value *V, unsigned Size, Align Alignment,
40cfca06d7SDimitry Andric                             const StackLifetime::LiveRange &Range) {
4101095a5dSDimitry Andric   StackObjects.push_back({V, Size, Alignment, Range});
42eb11fae6SDimitry Andric   ObjectAlignments[V] = Alignment;
4301095a5dSDimitry Andric   MaxAlignment = std::max(MaxAlignment, Alignment);
4401095a5dSDimitry Andric }
4501095a5dSDimitry Andric 
AdjustStackOffset(unsigned Offset,unsigned Size,Align Alignment)4601095a5dSDimitry Andric static unsigned AdjustStackOffset(unsigned Offset, unsigned Size,
4777fc4c14SDimitry Andric                                   Align Alignment) {
4801095a5dSDimitry Andric   return alignTo(Offset + Size, Alignment) - Size;
4901095a5dSDimitry Andric }
5001095a5dSDimitry Andric 
layoutObject(StackObject & Obj)5101095a5dSDimitry Andric void StackLayout::layoutObject(StackObject &Obj) {
5201095a5dSDimitry Andric   if (!ClLayout) {
5301095a5dSDimitry Andric     // If layout is disabled, just grab the next aligned address.
5401095a5dSDimitry Andric     // This effectively disables stack coloring as well.
5501095a5dSDimitry Andric     unsigned LastRegionEnd = Regions.empty() ? 0 : Regions.back().End;
5601095a5dSDimitry Andric     unsigned Start = AdjustStackOffset(LastRegionEnd, Obj.Size, Obj.Alignment);
5701095a5dSDimitry Andric     unsigned End = Start + Obj.Size;
5801095a5dSDimitry Andric     Regions.emplace_back(Start, End, Obj.Range);
5901095a5dSDimitry Andric     ObjectOffsets[Obj.Handle] = End;
6001095a5dSDimitry Andric     return;
6101095a5dSDimitry Andric   }
6201095a5dSDimitry Andric 
63eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << "Layout: size " << Obj.Size << ", align "
6477fc4c14SDimitry Andric                     << Obj.Alignment.value() << ", range " << Obj.Range
6577fc4c14SDimitry Andric                     << "\n");
6601095a5dSDimitry Andric   assert(Obj.Alignment <= MaxAlignment);
6701095a5dSDimitry Andric   unsigned Start = AdjustStackOffset(0, Obj.Size, Obj.Alignment);
6801095a5dSDimitry Andric   unsigned End = Start + Obj.Size;
69eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << "  First candidate: " << Start << " .. " << End << "\n");
7001095a5dSDimitry Andric   for (const StackRegion &R : Regions) {
71eb11fae6SDimitry Andric     LLVM_DEBUG(dbgs() << "  Examining region: " << R.Start << " .. " << R.End
7201095a5dSDimitry Andric                       << ", range " << R.Range << "\n");
7301095a5dSDimitry Andric     assert(End >= R.Start);
7401095a5dSDimitry Andric     if (Start >= R.End) {
75eb11fae6SDimitry Andric       LLVM_DEBUG(dbgs() << "  Does not intersect, skip.\n");
7601095a5dSDimitry Andric       continue;
7701095a5dSDimitry Andric     }
78cfca06d7SDimitry Andric     if (Obj.Range.overlaps(R.Range)) {
7901095a5dSDimitry Andric       // Find the next appropriate location.
8001095a5dSDimitry Andric       Start = AdjustStackOffset(R.End, Obj.Size, Obj.Alignment);
8101095a5dSDimitry Andric       End = Start + Obj.Size;
82eb11fae6SDimitry Andric       LLVM_DEBUG(dbgs() << "  Overlaps. Next candidate: " << Start << " .. "
83eb11fae6SDimitry Andric                         << End << "\n");
8401095a5dSDimitry Andric       continue;
8501095a5dSDimitry Andric     }
8601095a5dSDimitry Andric     if (End <= R.End) {
87eb11fae6SDimitry Andric       LLVM_DEBUG(dbgs() << "  Reusing region(s).\n");
8801095a5dSDimitry Andric       break;
8901095a5dSDimitry Andric     }
9001095a5dSDimitry Andric   }
9101095a5dSDimitry Andric 
9201095a5dSDimitry Andric   unsigned LastRegionEnd = Regions.empty() ? 0 : Regions.back().End;
9301095a5dSDimitry Andric   if (End > LastRegionEnd) {
9401095a5dSDimitry Andric     // Insert a new region at the end. Maybe two.
9501095a5dSDimitry Andric     if (Start > LastRegionEnd) {
96eb11fae6SDimitry Andric       LLVM_DEBUG(dbgs() << "  Creating gap region: " << LastRegionEnd << " .. "
9701095a5dSDimitry Andric                         << Start << "\n");
98cfca06d7SDimitry Andric       Regions.emplace_back(LastRegionEnd, Start, StackLifetime::LiveRange(0));
9901095a5dSDimitry Andric       LastRegionEnd = Start;
10001095a5dSDimitry Andric     }
101eb11fae6SDimitry Andric     LLVM_DEBUG(dbgs() << "  Creating new region: " << LastRegionEnd << " .. "
102eb11fae6SDimitry Andric                       << End << ", range " << Obj.Range << "\n");
10301095a5dSDimitry Andric     Regions.emplace_back(LastRegionEnd, End, Obj.Range);
10401095a5dSDimitry Andric     LastRegionEnd = End;
10501095a5dSDimitry Andric   }
10601095a5dSDimitry Andric 
10701095a5dSDimitry Andric   // Split starting and ending regions if necessary.
108a7fe922bSDimitry Andric   for (unsigned i = 0; i < Regions.size(); ++i) {
109a7fe922bSDimitry Andric     StackRegion &R = Regions[i];
11001095a5dSDimitry Andric     if (Start > R.Start && Start < R.End) {
11101095a5dSDimitry Andric       StackRegion R0 = R;
11201095a5dSDimitry Andric       R.Start = R0.End = Start;
11301095a5dSDimitry Andric       Regions.insert(&R, R0);
11401095a5dSDimitry Andric       continue;
11501095a5dSDimitry Andric     }
11601095a5dSDimitry Andric     if (End > R.Start && End < R.End) {
11701095a5dSDimitry Andric       StackRegion R0 = R;
11801095a5dSDimitry Andric       R0.End = R.Start = End;
11901095a5dSDimitry Andric       Regions.insert(&R, R0);
12001095a5dSDimitry Andric       break;
12101095a5dSDimitry Andric     }
12201095a5dSDimitry Andric   }
12301095a5dSDimitry Andric 
12401095a5dSDimitry Andric   // Update live ranges for all affected regions.
12501095a5dSDimitry Andric   for (StackRegion &R : Regions) {
12601095a5dSDimitry Andric     if (Start < R.End && End > R.Start)
127cfca06d7SDimitry Andric       R.Range.join(Obj.Range);
12801095a5dSDimitry Andric     if (End <= R.End)
12901095a5dSDimitry Andric       break;
13001095a5dSDimitry Andric   }
13101095a5dSDimitry Andric 
13201095a5dSDimitry Andric   ObjectOffsets[Obj.Handle] = End;
13301095a5dSDimitry Andric }
13401095a5dSDimitry Andric 
computeLayout()13501095a5dSDimitry Andric void StackLayout::computeLayout() {
13601095a5dSDimitry Andric   // Simple greedy algorithm.
13701095a5dSDimitry Andric   // If this is replaced with something smarter, it must preserve the property
13801095a5dSDimitry Andric   // that the first object is always at the offset 0 in the stack frame (for
13901095a5dSDimitry Andric   // StackProtectorSlot), or handle stack protector in some other way.
140b915e9e0SDimitry Andric 
141b915e9e0SDimitry Andric   // Sort objects by size (largest first) to reduce fragmentation.
142b915e9e0SDimitry Andric   if (StackObjects.size() > 2)
143b60736ecSDimitry Andric     llvm::stable_sort(drop_begin(StackObjects),
144b915e9e0SDimitry Andric                       [](const StackObject &a, const StackObject &b) {
145b915e9e0SDimitry Andric                         return a.Size > b.Size;
146b915e9e0SDimitry Andric                       });
147b915e9e0SDimitry Andric 
14801095a5dSDimitry Andric   for (auto &Obj : StackObjects)
14901095a5dSDimitry Andric     layoutObject(Obj);
15001095a5dSDimitry Andric 
151eb11fae6SDimitry Andric   LLVM_DEBUG(print(dbgs()));
15201095a5dSDimitry Andric }
153