clang  3.7.0
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  return;
31  IsDynInit &= !CGM.isInSanitizerBlacklist(GV, Loc, Ty, "init");
32  IsBlacklisted |= CGM.isInSanitizerBlacklist(GV, Loc, Ty);
33 
34  llvm::Metadata *LocDescr = nullptr;
35  llvm::Metadata *GlobalName = nullptr;
36  llvm::LLVMContext &VMContext = CGM.getLLVMContext();
37  if (!IsBlacklisted) {
38  // Don't generate source location and global name if it is blacklisted -
39  // it won't be instrumented anyway.
40  LocDescr = getLocationMetadata(Loc);
41  if (!Name.empty())
42  GlobalName = llvm::MDString::get(VMContext, Name);
43  }
44 
45  llvm::Metadata *GlobalMetadata[] = {
46  llvm::ConstantAsMetadata::get(GV), LocDescr, GlobalName,
47  llvm::ConstantAsMetadata::get(
48  llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), IsDynInit)),
49  llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
50  llvm::Type::getInt1Ty(VMContext), IsBlacklisted))};
51 
52  llvm::MDNode *ThisGlobal = llvm::MDNode::get(VMContext, GlobalMetadata);
53  llvm::NamedMDNode *AsanGlobals =
54  CGM.getModule().getOrInsertNamedMetadata("llvm.asan.globals");
55  AsanGlobals->addOperand(ThisGlobal);
56 }
57 
58 void SanitizerMetadata::reportGlobalToASan(llvm::GlobalVariable *GV,
59  const VarDecl &D, bool IsDynInit) {
60  if (!CGM.getLangOpts().Sanitize.hasOneOf(SanitizerKind::Address |
61  SanitizerKind::KernelAddress))
62  return;
63  std::string QualName;
64  llvm::raw_string_ostream OS(QualName);
65  D.printQualifiedName(OS);
66  reportGlobalToASan(GV, D.getLocation(), OS.str(), D.getType(), IsDynInit);
67 }
68 
69 void SanitizerMetadata::disableSanitizerForGlobal(llvm::GlobalVariable *GV) {
70  // For now, just make sure the global is not modified by the ASan
71  // instrumentation.
72  if (CGM.getLangOpts().Sanitize.hasOneOf(SanitizerKind::Address |
73  SanitizerKind::KernelAddress))
74  reportGlobalToASan(GV, SourceLocation(), "", QualType(), false, true);
75 }
76 
78  I->setMetadata(CGM.getModule().getMDKindID("nosanitize"),
79  llvm::MDNode::get(CGM.getLLVMContext(), None));
80 }
81 
82 llvm::MDNode *SanitizerMetadata::getLocationMetadata(SourceLocation Loc) {
84  if (!PLoc.isValid())
85  return nullptr;
86  llvm::LLVMContext &VMContext = CGM.getLLVMContext();
87  llvm::Metadata *LocMetadata[] = {
88  llvm::MDString::get(VMContext, PLoc.getFilename()),
89  llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
90  llvm::Type::getInt32Ty(VMContext), PLoc.getLine())),
91  llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
92  llvm::Type::getInt32Ty(VMContext), PLoc.getColumn())),
93  };
94  return llvm::MDNode::get(VMContext, LocMetadata);
95 }
unsigned getColumn() const
Return the presumed column number of this location.
bool isValid() const
llvm::Module & getModule() const
llvm::LLVMContext & getLLVMContext()
SanitizerSet Sanitize
Set of enabled sanitizers.
Definition: LangOptions.h:79
void reportGlobalToASan(llvm::GlobalVariable *GV, const VarDecl &D, bool IsDynInit=false)
void disableSanitizerForInstruction(llvm::Instruction *I)
unsigned getLine() const
Return the presumed line number of this location.
QualType getType() const
Definition: Decl.h:538
ASTContext & getContext() const
Represents an unpacked "presumed" location which can be presented to the user.
void disableSanitizerForGlobal(llvm::GlobalVariable *GV)
bool hasOneOf(SanitizerMask K) const
Check if one or more sanitizers are enabled.
Definition: Sanitizers.h:58
const char * getFilename() const
Return the presumed filename of this location.
Encodes a location in the source. The SourceManager can decode this to get at the full include stack...
const LangOptions & getLangOpts() const
void printQualifiedName(raw_ostream &OS) const
Definition: Decl.cpp:1367
bool isInSanitizerBlacklist(llvm::Function *Fn, SourceLocation Loc) const
SourceManager & getSourceManager()
Definition: ASTContext.h:494
SourceLocation getLocation() const
Definition: DeclBase.h:372
PresumedLoc getPresumedLoc(SourceLocation Loc, bool UseLineDirectives=true) const
Returns the "presumed" location of a SourceLocation specifies.