llvm.org GIT mirror llvm / 4e21d7a
Implement `createSanitizerCtor`, common helper function for all sanitizers Summary: This helper function creates a ctor function, which calls sanitizer's init function with given arguments. This constructor is then expected to be added to module's ctors. The patch helps unifying how sanitizer constructor functions are created, and how init functions are called across all sanitizers. Reviewers: kcc, samsonov Subscribers: llvm-commits Differential Revision: http://reviews.llvm.org/D8777 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@236627 91177308-0d34-0410-b5e6-96231b3b80d8 Ismail Pazarbasi 4 years ago
2 changed file(s) with 35 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
1313 #ifndef LLVM_TRANSFORMS_UTILS_MODULEUTILS_H
1414 #define LLVM_TRANSFORMS_UTILS_MODULEUTILS_H
1515
16 #include "llvm/ADT/ArrayRef.h"
17 #include // for std::pair
18
1619 namespace llvm {
1720
1821 class Module;
2023 class GlobalValue;
2124 class GlobalVariable;
2225 class Constant;
26 class StringRef;
27 class Value;
28 class Type;
2329 template class SmallPtrSetImpl;
2430
2531 /// Append F to the list of global ctors of module M with the given Priority.
4248 // with the same name, their prototypes must match, otherwise
4349 // getOrInsertFunction returns a bitcast.
4450 Function *checkSanitizerInterfaceFunction(Constant *FuncOrBitcast);
51
52 /// \brief Creates sanitizer constructor function, and calls sanitizer's init
53 /// function from it.
54 /// \return Returns pair of pointers to constructor, and init functions
55 /// respectively.
56 std::pair createSanitizerCtorAndInitFunctions(
57 Module &M, StringRef CtorName, StringRef InitName,
58 ArrayRef InitArgTypes, ArrayRef InitArgs);
4559 } // End llvm namespace
4660
4761 #endif // LLVM_TRANSFORMS_UTILS_MODULEUTILS_H
103103 Stream << "Sanitizer interface function redefined: " << *FuncOrBitcast;
104104 report_fatal_error(Err);
105105 }
106
107 std::pair llvm::createSanitizerCtorAndInitFunctions(
108 Module &M, StringRef CtorName, StringRef InitName,
109 ArrayRef InitArgTypes, ArrayRef InitArgs) {
110 assert(!InitName.empty() && "Expected init function name");
111 assert(InitArgTypes.size() == InitArgTypes.size() &&
112 "Sanitizer's init function expects different number of arguments");
113 Function *Ctor = Function::Create(
114 FunctionType::get(Type::getVoidTy(M.getContext()), false),
115 GlobalValue::InternalLinkage, CtorName, &M);
116 BasicBlock *CtorBB = BasicBlock::Create(M.getContext(), "", Ctor);
117 IRBuilder<> IRB(ReturnInst::Create(M.getContext(), CtorBB));
118 Function *InitFunction =
119 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
120 InitName, FunctionType::get(IRB.getVoidTy(), InitArgTypes, false),
121 AttributeSet()));
122 InitFunction->setLinkage(Function::ExternalLinkage);
123 IRB.CreateCall(InitFunction, InitArgs);
124 return std::make_pair(Ctor, InitFunction);
125 }
126