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