10b57cec5SDimitry Andric //===- SafeStackLayout.cpp - SafeStack frame layout -----------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "SafeStackLayout.h"
100b57cec5SDimitry Andric #include "llvm/IR/Value.h"
110b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
120b57cec5SDimitry Andric #include "llvm/Support/Compiler.h"
130b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
140b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
150b57cec5SDimitry Andric #include <algorithm>
160b57cec5SDimitry Andric #include <cassert>
170b57cec5SDimitry Andric
180b57cec5SDimitry Andric using namespace llvm;
190b57cec5SDimitry Andric using namespace llvm::safestack;
200b57cec5SDimitry Andric
210b57cec5SDimitry Andric #define DEBUG_TYPE "safestacklayout"
220b57cec5SDimitry Andric
230b57cec5SDimitry Andric static cl::opt<bool> ClLayout("safe-stack-layout",
240b57cec5SDimitry Andric cl::desc("enable safe stack layout"), cl::Hidden,
250b57cec5SDimitry Andric cl::init(true));
260b57cec5SDimitry Andric
print(raw_ostream & OS)270b57cec5SDimitry Andric LLVM_DUMP_METHOD void StackLayout::print(raw_ostream &OS) {
280b57cec5SDimitry Andric OS << "Stack regions:\n";
290b57cec5SDimitry Andric for (unsigned i = 0; i < Regions.size(); ++i) {
300b57cec5SDimitry Andric OS << " " << i << ": [" << Regions[i].Start << ", " << Regions[i].End
310b57cec5SDimitry Andric << "), range " << Regions[i].Range << "\n";
320b57cec5SDimitry Andric }
330b57cec5SDimitry Andric OS << "Stack objects:\n";
340b57cec5SDimitry Andric for (auto &IT : ObjectOffsets) {
350b57cec5SDimitry Andric OS << " at " << IT.getSecond() << ": " << *IT.getFirst() << "\n";
360b57cec5SDimitry Andric }
370b57cec5SDimitry Andric }
380b57cec5SDimitry Andric
addObject(const Value * V,unsigned Size,Align Alignment,const StackLifetime::LiveRange & Range)39*0eae32dcSDimitry Andric void StackLayout::addObject(const Value *V, unsigned Size, Align Alignment,
405ffd83dbSDimitry Andric const StackLifetime::LiveRange &Range) {
410b57cec5SDimitry Andric StackObjects.push_back({V, Size, Alignment, Range});
420b57cec5SDimitry Andric ObjectAlignments[V] = Alignment;
430b57cec5SDimitry Andric MaxAlignment = std::max(MaxAlignment, Alignment);
440b57cec5SDimitry Andric }
450b57cec5SDimitry Andric
AdjustStackOffset(unsigned Offset,unsigned Size,Align Alignment)460b57cec5SDimitry Andric static unsigned AdjustStackOffset(unsigned Offset, unsigned Size,
47*0eae32dcSDimitry Andric Align Alignment) {
480b57cec5SDimitry Andric return alignTo(Offset + Size, Alignment) - Size;
490b57cec5SDimitry Andric }
500b57cec5SDimitry Andric
layoutObject(StackObject & Obj)510b57cec5SDimitry Andric void StackLayout::layoutObject(StackObject &Obj) {
520b57cec5SDimitry Andric if (!ClLayout) {
530b57cec5SDimitry Andric // If layout is disabled, just grab the next aligned address.
540b57cec5SDimitry Andric // This effectively disables stack coloring as well.
550b57cec5SDimitry Andric unsigned LastRegionEnd = Regions.empty() ? 0 : Regions.back().End;
560b57cec5SDimitry Andric unsigned Start = AdjustStackOffset(LastRegionEnd, Obj.Size, Obj.Alignment);
570b57cec5SDimitry Andric unsigned End = Start + Obj.Size;
580b57cec5SDimitry Andric Regions.emplace_back(Start, End, Obj.Range);
590b57cec5SDimitry Andric ObjectOffsets[Obj.Handle] = End;
600b57cec5SDimitry Andric return;
610b57cec5SDimitry Andric }
620b57cec5SDimitry Andric
630b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Layout: size " << Obj.Size << ", align "
64*0eae32dcSDimitry Andric << Obj.Alignment.value() << ", range " << Obj.Range
65*0eae32dcSDimitry Andric << "\n");
660b57cec5SDimitry Andric assert(Obj.Alignment <= MaxAlignment);
670b57cec5SDimitry Andric unsigned Start = AdjustStackOffset(0, Obj.Size, Obj.Alignment);
680b57cec5SDimitry Andric unsigned End = Start + Obj.Size;
690b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " First candidate: " << Start << " .. " << End << "\n");
700b57cec5SDimitry Andric for (const StackRegion &R : Regions) {
710b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Examining region: " << R.Start << " .. " << R.End
720b57cec5SDimitry Andric << ", range " << R.Range << "\n");
730b57cec5SDimitry Andric assert(End >= R.Start);
740b57cec5SDimitry Andric if (Start >= R.End) {
750b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Does not intersect, skip.\n");
760b57cec5SDimitry Andric continue;
770b57cec5SDimitry Andric }
785ffd83dbSDimitry Andric if (Obj.Range.overlaps(R.Range)) {
790b57cec5SDimitry Andric // Find the next appropriate location.
800b57cec5SDimitry Andric Start = AdjustStackOffset(R.End, Obj.Size, Obj.Alignment);
810b57cec5SDimitry Andric End = Start + Obj.Size;
820b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Overlaps. Next candidate: " << Start << " .. "
830b57cec5SDimitry Andric << End << "\n");
840b57cec5SDimitry Andric continue;
850b57cec5SDimitry Andric }
860b57cec5SDimitry Andric if (End <= R.End) {
870b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Reusing region(s).\n");
880b57cec5SDimitry Andric break;
890b57cec5SDimitry Andric }
900b57cec5SDimitry Andric }
910b57cec5SDimitry Andric
920b57cec5SDimitry Andric unsigned LastRegionEnd = Regions.empty() ? 0 : Regions.back().End;
930b57cec5SDimitry Andric if (End > LastRegionEnd) {
940b57cec5SDimitry Andric // Insert a new region at the end. Maybe two.
950b57cec5SDimitry Andric if (Start > LastRegionEnd) {
960b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Creating gap region: " << LastRegionEnd << " .. "
970b57cec5SDimitry Andric << Start << "\n");
985ffd83dbSDimitry Andric Regions.emplace_back(LastRegionEnd, Start, StackLifetime::LiveRange(0));
990b57cec5SDimitry Andric LastRegionEnd = Start;
1000b57cec5SDimitry Andric }
1010b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Creating new region: " << LastRegionEnd << " .. "
1020b57cec5SDimitry Andric << End << ", range " << Obj.Range << "\n");
1030b57cec5SDimitry Andric Regions.emplace_back(LastRegionEnd, End, Obj.Range);
1040b57cec5SDimitry Andric LastRegionEnd = End;
1050b57cec5SDimitry Andric }
1060b57cec5SDimitry Andric
1070b57cec5SDimitry Andric // Split starting and ending regions if necessary.
1080b57cec5SDimitry Andric for (unsigned i = 0; i < Regions.size(); ++i) {
1090b57cec5SDimitry Andric StackRegion &R = Regions[i];
1100b57cec5SDimitry Andric if (Start > R.Start && Start < R.End) {
1110b57cec5SDimitry Andric StackRegion R0 = R;
1120b57cec5SDimitry Andric R.Start = R0.End = Start;
1130b57cec5SDimitry Andric Regions.insert(&R, R0);
1140b57cec5SDimitry Andric continue;
1150b57cec5SDimitry Andric }
1160b57cec5SDimitry Andric if (End > R.Start && End < R.End) {
1170b57cec5SDimitry Andric StackRegion R0 = R;
1180b57cec5SDimitry Andric R0.End = R.Start = End;
1190b57cec5SDimitry Andric Regions.insert(&R, R0);
1200b57cec5SDimitry Andric break;
1210b57cec5SDimitry Andric }
1220b57cec5SDimitry Andric }
1230b57cec5SDimitry Andric
1240b57cec5SDimitry Andric // Update live ranges for all affected regions.
1250b57cec5SDimitry Andric for (StackRegion &R : Regions) {
1260b57cec5SDimitry Andric if (Start < R.End && End > R.Start)
1275ffd83dbSDimitry Andric R.Range.join(Obj.Range);
1280b57cec5SDimitry Andric if (End <= R.End)
1290b57cec5SDimitry Andric break;
1300b57cec5SDimitry Andric }
1310b57cec5SDimitry Andric
1320b57cec5SDimitry Andric ObjectOffsets[Obj.Handle] = End;
1330b57cec5SDimitry Andric }
1340b57cec5SDimitry Andric
computeLayout()1350b57cec5SDimitry Andric void StackLayout::computeLayout() {
1360b57cec5SDimitry Andric // Simple greedy algorithm.
1370b57cec5SDimitry Andric // If this is replaced with something smarter, it must preserve the property
1380b57cec5SDimitry Andric // that the first object is always at the offset 0 in the stack frame (for
1390b57cec5SDimitry Andric // StackProtectorSlot), or handle stack protector in some other way.
1400b57cec5SDimitry Andric
1410b57cec5SDimitry Andric // Sort objects by size (largest first) to reduce fragmentation.
1420b57cec5SDimitry Andric if (StackObjects.size() > 2)
143e8d8bef9SDimitry Andric llvm::stable_sort(drop_begin(StackObjects),
1440b57cec5SDimitry Andric [](const StackObject &a, const StackObject &b) {
1450b57cec5SDimitry Andric return a.Size > b.Size;
1460b57cec5SDimitry Andric });
1470b57cec5SDimitry Andric
1480b57cec5SDimitry Andric for (auto &Obj : StackObjects)
1490b57cec5SDimitry Andric layoutObject(Obj);
1500b57cec5SDimitry Andric
1510b57cec5SDimitry Andric LLVM_DEBUG(print(dbgs()));
1520b57cec5SDimitry Andric }
153