blob: 2a2b0ed941968c3ff91e084a46d2f1b5d109925e [file] [log] [blame]
Alexey Samsonov4b8de112014-08-01 21:35:28 +00001//===--- 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 "llvm/ADT/StringRef.h"
16#include "llvm/IR/Constants.h"
17
18using namespace clang;
19using namespace CodeGen;
20
21SanitizerMetadata::SanitizerMetadata(CodeGenModule &CGM) : CGM(CGM) {}
22
23void SanitizerMetadata::reportGlobalToASan(llvm::GlobalVariable *GV,
24 SourceLocation Loc, StringRef Name,
25 bool IsDynInit, bool IsBlacklisted) {
26 if (!CGM.getLangOpts().Sanitize.Address)
27 return;
28 IsDynInit &= !CGM.getSanitizerBlacklist().isIn(*GV, "init");
29 IsBlacklisted |= CGM.getSanitizerBlacklist().isIn(*GV);
30
Alexey Samsonovd9ad5ce2014-08-02 00:35:50 +000031 llvm::Value *LocDescr = nullptr;
32 llvm::Value *GlobalName = nullptr;
Alexey Samsonov4b8de112014-08-01 21:35:28 +000033 llvm::LLVMContext &VMContext = CGM.getLLVMContext();
34 if (!IsBlacklisted) {
35 // Don't generate source location and global name if it is blacklisted -
36 // it won't be instrumented anyway.
Alexey Samsonovd9ad5ce2014-08-02 00:35:50 +000037 LocDescr = getLocationMetadata(Loc);
38 if (!Name.empty())
39 GlobalName = llvm::MDString::get(VMContext, Name);
Alexey Samsonov4b8de112014-08-01 21:35:28 +000040 }
41
42 llvm::Value *GlobalMetadata[] = {
43 GV, LocDescr, GlobalName,
44 llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), IsDynInit),
45 llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), IsBlacklisted)};
46
47 llvm::MDNode *ThisGlobal = llvm::MDNode::get(VMContext, GlobalMetadata);
48 llvm::NamedMDNode *AsanGlobals =
49 CGM.getModule().getOrInsertNamedMetadata("llvm.asan.globals");
50 AsanGlobals->addOperand(ThisGlobal);
51}
52
53void SanitizerMetadata::reportGlobalToASan(llvm::GlobalVariable *GV,
54 const VarDecl &D, bool IsDynInit) {
55 if (!CGM.getLangOpts().Sanitize.Address)
56 return;
57 std::string QualName;
58 llvm::raw_string_ostream OS(QualName);
59 D.printQualifiedName(OS);
60 reportGlobalToASan(GV, D.getLocation(), OS.str(), IsDynInit);
61}
62
63void SanitizerMetadata::disableSanitizerForGlobal(llvm::GlobalVariable *GV) {
64 // For now, just make sure the global is not modified by the ASan
65 // instrumentation.
66 if (CGM.getLangOpts().Sanitize.Address)
67 reportGlobalToASan(GV, SourceLocation(), "", false, true);
68}
Alexey Samsonovd9ad5ce2014-08-02 00:35:50 +000069
Kostya Serebryany4ee69042014-08-26 02:29:59 +000070void SanitizerMetadata::disableSanitizerForInstruction(llvm::Instruction *I) {
71 I->setMetadata(
72 CGM.getModule().getMDKindID("nosanitize"),
73 llvm::MDNode::get(CGM.getLLVMContext(), ArrayRef<llvm::Value *>()));
74}
75
Alexey Samsonovd9ad5ce2014-08-02 00:35:50 +000076llvm::MDNode *SanitizerMetadata::getLocationMetadata(SourceLocation Loc) {
77 PresumedLoc PLoc = CGM.getContext().getSourceManager().getPresumedLoc(Loc);
78 if (!PLoc.isValid())
79 return nullptr;
80 llvm::LLVMContext &VMContext = CGM.getLLVMContext();
81 llvm::Value *LocMetadata[] = {
82 llvm::MDString::get(VMContext, PLoc.getFilename()),
83 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), PLoc.getLine()),
84 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
85 PLoc.getColumn()),
86 };
87 return llvm::MDNode::get(VMContext, LocMetadata);
88}