blob: ebc9cd5529bc6f595fe580d6f65c613d135a4567 [file] [log] [blame]
Alexey Samsonov4b8de112014-08-01 21:35:28 +00001//===--- SanitizerMetadata.cpp - Blacklist for sanitizers -----------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Alexey Samsonov4b8de112014-08-01 21:35:28 +00006//
7//===----------------------------------------------------------------------===//
8//
9// Class which emits metadata consumed by sanitizer instrumentation passes.
10//
11//===----------------------------------------------------------------------===//
12#include "SanitizerMetadata.h"
13#include "CodeGenModule.h"
Alexey Samsonova0ac3c22014-10-17 22:37:33 +000014#include "clang/AST/Type.h"
Alexey Samsonov4b8de112014-08-01 21:35:28 +000015#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
Evgeniy Stepanovc5e7f562019-07-15 20:02:23 +000023static bool isAsanHwasanOrMemTag(const SanitizerSet& SS) {
24 return SS.hasOneOf(SanitizerKind::Address | SanitizerKind::KernelAddress |
25 SanitizerKind::HWAddress | SanitizerKind::KernelHWAddress |
26 SanitizerKind::MemTag);
27}
28
Alexey Samsonov4b8de112014-08-01 21:35:28 +000029void SanitizerMetadata::reportGlobalToASan(llvm::GlobalVariable *GV,
30 SourceLocation Loc, StringRef Name,
Alexey Samsonova0ac3c22014-10-17 22:37:33 +000031 QualType Ty, bool IsDynInit,
32 bool IsBlacklisted) {
Evgeniy Stepanovc5e7f562019-07-15 20:02:23 +000033 if (!isAsanHwasanOrMemTag(CGM.getLangOpts().Sanitize))
Alexey Samsonov4b8de112014-08-01 21:35:28 +000034 return;
Alexey Samsonova0ac3c22014-10-17 22:37:33 +000035 IsDynInit &= !CGM.isInSanitizerBlacklist(GV, Loc, Ty, "init");
36 IsBlacklisted |= CGM.isInSanitizerBlacklist(GV, Loc, Ty);
Alexey Samsonov4b8de112014-08-01 21:35:28 +000037
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +000038 llvm::Metadata *LocDescr = nullptr;
39 llvm::Metadata *GlobalName = nullptr;
Alexey Samsonov4b8de112014-08-01 21:35:28 +000040 llvm::LLVMContext &VMContext = CGM.getLLVMContext();
41 if (!IsBlacklisted) {
42 // Don't generate source location and global name if it is blacklisted -
43 // it won't be instrumented anyway.
Alexey Samsonovd9ad5ce2014-08-02 00:35:50 +000044 LocDescr = getLocationMetadata(Loc);
45 if (!Name.empty())
46 GlobalName = llvm::MDString::get(VMContext, Name);
Alexey Samsonov4b8de112014-08-01 21:35:28 +000047 }
48
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +000049 llvm::Metadata *GlobalMetadata[] = {
50 llvm::ConstantAsMetadata::get(GV), LocDescr, GlobalName,
51 llvm::ConstantAsMetadata::get(
52 llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), IsDynInit)),
53 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
54 llvm::Type::getInt1Ty(VMContext), IsBlacklisted))};
Alexey Samsonov4b8de112014-08-01 21:35:28 +000055
56 llvm::MDNode *ThisGlobal = llvm::MDNode::get(VMContext, GlobalMetadata);
57 llvm::NamedMDNode *AsanGlobals =
58 CGM.getModule().getOrInsertNamedMetadata("llvm.asan.globals");
59 AsanGlobals->addOperand(ThisGlobal);
60}
61
62void SanitizerMetadata::reportGlobalToASan(llvm::GlobalVariable *GV,
63 const VarDecl &D, bool IsDynInit) {
Evgeniy Stepanovc5e7f562019-07-15 20:02:23 +000064 if (!isAsanHwasanOrMemTag(CGM.getLangOpts().Sanitize))
Alexey Samsonov4b8de112014-08-01 21:35:28 +000065 return;
66 std::string QualName;
67 llvm::raw_string_ostream OS(QualName);
68 D.printQualifiedName(OS);
Douglas Katzman3ed0f642016-10-14 19:55:09 +000069
70 bool IsBlacklisted = false;
71 for (auto Attr : D.specific_attrs<NoSanitizeAttr>())
72 if (Attr->getMask() & SanitizerKind::Address)
73 IsBlacklisted = true;
74 reportGlobalToASan(GV, D.getLocation(), OS.str(), D.getType(), IsDynInit,
75 IsBlacklisted);
Alexey Samsonov4b8de112014-08-01 21:35:28 +000076}
77
78void SanitizerMetadata::disableSanitizerForGlobal(llvm::GlobalVariable *GV) {
79 // For now, just make sure the global is not modified by the ASan
80 // instrumentation.
Evgeniy Stepanovc5e7f562019-07-15 20:02:23 +000081 if (isAsanHwasanOrMemTag(CGM.getLangOpts().Sanitize))
Alexey Samsonova0ac3c22014-10-17 22:37:33 +000082 reportGlobalToASan(GV, SourceLocation(), "", QualType(), false, true);
Alexey Samsonov4b8de112014-08-01 21:35:28 +000083}
Alexey Samsonovd9ad5ce2014-08-02 00:35:50 +000084
Kostya Serebryany4ee69042014-08-26 02:29:59 +000085void SanitizerMetadata::disableSanitizerForInstruction(llvm::Instruction *I) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +000086 I->setMetadata(CGM.getModule().getMDKindID("nosanitize"),
87 llvm::MDNode::get(CGM.getLLVMContext(), None));
Kostya Serebryany4ee69042014-08-26 02:29:59 +000088}
89
Alexey Samsonovd9ad5ce2014-08-02 00:35:50 +000090llvm::MDNode *SanitizerMetadata::getLocationMetadata(SourceLocation Loc) {
91 PresumedLoc PLoc = CGM.getContext().getSourceManager().getPresumedLoc(Loc);
92 if (!PLoc.isValid())
93 return nullptr;
94 llvm::LLVMContext &VMContext = CGM.getLLVMContext();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +000095 llvm::Metadata *LocMetadata[] = {
Alexey Samsonovd9ad5ce2014-08-02 00:35:50 +000096 llvm::MDString::get(VMContext, PLoc.getFilename()),
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +000097 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
98 llvm::Type::getInt32Ty(VMContext), PLoc.getLine())),
99 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
100 llvm::Type::getInt32Ty(VMContext), PLoc.getColumn())),
Alexey Samsonovd9ad5ce2014-08-02 00:35:50 +0000101 };
102 return llvm::MDNode::get(VMContext, LocMetadata);
103}