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
SanitizerMetadata(CodeGenModule & CGM)22 SanitizerMetadata::SanitizerMetadata(CodeGenModule &CGM) : CGM(CGM) {}
23
reportGlobalToASan(llvm::GlobalVariable * GV,SourceLocation Loc,StringRef Name,QualType Ty,bool IsDynInit,bool IsBlacklisted)24 void SanitizerMetadata::reportGlobalToASan(llvm::GlobalVariable *GV,
25 SourceLocation Loc, StringRef Name,
26 QualType Ty, bool IsDynInit,
27 bool IsBlacklisted) {
28 if (!CGM.getLangOpts().Sanitize.hasOneOf(SanitizerKind::Address |
29 SanitizerKind::KernelAddress))
30 return;
31 IsDynInit &= !CGM.isInSanitizerBlacklist(GV, Loc, Ty, "init");
32 IsBlacklisted |= CGM.isInSanitizerBlacklist(GV, Loc, Ty);
33
34 llvm::Metadata *LocDescr = nullptr;
35 llvm::Metadata *GlobalName = nullptr;
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
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))};
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
reportGlobalToASan(llvm::GlobalVariable * GV,const VarDecl & D,bool IsDynInit)58 void SanitizerMetadata::reportGlobalToASan(llvm::GlobalVariable *GV,
59 const VarDecl &D, bool IsDynInit) {
60 if (!CGM.getLangOpts().Sanitize.hasOneOf(SanitizerKind::Address |
61 SanitizerKind::KernelAddress))
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
disableSanitizerForGlobal(llvm::GlobalVariable * GV)69 void SanitizerMetadata::disableSanitizerForGlobal(llvm::GlobalVariable *GV) {
70 // For now, just make sure the global is not modified by the ASan
71 // instrumentation.
72 if (CGM.getLangOpts().Sanitize.hasOneOf(SanitizerKind::Address |
73 SanitizerKind::KernelAddress))
74 reportGlobalToASan(GV, SourceLocation(), "", QualType(), false, true);
75 }
76
disableSanitizerForInstruction(llvm::Instruction * I)77 void SanitizerMetadata::disableSanitizerForInstruction(llvm::Instruction *I) {
78 I->setMetadata(CGM.getModule().getMDKindID("nosanitize"),
79 llvm::MDNode::get(CGM.getLLVMContext(), None));
80 }
81
getLocationMetadata(SourceLocation Loc)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();
87 llvm::Metadata *LocMetadata[] = {
88 llvm::MDString::get(VMContext, PLoc.getFilename()),
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())),
93 };
94 return llvm::MDNode::get(VMContext, LocMetadata);
95 }
96