[llvm-commits] [llvm] r74188 - in /llvm/trunk/lib/System: DynamicLibrary.cpp Win32/DynamicLibrary.inc
Owen Anderson
resistor at mac.com
Thu Jun 25 11:12:45 PDT 2009
Author: resistor
Date: Thu Jun 25 13:12:44 2009
New Revision: 74188
URL: http://llvm.org/viewvc/llvm-project?rev=74188&view=rev
Log:
Guard dynamic library loading.
I did my best at implementing this for Win32, but I don't have a way to test it.
Can someone with access to a Win32 machine test/fix this?
Modified:
llvm/trunk/lib/System/DynamicLibrary.cpp
llvm/trunk/lib/System/Win32/DynamicLibrary.inc
Modified: llvm/trunk/lib/System/DynamicLibrary.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/System/DynamicLibrary.cpp?rev=74188&r1=74187&r2=74188&view=diff
==============================================================================
--- llvm/trunk/lib/System/DynamicLibrary.cpp (original)
+++ llvm/trunk/lib/System/DynamicLibrary.cpp Thu Jun 25 13:12:44 2009
@@ -12,20 +12,22 @@
//===----------------------------------------------------------------------===//
#include "llvm/System/DynamicLibrary.h"
+#include "llvm/Support/ManagedStatic.h"
+#include "llvm/System/RWMutex.h"
#include "llvm/Config/config.h"
#include <cstdio>
#include <cstring>
#include <map>
// Collection of symbol name/value pairs to be searched prior to any libraries.
-std::map<std::string, void *> &g_symbols() {
- static std::map<std::string, void *> symbols;
- return symbols;
-}
+static std::map<std::string, void*> symbols;
+static llvm::sys::SmartRWMutex<true> SymbolsLock;
+
void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName,
void *symbolValue) {
- g_symbols()[symbolName] = symbolValue;
+ llvm::sys::SmartScopedWriter<true> Writer(&SymbolsLock);
+ symbols[symbolName] = symbolValue;
}
// It is not possible to use ltdl.c on VC++ builds as the terms of its LGPL
@@ -57,6 +59,7 @@
DynamicLibrary::DynamicLibrary() {}
DynamicLibrary::~DynamicLibrary() {
+ SmartScopedWriter<true> Writer(&SymbolsLock);
while(!OpenedHandles.empty()) {
void *H = OpenedHandles.back(); OpenedHandles.pop_back();
dlclose(H);
@@ -65,6 +68,7 @@
bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
std::string *ErrMsg) {
+ SmartScopedWriter<true> Writer(&SymbolsLock);
void *H = dlopen(Filename, RTLD_LAZY|RTLD_GLOBAL);
if (H == 0) {
if (ErrMsg)
@@ -77,20 +81,28 @@
void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
// check_ltdl_initialization();
-
+
// First check symbols added via AddSymbol().
- std::map<std::string, void *>::iterator I = g_symbols().find(symbolName);
- if (I != g_symbols().end())
+ SymbolsLock.reader_acquire();
+ std::map<std::string, void *>::iterator I = symbols.find(symbolName);
+ std::map<std::string, void *>::iterator E = symbols.end();
+ SymbolsLock.reader_release();
+
+ if (I != E)
return I->second;
+ SymbolsLock.writer_acquire();
// Now search the libraries.
for (std::vector<void *>::iterator I = OpenedHandles.begin(),
E = OpenedHandles.end(); I != E; ++I) {
//lt_ptr ptr = lt_dlsym(*I, symbolName);
void *ptr = dlsym(*I, symbolName);
- if (ptr)
+ if (ptr) {
+ SymbolsLock.writer_release();
return ptr;
+ }
}
+ SymbolsLock.writer_release();
#define EXPLICIT_SYMBOL(SYM) \
extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
Modified: llvm/trunk/lib/System/Win32/DynamicLibrary.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/System/Win32/DynamicLibrary.inc?rev=74188&r1=74187&r2=74188&view=diff
==============================================================================
--- llvm/trunk/lib/System/Win32/DynamicLibrary.inc (original)
+++ llvm/trunk/lib/System/Win32/DynamicLibrary.inc Thu Jun 25 13:12:44 2009
@@ -67,6 +67,7 @@
PVOID UserContext)
#endif
{
+ llvm::sys::SmartScopedWriter<true> Writer(&SymbolsLock);
// Ignore VC++ runtimes prior to 7.1. Somehow some of them get loaded
// into the process.
if (stricmp(ModuleName, "msvci70") != 0 &&
@@ -89,11 +90,13 @@
}
DynamicLibrary::DynamicLibrary() : handle(0) {
+ SmartScopedWriter<true> Writer(&SymbolsLock);
handle = GetModuleHandle(NULL);
OpenedHandles.push_back((HMODULE)handle);
}
DynamicLibrary::~DynamicLibrary() {
+ llvm::sys::SmartScopedWriter<true> Writer(&SymbolsLock);
if (handle == 0)
return;
@@ -113,8 +116,9 @@
}
bool DynamicLibrary::LoadLibraryPermanently(const char *filename,
- std::string *ErrMsg) {
+ std::string *ErrMsg) {
if (filename) {
+ llvm::sys::SmartScopedWriter<true> Writer(&SymbolsLock);
HMODULE a_handle = LoadLibrary(filename);
if (a_handle == 0)
@@ -166,17 +170,24 @@
void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
// First check symbols added via AddSymbol().
- std::map<std::string, void *>::iterator I = g_symbols().find(symbolName);
- if (I != g_symbols().end())
+ SymbolsLock.reader_acquire();
+ std::map<std::string, void *>::iterator I = symbols.find(symbolName);
+ std::map<std::string, void *>::iterator E = symbols.end();
+ SymbolsLock.reader_release();
+ if (I != E)
return I->second;
// Now search the libraries.
+ SymbolsLock.writer_acquire();
for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(),
E = OpenedHandles.end(); I != E; ++I) {
FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName);
- if (ptr)
+ if (ptr) {
+ SymbolsLock.writer_release();
return (void *) ptr;
+ }
}
+ SymbolsLock.writer_release();
#if defined(__MINGW32__)
{
More information about the llvm-commits
mailing list