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 
21 using namespace llvm;
22 
23 SanitizerStatReport::SanitizerStatReport(Module *M) : M(M) {
24   StatTy = ArrayType::get(Type::getInt8PtrTy(M->getContext()), 2);
25   EmptyModuleStatsTy = makeModuleStatsTy();
26 
27   ModuleStatsGV = new GlobalVariable(*M, EmptyModuleStatsTy, false,
28                                      GlobalValue::InternalLinkage, nullptr);
29 }
30 
31 ArrayType *SanitizerStatReport::makeModuleStatsArrayTy() {
32   return ArrayType::get(StatTy, Inits.size());
33 }
34 
35 StructType *SanitizerStatReport::makeModuleStatsTy() {
36   return StructType::get(M->getContext(), {Type::getInt8PtrTy(M->getContext()),
37                                            Type::getInt32Ty(M->getContext()),
38                                            makeModuleStatsArrayTy()});
39 }
40 
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);
47 
48   Inits.push_back(ConstantArray::get(
49       StatTy,
50       {Constant::getNullValue(Int8PtrTy),
51        ConstantExpr::getIntToPtr(
52            ConstantInt::get(IntPtrTy, uint64_t(SK) << (IntPtrTy->getBitWidth() -
53                                                        kSanitizerStatKindBits)),
54            Int8PtrTy)}));
55 
56   FunctionType *StatReportTy =
57       FunctionType::get(B.getVoidTy(), Int8PtrTy, 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, ConstantExpr::getBitCast(InitAddr, Int8PtrTy));
68 }
69 
70 void SanitizerStatReport::finish() {
71   if (Inits.empty()) {
72     ModuleStatsGV->eraseFromParent();
73     return;
74   }
75 
76   PointerType *Int8PtrTy = Type::getInt8PtrTy(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(
89       ConstantExpr::getBitCast(NewModuleStatsGV, ModuleStatsGV->getType()));
90   ModuleStatsGV->eraseFromParent();
91 
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);
96   IRBuilder<> B(BB);
97 
98   FunctionType *StatInitTy = FunctionType::get(VoidTy, Int8PtrTy, false);
99   FunctionCallee StatInit =
100       M->getOrInsertFunction("__sanitizer_stat_init", StatInitTy);
101 
102   B.CreateCall(StatInit, ConstantExpr::getBitCast(NewModuleStatsGV, Int8PtrTy));
103   B.CreateRetVoid();
104 
105   appendToGlobalCtors(*M, F, 0);
106 }
107