Skip to content

Commit

Permalink
Revert r296442 (and r296443), "Allow externally dlopen-ed libraries t…
Browse files Browse the repository at this point in the history
…o be registered as permanent libraries."

It broke clang/test/Analysis/checker-plugins.c

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@296463 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
chapuni committed Feb 28, 2017
1 parent 77b493a commit dd90731
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 66 deletions.
15 changes: 0 additions & 15 deletions include/llvm/Support/DynamicLibrary.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
#ifndef LLVM_SUPPORT_DYNAMICLIBRARY_H
#define LLVM_SUPPORT_DYNAMICLIBRARY_H

#include "llvm/Support/Mutex.h"

#include <string>

namespace llvm {
Expand Down Expand Up @@ -45,11 +43,6 @@ 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) {}

Expand All @@ -75,14 +68,6 @@ 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.
Expand Down
26 changes: 1 addition & 25 deletions lib/Support/DynamicLibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,34 +68,10 @@ 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 (!dyLib.isValid()) {
if (errMsg)
*errMsg = (filename) ? std::string(filename) : std::string() +
": Library already loaded";
if (!OpenedHandles->insert(handle).second)
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);
}
Expand Down
39 changes: 13 additions & 26 deletions lib/Support/Windows/DynamicLibrary.inc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
//
// This file provides the Win32 specific implementation of DynamicLibrary.
//
// FIXME: This file leaks OpenedHandles!
//
//===----------------------------------------------------------------------===//

#include "WindowsSupport.h"
Expand All @@ -33,7 +35,7 @@ using namespace sys;

typedef BOOL (WINAPI *fpEnumerateLoadedModules)(HANDLE,PENUMLOADED_MODULES_CALLBACK64,PVOID);
static fpEnumerateLoadedModules fEnumerateLoadedModules;
static llvm::ManagedStatic<DenseSet<HMODULE> > OpenedHandles;
static DenseSet<HMODULE> *OpenedHandles;

static bool loadDebugHelp(void) {
HMODULE hLib = ::LoadLibraryW(L"Dbghelp.dll");
Expand All @@ -57,6 +59,9 @@ 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");
Expand All @@ -76,41 +81,23 @@ 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) {
MakeErrMsg(errMsg, std::string(filename) + ": Can't open");
return DynamicLibrary();
}

DynamicLibrary dyLib = addPermanentLibraryWithLock(a_handle, lock, !filename);
if (OpenedHandles == 0)
OpenedHandles = new DenseSet<HMODULE>();

// If we've already loaded this library, FreeLibrary() the handle in order to
// keep the internal refcount at +1.
if (!dyLib.isValid()) {
MakeErrMsg(errMsg, std::string(filename) + ": Already loaded");
if (!OpenedHandles->insert(a_handle).second)
FreeLibrary(a_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((const HMODULE)handle).second && !isMainExec)
return DynamicLibrary();

return DynamicLibrary((HMODULE)handle);
return DynamicLibrary(a_handle);
}

// Stack probing routines are in the support library (e.g. libgcc), but we don't
Expand Down Expand Up @@ -150,7 +137,7 @@ void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
}

// Now search the libraries.
if (OpenedHandles.isConstructed()) {
if (OpenedHandles) {
for (DenseSet<HMODULE>::iterator I = OpenedHandles->begin(),
E = OpenedHandles->end(); I != E; ++I) {
FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName);
Expand Down

0 comments on commit dd90731

Please sign in to comment.