blob: 46191523c8b2885fccf9867fbed3637cf4b4371f [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
31 llvm::GlobalVariable *LocDescr = nullptr;
32 llvm::GlobalVariable *GlobalName = nullptr;
33 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.
37 PresumedLoc PLoc = CGM.getContext().getSourceManager().getPresumedLoc(Loc);
38 if (PLoc.isValid()) {
39 llvm::Constant *LocData[] = {
40 CGM.GetAddrOfConstantCString(PLoc.getFilename()),
41 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
42 PLoc.getLine()),
43 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
44 PLoc.getColumn()),
45 };
46 auto LocStruct = llvm::ConstantStruct::getAnon(LocData);
47 LocDescr = new llvm::GlobalVariable(
48 CGM.getModule(), LocStruct->getType(), true,
49 llvm::GlobalValue::PrivateLinkage, LocStruct, ".asan_loc_descr");
50 LocDescr->setUnnamedAddr(true);
51 // Add LocDescr to llvm.compiler.used, so that it won't be removed by
52 // the optimizer before the ASan instrumentation pass.
53 CGM.addCompilerUsedGlobal(LocDescr);
54 }
55 if (!Name.empty()) {
56 GlobalName = CGM.GetAddrOfConstantCString(Name);
57 // GlobalName shouldn't be removed by the optimizer.
58 CGM.addCompilerUsedGlobal(GlobalName);
59 }
60 }
61
62 llvm::Value *GlobalMetadata[] = {
63 GV, LocDescr, GlobalName,
64 llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), IsDynInit),
65 llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), IsBlacklisted)};
66
67 llvm::MDNode *ThisGlobal = llvm::MDNode::get(VMContext, GlobalMetadata);
68 llvm::NamedMDNode *AsanGlobals =
69 CGM.getModule().getOrInsertNamedMetadata("llvm.asan.globals");
70 AsanGlobals->addOperand(ThisGlobal);
71}
72
73void SanitizerMetadata::reportGlobalToASan(llvm::GlobalVariable *GV,
74 const VarDecl &D, bool IsDynInit) {
75 if (!CGM.getLangOpts().Sanitize.Address)
76 return;
77 std::string QualName;
78 llvm::raw_string_ostream OS(QualName);
79 D.printQualifiedName(OS);
80 reportGlobalToASan(GV, D.getLocation(), OS.str(), IsDynInit);
81}
82
83void SanitizerMetadata::disableSanitizerForGlobal(llvm::GlobalVariable *GV) {
84 // For now, just make sure the global is not modified by the ASan
85 // instrumentation.
86 if (CGM.getLangOpts().Sanitize.Address)
87 reportGlobalToASan(GV, SourceLocation(), "", false, true);
88}