blob: 86269b5513f34ca24ca534447c367a1e77110d88 [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"
Reid Kleckner98031782019-12-09 16:11:56 -080014#include "clang/AST/Attr.h"
Alexey Samsonova0ac3c22014-10-17 22:37:33 +000015#include "clang/AST/Type.h"
Reid Kleckner86565c12020-02-27 11:01:58 -080016#include "clang/Basic/SourceManager.h"
Alexey Samsonov4b8de112014-08-01 21:35:28 +000017#include "llvm/ADT/StringRef.h"
18#include "llvm/IR/Constants.h"
19
20using namespace clang;
21using namespace CodeGen;
22
23SanitizerMetadata::SanitizerMetadata(CodeGenModule &CGM) : CGM(CGM) {}
24
Evgeniy Stepanovc5e7f562019-07-15 20:02:23 +000025static bool isAsanHwasanOrMemTag(const SanitizerSet& SS) {
26 return SS.hasOneOf(SanitizerKind::Address | SanitizerKind::KernelAddress |
27 SanitizerKind::HWAddress | SanitizerKind::KernelHWAddress |
28 SanitizerKind::MemTag);
29}
30
Alexey Samsonov4b8de112014-08-01 21:35:28 +000031void SanitizerMetadata::reportGlobalToASan(llvm::GlobalVariable *GV,
32 SourceLocation Loc, StringRef Name,
Alexey Samsonova0ac3c22014-10-17 22:37:33 +000033 QualType Ty, bool IsDynInit,
34 bool IsBlacklisted) {
Evgeniy Stepanovc5e7f562019-07-15 20:02:23 +000035 if (!isAsanHwasanOrMemTag(CGM.getLangOpts().Sanitize))
Alexey Samsonov4b8de112014-08-01 21:35:28 +000036 return;
Alexey Samsonova0ac3c22014-10-17 22:37:33 +000037 IsDynInit &= !CGM.isInSanitizerBlacklist(GV, Loc, Ty, "init");
38 IsBlacklisted |= CGM.isInSanitizerBlacklist(GV, Loc, Ty);
Alexey Samsonov4b8de112014-08-01 21:35:28 +000039
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +000040 llvm::Metadata *LocDescr = nullptr;
41 llvm::Metadata *GlobalName = nullptr;
Alexey Samsonov4b8de112014-08-01 21:35:28 +000042 llvm::LLVMContext &VMContext = CGM.getLLVMContext();
43 if (!IsBlacklisted) {
44 // Don't generate source location and global name if it is blacklisted -
45 // it won't be instrumented anyway.
Alexey Samsonovd9ad5ce2014-08-02 00:35:50 +000046 LocDescr = getLocationMetadata(Loc);
47 if (!Name.empty())
48 GlobalName = llvm::MDString::get(VMContext, Name);
Alexey Samsonov4b8de112014-08-01 21:35:28 +000049 }
50
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +000051 llvm::Metadata *GlobalMetadata[] = {
52 llvm::ConstantAsMetadata::get(GV), LocDescr, GlobalName,
53 llvm::ConstantAsMetadata::get(
54 llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), IsDynInit)),
55 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
56 llvm::Type::getInt1Ty(VMContext), IsBlacklisted))};
Alexey Samsonov4b8de112014-08-01 21:35:28 +000057
58 llvm::MDNode *ThisGlobal = llvm::MDNode::get(VMContext, GlobalMetadata);
59 llvm::NamedMDNode *AsanGlobals =
60 CGM.getModule().getOrInsertNamedMetadata("llvm.asan.globals");
61 AsanGlobals->addOperand(ThisGlobal);
62}
63
64void SanitizerMetadata::reportGlobalToASan(llvm::GlobalVariable *GV,
65 const VarDecl &D, bool IsDynInit) {
Evgeniy Stepanovc5e7f562019-07-15 20:02:23 +000066 if (!isAsanHwasanOrMemTag(CGM.getLangOpts().Sanitize))
Alexey Samsonov4b8de112014-08-01 21:35:28 +000067 return;
68 std::string QualName;
69 llvm::raw_string_ostream OS(QualName);
70 D.printQualifiedName(OS);
Douglas Katzman3ed0f642016-10-14 19:55:09 +000071
72 bool IsBlacklisted = false;
73 for (auto Attr : D.specific_attrs<NoSanitizeAttr>())
74 if (Attr->getMask() & SanitizerKind::Address)
75 IsBlacklisted = true;
76 reportGlobalToASan(GV, D.getLocation(), OS.str(), D.getType(), IsDynInit,
77 IsBlacklisted);
Alexey Samsonov4b8de112014-08-01 21:35:28 +000078}
79
80void SanitizerMetadata::disableSanitizerForGlobal(llvm::GlobalVariable *GV) {
81 // For now, just make sure the global is not modified by the ASan
82 // instrumentation.
Evgeniy Stepanovc5e7f562019-07-15 20:02:23 +000083 if (isAsanHwasanOrMemTag(CGM.getLangOpts().Sanitize))
Alexey Samsonova0ac3c22014-10-17 22:37:33 +000084 reportGlobalToASan(GV, SourceLocation(), "", QualType(), false, true);
Alexey Samsonov4b8de112014-08-01 21:35:28 +000085}
Alexey Samsonovd9ad5ce2014-08-02 00:35:50 +000086
Kostya Serebryany4ee69042014-08-26 02:29:59 +000087void SanitizerMetadata::disableSanitizerForInstruction(llvm::Instruction *I) {
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +000088 I->setMetadata(CGM.getModule().getMDKindID("nosanitize"),
89 llvm::MDNode::get(CGM.getLLVMContext(), None));
Kostya Serebryany4ee69042014-08-26 02:29:59 +000090}
91
Alexey Samsonovd9ad5ce2014-08-02 00:35:50 +000092llvm::MDNode *SanitizerMetadata::getLocationMetadata(SourceLocation Loc) {
93 PresumedLoc PLoc = CGM.getContext().getSourceManager().getPresumedLoc(Loc);
94 if (!PLoc.isValid())
95 return nullptr;
96 llvm::LLVMContext &VMContext = CGM.getLLVMContext();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +000097 llvm::Metadata *LocMetadata[] = {
Alexey Samsonovd9ad5ce2014-08-02 00:35:50 +000098 llvm::MDString::get(VMContext, PLoc.getFilename()),
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +000099 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
100 llvm::Type::getInt32Ty(VMContext), PLoc.getLine())),
101 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
102 llvm::Type::getInt32Ty(VMContext), PLoc.getColumn())),
Alexey Samsonovd9ad5ce2014-08-02 00:35:50 +0000103 };
104 return llvm::MDNode::get(VMContext, LocMetadata);
105}