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