Skip to content

Commit

Permalink
Revert "module: Speed up NativeModule lifecycle"
Browse files Browse the repository at this point in the history
Now that the performance issue in our GLib patch has been fixed.

This reverts commit 1267288.
  • Loading branch information
oleavr committed Jan 17, 2025
1 parent b67f905 commit 18af5b3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
22 changes: 14 additions & 8 deletions gum/backend-darwin/gummodule-darwin.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
#include <mach-o/dyld.h>
#include <mach-o/nlist.h>

#define GUM_NATIVE_MODULE_LOCK(o) g_mutex_lock (&(o)->mutex)
#define GUM_NATIVE_MODULE_UNLOCK(o) g_mutex_unlock (&(o)->mutex)

typedef struct _GumEnumerateImportsContext GumEnumerateImportsContext;
typedef struct _GumEnumerateExportsContext GumEnumerateExportsContext;
typedef struct _GumEnumerateSymbolsContext GumEnumerateSymbolsContext;
Expand All @@ -29,6 +32,8 @@ struct _GumNativeModule
GumMemoryRange range;
GumDarwinModuleResolver * resolver;

GMutex mutex;

gpointer cached_handle;
gboolean attempted_handle_creation;

Expand Down Expand Up @@ -113,8 +118,6 @@ G_DEFINE_TYPE_EXTENDED (GumNativeModule,
G_IMPLEMENT_INTERFACE (GUM_TYPE_MODULE,
gum_native_module_iface_init))

G_LOCK_DEFINE_STATIC (gum_native_module);

static void
gum_native_module_class_init (GumNativeModuleClass * klass)
{
Expand Down Expand Up @@ -146,19 +149,20 @@ gum_native_module_iface_init (gpointer g_iface,
static void
gum_native_module_init (GumNativeModule * self)
{
g_mutex_init (&self->mutex);
}

static void
gum_native_module_dispose (GObject * object)
{
GumNativeModule * self = GUM_NATIVE_MODULE (object);

G_LOCK (gum_native_module);
GUM_NATIVE_MODULE_LOCK (self);

g_clear_object (&self->cached_darwin_module);
g_clear_pointer (&self->cached_handle, dlclose);

G_UNLOCK (gum_native_module);
GUM_NATIVE_MODULE_UNLOCK (self);

G_OBJECT_CLASS (gum_native_module_parent_class)->dispose (object);
}
Expand All @@ -168,6 +172,8 @@ gum_native_module_finalize (GObject * object)
{
GumNativeModule * self = GUM_NATIVE_MODULE (object);

g_mutex_clear (&self->mutex);

g_free (self->path);

G_OBJECT_CLASS (gum_native_module_parent_class)->finalize (object);
Expand Down Expand Up @@ -249,7 +255,7 @@ _gum_native_module_detach_resolver (GumNativeModule * self)
gpointer
_gum_native_module_get_handle (GumNativeModule * self)
{
G_LOCK (gum_native_module);
GUM_NATIVE_MODULE_LOCK (self);

if (!self->attempted_handle_creation)
{
Expand All @@ -259,15 +265,15 @@ _gum_native_module_get_handle (GumNativeModule * self)
self->cached_handle = dlopen (self->path, RTLD_LAZY);
}

G_UNLOCK (gum_native_module);
GUM_NATIVE_MODULE_UNLOCK (self);

return self->cached_handle;
}

GumDarwinModule *
_gum_native_module_get_darwin_module (GumNativeModule * self)
{
G_LOCK (gum_native_module);
GUM_NATIVE_MODULE_LOCK (self);

if (!self->attempted_darwin_module_creation)
{
Expand All @@ -279,7 +285,7 @@ _gum_native_module_get_darwin_module (GumNativeModule * self)
gum_darwin_module_ensure_image_loaded (self->cached_darwin_module, NULL);
}

G_UNLOCK (gum_native_module);
GUM_NATIVE_MODULE_UNLOCK (self);

return self->cached_darwin_module;
}
Expand Down
20 changes: 12 additions & 8 deletions gum/backend-elf/gummodule-elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

#include <dlfcn.h>

#define GUM_NATIVE_MODULE_LOCK(o) g_mutex_lock (&(o)->mutex)
#define GUM_NATIVE_MODULE_UNLOCK(o) g_mutex_unlock (&(o)->mutex)

typedef struct _GumEnumerateImportsContext GumEnumerateImportsContext;
typedef struct _GumEnumerateSymbolsContext GumEnumerateSymbolsContext;
typedef struct _GumEnumerateRangesContext GumEnumerateRangesContext;
Expand Down Expand Up @@ -82,8 +85,6 @@ G_DEFINE_TYPE_EXTENDED (GumNativeModule,
G_IMPLEMENT_INTERFACE (GUM_TYPE_MODULE,
gum_native_module_iface_init))

G_LOCK_DEFINE_STATIC (gum_native_module);

static void
gum_native_module_class_init (GumNativeModuleClass * klass)
{
Expand Down Expand Up @@ -115,14 +116,15 @@ gum_native_module_iface_init (gpointer g_iface,
static void
gum_native_module_init (GumNativeModule * self)
{
g_mutex_init (&self->mutex);
}

static void
gum_native_module_dispose (GObject * object)
{
GumNativeModule * self = GUM_NATIVE_MODULE (object);

G_LOCK (gum_native_module);
GUM_NATIVE_MODULE_LOCK (self);

g_clear_object (&self->cached_elf_module);

Expand All @@ -131,7 +133,7 @@ gum_native_module_dispose (GObject * object)
else
self->cached_handle = NULL;

G_UNLOCK (gum_native_module);
GUM_NATIVE_MODULE_UNLOCK (self);

G_OBJECT_CLASS (gum_native_module_parent_class)->dispose (object);
}
Expand All @@ -141,6 +143,8 @@ gum_native_module_finalize (GObject * object)
{
GumNativeModule * self = GUM_NATIVE_MODULE (object);

g_mutex_clear (&self->mutex);

g_free (self->path);

G_OBJECT_CLASS (gum_native_module_parent_class)->finalize (object);
Expand Down Expand Up @@ -186,7 +190,7 @@ _gum_native_module_make_handleless (const gchar * path,
gpointer
_gum_native_module_get_handle (GumNativeModule * self)
{
G_LOCK (gum_native_module);
GUM_NATIVE_MODULE_LOCK (self);

if (!self->attempted_handle_creation)
{
Expand All @@ -199,15 +203,15 @@ _gum_native_module_get_handle (GumNativeModule * self)
}
}

G_UNLOCK (gum_native_module);
GUM_NATIVE_MODULE_UNLOCK (self);

return self->cached_handle;
}

GumElfModule *
_gum_native_module_get_elf_module (GumNativeModule * self)
{
G_LOCK (gum_native_module);
GUM_NATIVE_MODULE_LOCK (self);

if (!self->attempted_elf_module_creation)
{
Expand All @@ -217,7 +221,7 @@ _gum_native_module_get_elf_module (GumNativeModule * self)
self->range.base_address, NULL);
}

G_UNLOCK (gum_native_module);
GUM_NATIVE_MODULE_UNLOCK (self);

return self->cached_elf_module;
}
Expand Down
2 changes: 2 additions & 0 deletions gum/backend-elf/gummodule-elf.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ struct _GumNativeModule
GDestroyNotify create_handle_data_destroy;
GDestroyNotify destroy_handle;

GMutex mutex;

gpointer cached_handle;
gboolean attempted_handle_creation;

Expand Down

0 comments on commit 18af5b3

Please sign in to comment.