Coverage Report

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