Coverage Report

Created: 2021-09-21 08:58

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