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                                            SanitizerKind::HWAddress |
31                                            SanitizerKind::KernelHWAddress))
32     return;
33   IsDynInit &= !CGM.isInSanitizerBlacklist(GV, Loc, Ty, "init");
34   IsBlacklisted |= CGM.isInSanitizerBlacklist(GV, Loc, Ty);
35 
36   llvm::Metadata *LocDescr = nullptr;
37   llvm::Metadata *GlobalName = nullptr;
38   llvm::LLVMContext &VMContext = CGM.getLLVMContext();
39   if (!IsBlacklisted) {
40     // Don't generate source location and global name if it is blacklisted -
41     // it won't be instrumented anyway.
42     LocDescr = getLocationMetadata(Loc);
43     if (!Name.empty())
44       GlobalName = llvm::MDString::get(VMContext, Name);
45   }
46 
47   llvm::Metadata *GlobalMetadata[] = {
48       llvm::ConstantAsMetadata::get(GV), LocDescr, GlobalName,
49       llvm::ConstantAsMetadata::get(
50           llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), IsDynInit)),
51       llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
52           llvm::Type::getInt1Ty(VMContext), IsBlacklisted))};
53 
54   llvm::MDNode *ThisGlobal = llvm::MDNode::get(VMContext, GlobalMetadata);
55   llvm::NamedMDNode *AsanGlobals =
56       CGM.getModule().getOrInsertNamedMetadata("llvm.asan.globals");
57   AsanGlobals->addOperand(ThisGlobal);
58 }
59 
reportGlobalToASan(llvm::GlobalVariable * GV,const VarDecl & D,bool IsDynInit)60 void SanitizerMetadata::reportGlobalToASan(llvm::GlobalVariable *GV,
61                                            const VarDecl &D, bool IsDynInit) {
62   if (!CGM.getLangOpts().Sanitize.hasOneOf(SanitizerKind::Address |
63                                            SanitizerKind::KernelAddress |
64                                            SanitizerKind::HWAddress |
65                                            SanitizerKind::KernelHWAddress))
66     return;
67   std::string QualName;
68   llvm::raw_string_ostream OS(QualName);
69   D.printQualifiedName(OS);
70 
71   bool IsBlacklisted = false;
72   for (auto Attr : D.specific_attrs<NoSanitizeAttr>())
73     if (Attr->getMask() & SanitizerKind::Address)
74       IsBlacklisted = true;
75   reportGlobalToASan(GV, D.getLocation(), OS.str(), D.getType(), IsDynInit,
76                      IsBlacklisted);
77 }
78 
disableSanitizerForGlobal(llvm::GlobalVariable * GV)79 void SanitizerMetadata::disableSanitizerForGlobal(llvm::GlobalVariable *GV) {
80   // For now, just make sure the global is not modified by the ASan
81   // instrumentation.
82   if (CGM.getLangOpts().Sanitize.hasOneOf(SanitizerKind::Address |
83                                           SanitizerKind::KernelAddress |
84                                           SanitizerKind::HWAddress |
85                                           SanitizerKind::KernelHWAddress))
86     reportGlobalToASan(GV, SourceLocation(), "", QualType(), false, true);
87 }
88 
disableSanitizerForInstruction(llvm::Instruction * I)89 void SanitizerMetadata::disableSanitizerForInstruction(llvm::Instruction *I) {
90   I->setMetadata(CGM.getModule().getMDKindID("nosanitize"),
91                  llvm::MDNode::get(CGM.getLLVMContext(), None));
92 }
93 
getLocationMetadata(SourceLocation Loc)94 llvm::MDNode *SanitizerMetadata::getLocationMetadata(SourceLocation Loc) {
95   PresumedLoc PLoc = CGM.getContext().getSourceManager().getPresumedLoc(Loc);
96   if (!PLoc.isValid())
97     return nullptr;
98   llvm::LLVMContext &VMContext = CGM.getLLVMContext();
99   llvm::Metadata *LocMetadata[] = {
100       llvm::MDString::get(VMContext, PLoc.getFilename()),
101       llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
102           llvm::Type::getInt32Ty(VMContext), PLoc.getLine())),
103       llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
104           llvm::Type::getInt32Ty(VMContext), PLoc.getColumn())),
105   };
106   return llvm::MDNode::get(VMContext, LocMetadata);
107 }
108