Index: clang/lib/CodeGen/BackendUtil.cpp =================================================================== --- clang/lib/CodeGen/BackendUtil.cpp +++ clang/lib/CodeGen/BackendUtil.cpp @@ -236,15 +236,15 @@ bool Recover = CGOpts.SanitizeRecover.has(SanitizerKind::Address); bool UseAfterScope = CGOpts.SanitizeAddressUseAfterScope; bool UseGlobalsGC = asanUseGlobalsGC(T, CGOpts); - PM.add(createAddressSanitizerFunctionPass(/*CompileKernel*/ false, Recover, - UseAfterScope)); + PM.add(createAddressSanitizerPass(/*CompileKernel*/ false, Recover, + UseAfterScope)); PM.add(createAddressSanitizerModulePass(/*CompileKernel*/ false, Recover, UseGlobalsGC)); } static void addKernelAddressSanitizerPasses(const PassManagerBuilder &Builder, legacy::PassManagerBase &PM) { - PM.add(createAddressSanitizerFunctionPass( + PM.add(createAddressSanitizerPass( /*CompileKernel*/ true, /*Recover*/ true, /*UseAfterScope*/ false)); PM.add(createAddressSanitizerModulePass( /*CompileKernel*/ true, /*Recover*/ true)); Index: llvm/bindings/go/llvm/InstrumentationBindings.cpp =================================================================== --- llvm/bindings/go/llvm/InstrumentationBindings.cpp +++ llvm/bindings/go/llvm/InstrumentationBindings.cpp @@ -20,7 +20,7 @@ using namespace llvm; void LLVMAddAddressSanitizerFunctionPass(LLVMPassManagerRef PM) { - unwrap(PM)->add(createAddressSanitizerFunctionPass()); + unwrap(PM)->add(createAddressSanitizerPass()); } void LLVMAddAddressSanitizerModulePass(LLVMPassManagerRef PM) { Index: llvm/include/llvm/Transforms/Instrumentation.h =================================================================== --- llvm/include/llvm/Transforms/Instrumentation.h +++ llvm/include/llvm/Transforms/Instrumentation.h @@ -138,9 +138,9 @@ const InstrProfOptions &Options = InstrProfOptions()); // Insert AddressSanitizer (address sanity checking) instrumentation -FunctionPass *createAddressSanitizerFunctionPass(bool CompileKernel = false, - bool Recover = false, - bool UseAfterScope = false); +ModulePass *createAddressSanitizerPass(bool CompileKernel = false, + bool Recover = false, + bool UseAfterScope = false); ModulePass *createAddressSanitizerModulePass(bool CompileKernel = false, bool Recover = false, bool UseGlobalsGC = true); Index: llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp =================================================================== --- llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp +++ llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp @@ -428,7 +428,7 @@ Entries.clear(); } - void init(Module &M) { + void init(const Module &M) { assert(!inited_); inited_ = true; NamedMDNode *Globals = M.getNamedMetadata("llvm.asan.globals"); @@ -597,13 +597,13 @@ namespace { /// AddressSanitizer: instrument the code in module to find memory bugs. -struct AddressSanitizer : public FunctionPass { +struct AddressSanitizer : public ModulePass { // Pass identification, replacement for typeid static char ID; explicit AddressSanitizer(bool CompileKernel = false, bool Recover = false, bool UseAfterScope = false) - : FunctionPass(ID), UseAfterScope(UseAfterScope || ClUseAfterScope) { + : ModulePass(ID), UseAfterScope(UseAfterScope || ClUseAfterScope) { this->Recover = ClRecover.getNumOccurrences() > 0 ? ClRecover : Recover; this->CompileKernel = ClEnableKasan.getNumOccurrences() > 0 ? ClEnableKasan : CompileKernel; @@ -615,7 +615,6 @@ } void getAnalysisUsage(AnalysisUsage &AU) const override { - AU.addRequired(); AU.addRequired(); } @@ -661,18 +660,16 @@ Value *SizeArgument, uint32_t Exp); void instrumentMemIntrinsic(MemIntrinsic *MI); Value *memToShadow(Value *Shadow, IRBuilder<> &IRB); - bool runOnFunction(Function &F) override; + bool instrumentModule(Module &M, const TargetLibraryInfo *TLI); + bool runOnModule(Module &M) override; bool maybeInsertAsanInitAtFunctionEntry(Function &F); void maybeInsertDynamicShadowAtFunctionEntry(Function &F); void markEscapedLocalAllocas(Function &F); - bool doInitialization(Module &M) override; - bool doFinalization(Module &M) override; - - DominatorTree &getDominatorTree() const { return *DT; } private: friend struct FunctionStackPoisoner; + bool instrumentFunction(Function &F, const TargetLibraryInfo *TLI); void initializeCallbacks(Module &M); bool LooksLikeCodeInBug11395(Instruction *I); @@ -704,7 +701,6 @@ bool UseAfterScope; Type *IntptrTy; ShadowMapping Mapping; - DominatorTree *DT; Function *AsanHandleNoReturnFunc; Function *AsanPtrCmpFunction, *AsanPtrSubFunction; Constant *AsanShadowGlobal; @@ -1027,13 +1023,6 @@ // ---------------------- Helpers. void initializeCallbacks(Module &M); - bool doesDominateAllExits(const Instruction *I) const { - for (auto Ret : RetVec) { - if (!ASan.getDominatorTree().dominates(I, Ret)) return false; - } - return true; - } - /// Finds alloca where the value comes from. AllocaInst *findAllocaForValue(Value *V); @@ -1065,16 +1054,14 @@ AddressSanitizer, "asan", "AddressSanitizer: detects use-after-free and out-of-bounds bugs.", false, false) -INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) INITIALIZE_PASS_END( AddressSanitizer, "asan", "AddressSanitizer: detects use-after-free and out-of-bounds bugs.", false, false) -FunctionPass *llvm::createAddressSanitizerFunctionPass(bool CompileKernel, - bool Recover, - bool UseAfterScope) { +ModulePass *llvm::createAddressSanitizerPass(bool CompileKernel, bool Recover, + bool UseAfterScope) { assert(!CompileKernel || Recover); return new AddressSanitizer(CompileKernel, Recover, UseAfterScope); } @@ -2374,25 +2361,6 @@ ArrayType::get(IRB.getInt8Ty(), 0)); } -// virtual -bool AddressSanitizer::doInitialization(Module &M) { - // Initialize the private fields. No one has accessed them before. - GlobalsMD.init(M); - - C = &(M.getContext()); - LongSize = M.getDataLayout().getPointerSizeInBits(); - IntptrTy = Type::getIntNTy(*C, LongSize); - TargetTriple = Triple(M.getTargetTriple()); - - Mapping = getShadowMapping(TargetTriple, LongSize, CompileKernel); - return true; -} - -bool AddressSanitizer::doFinalization(Module &M) { - GlobalsMD.reset(); - return false; -} - bool AddressSanitizer::maybeInsertAsanInitAtFunctionEntry(Function &F) { // For each NSObject descendant having a +load method, this method is invoked // by the ObjC runtime before any of the static constructors is called. @@ -2466,7 +2434,35 @@ } } -bool AddressSanitizer::runOnFunction(Function &F) { +bool AddressSanitizer::runOnModule(Module &M) { + const TargetLibraryInfo *TLI = + &getAnalysis().getTLI(); + return instrumentModule(M, TLI); +} + +bool AddressSanitizer::instrumentModule(Module &M, + const TargetLibraryInfo *TLI) { + bool Changed = false; + // Initialize the private fields. No one has accessed them before. + GlobalsMD.init(M); + + C = &(M.getContext()); + LongSize = M.getDataLayout().getPointerSizeInBits(); + IntptrTy = Type::getIntNTy(*C, LongSize); + TargetTriple = Triple(M.getTargetTriple()); + + Mapping = getShadowMapping(TargetTriple, LongSize, CompileKernel); + + for (Function &F : M) + Changed |= instrumentFunction(F, TLI); + + GlobalsMD.reset(); + + return Changed; +} + +bool AddressSanitizer::instrumentFunction(Function &F, + const TargetLibraryInfo *TLI) { if (F.getLinkage() == GlobalValue::AvailableExternallyLinkage) return false; if (!ClDebugFunc.empty() && ClDebugFunc == F.getName()) return false; if (F.getName().startswith("__asan_")) return false; @@ -2485,7 +2481,6 @@ LLVM_DEBUG(dbgs() << "ASAN instrumenting:\n" << F << "\n"); initializeCallbacks(*F.getParent()); - DT = &getAnalysis().getDomTree(); FunctionStateRAII CleanupObj(this); @@ -2506,8 +2501,6 @@ bool IsWrite; unsigned Alignment; uint64_t TypeSize; - const TargetLibraryInfo *TLI = - &getAnalysis().getTLI(); // Fill the set of memory operations to instrument. for (auto &BB : F) {