1 //===- SanitizerStats.cpp - Sanitizer statistics gathering ----------------===//
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
7 //===----------------------------------------------------------------------===//
9 // Implements code generation for sanitizer statistics gathering.
11 //===----------------------------------------------------------------------===//
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"
23 SanitizerStatReport::SanitizerStatReport(Module
*M
) : M(M
) {
24 StatTy
= ArrayType::get(PointerType::getUnqual(M
->getContext()), 2);
25 EmptyModuleStatsTy
= makeModuleStatsTy();
27 ModuleStatsGV
= new GlobalVariable(*M
, EmptyModuleStatsTy
, false,
28 GlobalValue::InternalLinkage
, nullptr);
31 ArrayType
*SanitizerStatReport::makeModuleStatsArrayTy() {
32 return ArrayType::get(StatTy
, Inits
.size());
35 StructType
*SanitizerStatReport::makeModuleStatsTy() {
36 return StructType::get(M
->getContext(),
37 {PointerType::getUnqual(M
->getContext()),
38 Type::getInt32Ty(M
->getContext()),
39 makeModuleStatsArrayTy()});
42 void 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);
49 Inits
.push_back(ConstantArray::get(
51 {Constant::getNullValue(PtrTy
),
52 ConstantExpr::getIntToPtr(
53 ConstantInt::get(IntPtrTy
, uint64_t(SK
) << (IntPtrTy
->getBitWidth() -
54 kSanitizerStatKindBits
)),
57 FunctionType
*StatReportTy
= FunctionType::get(B
.getVoidTy(), PtrTy
, false);
58 FunctionCallee StatReport
=
59 M
->getOrInsertFunction("__sanitizer_stat_report", StatReportTy
);
61 auto InitAddr
= ConstantExpr::getGetElementPtr(
62 EmptyModuleStatsTy
, ModuleStatsGV
,
64 ConstantInt::get(IntPtrTy
, 0), ConstantInt::get(B
.getInt32Ty(), 2),
65 ConstantInt::get(IntPtrTy
, Inits
.size() - 1),
67 B
.CreateCall(StatReport
, InitAddr
);
70 void SanitizerStatReport::finish() {
72 ModuleStatsGV
->eraseFromParent();
76 PointerType
*Int8PtrTy
= PointerType::getUnqual(M
->getContext());
77 IntegerType
*Int32Ty
= Type::getInt32Ty(M
->getContext());
78 Type
*VoidTy
= Type::getVoidTy(M
->getContext());
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();
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
);
97 FunctionType
*StatInitTy
= FunctionType::get(VoidTy
, Int8PtrTy
, false);
98 FunctionCallee StatInit
=
99 M
->getOrInsertFunction("__sanitizer_stat_init", StatInitTy
);
101 B
.CreateCall(StatInit
, NewModuleStatsGV
);
104 appendToGlobalCtors(*M
, F
, 0);