1 //===- SanitizerStats.cpp - Sanitizer statistics gathering ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Implements code generation for sanitizer statistics gathering.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Transforms/Utils/SanitizerStats.h"
15 #include "llvm/ADT/Triple.h"
16 #include "llvm/IR/Constants.h"
17 #include "llvm/IR/DerivedTypes.h"
18 #include "llvm/IR/GlobalVariable.h"
19 #include "llvm/IR/IRBuilder.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/Transforms/Utils/ModuleUtils.h"
25 SanitizerStatReport::SanitizerStatReport(Module *M) : M(M) {
26 StatTy = ArrayType::get(Type::getInt8PtrTy(M->getContext()), 2);
27 EmptyModuleStatsTy = makeModuleStatsTy();
29 ModuleStatsGV = new GlobalVariable(*M, EmptyModuleStatsTy, false,
30 GlobalValue::InternalLinkage, nullptr);
33 ArrayType *SanitizerStatReport::makeModuleStatsArrayTy() {
34 return ArrayType::get(StatTy, Inits.size());
37 StructType *SanitizerStatReport::makeModuleStatsTy() {
38 return StructType::get(M->getContext(), {Type::getInt8PtrTy(M->getContext()),
39 Type::getInt32Ty(M->getContext()),
40 makeModuleStatsArrayTy()});
43 void 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);
50 Inits.push_back(ConstantArray::get(
52 {Constant::getNullValue(Int8PtrTy),
53 ConstantExpr::getIntToPtr(
54 ConstantInt::get(IntPtrTy, uint64_t(SK) << (IntPtrTy->getBitWidth() -
55 kSanitizerStatKindBits)),
58 FunctionType *StatReportTy =
59 FunctionType::get(B.getVoidTy(), Int8PtrTy, false);
60 Constant *StatReport = M->getOrInsertFunction(
61 "__sanitizer_stat_report", StatReportTy);
63 auto InitAddr = ConstantExpr::getGetElementPtr(
64 EmptyModuleStatsTy, ModuleStatsGV,
66 ConstantInt::get(IntPtrTy, 0), ConstantInt::get(B.getInt32Ty(), 2),
67 ConstantInt::get(IntPtrTy, Inits.size() - 1),
69 B.CreateCall(StatReport, ConstantExpr::getBitCast(InitAddr, Int8PtrTy));
72 void SanitizerStatReport::finish() {
74 ModuleStatsGV->eraseFromParent();
78 PointerType *Int8PtrTy = Type::getInt8PtrTy(M->getContext());
79 IntegerType *Int32Ty = Type::getInt32Ty(M->getContext());
80 Type *VoidTy = Type::getVoidTy(M->getContext());
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();
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);
100 FunctionType *StatInitTy = FunctionType::get(VoidTy, Int8PtrTy, false);
101 Constant *StatInit = M->getOrInsertFunction(
102 "__sanitizer_stat_init", StatInitTy);
104 B.CreateCall(StatInit, ConstantExpr::getBitCast(NewModuleStatsGV, Int8PtrTy));
107 appendToGlobalCtors(*M, F, 0);