LLVM  6.0.0svn
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 
15 #include "llvm/ADT/Triple.h"
16 #include "llvm/IR/Constants.h"
17 #include "llvm/IR/DerivedTypes.h"
18 #include "llvm/IR/GlobalVariable.h"
19 #include "llvm/IR/IRBuilder.h"
20 #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 }
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
Constant * getOrInsertFunction(StringRef Name, FunctionType *T, AttributeList AttributeList)
Look up the specified function in the module symbol table.
Definition: Module.cpp:142
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
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:1115
void create(IRBuilder<> &B, SanitizerStatKind SK)
Generates code into B that increments a location-specific counter tagged with the given sanitizer kin...
static Constant * getIntToPtr(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:1665
F(f)
static Constant * get(ArrayType *T, ArrayRef< Constant *> V)
Definition: Constants.cpp:888
static Constant * getNullValue(Type *Ty)
Constructor to create a &#39;0&#39; constant of arbitrary type.
Definition: Constants.cpp:207
IntegerType * getInt32Ty()
Fetch the type representing a 32-bit integer.
Definition: IRBuilder.h:348
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:361
Class to represent struct types.
Definition: DerivedTypes.h:201
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:237
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:668
IntegerType * getIntPtrTy(const DataLayout &DL, unsigned AddrSpace=0)
Fetch the type representing a pointer to an integer value.
Definition: IRBuilder.h:391
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:336
Type * getVoidTy()
Fetch the type representing void.
Definition: IRBuilder.h:381
Class to represent function types.
Definition: DerivedTypes.h:103
BasicBlock * GetInsertBlock() const
Definition: IRBuilder.h:122
Class to represent array types.
Definition: DerivedTypes.h:369
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:430
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
Definition: DerivedTypes.h:66
SanitizerStatKind
Class to represent pointers.
Definition: DerivedTypes.h:467
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:1678
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 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...
void eraseFromParent()
eraseFromParent - This method unlinks &#39;this&#39; from the containing module and deletes it...
Definition: Globals.cpp:336
void finish()
Finalize module stats array and add global constructor to register it.
static Type * getVoidTy(LLVMContext &C)
Definition: Type.cpp:161
static FunctionType * get(Type *Result, ArrayRef< Type *> Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
Definition: Type.cpp:297
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:101
Class to represent integer types.
Definition: DerivedTypes.h:40
PointerType * getInt8PtrTy(unsigned AddrSpace=0)
Fetch the type representing a pointer to an 8-bit integer value.
Definition: IRBuilder.h:386
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:220
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:560
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
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:176
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:108
static ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
Definition: Type.cpp:568
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:556
static Constant * getAnon(ArrayRef< Constant *> V, bool Packed=false)
Return an anonymous struct that has the specified elements.
Definition: Constants.h:460
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, const Twine &N="", Module *M=nullptr)
Definition: Function.h:136
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:265
IntegerType * Int32Ty
CallInst * CreateCall(Value *Callee, ArrayRef< Value *> Args=None, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1663