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(Type::getInt8PtrTy(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(), {Type::getInt8PtrTy(M
->getContext()),
37 Type::getInt32Ty(M
->getContext()),
38 makeModuleStatsArrayTy()});
41 void SanitizerStatReport::create(IRBuilder
<> &B
, SanitizerStatKind SK
) {
42 Function
*F
= B
.GetInsertBlock()->getParent();
43 Module
*M
= F
->getParent();
44 PointerType
*Int8PtrTy
= B
.getInt8PtrTy();
45 IntegerType
*IntPtrTy
= B
.getIntPtrTy(M
->getDataLayout());
46 ArrayType
*StatTy
= ArrayType::get(Int8PtrTy
, 2);
48 Inits
.push_back(ConstantArray::get(
50 {Constant::getNullValue(Int8PtrTy
),
51 ConstantExpr::getIntToPtr(
52 ConstantInt::get(IntPtrTy
, uint64_t(SK
) << (IntPtrTy
->getBitWidth() -
53 kSanitizerStatKindBits
)),
56 FunctionType
*StatReportTy
=
57 FunctionType::get(B
.getVoidTy(), Int8PtrTy
, 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
, ConstantExpr::getBitCast(InitAddr
, Int8PtrTy
));
70 void SanitizerStatReport::finish() {
72 ModuleStatsGV
->eraseFromParent();
76 PointerType
*Int8PtrTy
= Type::getInt8PtrTy(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(
89 ConstantExpr::getBitCast(NewModuleStatsGV
, ModuleStatsGV
->getType()));
90 ModuleStatsGV
->eraseFromParent();
92 // Create a global constructor to register NewModuleStatsGV.
93 auto F
= Function::Create(FunctionType::get(VoidTy
, false),
94 GlobalValue::InternalLinkage
, "", M
);
95 auto BB
= BasicBlock::Create(M
->getContext(), "", F
);
98 FunctionType
*StatInitTy
= FunctionType::get(VoidTy
, Int8PtrTy
, false);
99 FunctionCallee StatInit
=
100 M
->getOrInsertFunction("__sanitizer_stat_init", StatInitTy
);
102 B
.CreateCall(StatInit
, ConstantExpr::getBitCast(NewModuleStatsGV
, Int8PtrTy
));
105 appendToGlobalCtors(*M
, F
, 0);