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