clang  11.0.0git
SanitizerMetadata.cpp
Go to the documentation of this file.
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"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/IR/Constants.h"
19 
20 using namespace clang;
21 using namespace CodeGen;
22 
23 SanitizerMetadata::SanitizerMetadata(CodeGenModule &CGM) : CGM(CGM) {}
24 
25 static bool isAsanHwasanOrMemTag(const SanitizerSet& SS) {
26  return SS.hasOneOf(SanitizerKind::Address | SanitizerKind::KernelAddress |
27  SanitizerKind::HWAddress | SanitizerKind::KernelHWAddress |
28  SanitizerKind::MemTag);
29 }
30 
31 void SanitizerMetadata::reportGlobalToASan(llvm::GlobalVariable *GV,
32  SourceLocation Loc, StringRef Name,
33  QualType Ty, bool IsDynInit,
34  bool IsExcluded) {
36  return;
37  IsDynInit &= !CGM.isInSanitizerBlacklist(GV, Loc, Ty, "init");
38  IsExcluded |= CGM.isInSanitizerBlacklist(GV, Loc, Ty);
39 
40  llvm::Metadata *LocDescr = nullptr;
41  llvm::Metadata *GlobalName = nullptr;
42  llvm::LLVMContext &VMContext = CGM.getLLVMContext();
43  if (!IsExcluded) {
44  // Don't generate source location and global name if it is blacklisted -
45  // it won't be instrumented anyway.
46  LocDescr = getLocationMetadata(Loc);
47  if (!Name.empty())
48  GlobalName = llvm::MDString::get(VMContext, Name);
49  }
50 
51  llvm::Metadata *GlobalMetadata[] = {
52  llvm::ConstantAsMetadata::get(GV), LocDescr, GlobalName,
53  llvm::ConstantAsMetadata::get(
54  llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), IsDynInit)),
55  llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
56  llvm::Type::getInt1Ty(VMContext), IsExcluded))};
57 
58  llvm::MDNode *ThisGlobal = llvm::MDNode::get(VMContext, GlobalMetadata);
59  llvm::NamedMDNode *AsanGlobals =
60  CGM.getModule().getOrInsertNamedMetadata("llvm.asan.globals");
61  AsanGlobals->addOperand(ThisGlobal);
62 }
63 
64 void SanitizerMetadata::reportGlobalToASan(llvm::GlobalVariable *GV,
65  const VarDecl &D, bool IsDynInit) {
67  return;
68  std::string QualName;
69  llvm::raw_string_ostream OS(QualName);
70  D.printQualifiedName(OS);
71 
72  bool IsExcluded = false;
73  for (auto Attr : D.specific_attrs<NoSanitizeAttr>())
74  if (Attr->getMask() & SanitizerKind::Address)
75  IsExcluded = true;
76  reportGlobalToASan(GV, D.getLocation(), OS.str(), D.getType(), IsDynInit,
77  IsExcluded);
78 }
79 
80 void SanitizerMetadata::disableSanitizerForGlobal(llvm::GlobalVariable *GV) {
81  // For now, just make sure the global is not modified by the ASan
82  // instrumentation.
84  reportGlobalToASan(GV, SourceLocation(), "", QualType(), false, true);
85 }
86 
88  I->setMetadata(CGM.getModule().getMDKindID("nosanitize"),
89  llvm::MDNode::get(CGM.getLLVMContext(), None));
90 }
91 
92 llvm::MDNode *SanitizerMetadata::getLocationMetadata(SourceLocation Loc) {
94  if (!PLoc.isValid())
95  return nullptr;
96  llvm::LLVMContext &VMContext = CGM.getLLVMContext();
97  llvm::Metadata *LocMetadata[] = {
98  llvm::MDString::get(VMContext, PLoc.getFilename()),
99  llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
100  llvm::Type::getInt32Ty(VMContext), PLoc.getLine())),
101  llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
102  llvm::Type::getInt32Ty(VMContext), PLoc.getColumn())),
103  };
104  return llvm::MDNode::get(VMContext, LocMetadata);
105 }
A (possibly-)qualified type.
Definition: Type.h:655
llvm::LLVMContext & getLLVMContext()
SanitizerSet Sanitize
Set of enabled sanitizers.
Definition: LangOptions.h:231
C Language Family Type Representation.
Defines the SourceManager interface.
Represents a variable declaration or definition.
Definition: Decl.h:820
bool hasOneOf(SanitizerMask K) const
Check if one or more sanitizers are enabled.
Definition: Sanitizers.h:159
void reportGlobalToASan(llvm::GlobalVariable *GV, const VarDecl &D, bool IsDynInit=false)
void disableSanitizerForInstruction(llvm::Instruction *I)
bool isInSanitizerBlacklist(SanitizerMask Kind, llvm::Function *Fn, SourceLocation Loc) const
bool isValid() const
unsigned getLine() const
Return the presumed line number of this location.
Represents an unpacked "presumed" location which can be presented to the user.
void printQualifiedName(raw_ostream &OS) const
Returns a human-readable qualified name for this declaration, like A::B::i, for i being member of nam...
Definition: Decl.cpp:1537
const LangOptions & getLangOpts() const
ASTContext & getContext() const
const char * getFilename() const
Return the presumed filename of this location.
void disableSanitizerForGlobal(llvm::GlobalVariable *GV)
unsigned getColumn() const
Return the presumed column number of this location.
Encodes a location in the source.
static bool isAsanHwasanOrMemTag(const SanitizerSet &SS)
This class organizes the cross-function state that is used while generating LLVM code.
Dataflow Directional Tag Classes.
PresumedLoc getPresumedLoc(SourceLocation Loc, bool UseLineDirectives=true) const
Returns the "presumed" location of a SourceLocation specifies.
llvm::Module & getModule() const
SourceManager & getSourceManager()
Definition: ASTContext.h:619
llvm::iterator_range< specific_attr_iterator< T > > specific_attrs() const
Definition: DeclBase.h:529
QualType getType() const
Definition: Decl.h:630
Attr - This represents one attribute.
Definition: Attr.h:46
SourceLocation getLocation() const
Definition: DeclBase.h:430