clang  6.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  return;
32  IsDynInit &= !CGM.isInSanitizerBlacklist(GV, Loc, Ty, "init");
33  IsBlacklisted |= CGM.isInSanitizerBlacklist(GV, Loc, Ty);
34 
35  llvm::Metadata *LocDescr = nullptr;
36  llvm::Metadata *GlobalName = nullptr;
37  llvm::LLVMContext &VMContext = CGM.getLLVMContext();
38  if (!IsBlacklisted) {
39  // Don't generate source location and global name if it is blacklisted -
40  // it won't be instrumented anyway.
41  LocDescr = getLocationMetadata(Loc);
42  if (!Name.empty())
43  GlobalName = llvm::MDString::get(VMContext, Name);
44  }
45 
46  llvm::Metadata *GlobalMetadata[] = {
47  llvm::ConstantAsMetadata::get(GV), LocDescr, GlobalName,
48  llvm::ConstantAsMetadata::get(
49  llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), IsDynInit)),
50  llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
51  llvm::Type::getInt1Ty(VMContext), IsBlacklisted))};
52 
53  llvm::MDNode *ThisGlobal = llvm::MDNode::get(VMContext, GlobalMetadata);
54  llvm::NamedMDNode *AsanGlobals =
55  CGM.getModule().getOrInsertNamedMetadata("llvm.asan.globals");
56  AsanGlobals->addOperand(ThisGlobal);
57 }
58 
59 void SanitizerMetadata::reportGlobalToASan(llvm::GlobalVariable *GV,
60  const VarDecl &D, bool IsDynInit) {
61  if (!CGM.getLangOpts().Sanitize.hasOneOf(SanitizerKind::Address |
62  SanitizerKind::KernelAddress |
63  SanitizerKind::HWAddress))
64  return;
65  std::string QualName;
66  llvm::raw_string_ostream OS(QualName);
67  D.printQualifiedName(OS);
68 
69  bool IsBlacklisted = false;
70  for (auto Attr : D.specific_attrs<NoSanitizeAttr>())
71  if (Attr->getMask() & SanitizerKind::Address)
72  IsBlacklisted = true;
73  reportGlobalToASan(GV, D.getLocation(), OS.str(), D.getType(), IsDynInit,
74  IsBlacklisted);
75 }
76 
77 void SanitizerMetadata::disableSanitizerForGlobal(llvm::GlobalVariable *GV) {
78  // For now, just make sure the global is not modified by the ASan
79  // instrumentation.
80  if (CGM.getLangOpts().Sanitize.hasOneOf(SanitizerKind::Address |
81  SanitizerKind::KernelAddress |
82  SanitizerKind::HWAddress))
83  reportGlobalToASan(GV, SourceLocation(), "", QualType(), false, true);
84 }
85 
87  I->setMetadata(CGM.getModule().getMDKindID("nosanitize"),
88  llvm::MDNode::get(CGM.getLLVMContext(), None));
89 }
90 
91 llvm::MDNode *SanitizerMetadata::getLocationMetadata(SourceLocation 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 }
A (possibly-)qualified type.
Definition: Type.h:653
llvm::LLVMContext & getLLVMContext()
SanitizerSet Sanitize
Set of enabled sanitizers.
Definition: LangOptions.h:101
C Language Family Type Representation.
VarDecl - An instance of this class is created to represent a variable declaration or definition...
Definition: Decl.h:807
bool hasOneOf(SanitizerMask K) const
Check if one or more sanitizers are enabled.
Definition: Sanitizers.h:56
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
printQualifiedName - Returns human-readable qualified name for declaration, like A::B::i, for i being member of namespace A::B.
Definition: Decl.cpp:1478
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:643
llvm::iterator_range< specific_attr_iterator< T > > specific_attrs() const
Definition: DeclBase.h:517
QualType getType() const
Definition: Decl.h:639
Attr - This represents one attribute.
Definition: Attr.h:43
SourceLocation getLocation() const
Definition: DeclBase.h:416