[PATCH] [MCJIT] Fix a crash in getPointerToFunction

Keno Fischer kfischer at college.harvard.edu
Fri Jun 19 18:58:16 PDT 2015


Hi lhames,

If the first function passed to getPointerToFunction has private linkage,
MCJIT crashes, because the TargetMachine's TargetLoweringObjectFile has
not been inititalized yet. Fix this by moving the name resolution past
code generation (which initializes the TargetLoweringObjectFile) in the
common case, and returning null early for declarations with private
linkage.

http://reviews.llvm.org/D10588

Files:
  lib/ExecutionEngine/MCJIT/MCJIT.cpp
  unittests/ExecutionEngine/MCJIT/MCJITTest.cpp
  unittests/ExecutionEngine/MCJIT/MCJITTestBase.h

Index: lib/ExecutionEngine/MCJIT/MCJIT.cpp
===================================================================
--- lib/ExecutionEngine/MCJIT/MCJIT.cpp
+++ lib/ExecutionEngine/MCJIT/MCJIT.cpp
@@ -371,9 +371,11 @@
 
   Mangler Mang(TM->getDataLayout());
   SmallString<128> Name;
-  TM->getNameWithPrefix(Name, F, Mang);
 
   if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) {
+    if (F->hasPrivateLinkage())
+      return nullptr;
+    TM->getNameWithPrefix(Name, F, Mang);
     bool AbortOnFailure = !F->hasExternalWeakLinkage();
     void *Addr = getPointerToNamedFunction(Name, AbortOnFailure);
     updateGlobalMapping(F, Addr);
@@ -394,6 +396,10 @@
     return nullptr;
   }
 
+  // Do not move this earlier - this function may rely on the target machine
+  // having been initialized by code generation.
+  TM->getNameWithPrefix(Name, F, Mang);
+
   // FIXME: Should the Dyld be retaining module information? Probably not.
   //
   // This is the accessor for the target address, so make sure to check the
Index: unittests/ExecutionEngine/MCJIT/MCJITTest.cpp
===================================================================
--- unittests/ExecutionEngine/MCJIT/MCJITTest.cpp
+++ unittests/ExecutionEngine/MCJIT/MCJITTest.cpp
@@ -199,4 +199,16 @@
   EXPECT_EQ(A, B) << "Repeat calls to getPointerToFunction fail.";
 }
 
+TEST_F(MCJITTest, private_function_lookup) {
+  SKIP_UNSUPPORTED_PLATFORM;
+
+  Function *Foo = insertMainFunction(M.get(), 0, GlobalValue::PrivateLinkage);
+  createJIT(std::move(M));
+  void *A = TheJIT->getPointerToFunction(Foo);
+  void *B = TheJIT->getPointerToFunction(Foo);
+
+  EXPECT_TRUE(A != 0) << "Failed lookup - test not correctly configured.";
+  EXPECT_EQ(A, B)
+      << "Repeat calls to getPointerToFunction fail for private function.";
+}
 }
Index: unittests/ExecutionEngine/MCJIT/MCJITTestBase.h
===================================================================
--- unittests/ExecutionEngine/MCJIT/MCJITTestBase.h
+++ unittests/ExecutionEngine/MCJIT/MCJITTestBase.h
@@ -46,11 +46,12 @@
     return M;
   }
 
-  template<typename FuncType>
-  Function *startFunction(Module *M, StringRef Name) {
+  template <typename FuncType>
+  Function *startFunction(
+      Module *M, StringRef Name,
+      GlobalValue::LinkageTypes linkage = GlobalValue::ExternalLinkage) {
     Function *Result = Function::Create(
-      TypeBuilder<FuncType, false>::get(Context),
-      GlobalValue::ExternalLinkage, Name, M);
+        TypeBuilder<FuncType, false>::get(Context), linkage, Name, M);
 
     BasicBlock *BB = BasicBlock::Create(Context, Name, Result);
     Builder.SetInsertPoint(BB);
@@ -82,8 +83,10 @@
   // Inserts a function named 'main' that returns a uint32_t:
   //    int32_t main() { return X; }
   // where X is given by returnCode
-  Function *insertMainFunction(Module *M, uint32_t returnCode) {
-    Function *Result = startFunction<int32_t(void)>(M, "main");
+  Function *insertMainFunction(
+      Module *M, uint32_t returnCode,
+      GlobalValue::LinkageTypes linkage = GlobalValue::ExternalLinkage) {
+    Function *Result = startFunction<int32_t(void)>(M, "main", linkage);
 
     Value *ReturnVal = ConstantInt::get(Context, APInt(32, returnCode));
     endFunctionWithRet(Result, ReturnVal);

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D10588.28074.patch
Type: text/x-patch
Size: 3268 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150620/2aad8d47/attachment.bin>


More information about the llvm-commits mailing list