[ASan] Use metadata to pass source-level information from Clang to ASan.
Instead of creating global variables for source locations and global names,
just create metadata nodes and strings. They will be transformed into actual
globals in the instrumentation pass (if necessary). This approach is more
flexible:
1) we don't have to ensure that our custom globals survive all the optimizations
2) if globals are discarded for some reason, we will simply ignore metadata for them
and won't have to erase corresponding globals
3) metadata for source locations can be reused for other purposes: e.g. we may
attach source location metadata to alloca instructions and provide better descriptions
for stack variables in ASan error reports.
No functionality change.
llvm-svn: 214604
diff --git a/clang/lib/CodeGen/SanitizerMetadata.cpp b/clang/lib/CodeGen/SanitizerMetadata.cpp
index 4619152..dd8c133 100644
--- a/clang/lib/CodeGen/SanitizerMetadata.cpp
+++ b/clang/lib/CodeGen/SanitizerMetadata.cpp
@@ -28,35 +28,15 @@
IsDynInit &= !CGM.getSanitizerBlacklist().isIn(*GV, "init");
IsBlacklisted |= CGM.getSanitizerBlacklist().isIn(*GV);
- llvm::GlobalVariable *LocDescr = nullptr;
- llvm::GlobalVariable *GlobalName = nullptr;
+ llvm::Value *LocDescr = nullptr;
+ llvm::Value *GlobalName = nullptr;
llvm::LLVMContext &VMContext = CGM.getLLVMContext();
if (!IsBlacklisted) {
// Don't generate source location and global name if it is blacklisted -
// it won't be instrumented anyway.
- PresumedLoc PLoc = CGM.getContext().getSourceManager().getPresumedLoc(Loc);
- if (PLoc.isValid()) {
- llvm::Constant *LocData[] = {
- CGM.GetAddrOfConstantCString(PLoc.getFilename()),
- llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
- PLoc.getLine()),
- llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
- PLoc.getColumn()),
- };
- auto LocStruct = llvm::ConstantStruct::getAnon(LocData);
- LocDescr = new llvm::GlobalVariable(
- CGM.getModule(), LocStruct->getType(), true,
- llvm::GlobalValue::PrivateLinkage, LocStruct, ".asan_loc_descr");
- LocDescr->setUnnamedAddr(true);
- // Add LocDescr to llvm.compiler.used, so that it won't be removed by
- // the optimizer before the ASan instrumentation pass.
- CGM.addCompilerUsedGlobal(LocDescr);
- }
- if (!Name.empty()) {
- GlobalName = CGM.GetAddrOfConstantCString(Name);
- // GlobalName shouldn't be removed by the optimizer.
- CGM.addCompilerUsedGlobal(GlobalName);
- }
+ LocDescr = getLocationMetadata(Loc);
+ if (!Name.empty())
+ GlobalName = llvm::MDString::get(VMContext, Name);
}
llvm::Value *GlobalMetadata[] = {
@@ -86,3 +66,17 @@
if (CGM.getLangOpts().Sanitize.Address)
reportGlobalToASan(GV, SourceLocation(), "", false, true);
}
+
+llvm::MDNode *SanitizerMetadata::getLocationMetadata(SourceLocation Loc) {
+ PresumedLoc PLoc = CGM.getContext().getSourceManager().getPresumedLoc(Loc);
+ if (!PLoc.isValid())
+ return nullptr;
+ llvm::LLVMContext &VMContext = CGM.getLLVMContext();
+ llvm::Value *LocMetadata[] = {
+ llvm::MDString::get(VMContext, PLoc.getFilename()),
+ llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), PLoc.getLine()),
+ llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
+ PLoc.getColumn()),
+ };
+ return llvm::MDNode::get(VMContext, LocMetadata);
+}
diff --git a/clang/lib/CodeGen/SanitizerMetadata.h b/clang/lib/CodeGen/SanitizerMetadata.h
index 3062b4a..6d66cbe 100644
--- a/clang/lib/CodeGen/SanitizerMetadata.h
+++ b/clang/lib/CodeGen/SanitizerMetadata.h
@@ -18,6 +18,7 @@
namespace llvm {
class GlobalVariable;
+class MDNode;
}
namespace clang {
@@ -40,6 +41,8 @@
StringRef Name, bool IsDynInit = false,
bool IsBlacklisted = false);
void disableSanitizerForGlobal(llvm::GlobalVariable *GV);
+private:
+ llvm::MDNode *getLocationMetadata(SourceLocation Loc);
};
} // end namespace CodeGen
} // end namespace clang