LLVM  10.0.0svn
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 //===----------------------------------------------------------------------===//
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"
22 using namespace llvm;
25  StatTy = ArrayType::get(Type::getInt8PtrTy(M->getContext()), 2);
26  EmptyModuleStatsTy = makeModuleStatsTy();
28  ModuleStatsGV = new GlobalVariable(*M, EmptyModuleStatsTy, false,
30 }
32 ArrayType *SanitizerStatReport::makeModuleStatsArrayTy() {
33  return ArrayType::get(StatTy, Inits.size());
34 }
36 StructType *SanitizerStatReport::makeModuleStatsTy() {
39  makeModuleStatsArrayTy()});
40 }
44  Module *M = F->getParent();
45  PointerType *Int8PtrTy = B.getInt8PtrTy();
46  IntegerType *IntPtrTy = B.getIntPtrTy(M->getDataLayout());
47  ArrayType *StatTy = ArrayType::get(Int8PtrTy, 2);
49  Inits.push_back(ConstantArray::get(
50  StatTy,
51  {Constant::getNullValue(Int8PtrTy),
53  ConstantInt::get(IntPtrTy, uint64_t(SK) << (IntPtrTy->getBitWidth() -
55  Int8PtrTy)}));
57  FunctionType *StatReportTy =
58  FunctionType::get(B.getVoidTy(), Int8PtrTy, false);
59  FunctionCallee StatReport =
60  M->getOrInsertFunction("__sanitizer_stat_report", StatReportTy);
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 }
72  if (Inits.empty()) {
73  ModuleStatsGV->eraseFromParent();
74  return;
75  }
77  PointerType *Int8PtrTy = Type::getInt8PtrTy(M->getContext());
79  Type *VoidTy = Type::getVoidTy(M->getContext());
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();
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);
99  FunctionType *StatInitTy = FunctionType::get(VoidTy, Int8PtrTy, false);
100  FunctionCallee StatInit =
101  M->getOrInsertFunction("__sanitizer_stat_init", StatInitTy);
103  B.CreateCall(StatInit, ConstantExpr::getBitCast(NewModuleStatsGV, Int8PtrTy));
104  B.CreateRetVoid();
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:66
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:1153
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
Definition: DerivedTypes.h:170
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:1791
static Constant * get(ArrayType *T, ArrayRef< Constant *> V)
Definition: Constants.cpp:1014
static Constant * getNullValue(Type *Ty)
Constructor to create a &#39;0&#39; constant of arbitrary type.
Definition: Constants.cpp:289
IntegerType * getInt32Ty()
Fetch the type representing a 32-bit integer.
Definition: IRBuilder.h:383
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:369
Class to represent struct types.
Definition: DerivedTypes.h:238
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:245
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:779
IntegerType * getIntPtrTy(const DataLayout &DL, unsigned AddrSpace=0)
Fetch the type representing a pointer to an integer value.
Definition: IRBuilder.h:426
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:346
Type * getVoidTy()
Fetch the type representing void.
Definition: IRBuilder.h:416
Class to represent function types.
Definition: DerivedTypes.h:108
BasicBlock * GetInsertBlock() const
Definition: IRBuilder.h:126
Class to represent array types.
Definition: DerivedTypes.h:408
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:429
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
Definition: DerivedTypes.h:71
Class to represent pointers.
Definition: DerivedTypes.h:579
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:1804
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Definition: Function.h:135
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...
void eraseFromParent()
eraseFromParent - This method unlinks &#39;this&#39; from the containing module and deletes it...
Definition: Globals.cpp:384
void finish()
Finalize module stats array and add global constructor to register it.
static Type * getVoidTy(LLVMContext &C)
Definition: Type.cpp:165
static FunctionType * get(Type *Result, ArrayRef< Type *> Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
Definition: Type.cpp:301
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:99
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:421
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:224
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:653
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:63
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:180
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:106
static ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
Definition: Type.cpp:587
Rename collisions when linking (static functions).
Definition: GlobalValue.h:55
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value *> Args=None, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2239
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:575
static Constant * getAnon(ArrayRef< Constant *> V, bool Packed=false)
Return an anonymous struct that has the specified elements.
Definition: Constants.h:468
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:277
IntegerType * Int32Ty