blob: dd8c133621595db664d9020d4e4ea16d042e6a60 [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
70llvm::MDNode *SanitizerMetadata::getLocationMetadata(SourceLocation Loc) {
71 PresumedLoc PLoc = CGM.getContext().getSourceManager().getPresumedLoc(Loc);
72 if (!PLoc.isValid())
73 return nullptr;
74 llvm::LLVMContext &VMContext = CGM.getLLVMContext();
75 llvm::Value *LocMetadata[] = {
76 llvm::MDString::get(VMContext, PLoc.getFilename()),
77 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), PLoc.getLine()),
78 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
79 PLoc.getColumn()),
80 };
81 return llvm::MDNode::get(VMContext, LocMetadata);
82}