Skip to content

Commit

Permalink
Fix crashes when libobjcdarwin is unloaded
Browse files Browse the repository at this point in the history
  • Loading branch information
LubosD committed Feb 28, 2014
1 parent 243b0ad commit d117b0c
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 4 deletions.
15 changes: 15 additions & 0 deletions src/libdyld/MachOMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,21 @@ void MachOMgr::registerUnloadHook(LoaderHookFunc* func)
m_unloadHooks.insert(func);
}

void MachOMgr::deregisterLoadHook(LoaderHookFunc* func)
{
Darling::RWMutexWriteLock l(m_lock);

m_loadHooks.erase(func);
}

void MachOMgr::deregisterUnloadHook(LoaderHookFunc* func)
{
Darling::RWMutexWriteLock l(m_lock);

m_unloadHooks.erase(func);
}


void MachOMgr::add(MachOObject* obj, bool mainModule)
{
Darling::RWMutexWriteLock l(m_lock);
Expand Down
3 changes: 3 additions & 0 deletions src/libdyld/MachOMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ class MachOMgr
void registerLoadHook(LoaderHookFunc* func);
void registerUnloadHook(LoaderHookFunc* func);

void deregisterLoadHook(LoaderHookFunc* func);
void deregisterUnloadHook(LoaderHookFunc* func);

// DYLD_BIND_AT_LAUNCH
inline void setBindAtLaunch(bool bindAtLaunch) { m_bindAtLaunch = bindAtLaunch; }
inline bool bindAtLaunch() const { return m_bindAtLaunch || useTrampolines(); }
Expand Down
8 changes: 4 additions & 4 deletions src/libdyld/dl_public.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
static thread_local char g_lastError[1024];
static thread_local bool g_lastErrorRead = false;

static std::set<Darling::DlsymHookFunc> g_dlsymHooks;
static std::set<Darling::DlsymHookFunc>* g_dlsymHooks = new std::set<Darling::DlsymHookFunc>;

using namespace Darling;

Expand Down Expand Up @@ -116,7 +116,7 @@ static std::string processSymbolViaHooks(const std::string& symbol)

strcpy(translated, symbol.c_str());

for (Darling::DlsymHookFunc hook : g_dlsymHooks)
for (Darling::DlsymHookFunc hook : *g_dlsymHooks)
{
if (hook(translated))
return translated;
Expand Down Expand Up @@ -268,11 +268,11 @@ const char* NSLibraryNameForModule(NSModule m)

void Darling::registerDlsymHook(DlsymHookFunc func)
{
g_dlsymHooks.insert(func);
g_dlsymHooks->insert(func);
}

void Darling::deregisterDlsymHook(DlsymHookFunc func)
{
//g_dlsymHooks.erase(func);
g_dlsymHooks->erase(func);
}

10 changes: 10 additions & 0 deletions src/libdyld/dyld_public.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ void _dyld_register_func_for_remove_image(MachOMgr::LoaderHookFunc* func)
MachOMgr::instance()->registerUnloadHook(func);
}

void _dyld_deregister_func_for_add_image(MachOMgr::LoaderHookFunc* func)
{
MachOMgr::instance()->deregisterLoadHook(func);
}

void _dyld_deregister_func_for_remove_image(MachOMgr::LoaderHookFunc* func)
{
MachOMgr::instance()->deregisterUnloadHook(func);
}


const char* dyld_image_path_containing_address(const void* addr)
{
Expand Down
2 changes: 2 additions & 0 deletions src/libdyld/dyld_public.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ char* getsectdata(const struct mach_header* header, const char* segname, const c

void _dyld_register_func_for_add_image(Darling::MachOMgr::LoaderHookFunc* func);
void _dyld_register_func_for_remove_image(Darling::MachOMgr::LoaderHookFunc* func);
void _dyld_deregister_func_for_add_image(Darling::MachOMgr::LoaderHookFunc* func);
void _dyld_deregister_func_for_remove_image(Darling::MachOMgr::LoaderHookFunc* func);

void __dyld_make_delayed_module_initializer_calls();
void __dyld_mod_term_funcs();
Expand Down
6 changes: 6 additions & 0 deletions src/libobjcdarwin/ClassRegister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ __attribute__((constructor))
//std::cout << "Done registering\n";
}

__attribute__((destructor))
void DeregisterHooks()
{
_dyld_deregister_func_for_add_image(ProcessImageLoad);
_dyld_deregister_func_for_remove_image(ProcessImageUnload);
}

void ProcessImageLoad(const struct mach_header* mh, intptr_t slide)
{
Expand Down

0 comments on commit d117b0c

Please sign in to comment.