kstoimenov updated this revision to Diff 381365. kstoimenov added a comment.
Removed the top level flag and addressed comments. Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D112098/new/ https://reviews.llvm.org/D112098 Files: llvm/include/llvm/Analysis/StackSafetyAnalysis.h llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp llvm/test/Instrumentation/AddressSanitizer/asan-stack-safety.ll
Index: llvm/test/Instrumentation/AddressSanitizer/asan-stack-safety.ll =================================================================== --- /dev/null +++ llvm/test/Instrumentation/AddressSanitizer/asan-stack-safety.ll @@ -0,0 +1,13 @@ +; REQUIRES: x86-registered-target + +; RUN: opt < %s -passes='asan-pipeline' -S -asan-instrumentation-with-call-threshold=0 -o - | FileCheck %s --check-prefixes=NOSAFETY +; NOSAFETY: call void @__asan_load1 +; RUN: opt < %s -passes='asan-pipeline' -S -asan-use-stack-safety=true -asan-instrumentation-with-call-threshold=0 -o - | FileCheck %s --check-prefixes=SAFETY +; SAFETY-NOT: call void @__asan_load1 + +define i32 @stack-safety() sanitize_address { + %buf = alloca [10 x i8], align 1 + %arrayidx = getelementptr inbounds [10 x i8], [10 x i8]* %buf, i64 0, i64 0 + %1 = load i8, i8* %arrayidx, align 1 + ret i32 0 +} Index: llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp =================================================================== --- llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp +++ llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp @@ -26,6 +26,7 @@ #include "llvm/ADT/Triple.h" #include "llvm/ADT/Twine.h" #include "llvm/Analysis/MemoryBuiltins.h" +#include "llvm/Analysis/StackSafetyAnalysis.h" #include "llvm/Analysis/TargetLibraryInfo.h" #include "llvm/Analysis/ValueTracking.h" #include "llvm/BinaryFormat/MachO.h" @@ -47,6 +48,7 @@ #include "llvm/IR/GlobalVariable.h" #include "llvm/IR/IRBuilder.h" #include "llvm/IR/InlineAsm.h" +#include "llvm/IR/InstIterator.h" #include "llvm/IR/InstVisitor.h" #include "llvm/IR/InstrTypes.h" #include "llvm/IR/Instruction.h" @@ -211,6 +213,11 @@ "asan-instrument-writes", cl::desc("instrument write instructions"), cl::Hidden, cl::init(true)); +static cl::opt<bool> + ClUseStackSafety("asan-use-stack-safety", cl::Hidden, cl::init(false), + cl::Hidden, cl::desc("Use Stack Safety analysis results"), + cl::Optional); + static cl::opt<bool> ClInstrumentAtomics( "asan-instrument-atomics", cl::desc("instrument atomic instructions (rmw, cmpxchg)"), cl::Hidden, @@ -647,8 +654,8 @@ /// AddressSanitizer: instrument the code in module to find memory bugs. struct AddressSanitizer { AddressSanitizer(Module &M, const GlobalsMetadata *GlobalsMD, - bool CompileKernel = false, bool Recover = false, - bool UseAfterScope = false, + const StackSafetyGlobalInfo *SSI, bool CompileKernel = false, + bool Recover = false, bool UseAfterScope = false, AsanDetectStackUseAfterReturnMode UseAfterReturn = AsanDetectStackUseAfterReturnMode::Runtime) : CompileKernel(ClEnableKasan.getNumOccurrences() > 0 ? ClEnableKasan @@ -657,7 +664,7 @@ UseAfterScope(UseAfterScope || ClUseAfterScope), UseAfterReturn(ClUseAfterReturn.getNumOccurrences() ? ClUseAfterReturn : UseAfterReturn), - GlobalsMD(*GlobalsMD) { + GlobalsMD(*GlobalsMD), SSI(SSI) { C = &(M.getContext()); LongSize = M.getDataLayout().getPointerSizeInBits(); IntptrTy = Type::getIntNTy(*C, LongSize); @@ -686,7 +693,7 @@ /// Check if we want (and can) handle this alloca. bool isInterestingAlloca(const AllocaInst &AI); - bool ignoreAccess(Value *Ptr); + bool ignoreAccess(Instruction *Inst, Value *Ptr); void getInterestingMemoryOperands( Instruction *I, SmallVectorImpl<InterestingMemoryOperand> &Interesting); @@ -771,6 +778,7 @@ FunctionCallee AsanMemmove, AsanMemcpy, AsanMemset; Value *LocalDynamicShadow = nullptr; const GlobalsMetadata &GlobalsMD; + const StackSafetyGlobalInfo *SSI; DenseMap<const AllocaInst *, bool> ProcessedAllocas; FunctionCallee AMDGPUAddressShared; @@ -801,12 +809,16 @@ } bool runOnFunction(Function &F) override { + if (ClUseStackSafety) { + report_fatal_error("Stack safety analysis is not supported " + "with legacy pass manager."); + } GlobalsMetadata &GlobalsMD = getAnalysis<ASanGlobalsMetadataWrapperPass>().getGlobalsMD(); const TargetLibraryInfo *TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F); - AddressSanitizer ASan(*F.getParent(), &GlobalsMD, CompileKernel, Recover, - UseAfterScope, UseAfterReturn); + AddressSanitizer ASan(*F.getParent(), &GlobalsMD, nullptr, CompileKernel, + Recover, UseAfterScope, UseAfterReturn); return ASan.instrumentFunction(F, TLI); } @@ -1251,6 +1263,9 @@ GlobalsMetadata ASanGlobalsMetadataAnalysis::run(Module &M, ModuleAnalysisManager &AM) { + if (ClUseStackSafety && !AM.getCachedResult<StackSafetyGlobalAnalysis>(M)) { + AM.getResult<StackSafetyGlobalAnalysis>(M); + } return GlobalsMetadata(M); } @@ -1260,8 +1275,11 @@ Module &M = *F.getParent(); if (auto *R = MAMProxy.getCachedResult<ASanGlobalsMetadataAnalysis>(M)) { const TargetLibraryInfo *TLI = &AM.getResult<TargetLibraryAnalysis>(F); - AddressSanitizer Sanitizer(M, R, Options.CompileKernel, Options.Recover, - Options.UseAfterScope, Options.UseAfterReturn); + const StackSafetyGlobalInfo *SSI = + MAMProxy.getCachedResult<StackSafetyGlobalAnalysis>(M); + AddressSanitizer Sanitizer(M, R, SSI, Options.CompileKernel, + Options.Recover, Options.UseAfterScope, + Options.UseAfterReturn); if (Sanitizer.instrumentFunction(F, TLI)) return PreservedAnalyses::none(); return PreservedAnalyses::all(); @@ -1460,7 +1478,7 @@ return IsInteresting; } -bool AddressSanitizer::ignoreAccess(Value *Ptr) { +bool AddressSanitizer::ignoreAccess(Instruction *Inst, Value *Ptr) { // Instrument acesses from different address spaces only for AMDGPU. Type *PtrTy = cast<PointerType>(Ptr->getType()->getScalarType()); if (PtrTy->getPointerAddressSpace() != 0 && @@ -1481,6 +1499,11 @@ if (ClSkipPromotableAllocas && !isInterestingAlloca(*AI)) return true; + if (ClUseStackSafety && SSI != nullptr && findAllocaForValue(Ptr) && + SSI->stackAccessIsSafe(*Inst)) { + return true; + } + return false; } @@ -1495,22 +1518,22 @@ return; if (LoadInst *LI = dyn_cast<LoadInst>(I)) { - if (!ClInstrumentReads || ignoreAccess(LI->getPointerOperand())) + if (!ClInstrumentReads || ignoreAccess(I, LI->getPointerOperand())) return; Interesting.emplace_back(I, LI->getPointerOperandIndex(), false, LI->getType(), LI->getAlign()); } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { - if (!ClInstrumentWrites || ignoreAccess(SI->getPointerOperand())) + if (!ClInstrumentWrites || ignoreAccess(I, SI->getPointerOperand())) return; Interesting.emplace_back(I, SI->getPointerOperandIndex(), true, SI->getValueOperand()->getType(), SI->getAlign()); } else if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(I)) { - if (!ClInstrumentAtomics || ignoreAccess(RMW->getPointerOperand())) + if (!ClInstrumentAtomics || ignoreAccess(I, RMW->getPointerOperand())) return; Interesting.emplace_back(I, RMW->getPointerOperandIndex(), true, RMW->getValOperand()->getType(), None); } else if (AtomicCmpXchgInst *XCHG = dyn_cast<AtomicCmpXchgInst>(I)) { - if (!ClInstrumentAtomics || ignoreAccess(XCHG->getPointerOperand())) + if (!ClInstrumentAtomics || ignoreAccess(I, XCHG->getPointerOperand())) return; Interesting.emplace_back(I, XCHG->getPointerOperandIndex(), true, XCHG->getCompareOperand()->getType(), None); @@ -1525,7 +1548,7 @@ return; auto BasePtr = CI->getOperand(OpOffset); - if (ignoreAccess(BasePtr)) + if (ignoreAccess(I, BasePtr)) return; auto Ty = cast<PointerType>(BasePtr->getType())->getElementType(); MaybeAlign Alignment = Align(1); @@ -1537,7 +1560,7 @@ } else { for (unsigned ArgNo = 0; ArgNo < CI->arg_size(); ArgNo++) { if (!ClInstrumentByval || !CI->isByValArgument(ArgNo) || - ignoreAccess(CI->getArgOperand(ArgNo))) + ignoreAccess(I, CI->getArgOperand(ArgNo))) continue; Type *Ty = CI->getParamByValType(ArgNo); Interesting.emplace_back(I, ArgNo, false, Ty, Align(1)); Index: llvm/include/llvm/Analysis/StackSafetyAnalysis.h =================================================================== --- llvm/include/llvm/Analysis/StackSafetyAnalysis.h +++ llvm/include/llvm/Analysis/StackSafetyAnalysis.h @@ -86,6 +86,17 @@ bool stackAccessIsSafe(const Instruction &I) const; void print(raw_ostream &O) const; void dump() const; + + /// Handle invalidation from the pass manager. + /// These results are never invalidated. + bool invalidate(Module &, const PreservedAnalyses &, + ModuleAnalysisManager::Invalidator &) { + return false; + } + bool invalidate(Function &, const PreservedAnalyses &, + FunctionAnalysisManager::Invalidator &) { + return false; + } }; /// StackSafetyInfo wrapper for the new pass manager.
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits