summaryrefslogtreecommitdiffstats
path: root/clang/lib/CodeGen/SanitizerMetadata.cpp
diff options
context:
space:
mode:
authorAlexey Samsonov <vonosmas@gmail.com>2014-08-02 00:35:50 +0000
committerAlexey Samsonov <vonosmas@gmail.com>2014-08-02 00:35:50 +0000
commitd9ad5cec0c8ff8fe4bdbdb638b24148b468dc297 (patch)
tree98e0a49695ee9727c0c33ede34e45dc075abd94c /clang/lib/CodeGen/SanitizerMetadata.cpp
parentbb006ce29171aef3eb7bdebf52fcd0a5c21385fe (diff)
downloadbcm5719-llvm-d9ad5cec0c8ff8fe4bdbdb638b24148b468dc297.tar.gz
bcm5719-llvm-d9ad5cec0c8ff8fe4bdbdb638b24148b468dc297.zip
[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
Diffstat (limited to 'clang/lib/CodeGen/SanitizerMetadata.cpp')
-rw-r--r--clang/lib/CodeGen/SanitizerMetadata.cpp44
1 files changed, 19 insertions, 25 deletions
diff --git a/clang/lib/CodeGen/SanitizerMetadata.cpp b/clang/lib/CodeGen/SanitizerMetadata.cpp
index 46191523c8b..dd8c1336215 100644
--- a/clang/lib/CodeGen/SanitizerMetadata.cpp
+++ b/clang/lib/CodeGen/SanitizerMetadata.cpp
@@ -28,35 +28,15 @@ void SanitizerMetadata::reportGlobalToASan(llvm::GlobalVariable *GV,
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 @@ void SanitizerMetadata::disableSanitizerForGlobal(llvm::GlobalVariable *GV) {
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);
+}
OpenPOWER on IntegriCloud