LLVM  4.0.0
SanitizerStats.cpp
Go to the documentation of this file.
1 //===- SanitizerStats.cpp - Sanitizer statistics gathering ----------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Implements code generation for sanitizer statistics gathering.
11 //
12 //===----------------------------------------------------------------------===//
13 
16 #include "llvm/ADT/Triple.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/DerivedTypes.h"
19 #include "llvm/IR/GlobalVariable.h"
20 #include "llvm/IR/IRBuilder.h"
21 #include "llvm/IR/Module.h"
22 
23 using namespace llvm;
24 
26  StatTy = ArrayType::get(Type::getInt8PtrTy(M->getContext()), 2);
27  EmptyModuleStatsTy = makeModuleStatsTy();
28 
29  ModuleStatsGV = new GlobalVariable(*M, EmptyModuleStatsTy, false,
31 }
32 
33 ArrayType *SanitizerStatReport::makeModuleStatsArrayTy() {
34  return ArrayType::get(StatTy, Inits.size());
35 }
36 
37 StructType *SanitizerStatReport::makeModuleStatsTy() {
40  makeModuleStatsArrayTy()});
41 }
42 
45  Module *M = F->getParent();
46  PointerType *Int8PtrTy = B.getInt8PtrTy();
47  IntegerType *IntPtrTy = B.getIntPtrTy(M->getDataLayout());
48  ArrayType *StatTy = ArrayType::get(Int8PtrTy, 2);
49 
50  Inits.push_back(ConstantArray::get(
51  StatTy,
52  {Constant::getNullValue(Int8PtrTy),
54  ConstantInt::get(IntPtrTy, uint64_t(SK) << (IntPtrTy->getBitWidth() -
56  Int8PtrTy)}));
57 
58  FunctionType *StatReportTy =
59  FunctionType::get(B.getVoidTy(), Int8PtrTy, false);
60  Constant *StatReport = M->getOrInsertFunction(
61  "__sanitizer_stat_report", StatReportTy);
62 
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),
68  });
69  B.CreateCall(StatReport, ConstantExpr::getBitCast(InitAddr, Int8PtrTy));
70 }
71 
73  if (Inits.empty()) {
74  ModuleStatsGV->eraseFromParent();
75  return;
76  }
77 
78  PointerType *Int8PtrTy = Type::getInt8PtrTy(M->getContext());
80  Type *VoidTy = Type::getVoidTy(M->getContext());
81 
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,
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();
93 
94  // Create a global constructor to register NewModuleStatsGV.
95  auto F = Function::Create(FunctionType::get(VoidTy, false),
97  auto BB = BasicBlock::Create(M->getContext(), "", F);
98  IRBuilder<> B(BB);
99 
100  FunctionType *StatInitTy = FunctionType::get(VoidTy, Int8PtrTy, false);
101  Constant *StatInit = M->getOrInsertFunction(
102  "__sanitizer_stat_init", StatInitTy);
103 
104  B.CreateCall(StatInit, ConstantExpr::getBitCast(NewModuleStatsGV, Int8PtrTy));
105  B.CreateRetVoid();
106 
107  appendToGlobalCtors(*M, F, 0);
108 }
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:52
static Constant * getAnon(ArrayRef< Constant * > V, bool Packed=false)
Return an anonymous struct that has the specified elements.
Definition: Constants.h:459
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:1126
static Constant * getIntToPtr(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:1682
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:100
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
Definition: DerivedTypes.h:65
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:195
IntegerType * getInt32Ty()
Fetch the type representing a 32-bit integer.
Definition: IRBuilder.h:347
Class to represent struct types.
Definition: DerivedTypes.h:199
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:588
IntegerType * getIntPtrTy(const DataLayout &DL, unsigned AddrSpace=0)
Fetch the type representing a pointer to an integer value.
Definition: IRBuilder.h:390
void eraseFromParent() override
eraseFromParent - This method unlinks 'this' from the containing module and deletes it...
Definition: Globals.cpp:319
Type * getVoidTy()
Fetch the type representing void.
Definition: IRBuilder.h:380
Class to represent function types.
Definition: DerivedTypes.h:102
#define F(x, y, z)
Definition: MD5.cpp:51
Class to represent array types.
Definition: DerivedTypes.h:345
static FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
Definition: Type.cpp:291
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
static GCRegistry::Add< OcamlGC > B("ocaml","ocaml 3.10-compatible GC")
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:401
SanitizerStatKind
Class to represent pointers.
Definition: DerivedTypes.h:443
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:1695
Constant * getOrInsertFunction(StringRef Name, FunctionType *T, AttributeSet AttributeList)
Look up the specified function in the module symbol table.
Definition: Module.cpp:123
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
This is an important base class in LLVM.
Definition: Constant.h:42
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:888
void finish()
Finalize module stats array and add global constructor to register it.
static Type * getVoidTy(LLVMContext &C)
Definition: Type.cpp:154
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:93
Class to represent integer types.
Definition: DerivedTypes.h:39
PointerType * getInt8PtrTy(unsigned AddrSpace=0)
Fetch the type representing a pointer to an 8-bit integer value.
Definition: IRBuilder.h:385
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:213
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:330
Module.h This file contains the declarations for the Module class.
BasicBlock * GetInsertBlock() const
Definition: IRBuilder.h:121
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:558
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:84
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:259
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:384
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:169
CallInst * CreateCall(Value *Callee, ArrayRef< Value * > Args=None, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1579
static ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
Definition: Type.cpp:606
Rename collisions when linking (static functions).
Definition: GlobalValue.h:56
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:537
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, const Twine &N="", Module *M=nullptr)
Definition: Function.h:117
IntegerType * Int32Ty
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:222