blob: 2a338bac4b412c352674b812af60368ec11493d0 [file] [log] [blame]
Stephen Hines176edba2014-12-01 14:53:08 -08001//===--- 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
19using namespace clang;
20using namespace CodeGen;
21
22SanitizerMetadata::SanitizerMetadata(CodeGenModule &CGM) : CGM(CGM) {}
23
24void SanitizerMetadata::reportGlobalToASan(llvm::GlobalVariable *GV,
25 SourceLocation Loc, StringRef Name,
26 QualType Ty, bool IsDynInit,
27 bool IsBlacklisted) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -080028 if (!CGM.getLangOpts().Sanitize.hasOneOf(SanitizerKind::Address |
29 SanitizerKind::KernelAddress))
Stephen Hines176edba2014-12-01 14:53:08 -080030 return;
31 IsDynInit &= !CGM.isInSanitizerBlacklist(GV, Loc, Ty, "init");
32 IsBlacklisted |= CGM.isInSanitizerBlacklist(GV, Loc, Ty);
33
Stephen Hines0e2c34f2015-03-23 12:09:02 -070034 llvm::Metadata *LocDescr = nullptr;
35 llvm::Metadata *GlobalName = nullptr;
Stephen Hines176edba2014-12-01 14:53:08 -080036 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
Stephen Hines0e2c34f2015-03-23 12:09:02 -070045 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))};
Stephen Hines176edba2014-12-01 14:53:08 -080051
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
58void SanitizerMetadata::reportGlobalToASan(llvm::GlobalVariable *GV,
59 const VarDecl &D, bool IsDynInit) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -080060 if (!CGM.getLangOpts().Sanitize.hasOneOf(SanitizerKind::Address |
61 SanitizerKind::KernelAddress))
Stephen Hines176edba2014-12-01 14:53:08 -080062 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
69void SanitizerMetadata::disableSanitizerForGlobal(llvm::GlobalVariable *GV) {
70 // For now, just make sure the global is not modified by the ASan
71 // instrumentation.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -080072 if (CGM.getLangOpts().Sanitize.hasOneOf(SanitizerKind::Address |
73 SanitizerKind::KernelAddress))
Stephen Hines176edba2014-12-01 14:53:08 -080074 reportGlobalToASan(GV, SourceLocation(), "", QualType(), false, true);
75}
76
77void SanitizerMetadata::disableSanitizerForInstruction(llvm::Instruction *I) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -070078 I->setMetadata(CGM.getModule().getMDKindID("nosanitize"),
79 llvm::MDNode::get(CGM.getLLVMContext(), None));
Stephen Hines176edba2014-12-01 14:53:08 -080080}
81
82llvm::MDNode *SanitizerMetadata::getLocationMetadata(SourceLocation Loc) {
83 PresumedLoc PLoc = CGM.getContext().getSourceManager().getPresumedLoc(Loc);
84 if (!PLoc.isValid())
85 return nullptr;
86 llvm::LLVMContext &VMContext = CGM.getLLVMContext();
Stephen Hines0e2c34f2015-03-23 12:09:02 -070087 llvm::Metadata *LocMetadata[] = {
Stephen Hines176edba2014-12-01 14:53:08 -080088 llvm::MDString::get(VMContext, PLoc.getFilename()),
Stephen Hines0e2c34f2015-03-23 12:09:02 -070089 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())),
Stephen Hines176edba2014-12-01 14:53:08 -080093 };
94 return llvm::MDNode::get(VMContext, LocMetadata);
95}