From 6b15d0178730cb87cdb157d0f44217253db98dd0 Mon Sep 17 00:00:00 2001 From: Francesco Tamagni Date: Wed, 27 Mar 2024 16:50:40 +0100 Subject: [PATCH] cloak: Add with_lock_held() and is_locked() --- gum/gumcloak.c | 20 ++++++++++++++++++++ gum/gumcloak.h | 6 ++++++ vapi/frida-gum-1.0.vapi | 4 ++++ 3 files changed, 30 insertions(+) diff --git a/gum/gumcloak.c b/gum/gumcloak.c index 237657a44..37fd4ae2a 100644 --- a/gum/gumcloak.c +++ b/gum/gumcloak.c @@ -1,5 +1,6 @@ /* * Copyright (C) 2017-2023 Ole André Vadla Ravnås + * Copyright (C) 2024 Francesco Tamagni * * Licence: wxWindows Library Licence, Version 3.1 */ @@ -735,3 +736,22 @@ gum_fd_compare (gconstpointer element_a, return -1; return 1; } + +void +gum_cloak_with_lock_held (GumCloakLockedFunc func, + gpointer user_data) +{ + gum_spinlock_acquire (&cloak_lock); + func (user_data); + gum_spinlock_release (&cloak_lock); +} + +gboolean +gum_cloak_is_locked (void) +{ + if (!gum_spinlock_try_acquire (&cloak_lock)) + return TRUE; + + gum_spinlock_release (&cloak_lock); + return FALSE; +} diff --git a/gum/gumcloak.h b/gum/gumcloak.h index 0129cdc22..0a5cdf2cf 100644 --- a/gum/gumcloak.h +++ b/gum/gumcloak.h @@ -1,5 +1,6 @@ /* * Copyright (C) 2017-2023 Ole André Vadla Ravnås + * Copyright (C) 2024 Francesco Tamagni * * Licence: wxWindows Library Licence, Version 3.1 */ @@ -19,6 +20,7 @@ typedef gboolean (* GumCloakFoundThreadFunc) (GumThreadId id, typedef gboolean (* GumCloakFoundRangeFunc) (const GumMemoryRange * range, gpointer user_data); typedef gboolean (* GumCloakFoundFDFunc) (gint fd, gpointer user_data); +typedef void (* GumCloakLockedFunc) (gpointer user_data); GUM_API void gum_cloak_add_thread (GumThreadId id); GUM_API void gum_cloak_remove_thread (GumThreadId id); @@ -39,6 +41,10 @@ GUM_API gboolean gum_cloak_has_file_descriptor (gint fd); GUM_API void gum_cloak_enumerate_file_descriptors (GumCloakFoundFDFunc func, gpointer user_data); +GUM_API void gum_cloak_with_lock_held (GumCloakLockedFunc func, + gpointer user_data); +GUM_API gboolean gum_cloak_is_locked (void); + G_END_DECLS #endif diff --git a/vapi/frida-gum-1.0.vapi b/vapi/frida-gum-1.0.vapi index 41fbc8875..171f4f5b7 100644 --- a/vapi/frida-gum-1.0.vapi +++ b/vapi/frida-gum-1.0.vapi @@ -387,9 +387,13 @@ namespace Gum { public bool has_file_descriptor (int fd); public void enumerate_file_descriptors (Gum.Cloak.FoundFDFunc func); + public void with_lock_held (Gum.Cloak.LockedFunc func); + public bool is_locked (); + public delegate bool FoundThreadFunc (Gum.ThreadId id); public delegate bool FoundRangeFunc (Gum.MemoryRange range); public delegate bool FoundFDFunc (int fd); + public delegate void LockedFunc (); } public struct CpuContext {