clang  8.0.0svn
SanitizerMetadata.cpp
Go to the documentation of this file.
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 
22 SanitizerMetadata::SanitizerMetadata(CodeGenModule &CGM) : CGM(CGM) {}
23 
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 
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 
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 
90  I->setMetadata(CGM.getModule().getMDKindID("nosanitize"),
91  llvm::MDNode::get(CGM.getLLVMContext(), None));
92 }
93 
94 llvm::MDNode *SanitizerMetadata::getLocationMetadata(SourceLocation 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 }
A (possibly-)qualified type.
Definition: Type.h:642
llvm::LLVMContext & getLLVMContext()
SanitizerSet Sanitize
Set of enabled sanitizers.
Definition: LangOptions.h:176
C Language Family Type Representation.
Represents a variable declaration or definition.
Definition: Decl.h:812
bool hasOneOf(SanitizerMask K) const
Check if one or more sanitizers are enabled.
Definition: Sanitizers.h:58
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:1527
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.
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:661
llvm::iterator_range< specific_attr_iterator< T > > specific_attrs() const
Definition: DeclBase.h:513
QualType getType() const
Definition: Decl.h:647
Attr - This represents one attribute.
Definition: Attr.h:43
SourceLocation getLocation() const
Definition: DeclBase.h:418