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