]> granicus.if.org Git - llvm/commitdiff
Allow externally dlopen-ed libraries to be registered as permanent libraries.
authorVassil Vassilev <v.g.vassilev@gmail.com>
Tue, 28 Feb 2017 07:11:59 +0000 (07:11 +0000)
committerVassil Vassilev <v.g.vassilev@gmail.com>
Tue, 28 Feb 2017 07:11:59 +0000 (07:11 +0000)
This is also useful in cases when llvm is in a shared library. First we dlopen
the llvm shared library and then we register it as a permanent library in order
to keep the JIT and other services working.

Patch reviewed by Vedant Kumar (D29955)!

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@296442 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Support/DynamicLibrary.h
lib/Support/DynamicLibrary.cpp
lib/Support/Windows/DynamicLibrary.inc

index a7d22212dbdb5e39e295048136fb530853ccba12..ea49a16dd025e6d7b1c19c7e0d058bb52ebce88b 100644 (file)
@@ -14,6 +14,8 @@
 #ifndef LLVM_SUPPORT_DYNAMICLIBRARY_H
 #define LLVM_SUPPORT_DYNAMICLIBRARY_H
 
+#include "llvm/Support/Mutex.h"
+
 #include <string>
 
 namespace llvm {
@@ -43,6 +45,11 @@ namespace sys {
     // Opaque data used to interface with OS-specific dynamic library handling.
     void *Data;
 
+    // Adds a opened library handle to the list of OpenedHandles.
+    static DynamicLibrary addPermanentLibraryWithLock(void *handle,
+                                                   sys::SmartScopedLock<true> &,
+                                                      bool isMainExec);
+
   public:
     explicit DynamicLibrary(void *data = &Invalid) : Data(data) {}
 
@@ -68,6 +75,14 @@ namespace sys {
     static DynamicLibrary getPermanentLibrary(const char *filename,
                                               std::string *errMsg = nullptr);
 
+    /// Registers an externally loaded library. The library will be unloaded
+    /// when the program terminates.
+    ///
+    /// It is safe to call this function multiple times for the same library.
+    ///
+    /// \returns An empty \p DynamicLibrary on failure.
+    static DynamicLibrary addPermanentLibrary(void *handle);
+
     /// This function permanently loads the dynamic library at the given path.
     /// Use this instead of getPermanentLibrary() when you won't need to get
     /// symbols from the library itself.
index 74d94d33d5adb9789569056244d49892c52537a9..d3983fb4437adc2c1c12d46140a36ce9fa8f3f33 100644 (file)
@@ -68,10 +68,34 @@ DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename,
     handle = RTLD_DEFAULT;
 #endif
 
+  DynamicLibrary dyLib = addPermanentLibraryWithLock(handle, lock, !filename);
+
   // If we've already loaded this library, dlclose() the handle in order to
   // keep the internal refcount at +1.
-  if (!OpenedHandles->insert(handle).second)
+  if (!dyLib.isValid()) {
+    if (errMsg)
+      *errMsg = (filename) ? std::string(filename) : std::string() +
+        ": Library already loaded";
     dlclose(handle);
+  }
+
+  return dyLib;
+}
+
+DynamicLibrary DynamicLibrary::addPermanentLibrary(void *handle) {
+  SmartScopedLock<true> lock(*SymbolsMutex);
+  return addPermanentLibraryWithLock(handle, lock, false);
+}
+
+DynamicLibrary DynamicLibrary::addPermanentLibraryWithLock(void *handle,
+                                                   sys::SmartScopedLock<true> &,
+                                                           bool isMainExec) {
+  // If we've already loaded this library, tell the caller.
+  // FIXME: Note that multiple requests for adding the main executable is not
+  // considered as an error. If we don't want to treat the main executable as a
+  // special case we need to do a cleanup in the MCJIT tests and API.
+  if (!OpenedHandles->insert(handle).second && !isMainExec)
+    return DynamicLibrary();
 
   return DynamicLibrary(handle);
 }
index 87dde46de3cdffde4ab8ed88f1cf7287006b30b6..264754942ba28374d429163c1d181523f23ae039 100644 (file)
@@ -9,8 +9,6 @@
 //
 // This file provides the Win32 specific implementation of DynamicLibrary.
 //
-// FIXME: This file leaks OpenedHandles!
-//
 //===----------------------------------------------------------------------===//
 
 #include "WindowsSupport.h"
@@ -35,7 +33,7 @@ using namespace sys;
 
 typedef BOOL (WINAPI *fpEnumerateLoadedModules)(HANDLE,PENUMLOADED_MODULES_CALLBACK64,PVOID);
 static fpEnumerateLoadedModules fEnumerateLoadedModules;
-static DenseSet<HMODULE> *OpenedHandles;
+static llvm::ManagedStatic<DenseSet<HMODULE> > OpenedHandles;
 
 static bool loadDebugHelp(void) {
   HMODULE hLib = ::LoadLibraryW(L"Dbghelp.dll");
@@ -59,9 +57,6 @@ DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename,
 
   if (!filename) {
     // When no file is specified, enumerate all DLLs and EXEs in the process.
-    if (OpenedHandles == 0)
-      OpenedHandles = new DenseSet<HMODULE>();
-
     if (!fEnumerateLoadedModules) {
       if (!loadDebugHelp()) {
         assert(false && "These APIs should always be available");
@@ -81,7 +76,7 @@ DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename,
     MakeErrMsg(errMsg, std::string(filename) + ": Can't convert to UTF-16");
     return DynamicLibrary();
   }
-  
+
   HMODULE a_handle = LoadLibraryW(filenameUnicode.data());
 
   if (a_handle == 0) {
@@ -89,15 +84,33 @@ DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename,
     return DynamicLibrary();
   }
 
-  if (OpenedHandles == 0)
-    OpenedHandles = new DenseSet<HMODULE>();
-
+  DynamicLibrary dyLib = addPermanentLibraryWithLock(a_handle, lock, !filename);
   // If we've already loaded this library, FreeLibrary() the handle in order to
   // keep the internal refcount at +1.
-  if (!OpenedHandles->insert(a_handle).second)
+  if (!dyLib.isValid()) {
+    MakeErrMsg(errMsg, std::string(filename) + ": Already loaded");
     FreeLibrary(a_handle);
+  }
+
+  return dyLib;
+}
+
+DynamicLibrary DynamicLibrary::addPermanentLibrary(void *handle) {
+  SmartScopedLock<true> lock(*SymbolsMutex);
+  return addPermanentLibraryWithLock(handle, lock);
+}
+
+DynamicLibrary DynamicLibrary::addPermanentLibraryWithLock(void *handle,
+                                                   sys::SmartScopedLock<true> &,
+                                                           bool isMainExec) {
+  // If we've already loaded this library, tell the caller.
+  // FIXME: Note that multiple requests for adding the main executable is not
+  // considered as an error. If we don't want to treat the main executable as a
+  // special case we need to do a cleanup in the MCJIT tests and API.
+  if (!OpenedHandles->insert((const HMODULE)handle).second && !isMainExec)
+    return DynamicLibrary();
 
-  return DynamicLibrary(a_handle);
+  return DynamicLibrary((HMODULE)handle);
 }
 
 // Stack probing routines are in the support library (e.g. libgcc), but we don't