Index: include/llvm/Support/DynamicLibrary.h =================================================================== --- include/llvm/Support/DynamicLibrary.h +++ include/llvm/Support/DynamicLibrary.h @@ -14,6 +14,8 @@ #ifndef LLVM_SUPPORT_DYNAMICLIBRARY_H #define LLVM_SUPPORT_DYNAMICLIBRARY_H +#include "llvm/Support/Mutex.h" + #include namespace llvm { @@ -43,6 +45,10 @@ // 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 &); + public: explicit DynamicLibrary(void *data = &Invalid) : Data(data) {} @@ -68,6 +74,14 @@ 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: lib/Support/DynamicLibrary.cpp =================================================================== --- lib/Support/DynamicLibrary.cpp +++ lib/Support/DynamicLibrary.cpp @@ -68,10 +68,28 @@ handle = RTLD_DEFAULT; #endif + DynamicLibrary dyLib = addPermanentLibraryWithLock(handle, lock); + // 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 = std::string(filename) + ": Library already loaded"; dlclose(handle); + } + + return dyLib; +} + +DynamicLibrary DynamicLibrary::addPermanentLibrary(void *handle) { + SmartScopedLock lock(*SymbolsMutex); + return addPermanentLibraryWithLock(handle, lock); +} + +DynamicLibrary DynamicLibrary::addPermanentLibraryWithLock(void *handle, + sys::SmartScopedLock &) { + // If we've already loaded this library, tell the caller. + if (!OpenedHandles->insert(handle).second) + return DynamicLibrary(); return DynamicLibrary(handle); } Index: lib/Support/Windows/DynamicLibrary.inc =================================================================== --- lib/Support/Windows/DynamicLibrary.inc +++ lib/Support/Windows/DynamicLibrary.inc @@ -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 @@ typedef BOOL (WINAPI *fpEnumerateLoadedModules)(HANDLE,PENUMLOADED_MODULES_CALLBACK64,PVOID); static fpEnumerateLoadedModules fEnumerateLoadedModules; -static DenseSet *OpenedHandles; +static llvm::ManagedStatic > OpenedHandles; static bool loadDebugHelp(void) { HMODULE hLib = ::LoadLibraryW(L"Dbghelp.dll"); @@ -59,9 +57,6 @@ if (!filename) { // When no file is specified, enumerate all DLLs and EXEs in the process. - if (OpenedHandles == 0) - OpenedHandles = new DenseSet(); - if (!fEnumerateLoadedModules) { if (!loadDebugHelp()) { assert(false && "These APIs should always be available"); @@ -81,7 +76,7 @@ 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,29 @@ return DynamicLibrary(); } - if (OpenedHandles == 0) - OpenedHandles = new DenseSet(); - + DynamicLibrary dyLib = addPermanentLibraryWithLock(a_handle, lock); // 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 lock(*SymbolsMutex); + return addPermanentLibraryWithLock(handle, lock); +} + +DynamicLibrary DynamicLibrary::addPermanentLibraryWithLock(void *handle, + sys::SmartScopedLock &) { + // If we've already loaded this library, tell the caller. + if (!OpenedHandles->insert((const HMODULE)handle).second) + 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