[PATCH] Implement `createSanitizerCtor`, common helper function for all sanitizers

Ismail Pazarbasi ismail.pazarbasi at gmail.com
Wed Apr 1 09:44:35 PDT 2015


Hi kcc, samsonov,

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.

http://reviews.llvm.org/D8777

Files:
  include/llvm/Transforms/Utils/ModuleUtils.h
  lib/Transforms/Utils/ModuleUtils.cpp

Index: include/llvm/Transforms/Utils/ModuleUtils.h
===================================================================
--- include/llvm/Transforms/Utils/ModuleUtils.h
+++ include/llvm/Transforms/Utils/ModuleUtils.h
@@ -14,13 +14,18 @@
 #ifndef LLVM_TRANSFORMS_UTILS_MODULEUTILS_H
 #define LLVM_TRANSFORMS_UTILS_MODULEUTILS_H
 
+#include "llvm/ADT/ArrayRef.h"
+
 namespace llvm {
 
 class Module;
 class Function;
 class GlobalValue;
 class GlobalVariable;
 class Constant;
+class StringRef;
+class Value;
+class Type;
 template <class PtrType> class SmallPtrSetImpl;
 
 /// Append F to the list of global ctors of module M with the given Priority.
@@ -43,6 +48,15 @@
 // with the same name, their prototypes must match, otherwise
 // getOrInsertFunction returns a bitcast.
 Function *checkInterfaceFunction(Constant *FuncOrBitcast);
+
+/// \brief Creates sanitizer constructor function, and calls sanitizer's init
+/// function from it.
+Function *createSanitizerCtor(Module &M, StringRef CtorName, StringRef InitName,
+                              ArrayRef<Type *> InitType = ArrayRef<Type *>(),
+                              ArrayRef<Value *> InitArgs = ArrayRef<Value *>(),
+                              Function **InitFunctionPtr = nullptr);
+Function *createSanitizerCtor(Module &M, StringRef CtorName, StringRef InitName,
+                              Function **InitFunctionPtr);
 } // End llvm namespace
 
 #endif //  LLVM_TRANSFORMS_UTILS_MODULEUTILS_H
Index: lib/Transforms/Utils/ModuleUtils.cpp
===================================================================
--- lib/Transforms/Utils/ModuleUtils.cpp
+++ lib/Transforms/Utils/ModuleUtils.cpp
@@ -104,3 +104,32 @@
   Stream << "Sanitizer interface function redefined: " << *FuncOrBitcast;
   report_fatal_error(Err);
 }
+
+Function *llvm::createSanitizerCtor(Module &M, llvm::StringRef CtorName,
+                                    llvm::StringRef InitName,
+                                    llvm::ArrayRef<Type *> InitType,
+                                    llvm::ArrayRef<Value *> InitArgs,
+                                    llvm::Function **InitFunctionPtr) {
+  assert(!InitName.empty() && "Expected init function name");
+  Function *Ctor = Function::Create(
+      FunctionType::get(Type::getVoidTy(M.getContext()), false),
+      GlobalValue::InternalLinkage, CtorName, &M);
+  BasicBlock *CtorBB = BasicBlock::Create(M.getContext(), "", Ctor);
+  IRBuilder<> IRB(ReturnInst::Create(M.getContext(), CtorBB));
+  Function *InitFunction = checkInterfaceFunction(M.getOrInsertFunction(
+      InitName, FunctionType::get(IRB.getVoidTy(), InitType, false),
+      AttributeSet()));
+  InitFunction->setLinkage(Function::ExternalLinkage);
+  IRB.CreateCall(InitFunction, InitArgs);
+  if (InitFunctionPtr)
+    *InitFunctionPtr = InitFunction;
+  return Ctor;
+}
+
+Function *llvm::createSanitizerCtor(Module &M, llvm::StringRef CtorName,
+                                    llvm::StringRef InitName,
+                                    llvm::Function **InitFunctionPtr) {
+  return llvm::createSanitizerCtor(M, CtorName, InitName, {}, {},
+                                   InitFunctionPtr);
+}
+

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D8777.23069.patch
Type: text/x-patch
Size: 3180 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150401/53078312/attachment.bin>


More information about the llvm-commits mailing list