71 changes: 28 additions & 43 deletions llvm/lib/Analysis/StackSafetyAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "llvm/Analysis/StackSafetyAnalysis.h"
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/Analysis/ScalarEvolutionExpressions.h"
#include "llvm/IR/ConstantRange.h"
#include "llvm/IR/DerivedTypes.h"
Expand Down Expand Up @@ -166,7 +167,7 @@ struct FunctionInfo {
if (F) {
size_t Pos = 0;
for (auto &I : instructions(F)) {
if (const auto *AI = dyn_cast<AllocaInst>(&I)) {
if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
auto &AS = Allocas[Pos];
O << " " << AI->getName() << "["
<< getStaticAllocaSizeRange(*AI).getUpper() << "]: " << AS << "\n";
Expand All @@ -189,6 +190,7 @@ struct StackSafetyInfo::InfoTy {

struct StackSafetyGlobalInfo::InfoTy {
GVToSSI Info;
SmallPtrSet<const AllocaInst *, 8> SafeAllocas;
};

namespace {
Expand Down Expand Up @@ -546,31 +548,6 @@ StackSafetyDataFlowAnalysis::run() {
return Functions;
}

bool setStackSafetyMetadata(Module &M, const GVToSSI &SSGI) {
bool Changed = false;
for (auto &F : M.functions()) {
if (F.isDeclaration() || F.hasOptNone())
continue;
auto Iter = SSGI.find(&F);
if (Iter == SSGI.end())
continue;
const FunctionInfo &Summary = Iter->second;
size_t Pos = 0;
for (auto &I : instructions(F)) {
if (auto *AI = dyn_cast<AllocaInst>(&I)) {
auto &AS = Summary.Allocas[Pos];
if (getStaticAllocaSizeRange(*AI).contains(AS.Range)) {
AI->setMetadata(M.getMDKindID("stack-safe"),
MDNode::get(M.getContext(), None));
Changed = true;
}
++Pos;
}
}
}
return Changed;
}

const Function *findCalleeInModule(const GlobalValue *GV) {
while (GV) {
if (GV->isInterposable() || !GV->isDSOLocal())
Expand Down Expand Up @@ -683,7 +660,24 @@ const StackSafetyGlobalInfo::InfoTy &StackSafetyGlobalInfo::getInfo() const {
Functions.emplace(&F, std::move(FI));
}
}
Info.reset(new InfoTy{createGlobalStackSafetyInfo(std::move(Functions))});
Info.reset(
new InfoTy{createGlobalStackSafetyInfo(std::move(Functions)), {}});
for (auto &KV : Info->Info) {
if (!KV.first->isDeclaration()) {
size_t Pos = 0;
// FIXME: Convert FunctionInfo::Allocas into map<AllocaInst*, UseInfo>
// and do not rely on alloca index.
for (auto &I : instructions(*cast<Function>(KV.first))) {
if (const auto &AI = dyn_cast<AllocaInst>(&I)) {
if (getStaticAllocaSizeRange(*AI).contains(
KV.second.Allocas[Pos].Range)) {
Info->SafeAllocas.insert(AI);
}
++Pos;
}
}
}
}
if (StackSafetyPrint)
print(errs());
}
Expand All @@ -707,8 +701,9 @@ StackSafetyGlobalInfo::operator=(StackSafetyGlobalInfo &&) = default;

StackSafetyGlobalInfo::~StackSafetyGlobalInfo() = default;

bool StackSafetyGlobalInfo::setMetadata(Module &M) const {
return setStackSafetyMetadata(M, getInfo().Info);
bool StackSafetyGlobalInfo::isSafe(const AllocaInst &AI) const {
const auto &Info = getInfo();
return Info.SafeAllocas.find(&AI) != Info.SafeAllocas.end();
}

void StackSafetyGlobalInfo::print(raw_ostream &O) const {
Expand Down Expand Up @@ -781,13 +776,6 @@ PreservedAnalyses StackSafetyGlobalPrinterPass::run(Module &M,
return PreservedAnalyses::all();
}

PreservedAnalyses
StackSafetyGlobalAnnotatorPass::run(Module &M, ModuleAnalysisManager &AM) {
auto &SSGI = AM.getResult<StackSafetyGlobalAnalysis>(M);
SSGI.setMetadata(M);
return PreservedAnalyses::all();
}

char StackSafetyGlobalInfoWrapperPass::ID = 0;

StackSafetyGlobalInfoWrapperPass::StackSafetyGlobalInfoWrapperPass()
Expand All @@ -805,18 +793,15 @@ void StackSafetyGlobalInfoWrapperPass::print(raw_ostream &O,

void StackSafetyGlobalInfoWrapperPass::getAnalysisUsage(
AnalysisUsage &AU) const {
AU.setPreservesAll();
AU.addRequired<StackSafetyInfoWrapperPass>();
}

bool StackSafetyGlobalInfoWrapperPass::runOnModule(Module &M) {
SSGI = {&M, [this](Function &F) -> const StackSafetyInfo & {
return getAnalysis<StackSafetyInfoWrapperPass>(F).getResult();
}};
return SSGI.setMetadata(M);
}

ModulePass *llvm::createStackSafetyGlobalInfoWrapperPass() {
return new StackSafetyGlobalInfoWrapperPass();
return false;
}

static const char LocalPassArg[] = "stack-safety-local";
Expand All @@ -829,7 +814,7 @@ INITIALIZE_PASS_END(StackSafetyInfoWrapperPass, LocalPassArg, LocalPassName,

static const char GlobalPassName[] = "Stack Safety Analysis";
INITIALIZE_PASS_BEGIN(StackSafetyGlobalInfoWrapperPass, DEBUG_TYPE,
GlobalPassName, false, false)
GlobalPassName, false, true)
INITIALIZE_PASS_DEPENDENCY(StackSafetyInfoWrapperPass)
INITIALIZE_PASS_END(StackSafetyGlobalInfoWrapperPass, DEBUG_TYPE,
GlobalPassName, false, false)
GlobalPassName, false, true)
1 change: 0 additions & 1 deletion llvm/lib/Passes/PassRegistry.def
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ MODULE_PASS("tsan-module", ThreadSanitizerPass())
MODULE_PASS("kasan-module", ModuleAddressSanitizerPass(/*CompileKernel=*/true, false, true, false))
MODULE_PASS("sancov-module", ModuleSanitizerCoveragePass())
MODULE_PASS("poison-checking", PoisonCheckingPass())
MODULE_PASS("stack-safety-annotator", StackSafetyGlobalAnnotatorPass())
#undef MODULE_PASS

#ifndef CGSCC_ANALYSIS
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/AArch64/AArch64.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ createAArch64InstructionSelector(const AArch64TargetMachine &,
AArch64Subtarget &, AArch64RegisterBankInfo &);
FunctionPass *createAArch64PreLegalizeCombiner(bool IsOptNone);
FunctionPass *createAArch64PostLegalizeCombiner(bool IsOptNone);
FunctionPass *createAArch64StackTaggingPass(bool MergeInit);
FunctionPass *createAArch64StackTaggingPass(bool IsOptNone);
FunctionPass *createAArch64StackTaggingPreRAPass();

void initializeAArch64A53Fix835769Pass(PassRegistry&);
Expand Down
38 changes: 27 additions & 11 deletions llvm/lib/Target/AArch64/AArch64StackTagging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/ScalarEvolution.h"
#include "llvm/Analysis/ScalarEvolutionExpressions.h"
#include "llvm/Analysis/StackSafetyAnalysis.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/CodeGen/LiveRegUnits.h"
#include "llvm/CodeGen/MachineBasicBlock.h"
Expand All @@ -44,6 +45,7 @@
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/IntrinsicsAArch64.h"
#include "llvm/IR/Metadata.h"
#include "llvm/InitializePasses.h"
#include "llvm/Pass.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/Debug.h"
Expand All @@ -61,6 +63,11 @@ static cl::opt<bool> ClMergeInit(
"stack-tagging-merge-init", cl::Hidden, cl::init(true), cl::ZeroOrMore,
cl::desc("merge stack variable initializers with tagging when possible"));

static cl::opt<bool>
ClUseStackSafety("stack-tagging-use-stack-safety", cl::Hidden,
cl::init(true), cl::ZeroOrMore,
cl::desc("Use Stack Safety analysis results"));

static cl::opt<unsigned> ClScanLimit("stack-tagging-merge-init-scan-limit",
cl::init(40), cl::Hidden);

Expand Down Expand Up @@ -275,15 +282,17 @@ class AArch64StackTagging : public FunctionPass {
int Tag; // -1 for non-tagged allocations
};

bool MergeInit;
const bool MergeInit;
const bool UseStackSafety;

public:
static char ID; // Pass ID, replacement for typeid

AArch64StackTagging(bool MergeInit = true)
AArch64StackTagging(bool IsOptNone = false)
: FunctionPass(ID),
MergeInit(ClMergeInit.getNumOccurrences() > 0 ? ClMergeInit
: MergeInit) {
MergeInit(ClMergeInit.getNumOccurrences() ? ClMergeInit : !IsOptNone),
UseStackSafety(ClUseStackSafety.getNumOccurrences() ? ClUseStackSafety
: !IsOptNone) {
initializeAArch64StackTaggingPass(*PassRegistry::getPassRegistry());
}

Expand All @@ -305,13 +314,16 @@ class AArch64StackTagging : public FunctionPass {
StringRef getPassName() const override { return "AArch64 Stack Tagging"; }

private:
Function *F;
Function *SetTagFunc;
const DataLayout *DL;
AAResults *AA;
Function *F = nullptr;
Function *SetTagFunc = nullptr;
const DataLayout *DL = nullptr;
AAResults *AA = nullptr;
const StackSafetyGlobalInfo *SSI = nullptr;

void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.setPreservesCFG();
if (UseStackSafety)
AU.addRequired<StackSafetyGlobalInfoWrapperPass>();
if (MergeInit)
AU.addRequired<AAResultsWrapperPass>();
}
Expand All @@ -323,11 +335,13 @@ char AArch64StackTagging::ID = 0;

INITIALIZE_PASS_BEGIN(AArch64StackTagging, DEBUG_TYPE, "AArch64 Stack Tagging",
false, false)
INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
INITIALIZE_PASS_DEPENDENCY(StackSafetyGlobalInfoWrapperPass)
INITIALIZE_PASS_END(AArch64StackTagging, DEBUG_TYPE, "AArch64 Stack Tagging",
false, false)

FunctionPass *llvm::createAArch64StackTaggingPass(bool MergeInit) {
return new AArch64StackTagging(MergeInit);
FunctionPass *llvm::createAArch64StackTaggingPass(bool IsOptNone) {
return new AArch64StackTagging(IsOptNone);
}

Instruction *AArch64StackTagging::collectInitializers(Instruction *StartInst,
Expand Down Expand Up @@ -402,7 +416,7 @@ bool AArch64StackTagging::isInterestingAlloca(const AllocaInst &AI) {
// swifterror allocas are register promoted by ISel
!AI.isSwiftError() &&
// safe allocas are not interesting
!AI.getMetadata("stack-safe");
!(SSI && SSI->isSafe(AI));
return IsInteresting;
}

Expand Down Expand Up @@ -518,6 +532,8 @@ bool AArch64StackTagging::runOnFunction(Function &Fn) {
if (!Fn.hasFnAttribute(Attribute::SanitizeMemTag))
return false;

if (UseStackSafety)
SSI = &getAnalysis<StackSafetyGlobalInfoWrapperPass>().getResult();
F = &Fn;
DL = &Fn.getParent()->getDataLayout();
if (MergeInit)
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Target/AArch64/AArch64TargetMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -486,8 +486,8 @@ void AArch64PassConfig::addIRPasses() {
addPass(createLICMPass());
}

addPass(createAArch64StackTaggingPass(/* MergeInit = */ TM->getOptLevel() !=
CodeGenOpt::None));
addPass(createAArch64StackTaggingPass(
/*IsOptNone=*/TM->getOptLevel() == CodeGenOpt::None));

// Add Control Flow Guard checks.
if (TM->getTargetTriple().isOSWindows())
Expand Down
34 changes: 0 additions & 34 deletions llvm/test/Analysis/StackSafetyAnalysis/ipa-attr.ll

This file was deleted.

65 changes: 0 additions & 65 deletions llvm/test/Analysis/StackSafetyAnalysis/scev-udiv.ll

This file was deleted.

19 changes: 19 additions & 0 deletions llvm/test/CodeGen/AArch64/O3-pipeline.ll
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@
; CHECK-NEXT: Interleaved Load Combine Pass
; CHECK-NEXT: Dominator Tree Construction
; CHECK-NEXT: Interleaved Access Pass
; CHECK-NEXT: Stack Safety Analysis
; CHECK-NEXT: FunctionPass Manager
; CHECK-NEXT: Dominator Tree Construction
; CHECK-NEXT: Natural Loop Information
; CHECK-NEXT: Scalar Evolution Analysis
; CHECK-NEXT: Stack Safety Local Analysis
; CHECK-NEXT: FunctionPass Manager
; CHECK-NEXT: Dominator Tree Construction
; CHECK-NEXT: Basic Alias Analysis (stateless AA impl)
; CHECK-NEXT: Function Alias Analysis Results
; CHECK-NEXT: AArch64 Stack Tagging
Expand Down Expand Up @@ -200,6 +208,17 @@
; CHECK-NEXT: Pass Arguments: -domtree
; CHECK-NEXT: FunctionPass Manager
; CHECK-NEXT: Dominator Tree Construction
; CHECK-NEXT: Pass Arguments: -assumption-cache-tracker -targetlibinfo -domtree -loops -scalar-evolution -stack-safety-local
; CHECK-NEXT: Assumption Cache Tracker
; CHECK-NEXT: Target Library Information
; CHECK-NEXT: FunctionPass Manager
; CHECK-NEXT: Dominator Tree Construction
; CHECK-NEXT: Natural Loop Information
; CHECK-NEXT: Scalar Evolution Analysis
; CHECK-NEXT: Stack Safety Local Analysis
; CHECK-NEXT: Pass Arguments: -domtree
; CHECK-NEXT: FunctionPass Manager
; CHECK-NEXT: Dominator Tree Construction

define void @f() {
ret void
Expand Down
34 changes: 25 additions & 9 deletions llvm/test/CodeGen/AArch64/stack-tagging.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
; RUN: opt < %s -stack-tagging -S -o - | FileCheck %s
; RUN: opt < %s -stack-tagging -S -o - | FileCheck %s --check-prefixes=CHECK,SSI
; RUN: opt < %s -stack-tagging -stack-tagging-use-stack-safety=0 -S -o - | FileCheck %s --check-prefixes=CHECK,NOSSI

target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
target triple = "aarch64--linux-android"
Expand All @@ -8,6 +9,11 @@ declare void @use32(i32*)
declare void @llvm.lifetime.start.p0i8(i64, i8* nocapture)
declare void @llvm.lifetime.end.p0i8(i64, i8* nocapture)

define dso_local void @noUse32(i32*) sanitize_memtag {
entry:
ret void
}

define void @OneVar() sanitize_memtag {
entry:
%x = alloca i32, align 4
Expand All @@ -33,7 +39,7 @@ entry:
%x1 = alloca i32, align 4
%x2 = alloca i8, align 4
%x3 = alloca i32, i32 11, align 4
%x4 = alloca i32, align 4, !stack-safe !0
%x4 = alloca i32, align 4
call void @use32(i32* %x1)
call void @use8(i8* %x2)
call void @use32(i32* %x3)
Expand All @@ -50,9 +56,12 @@ entry:
; CHECK: alloca { [11 x i32], [4 x i8] }, align 16
; CHECK: call { [11 x i32], [4 x i8] }* @llvm.aarch64.tagp.{{.*}}({ [11 x i32], [4 x i8] }* {{.*}}, i64 2)
; CHECK: call void @llvm.aarch64.settag(i8* {{.*}}, i64 48)
; CHECK: alloca i32, align 4
; CHECK-NOT: @llvm.aarch64.tagp
; CHECK-NOT: @llvm.aarch64.settag
; SSI: alloca i32, align 4
; NOSSI: alloca { i32, [12 x i8] }, align 16
; NOSSI: @llvm.aarch64.tagp.
; NOSSI: call void @llvm.aarch64.settag(i8* {{.*}}, i64 16)
; SSI-NOT: @llvm.aarch64.tagp
; SSI-NOT: @llvm.aarch64.settag

; CHECK: call void @use32(
; CHECK: call void @use8(
Expand All @@ -61,6 +70,7 @@ entry:
; CHECK: call void @llvm.aarch64.settag(i8* {{.*}}, i64 16)
; CHECK: call void @llvm.aarch64.settag(i8* {{.*}}, i64 16)
; CHECK: call void @llvm.aarch64.settag(i8* {{.*}}, i64 48)
; NOSSI: call void @llvm.aarch64.settag(i8* {{.*}}, i64 16)
; CHECK-NEXT: ret void


Expand Down Expand Up @@ -161,12 +171,15 @@ entry:
another_bb:
call void @llvm.lifetime.start.p0i8(i64 4, i8* nonnull %cz)
store i32 7, i32* %z
call void @noUse32(i32* %z)
call void @llvm.lifetime.end.p0i8(i64 4, i8* nonnull %cz)
call void @llvm.lifetime.start.p0i8(i64 4, i8* nonnull %cz)
store i32 7, i32* %z
call void @llvm.lifetime.end.p0i8(i64 4, i8* nonnull %cz)
call void @llvm.lifetime.start.p0i8(i64 4, i8* nonnull %cxcy)
store i32 8, i32* %xy
call void @noUse32(i32* %x)
call void @noUse32(i32* %y)
call void @llvm.lifetime.end.p0i8(i64 4, i8* nonnull %cxcy)
ret void
}
Expand All @@ -179,15 +192,18 @@ another_bb:
; CHECK: alloca { i32, [12 x i8] }, align 16
; CHECK: call { i32, [12 x i8] }* @llvm.aarch64.tagp
; CHECK: call void @llvm.aarch64.settag(
; CHECK: alloca { i32, [12 x i8] }, align 16
; CHECK: call { i32, [12 x i8] }* @llvm.aarch64.tagp
; CHECK: call void @llvm.aarch64.settag(
; SSI: alloca i32, align 4
; NOSSI: alloca { i32, [12 x i8] }, align 16
; NOSSI: call { i32, [12 x i8] }* @llvm.aarch64.tagp
; NOSSI: call void @llvm.aarch64.settag(
; CHECK: store i32
; CHECK: call void @noUse32(i32*
; CHECK: store i32
; CHECK: store i32
; CHECK: call void @noUse32(i32*
; CHECK: call void @llvm.aarch64.settag(
; CHECK: call void @llvm.aarch64.settag(
; CHECK: call void @llvm.aarch64.settag(
; NOSSI: call void @llvm.aarch64.settag(
; CHECK: ret void

!0 = !{}