Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Transforms/Utils/SanitizerStats.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- SanitizerStats.cpp - Sanitizer statistics gathering ----------------===//
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
// Implements code generation for sanitizer statistics gathering.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "llvm/Transforms/Utils/SanitizerStats.h"
14
#include "llvm/ADT/Triple.h"
15
#include "llvm/IR/Constants.h"
16
#include "llvm/IR/DerivedTypes.h"
17
#include "llvm/IR/GlobalVariable.h"
18
#include "llvm/IR/IRBuilder.h"
19
#include "llvm/IR/Module.h"
20
#include "llvm/Transforms/Utils/ModuleUtils.h"
21
22
using namespace llvm;
23
24
2
SanitizerStatReport::SanitizerStatReport(Module *M) : M(M) {
25
2
  StatTy = ArrayType::get(Type::getInt8PtrTy(M->getContext()), 2);
26
2
  EmptyModuleStatsTy = makeModuleStatsTy();
27
2
28
2
  ModuleStatsGV = new GlobalVariable(*M, EmptyModuleStatsTy, false,
29
2
                                     GlobalValue::InternalLinkage, nullptr);
30
2
}
31
32
6
ArrayType *SanitizerStatReport::makeModuleStatsArrayTy() {
33
6
  return ArrayType::get(StatTy, Inits.size());
34
6
}
35
36
4
StructType *SanitizerStatReport::makeModuleStatsTy() {
37
4
  return StructType::get(M->getContext(), {Type::getInt8PtrTy(M->getContext()),
38
4
                                           Type::getInt32Ty(M->getContext()),
39
4
                                           makeModuleStatsArrayTy()});
40
4
}
41
42
10
void SanitizerStatReport::create(IRBuilder<> &B, SanitizerStatKind SK) {
43
10
  Function *F = B.GetInsertBlock()->getParent();
44
10
  Module *M = F->getParent();
45
10
  PointerType *Int8PtrTy = B.getInt8PtrTy();
46
10
  IntegerType *IntPtrTy = B.getIntPtrTy(M->getDataLayout());
47
10
  ArrayType *StatTy = ArrayType::get(Int8PtrTy, 2);
48
10
49
10
  Inits.push_back(ConstantArray::get(
50
10
      StatTy,
51
10
      {Constant::getNullValue(Int8PtrTy),
52
10
       ConstantExpr::getIntToPtr(
53
10
           ConstantInt::get(IntPtrTy, uint64_t(SK) << (IntPtrTy->getBitWidth() -
54
10
                                                       kSanitizerStatKindBits)),
55
10
           Int8PtrTy)}));
56
10
57
10
  FunctionType *StatReportTy =
58
10
      FunctionType::get(B.getVoidTy(), Int8PtrTy, false);
59
10
  FunctionCallee StatReport =
60
10
      M->getOrInsertFunction("__sanitizer_stat_report", StatReportTy);
61
10
62
10
  auto InitAddr = ConstantExpr::getGetElementPtr(
63
10
      EmptyModuleStatsTy, ModuleStatsGV,
64
10
      ArrayRef<Constant *>{
65
10
          ConstantInt::get(IntPtrTy, 0), ConstantInt::get(B.getInt32Ty(), 2),
66
10
          ConstantInt::get(IntPtrTy, Inits.size() - 1),
67
10
      });
68
10
  B.CreateCall(StatReport, ConstantExpr::getBitCast(InitAddr, Int8PtrTy));
69
10
}
70
71
2
void SanitizerStatReport::finish() {
72
2
  if (Inits.empty()) {
73
0
    ModuleStatsGV->eraseFromParent();
74
0
    return;
75
0
  }
76
2
77
2
  PointerType *Int8PtrTy = Type::getInt8PtrTy(M->getContext());
78
2
  IntegerType *Int32Ty = Type::getInt32Ty(M->getContext());
79
2
  Type *VoidTy = Type::getVoidTy(M->getContext());
80
2
81
2
  // Create a new ModuleStatsGV to replace the old one. We can't just set the
82
2
  // old one's initializer because its type is different.
83
2
  auto NewModuleStatsGV = new GlobalVariable(
84
2
      *M, makeModuleStatsTy(), false, GlobalValue::InternalLinkage,
85
2
      ConstantStruct::getAnon(
86
2
          {Constant::getNullValue(Int8PtrTy),
87
2
           ConstantInt::get(Int32Ty, Inits.size()),
88
2
           ConstantArray::get(makeModuleStatsArrayTy(), Inits)}));
89
2
  ModuleStatsGV->replaceAllUsesWith(
90
2
      ConstantExpr::getBitCast(NewModuleStatsGV, ModuleStatsGV->getType()));
91
2
  ModuleStatsGV->eraseFromParent();
92
2
93
2
  // Create a global constructor to register NewModuleStatsGV.
94
2
  auto F = Function::Create(FunctionType::get(VoidTy, false),
95
2
                            GlobalValue::InternalLinkage, "", M);
96
2
  auto BB = BasicBlock::Create(M->getContext(), "", F);
97
2
  IRBuilder<> B(BB);
98
2
99
2
  FunctionType *StatInitTy = FunctionType::get(VoidTy, Int8PtrTy, false);
100
2
  FunctionCallee StatInit =
101
2
      M->getOrInsertFunction("__sanitizer_stat_init", StatInitTy);
102
2
103
2
  B.CreateCall(StatInit, ConstantExpr::getBitCast(NewModuleStatsGV, Int8PtrTy));
104
2
  B.CreateRetVoid();
105
2
106
2
  appendToGlobalCtors(*M, F, 0);
107
2
}