Coverage Report

Created: 2020-09-15 12:33

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