blob: 9848e3e452f4b04b4f4333dcbc60028d6687be7f [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"
Alexey Samsonova0ac3c22014-10-17 22:37:33 +000015#include "clang/AST/Type.h"
Alexey Samsonov4b8de112014-08-01 21:35:28 +000016#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,
Alexey Samsonova0ac3c22014-10-17 22:37:33 +000026 QualType Ty, bool IsDynInit,
27 bool IsBlacklisted) {
Alexander Potapenkob9b73ef2015-06-19 12:19:07 +000028 if (!CGM.getLangOpts().Sanitize.hasOneOf(SanitizerKind::Address |
29 SanitizerKind::KernelAddress))
Alexey Samsonov4b8de112014-08-01 21:35:28 +000030 return;
Alexey Samsonova0ac3c22014-10-17 22:37:33 +000031 IsDynInit &= !CGM.isInSanitizerBlacklist(GV, Loc, Ty, "init");
32 IsBlacklisted |= CGM.isInSanitizerBlacklist(GV, Loc, Ty);
Alexey Samsonov4b8de112014-08-01 21:35:28 +000033
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +000034 llvm::Metadata *LocDescr = nullptr;
35 llvm::Metadata *GlobalName = nullptr;
Alexey Samsonov4b8de112014-08-01 21:35:28 +000036 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.
Alexey Samsonovd9ad5ce2014-08-02 00:35:50 +000040 LocDescr = getLocationMetadata(Loc);
41 if (!Name.empty())
42 GlobalName = llvm::MDString::get(VMContext, Name);
Alexey Samsonov4b8de112014-08-01 21:35:28 +000043 }
44
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +000045 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))};
Alexey Samsonov4b8de112014-08-01 21:35:28 +000051
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) {
Alexander Potapenkob9b73ef2015-06-19 12:19:07 +000060 if (!CGM.getLangOpts().Sanitize.hasOneOf(SanitizerKind::Address |
61 SanitizerKind::KernelAddress))
Alexey Samsonov4b8de112014-08-01 21:35:28 +000062 return;
63 std::string QualName;
64 llvm::raw_string_ostream OS(QualName);
65 D.printQualifiedName(OS);
Douglas Katzman3ed0f642016-10-14 19:55:09 +000066
67 bool IsBlacklisted = false;
68 for (auto Attr : D.specific_attrs<NoSanitizeAttr>())
69 if (Attr->getMask() & SanitizerKind::Address)
70 IsBlacklisted = true;
71 reportGlobalToASan(GV, D.getLocation(), OS.str(), D.getType(), IsDynInit,
72 IsBlacklisted);
Alexey Samsonov4b8de112014-08-01 21:35:28 +000073}
74
75void SanitizerMetadata::disableSanitizerForGlobal(llvm::GlobalVariable *GV) {
76 // For now, just make sure the global is not modified by the ASan
77 // instrumentation.
Alexander Potapenkob9b73ef2015-06-19 12:19:07 +000078 if (CGM.getLangOpts().Sanitize.hasOneOf(SanitizerKind::Address |
79 SanitizerKind::KernelAddress))
Alexey Samsonova0ac3c22014-10-17 22:37:33 +000080 reportGlobalToASan(GV, SourceLocation(), "", QualType(), false, true);
Alexey Samsonov4b8de112014-08-01 21:35:28 +000081}
Alexey Samsonovd9ad5ce2014-08-02 00:35:50 +000082
Kostya Serebryany4ee69042014-08-26 02:29:59 +000083void SanitizerMetadata::disableSanitizerForInstruction(llvm::Instruction *I) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +000084 I->setMetadata(CGM.getModule().getMDKindID("nosanitize"),
85 llvm::MDNode::get(CGM.getLLVMContext(), None));
Kostya Serebryany4ee69042014-08-26 02:29:59 +000086}
87
Alexey Samsonovd9ad5ce2014-08-02 00:35:50 +000088llvm::MDNode *SanitizerMetadata::getLocationMetadata(SourceLocation Loc) {
89 PresumedLoc PLoc = CGM.getContext().getSourceManager().getPresumedLoc(Loc);
90 if (!PLoc.isValid())
91 return nullptr;
92 llvm::LLVMContext &VMContext = CGM.getLLVMContext();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +000093 llvm::Metadata *LocMetadata[] = {
Alexey Samsonovd9ad5ce2014-08-02 00:35:50 +000094 llvm::MDString::get(VMContext, PLoc.getFilename()),
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +000095 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
96 llvm::Type::getInt32Ty(VMContext), PLoc.getLine())),
97 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
98 llvm::Type::getInt32Ty(VMContext), PLoc.getColumn())),
Alexey Samsonovd9ad5ce2014-08-02 00:35:50 +000099 };
100 return llvm::MDNode::get(VMContext, LocMetadata);
101}