Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1 | //===--- SanitizerMetadata.cpp - Blacklist for sanitizers -----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Class which emits metadata consumed by sanitizer instrumentation passes. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | #include "SanitizerMetadata.h" |
| 14 | #include "CodeGenModule.h" |
| 15 | #include "clang/AST/Type.h" |
| 16 | #include "llvm/ADT/StringRef.h" |
| 17 | #include "llvm/IR/Constants.h" |
| 18 | |
| 19 | using namespace clang; |
| 20 | using namespace CodeGen; |
| 21 | |
| 22 | SanitizerMetadata::SanitizerMetadata(CodeGenModule &CGM) : CGM(CGM) {} |
| 23 | |
| 24 | void SanitizerMetadata::reportGlobalToASan(llvm::GlobalVariable *GV, |
| 25 | SourceLocation Loc, StringRef Name, |
| 26 | QualType Ty, bool IsDynInit, |
| 27 | bool IsBlacklisted) { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 28 | if (!CGM.getLangOpts().Sanitize.hasOneOf(SanitizerKind::Address | |
| 29 | SanitizerKind::KernelAddress)) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 30 | return; |
| 31 | IsDynInit &= !CGM.isInSanitizerBlacklist(GV, Loc, Ty, "init"); |
| 32 | IsBlacklisted |= CGM.isInSanitizerBlacklist(GV, Loc, Ty); |
| 33 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 34 | llvm::Metadata *LocDescr = nullptr; |
| 35 | llvm::Metadata *GlobalName = nullptr; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 36 | llvm::LLVMContext &VMContext = CGM.getLLVMContext(); |
| 37 | if (!IsBlacklisted) { |
| 38 | // Don't generate source location and global name if it is blacklisted - |
| 39 | // it won't be instrumented anyway. |
| 40 | LocDescr = getLocationMetadata(Loc); |
| 41 | if (!Name.empty()) |
| 42 | GlobalName = llvm::MDString::get(VMContext, Name); |
| 43 | } |
| 44 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 45 | llvm::Metadata *GlobalMetadata[] = { |
| 46 | llvm::ConstantAsMetadata::get(GV), LocDescr, GlobalName, |
| 47 | llvm::ConstantAsMetadata::get( |
| 48 | llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), IsDynInit)), |
| 49 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
| 50 | llvm::Type::getInt1Ty(VMContext), IsBlacklisted))}; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 51 | |
| 52 | llvm::MDNode *ThisGlobal = llvm::MDNode::get(VMContext, GlobalMetadata); |
| 53 | llvm::NamedMDNode *AsanGlobals = |
| 54 | CGM.getModule().getOrInsertNamedMetadata("llvm.asan.globals"); |
| 55 | AsanGlobals->addOperand(ThisGlobal); |
| 56 | } |
| 57 | |
| 58 | void SanitizerMetadata::reportGlobalToASan(llvm::GlobalVariable *GV, |
| 59 | const VarDecl &D, bool IsDynInit) { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 60 | if (!CGM.getLangOpts().Sanitize.hasOneOf(SanitizerKind::Address | |
| 61 | SanitizerKind::KernelAddress)) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 62 | return; |
| 63 | std::string QualName; |
| 64 | llvm::raw_string_ostream OS(QualName); |
| 65 | D.printQualifiedName(OS); |
| 66 | reportGlobalToASan(GV, D.getLocation(), OS.str(), D.getType(), IsDynInit); |
| 67 | } |
| 68 | |
| 69 | void SanitizerMetadata::disableSanitizerForGlobal(llvm::GlobalVariable *GV) { |
| 70 | // For now, just make sure the global is not modified by the ASan |
| 71 | // instrumentation. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 72 | if (CGM.getLangOpts().Sanitize.hasOneOf(SanitizerKind::Address | |
| 73 | SanitizerKind::KernelAddress)) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 74 | reportGlobalToASan(GV, SourceLocation(), "", QualType(), false, true); |
| 75 | } |
| 76 | |
| 77 | void SanitizerMetadata::disableSanitizerForInstruction(llvm::Instruction *I) { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 78 | I->setMetadata(CGM.getModule().getMDKindID("nosanitize"), |
| 79 | llvm::MDNode::get(CGM.getLLVMContext(), None)); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | llvm::MDNode *SanitizerMetadata::getLocationMetadata(SourceLocation Loc) { |
| 83 | PresumedLoc PLoc = CGM.getContext().getSourceManager().getPresumedLoc(Loc); |
| 84 | if (!PLoc.isValid()) |
| 85 | return nullptr; |
| 86 | llvm::LLVMContext &VMContext = CGM.getLLVMContext(); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 87 | llvm::Metadata *LocMetadata[] = { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 88 | llvm::MDString::get(VMContext, PLoc.getFilename()), |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 89 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
| 90 | llvm::Type::getInt32Ty(VMContext), PLoc.getLine())), |
| 91 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
| 92 | llvm::Type::getInt32Ty(VMContext), PLoc.getColumn())), |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 93 | }; |
| 94 | return llvm::MDNode::get(VMContext, LocMetadata); |
| 95 | } |