Coverage Report

Created: 2020-03-31 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/CodeGen/SanitizerMetadata.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- SanitizerMetadata.cpp - Blacklist for sanitizers -----------------===//
2
//
3
// 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
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// Class which emits metadata consumed by sanitizer instrumentation passes.
10
//
11
//===----------------------------------------------------------------------===//
12
#include "SanitizerMetadata.h"
13
#include "CodeGenModule.h"
14
#include "clang/AST/Attr.h"
15
#include "clang/AST/Type.h"
16
#include "clang/Basic/SourceManager.h"
17
#include "llvm/ADT/StringRef.h"
18
#include "llvm/IR/Constants.h"
19
20
using namespace clang;
21
using namespace CodeGen;
22
23
26.1k
SanitizerMetadata::SanitizerMetadata(CodeGenModule &CGM) : CGM(CGM) {}
24
25
72.5k
static bool isAsanHwasanOrMemTag(const SanitizerSet& SS) {
26
72.5k
  return SS.hasOneOf(SanitizerKind::Address | SanitizerKind::KernelAddress |
27
72.5k
                     SanitizerKind::HWAddress | SanitizerKind::KernelHWAddress |
28
72.5k
                     SanitizerKind::MemTag);
29
72.5k
}
30
31
void SanitizerMetadata::reportGlobalToASan(llvm::GlobalVariable *GV,
32
                                           SourceLocation Loc, StringRef Name,
33
                                           QualType Ty, bool IsDynInit,
34
39.8k
                                           bool IsBlacklisted) {
35
39.8k
  if (!isAsanHwasanOrMemTag(CGM.getLangOpts().Sanitize))
36
39.6k
    return;
37
176
  IsDynInit &= !CGM.isInSanitizerBlacklist(GV, Loc, Ty, "init");
38
176
  IsBlacklisted |= CGM.isInSanitizerBlacklist(GV, Loc, Ty);
39
176
40
176
  llvm::Metadata *LocDescr = nullptr;
41
176
  llvm::Metadata *GlobalName = nullptr;
42
176
  llvm::LLVMContext &VMContext = CGM.getLLVMContext();
43
176
  if (!IsBlacklisted) {
44
136
    // Don't generate source location and global name if it is blacklisted -
45
136
    // it won't be instrumented anyway.
46
136
    LocDescr = getLocationMetadata(Loc);
47
136
    if (!Name.empty())
48
136
      GlobalName = llvm::MDString::get(VMContext, Name);
49
136
  }
50
176
51
176
  llvm::Metadata *GlobalMetadata[] = {
52
176
      llvm::ConstantAsMetadata::get(GV), LocDescr, GlobalName,
53
176
      llvm::ConstantAsMetadata::get(
54
176
          llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), IsDynInit)),
55
176
      llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
56
176
          llvm::Type::getInt1Ty(VMContext), IsBlacklisted))};
57
176
58
176
  llvm::MDNode *ThisGlobal = llvm::MDNode::get(VMContext, GlobalMetadata);
59
176
  llvm::NamedMDNode *AsanGlobals =
60
176
      CGM.getModule().getOrInsertNamedMetadata("llvm.asan.globals");
61
176
  AsanGlobals->addOperand(ThisGlobal);
62
176
}
63
64
void SanitizerMetadata::reportGlobalToASan(llvm::GlobalVariable *GV,
65
28.8k
                                           const VarDecl &D, bool IsDynInit) {
66
28.8k
  if (!isAsanHwasanOrMemTag(CGM.getLangOpts().Sanitize))
67
28.6k
    return;
68
139
  std::string QualName;
69
139
  llvm::raw_string_ostream OS(QualName);
70
139
  D.printQualifiedName(OS);
71
139
72
139
  bool IsBlacklisted = false;
73
139
  for (auto Attr : D.specific_attrs<NoSanitizeAttr>())
74
2
    if (Attr->getMask() & SanitizerKind::Address)
75
2
      IsBlacklisted = true;
76
139
  reportGlobalToASan(GV, D.getLocation(), OS.str(), D.getType(), IsDynInit,
77
139
                     IsBlacklisted);
78
139
}
79
80
3.91k
void SanitizerMetadata::disableSanitizerForGlobal(llvm::GlobalVariable *GV) {
81
3.91k
  // For now, just make sure the global is not modified by the ASan
82
3.91k
  // instrumentation.
83
3.91k
  if (isAsanHwasanOrMemTag(CGM.getLangOpts().Sanitize))
84
29
    reportGlobalToASan(GV, SourceLocation(), "", QualType(), false, true);
85
3.91k
}
86
87
14.9k
void SanitizerMetadata::disableSanitizerForInstruction(llvm::Instruction *I) {
88
14.9k
  I->setMetadata(CGM.getModule().getMDKindID("nosanitize"),
89
14.9k
                 llvm::MDNode::get(CGM.getLLVMContext(), None));
90
14.9k
}
91
92
136
llvm::MDNode *SanitizerMetadata::getLocationMetadata(SourceLocation Loc) {
93
136
  PresumedLoc PLoc = CGM.getContext().getSourceManager().getPresumedLoc(Loc);
94
136
  if (!PLoc.isValid())
95
0
    return nullptr;
96
136
  llvm::LLVMContext &VMContext = CGM.getLLVMContext();
97
136
  llvm::Metadata *LocMetadata[] = {
98
136
      llvm::MDString::get(VMContext, PLoc.getFilename()),
99
136
      llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
100
136
          llvm::Type::getInt32Ty(VMContext), PLoc.getLine())),
101
136
      llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
102
136
          llvm::Type::getInt32Ty(VMContext), PLoc.getColumn())),
103
136
  };
104
136
  return llvm::MDNode::get(VMContext, LocMetadata);
105
136
}