Skip to content

Commit

Permalink
module: Speed up NativeModule lifecycle
Browse files Browse the repository at this point in the history
By using a single lock for all Module objects.

We should be able to revert this once we've improved our GLib static
allocation cleanup patch to use a more suitable data structure for
keeping track of mutexes.

Kudos to @mrmacete for profiling.
  • Loading branch information
oleavr committed Jan 13, 2025
1 parent dee4095 commit 1267288
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 28 deletions.
22 changes: 8 additions & 14 deletions gum/backend-darwin/gummodule-darwin.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
#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 @@ -32,8 +29,6 @@ struct _GumNativeModule
GumMemoryRange range;
GumDarwinModuleResolver * resolver;

GMutex mutex;

gpointer cached_handle;
gboolean attempted_handle_creation;

Expand Down Expand Up @@ -118,6 +113,8 @@ 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 @@ -149,20 +146,19 @@ 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);

GUM_NATIVE_MODULE_LOCK (self);
G_LOCK (gum_native_module);

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

GUM_NATIVE_MODULE_UNLOCK (self);
G_UNLOCK (gum_native_module);

G_OBJECT_CLASS (gum_native_module_parent_class)->dispose (object);
}
Expand All @@ -172,8 +168,6 @@ 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 @@ -237,7 +231,7 @@ _gum_native_module_detach_resolver (GumNativeModule * self)
gpointer
_gum_native_module_get_handle (GumNativeModule * self)
{
GUM_NATIVE_MODULE_LOCK (self);
G_LOCK (gum_native_module);

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

GUM_NATIVE_MODULE_UNLOCK (self);
G_UNLOCK (gum_native_module);

return self->cached_handle;
}

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

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

GUM_NATIVE_MODULE_UNLOCK (self);
G_UNLOCK (gum_native_module);

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

#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 @@ -85,6 +82,8 @@ 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 @@ -116,15 +115,14 @@ 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);

GUM_NATIVE_MODULE_LOCK (self);
G_LOCK (gum_native_module);

g_clear_object (&self->cached_elf_module);

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

GUM_NATIVE_MODULE_UNLOCK (self);
G_UNLOCK (gum_native_module);

G_OBJECT_CLASS (gum_native_module_parent_class)->dispose (object);
}
Expand All @@ -143,8 +141,6 @@ 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 @@ -190,7 +186,7 @@ _gum_native_module_make_handleless (const gchar * path,
gpointer
_gum_native_module_get_handle (GumNativeModule * self)
{
GUM_NATIVE_MODULE_LOCK (self);
G_LOCK (gum_native_module);

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

GUM_NATIVE_MODULE_UNLOCK (self);
G_UNLOCK (gum_native_module);

return self->cached_handle;
}

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

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

GUM_NATIVE_MODULE_UNLOCK (self);
G_UNLOCK (gum_native_module);

return self->cached_elf_module;
}
Expand Down
2 changes: 0 additions & 2 deletions gum/backend-elf/gummodule-elf.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ 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 1267288

Please sign in to comment.