1 //===--- SanitizerMetadata.cpp - Blacklist for sanitizers -----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Class which emits metadata consumed by sanitizer instrumentation passes.
10 //
11 //===----------------------------------------------------------------------===//
12 #include "SanitizerMetadata.h"
13 #include "CodeGenModule.h"
14 #include "clang/AST/Attr.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 
isAsanHwasanOrMemTag(const SanitizerSet & SS)24 static bool isAsanHwasanOrMemTag(const SanitizerSet& SS) {
25   return SS.hasOneOf(SanitizerKind::Address | SanitizerKind::KernelAddress |
26                      SanitizerKind::HWAddress | SanitizerKind::KernelHWAddress |
27                      SanitizerKind::MemTag);
28 }
29 
reportGlobalToASan(llvm::GlobalVariable * GV,SourceLocation Loc,StringRef Name,QualType Ty,bool IsDynInit,bool IsBlacklisted)30 void SanitizerMetadata::reportGlobalToASan(llvm::GlobalVariable *GV,
31                                            SourceLocation Loc, StringRef Name,
32                                            QualType Ty, bool IsDynInit,
33                                            bool IsBlacklisted) {
34   if (!isAsanHwasanOrMemTag(CGM.getLangOpts().Sanitize))
35     return;
36   IsDynInit &= !CGM.isInSanitizerBlacklist(GV, Loc, Ty, "init");
37   IsBlacklisted |= CGM.isInSanitizerBlacklist(GV, Loc, Ty);
38 
39   llvm::Metadata *LocDescr = nullptr;
40   llvm::Metadata *GlobalName = nullptr;
41   llvm::LLVMContext &VMContext = CGM.getLLVMContext();
42   if (!IsBlacklisted) {
43     // Don't generate source location and global name if it is blacklisted -
44     // it won't be instrumented anyway.
45     LocDescr = getLocationMetadata(Loc);
46     if (!Name.empty())
47       GlobalName = llvm::MDString::get(VMContext, Name);
48   }
49 
50   llvm::Metadata *GlobalMetadata[] = {
51       llvm::ConstantAsMetadata::get(GV), LocDescr, GlobalName,
52       llvm::ConstantAsMetadata::get(
53           llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), IsDynInit)),
54       llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
55           llvm::Type::getInt1Ty(VMContext), IsBlacklisted))};
56 
57   llvm::MDNode *ThisGlobal = llvm::MDNode::get(VMContext, GlobalMetadata);
58   llvm::NamedMDNode *AsanGlobals =
59       CGM.getModule().getOrInsertNamedMetadata("llvm.asan.globals");
60   AsanGlobals->addOperand(ThisGlobal);
61 }
62 
reportGlobalToASan(llvm::GlobalVariable * GV,const VarDecl & D,bool IsDynInit)63 void SanitizerMetadata::reportGlobalToASan(llvm::GlobalVariable *GV,
64                                            const VarDecl &D, bool IsDynInit) {
65   if (!isAsanHwasanOrMemTag(CGM.getLangOpts().Sanitize))
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 (isAsanHwasanOrMemTag(CGM.getLangOpts().Sanitize))
83     reportGlobalToASan(GV, SourceLocation(), "", QualType(), false, true);
84 }
85 
disableSanitizerForInstruction(llvm::Instruction * I)86 void SanitizerMetadata::disableSanitizerForInstruction(llvm::Instruction *I) {
87   I->setMetadata(CGM.getModule().getMDKindID("nosanitize"),
88                  llvm::MDNode::get(CGM.getLLVMContext(), None));
89 }
90 
getLocationMetadata(SourceLocation Loc)91 llvm::MDNode *SanitizerMetadata::getLocationMetadata(SourceLocation Loc) {
92   PresumedLoc PLoc = CGM.getContext().getSourceManager().getPresumedLoc(Loc);
93   if (!PLoc.isValid())
94     return nullptr;
95   llvm::LLVMContext &VMContext = CGM.getLLVMContext();
96   llvm::Metadata *LocMetadata[] = {
97       llvm::MDString::get(VMContext, PLoc.getFilename()),
98       llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
99           llvm::Type::getInt32Ty(VMContext), PLoc.getLine())),
100       llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
101           llvm::Type::getInt32Ty(VMContext), PLoc.getColumn())),
102   };
103   return llvm::MDNode::get(VMContext, LocMetadata);
104 }
105