Coverage Report

Created: 2019-07-24 05:18

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