LLVM  12.0.0git
SanitizerStats.cpp
Go to the documentation of this file.
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 
14 #include "llvm/ADT/Triple.h"
15 #include "llvm/IR/Constants.h"
16 #include "llvm/IR/DerivedTypes.h"
17 #include "llvm/IR/GlobalVariable.h"
18 #include "llvm/IR/IRBuilder.h"
19 #include "llvm/IR/Module.h"
21 
22 using namespace llvm;
23 
25  StatTy = ArrayType::get(Type::getInt8PtrTy(M->getContext()), 2);
26  EmptyModuleStatsTy = makeModuleStatsTy();
27 
28  ModuleStatsGV = new GlobalVariable(*M, EmptyModuleStatsTy, false,
30 }
31 
32 ArrayType *SanitizerStatReport::makeModuleStatsArrayTy() {
33  return ArrayType::get(StatTy, Inits.size());
34 }
35 
36 StructType *SanitizerStatReport::makeModuleStatsTy() {
39  makeModuleStatsArrayTy()});
40 }
41 
43  Function *F = B.GetInsertBlock()->getParent();
44  Module *M = F->getParent();
45  PointerType *Int8PtrTy = B.getInt8PtrTy();
46  IntegerType *IntPtrTy = B.getIntPtrTy(M->getDataLayout());
47  ArrayType *StatTy = ArrayType::get(Int8PtrTy, 2);
48 
49  Inits.push_back(ConstantArray::get(
50  StatTy,
51  {Constant::getNullValue(Int8PtrTy),
53  ConstantInt::get(IntPtrTy, uint64_t(SK) << (IntPtrTy->getBitWidth() -
55  Int8PtrTy)}));
56 
57  FunctionType *StatReportTy =
58  FunctionType::get(B.getVoidTy(), Int8PtrTy, false);
59  FunctionCallee StatReport =
60  M->getOrInsertFunction("__sanitizer_stat_report", StatReportTy);
61 
62  auto InitAddr = ConstantExpr::getGetElementPtr(
63  EmptyModuleStatsTy, ModuleStatsGV,
65  ConstantInt::get(IntPtrTy, 0), ConstantInt::get(B.getInt32Ty(), 2),
66  ConstantInt::get(IntPtrTy, Inits.size() - 1),
67  });
68  B.CreateCall(StatReport, ConstantExpr::getBitCast(InitAddr, Int8PtrTy));
69 }
70 
72  if (Inits.empty()) {
73  ModuleStatsGV->eraseFromParent();
74  return;
75  }
76 
77  PointerType *Int8PtrTy = Type::getInt8PtrTy(M->getContext());
79  Type *VoidTy = Type::getVoidTy(M->getContext());
80 
81  // Create a new ModuleStatsGV to replace the old one. We can't just set the
82  // old one's initializer because its type is different.
83  auto NewModuleStatsGV = new GlobalVariable(
84  *M, makeModuleStatsTy(), false, GlobalValue::InternalLinkage,
86  {Constant::getNullValue(Int8PtrTy),
87  ConstantInt::get(Int32Ty, Inits.size()),
88  ConstantArray::get(makeModuleStatsArrayTy(), Inits)}));
89  ModuleStatsGV->replaceAllUsesWith(
90  ConstantExpr::getBitCast(NewModuleStatsGV, ModuleStatsGV->getType()));
91  ModuleStatsGV->eraseFromParent();
92 
93  // Create a global constructor to register NewModuleStatsGV.
94  auto F = Function::Create(FunctionType::get(VoidTy, false),
96  auto BB = BasicBlock::Create(M->getContext(), "", F);
97  IRBuilder<> B(BB);
98 
99  FunctionType *StatInitTy = FunctionType::get(VoidTy, Int8PtrTy, false);
100  FunctionCallee StatInit =
101  M->getOrInsertFunction("__sanitizer_stat_init", StatInitTy);
102 
103  B.CreateCall(StatInit, ConstantExpr::getBitCast(NewModuleStatsGV, Int8PtrTy));
104  B.CreateRetVoid();
105 
106  appendToGlobalCtors(*M, F, 0);
107 }
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
static Constant * getAnon(ArrayRef< Constant * > V, bool Packed=false)
Return an anonymous struct that has the specified elements.
Definition: Constants.h:478
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
Definition: DerivedTypes.h:164
void create(IRBuilder<> &B, SanitizerStatKind SK)
Generates code into B that increments a location-specific counter tagged with the given sanitizer kin...
static Constant * getGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList, bool InBounds=false, Optional< unsigned > InRangeIndex=None, Type *OnlyIfReducedTy=nullptr)
Getelementptr form.
Definition: Constants.h:1210
static Constant * getIntToPtr(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2159
F(f)
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:347
Class to represent struct types.
Definition: DerivedTypes.h:212
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:253
Class to represent function types.
Definition: DerivedTypes.h:102
Class to represent array types.
Definition: DerivedTypes.h:356
static FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
Definition: Type.cpp:321
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:523
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
Definition: DerivedTypes.h:71
SanitizerStatKind
Class to represent pointers.
Definition: DerivedTypes.h:655
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2173
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Definition: Function.h:137
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:46
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static Constant * get(ArrayType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:1230
void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it.
Definition: Globals.cpp:385
void finish()
Finalize module stats array and add global constructor to register it.
static Type * getVoidTy(LLVMContext &C)
Definition: Type.cpp:180
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:100
Class to represent integer types.
Definition: DerivedTypes.h:40
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:249
static StructType * get(LLVMContext &Context, ArrayRef< Type * > Elements, bool isPacked=false)
This static method is the primary way to create a literal StructType.
Definition: Type.cpp:366
Module.h This file contains the declarations for the Module class.
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:867
void appendToGlobalCtors(Module &M, Function *F, int Priority, Constant *Data=nullptr)
Append F to the list of global ctors of module M with the given Priority.
Definition: ModuleUtils.cpp:66
FunctionCallee getOrInsertFunction(StringRef Name, FunctionType *T, AttributeList AttributeList)
Look up the specified function in the module symbol table.
Definition: Module.cpp:143
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:197
static ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
Definition: Type.cpp:581
Rename collisions when linking (static functions).
Definition: GlobalValue.h:55
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:271
IntegerType * Int32Ty