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