diff options
Diffstat (limited to 'clang/lib')
-rw-r--r-- | clang/lib/CodeGen/CGDecl.cpp | 2 | ||||
-rw-r--r-- | clang/lib/CodeGen/CodeGenModule.cpp | 42 | ||||
-rw-r--r-- | clang/lib/CodeGen/CodeGenModule.h | 4 |
3 files changed, 32 insertions, 16 deletions
diff --git a/clang/lib/CodeGen/CGDecl.cpp b/clang/lib/CodeGen/CGDecl.cpp index 9bd61d7164f..91f80419304 100644 --- a/clang/lib/CodeGen/CGDecl.cpp +++ b/clang/lib/CodeGen/CGDecl.cpp @@ -345,7 +345,7 @@ void CodeGenFunction::EmitStaticVarDecl(const VarDecl &D, DMEntry = castedAddr; CGM.setStaticLocalDeclAddress(&D, castedAddr); - CGM.reportGlobalToASan(var, D.getLocation()); + CGM.reportGlobalToASan(var, D); // Emit global variable debug descriptor for static vars. CGDebugInfo *DI = getDebugInfo(); diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 2f87392c360..66ac524f2b9 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -1952,7 +1952,7 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) { if (NeedsGlobalCtor || NeedsGlobalDtor) EmitCXXGlobalVarDeclInitFunc(D, GV, NeedsGlobalCtor); - reportGlobalToASan(GV, D->getLocation(), NeedsGlobalCtor); + reportGlobalToASan(GV, *D, NeedsGlobalCtor); // Emit global variable debug information. if (CGDebugInfo *DI = getModuleDebugInfo()) @@ -1961,24 +1961,25 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) { } void CodeGenModule::reportGlobalToASan(llvm::GlobalVariable *GV, - SourceLocation Loc, bool IsDynInit) { + SourceLocation Loc, StringRef Name, + bool IsDynInit) { if (!LangOpts.Sanitize.Address) return; IsDynInit &= !SanitizerBL.isIn(*GV, "init"); bool IsBlacklisted = SanitizerBL.isIn(*GV); - llvm::LLVMContext &LLVMCtx = TheModule.getContext(); - llvm::GlobalVariable *LocDescr = nullptr; + llvm::GlobalVariable *GlobalName = nullptr; if (!IsBlacklisted) { - // Don't generate source location if a global is blacklisted - it won't - // be instrumented anyway. + // Don't generate source location and global name if it is blacklisted - + // it won't be instrumented anyway. PresumedLoc PLoc = Context.getSourceManager().getPresumedLoc(Loc); if (PLoc.isValid()) { llvm::Constant *LocData[] = { GetAddrOfConstantCString(PLoc.getFilename()), - llvm::ConstantInt::get(llvm::Type::getInt32Ty(LLVMCtx), PLoc.getLine()), - llvm::ConstantInt::get(llvm::Type::getInt32Ty(LLVMCtx), + llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), + PLoc.getLine()), + llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), PLoc.getColumn()), }; auto LocStruct = llvm::ConstantStruct::getAnon(LocData); @@ -1990,14 +1991,17 @@ void CodeGenModule::reportGlobalToASan(llvm::GlobalVariable *GV, // the optimizer before the ASan instrumentation pass. addCompilerUsedGlobal(LocDescr); } + if (!Name.empty()) { + GlobalName = GetAddrOfConstantCString(Name); + // GlobalName shouldn't be removed by the optimizer. + addCompilerUsedGlobal(GlobalName); + } } llvm::Value *GlobalMetadata[] = { - GV, - LocDescr, - llvm::ConstantInt::get(llvm::Type::getInt1Ty(LLVMCtx), IsDynInit), - llvm::ConstantInt::get(llvm::Type::getInt1Ty(LLVMCtx), IsBlacklisted) - }; + GV, LocDescr, GlobalName, + llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), IsDynInit), + llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), IsBlacklisted)}; llvm::MDNode *ThisGlobal = llvm::MDNode::get(VMContext, GlobalMetadata); llvm::NamedMDNode *AsanGlobals = @@ -2005,6 +2009,16 @@ void CodeGenModule::reportGlobalToASan(llvm::GlobalVariable *GV, AsanGlobals->addOperand(ThisGlobal); } +void CodeGenModule::reportGlobalToASan(llvm::GlobalVariable *GV, + const VarDecl &D, bool IsDynInit) { + if (!LangOpts.Sanitize.Address) + return; + std::string QualName; + llvm::raw_string_ostream OS(QualName); + D.printQualifiedName(OS); + reportGlobalToASan(GV, D.getLocation(), OS.str(), IsDynInit); +} + static bool isVarDeclStrongDefinition(const VarDecl *D, bool NoCommon) { // Don't give variables common linkage if -fno-common was specified unless it // was overridden by a NoCommon attribute. @@ -2810,7 +2824,7 @@ CodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S) { if (Entry) Entry->setValue(GV); - reportGlobalToASan(GV, S->getStrTokenLoc(0)); + reportGlobalToASan(GV, S->getStrTokenLoc(0), "<string literal>"); return GV; } diff --git a/clang/lib/CodeGen/CodeGenModule.h b/clang/lib/CodeGen/CodeGenModule.h index 34254697467..d6b694a8824 100644 --- a/clang/lib/CodeGen/CodeGenModule.h +++ b/clang/lib/CodeGen/CodeGenModule.h @@ -1015,8 +1015,10 @@ public: return SanitizerBL; } - void reportGlobalToASan(llvm::GlobalVariable *GV, SourceLocation Loc, + void reportGlobalToASan(llvm::GlobalVariable *GV, const VarDecl &D, bool IsDynInit = false); + void reportGlobalToASan(llvm::GlobalVariable *GV, SourceLocation Loc, + StringRef Name, bool IsDynInit = false); void addDeferredVTable(const CXXRecordDecl *RD) { DeferredVTables.push_back(RD); |