13ca95b02SDimitry Andric //===-- SafeStackLayout.cpp - SafeStack frame layout -----------*- C++ -*--===// 23ca95b02SDimitry Andric // 33ca95b02SDimitry Andric // The LLVM Compiler Infrastructure 43ca95b02SDimitry Andric // 53ca95b02SDimitry Andric // This file is distributed under the University of Illinois Open Source 63ca95b02SDimitry Andric // License. See LICENSE.TXT for details. 73ca95b02SDimitry Andric // 83ca95b02SDimitry Andric //===----------------------------------------------------------------------===// 93ca95b02SDimitry Andric 103ca95b02SDimitry Andric #include "SafeStackLayout.h" 113ca95b02SDimitry Andric 123ca95b02SDimitry Andric #include "llvm/IR/Instructions.h" 133ca95b02SDimitry Andric #include "llvm/Support/Debug.h" 143ca95b02SDimitry Andric 153ca95b02SDimitry Andric using namespace llvm; 163ca95b02SDimitry Andric using namespace llvm::safestack; 173ca95b02SDimitry Andric 183ca95b02SDimitry Andric #define DEBUG_TYPE "safestacklayout" 193ca95b02SDimitry Andric 203ca95b02SDimitry Andric static cl::opt<bool> ClLayout("safe-stack-layout", 213ca95b02SDimitry Andric cl::desc("enable safe stack layout"), cl::Hidden, 223ca95b02SDimitry Andric cl::init(true)); 233ca95b02SDimitry Andric 243ca95b02SDimitry Andric LLVM_DUMP_METHOD void StackLayout::print(raw_ostream &OS) { 253ca95b02SDimitry Andric OS << "Stack regions:\n"; 263ca95b02SDimitry Andric for (unsigned i = 0; i < Regions.size(); ++i) { 273ca95b02SDimitry Andric OS << " " << i << ": [" << Regions[i].Start << ", " << Regions[i].End 283ca95b02SDimitry Andric << "), range " << Regions[i].Range << "\n"; 293ca95b02SDimitry Andric } 303ca95b02SDimitry Andric OS << "Stack objects:\n"; 313ca95b02SDimitry Andric for (auto &IT : ObjectOffsets) { 323ca95b02SDimitry Andric OS << " at " << IT.getSecond() << ": " << *IT.getFirst() << "\n"; 333ca95b02SDimitry Andric } 343ca95b02SDimitry Andric } 353ca95b02SDimitry Andric 363ca95b02SDimitry Andric void StackLayout::addObject(const Value *V, unsigned Size, unsigned Alignment, 373ca95b02SDimitry Andric const StackColoring::LiveRange &Range) { 383ca95b02SDimitry Andric StackObjects.push_back({V, Size, Alignment, Range}); 393ca95b02SDimitry Andric MaxAlignment = std::max(MaxAlignment, Alignment); 403ca95b02SDimitry Andric } 413ca95b02SDimitry Andric 423ca95b02SDimitry Andric static unsigned AdjustStackOffset(unsigned Offset, unsigned Size, 433ca95b02SDimitry Andric unsigned Alignment) { 443ca95b02SDimitry Andric return alignTo(Offset + Size, Alignment) - Size; 453ca95b02SDimitry Andric } 463ca95b02SDimitry Andric 473ca95b02SDimitry Andric void StackLayout::layoutObject(StackObject &Obj) { 483ca95b02SDimitry Andric if (!ClLayout) { 493ca95b02SDimitry Andric // If layout is disabled, just grab the next aligned address. 503ca95b02SDimitry Andric // This effectively disables stack coloring as well. 513ca95b02SDimitry Andric unsigned LastRegionEnd = Regions.empty() ? 0 : Regions.back().End; 523ca95b02SDimitry Andric unsigned Start = AdjustStackOffset(LastRegionEnd, Obj.Size, Obj.Alignment); 533ca95b02SDimitry Andric unsigned End = Start + Obj.Size; 543ca95b02SDimitry Andric Regions.emplace_back(Start, End, Obj.Range); 553ca95b02SDimitry Andric ObjectOffsets[Obj.Handle] = End; 563ca95b02SDimitry Andric return; 573ca95b02SDimitry Andric } 583ca95b02SDimitry Andric 593ca95b02SDimitry Andric DEBUG(dbgs() << "Layout: size " << Obj.Size << ", align " << Obj.Alignment 603ca95b02SDimitry Andric << ", range " << Obj.Range << "\n"); 613ca95b02SDimitry Andric assert(Obj.Alignment <= MaxAlignment); 623ca95b02SDimitry Andric unsigned Start = AdjustStackOffset(0, Obj.Size, Obj.Alignment); 633ca95b02SDimitry Andric unsigned End = Start + Obj.Size; 643ca95b02SDimitry Andric DEBUG(dbgs() << " First candidate: " << Start << " .. " << End << "\n"); 653ca95b02SDimitry Andric for (const StackRegion &R : Regions) { 663ca95b02SDimitry Andric DEBUG(dbgs() << " Examining region: " << R.Start << " .. " << R.End 673ca95b02SDimitry Andric << ", range " << R.Range << "\n"); 683ca95b02SDimitry Andric assert(End >= R.Start); 693ca95b02SDimitry Andric if (Start >= R.End) { 703ca95b02SDimitry Andric DEBUG(dbgs() << " Does not intersect, skip.\n"); 713ca95b02SDimitry Andric continue; 723ca95b02SDimitry Andric } 733ca95b02SDimitry Andric if (Obj.Range.Overlaps(R.Range)) { 743ca95b02SDimitry Andric // Find the next appropriate location. 753ca95b02SDimitry Andric Start = AdjustStackOffset(R.End, Obj.Size, Obj.Alignment); 763ca95b02SDimitry Andric End = Start + Obj.Size; 773ca95b02SDimitry Andric DEBUG(dbgs() << " Overlaps. Next candidate: " << Start << " .. " << End 783ca95b02SDimitry Andric << "\n"); 793ca95b02SDimitry Andric continue; 803ca95b02SDimitry Andric } 813ca95b02SDimitry Andric if (End <= R.End) { 823ca95b02SDimitry Andric DEBUG(dbgs() << " Reusing region(s).\n"); 833ca95b02SDimitry Andric break; 843ca95b02SDimitry Andric } 853ca95b02SDimitry Andric } 863ca95b02SDimitry Andric 873ca95b02SDimitry Andric unsigned LastRegionEnd = Regions.empty() ? 0 : Regions.back().End; 883ca95b02SDimitry Andric if (End > LastRegionEnd) { 893ca95b02SDimitry Andric // Insert a new region at the end. Maybe two. 903ca95b02SDimitry Andric if (Start > LastRegionEnd) { 913ca95b02SDimitry Andric DEBUG(dbgs() << " Creating gap region: " << LastRegionEnd << " .. " 923ca95b02SDimitry Andric << Start << "\n"); 933ca95b02SDimitry Andric Regions.emplace_back(LastRegionEnd, Start, StackColoring::LiveRange()); 943ca95b02SDimitry Andric LastRegionEnd = Start; 953ca95b02SDimitry Andric } 963ca95b02SDimitry Andric DEBUG(dbgs() << " Creating new region: " << LastRegionEnd << " .. " << End 973ca95b02SDimitry Andric << ", range " << Obj.Range << "\n"); 983ca95b02SDimitry Andric Regions.emplace_back(LastRegionEnd, End, Obj.Range); 993ca95b02SDimitry Andric LastRegionEnd = End; 1003ca95b02SDimitry Andric } 1013ca95b02SDimitry Andric 1023ca95b02SDimitry Andric // Split starting and ending regions if necessary. 1033ca95b02SDimitry Andric for (StackRegion &R : Regions) { 1043ca95b02SDimitry Andric if (Start > R.Start && Start < R.End) { 1053ca95b02SDimitry Andric StackRegion R0 = R; 1063ca95b02SDimitry Andric R.Start = R0.End = Start; 1073ca95b02SDimitry Andric Regions.insert(&R, R0); 1083ca95b02SDimitry Andric continue; 1093ca95b02SDimitry Andric } 1103ca95b02SDimitry Andric if (End > R.Start && End < R.End) { 1113ca95b02SDimitry Andric StackRegion R0 = R; 1123ca95b02SDimitry Andric R0.End = R.Start = End; 1133ca95b02SDimitry Andric Regions.insert(&R, R0); 1143ca95b02SDimitry Andric break; 1153ca95b02SDimitry Andric } 1163ca95b02SDimitry Andric } 1173ca95b02SDimitry Andric 1183ca95b02SDimitry Andric // Update live ranges for all affected regions. 1193ca95b02SDimitry Andric for (StackRegion &R : Regions) { 1203ca95b02SDimitry Andric if (Start < R.End && End > R.Start) 1213ca95b02SDimitry Andric R.Range.Join(Obj.Range); 1223ca95b02SDimitry Andric if (End <= R.End) 1233ca95b02SDimitry Andric break; 1243ca95b02SDimitry Andric } 1253ca95b02SDimitry Andric 1263ca95b02SDimitry Andric ObjectOffsets[Obj.Handle] = End; 1273ca95b02SDimitry Andric } 1283ca95b02SDimitry Andric 1293ca95b02SDimitry Andric void StackLayout::computeLayout() { 1303ca95b02SDimitry Andric // Simple greedy algorithm. 1313ca95b02SDimitry Andric // If this is replaced with something smarter, it must preserve the property 1323ca95b02SDimitry Andric // that the first object is always at the offset 0 in the stack frame (for 1333ca95b02SDimitry Andric // StackProtectorSlot), or handle stack protector in some other way. 1343ca95b02SDimitry Andric for (auto &Obj : StackObjects) 1353ca95b02SDimitry Andric layoutObject(Obj); 1363ca95b02SDimitry Andric 1373ca95b02SDimitry Andric DEBUG(print(dbgs())); 1383ca95b02SDimitry Andric } 139