Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose Cloak and Interceptor locks for usage in ThreadSuspendMonitor #787

Merged
merged 4 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions gum/backend-arm/gumspinlock-arm.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2010-2021 Ole André Vadla Ravnås <[email protected]>
* Copyright (C) 2024 Francesco Tamagni <[email protected]>
*
* Licence: wxWindows Library Licence, Version 3.1
*/
Expand Down Expand Up @@ -35,6 +36,19 @@ gum_spinlock_acquire (GumSpinlock * spinlock)
#endif
}

gboolean
gum_spinlock_try_acquire (GumSpinlock * spinlock)
{
GumSpinlockImpl * self = (GumSpinlockImpl *) spinlock;

if (self->is_held)
return FALSE;

gum_spinlock_acquire (spinlock);

return TRUE;
}

void
gum_spinlock_release (GumSpinlock * spinlock)
{
Expand Down
14 changes: 14 additions & 0 deletions gum/backend-arm64/gumspinlock-arm64.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2014-2019 Ole André Vadla Ravnås <[email protected]>
* Copyright (C) 2024 Francesco Tamagni <[email protected]>
*
* Licence: wxWindows Library Licence, Version 3.1
*/
Expand Down Expand Up @@ -30,6 +31,19 @@ gum_spinlock_acquire (GumSpinlock * spinlock)
;
}

gboolean
gum_spinlock_try_acquire (GumSpinlock * spinlock)
{
GumSpinlockImpl * self = (GumSpinlockImpl *) spinlock;

if (self->is_held)
return FALSE;

gum_spinlock_acquire (spinlock);

return TRUE;
}

void
gum_spinlock_release (GumSpinlock * spinlock)
{
Expand Down
14 changes: 14 additions & 0 deletions gum/backend-mips/gumspinlock-mips.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2014-2019 Ole André Vadla Ravnås <[email protected]>
* Copyright (C) 2024 Francesco Tamagni <[email protected]>
*
* Licence: wxWindows Library Licence, Version 3.1
*/
Expand Down Expand Up @@ -30,6 +31,19 @@ gum_spinlock_acquire (GumSpinlock * spinlock)
;
}

gboolean
gum_spinlock_try_acquire (GumSpinlock * spinlock)
{
GumSpinlockImpl * self = (GumSpinlockImpl *) spinlock;

if (self->is_held)
return FALSE;

gum_spinlock_acquire (spinlock);

return TRUE;
}

void
gum_spinlock_release (GumSpinlock * spinlock)
{
Expand Down
14 changes: 14 additions & 0 deletions gum/backend-x86/gumspinlock-amd64-msc.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2010-2019 Ole André Vadla Ravnås <[email protected]>
* Copyright (C) 2024 Francesco Tamagni <[email protected]>
*
* Licence: wxWindows Library Licence, Version 3.1
*/
Expand Down Expand Up @@ -30,6 +31,19 @@ gum_spinlock_acquire (GumSpinlock * spinlock)
;
}

gboolean
gum_spinlock_try_acquire (GumSpinlock * spinlock)
{
GumSpinlockImpl * self = (GumSpinlockImpl *) spinlock;

if (self->is_held)
return FALSE;

gum_spinlock_acquire (spinlock);

return TRUE;
}

void
gum_spinlock_release (GumSpinlock * spinlock)
{
Expand Down
14 changes: 14 additions & 0 deletions gum/backend-x86/gumspinlock-ia32-msc.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2010-2019 Ole André Vadla Ravnås <[email protected]>
* Copyright (C) 2024 Francesco Tamagni <[email protected]>
*
* Licence: wxWindows Library Licence, Version 3.1
*/
Expand Down Expand Up @@ -35,6 +36,19 @@ gum_spinlock_acquire (GumSpinlock * spinlock)
}
}

gboolean
gum_spinlock_try_acquire (GumSpinlock * spinlock)
{
volatile guint32 is_held = *(guint32 *) spinlock;

if (is_held == 1)
return FALSE;

gum_spinlock_acquire (spinlock);

return TRUE;
}

__declspec (naked) void
gum_spinlock_release (GumSpinlock * spinlock)
{
Expand Down
14 changes: 14 additions & 0 deletions gum/backend-x86/gumspinlock-x86.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2010-2019 Ole André Vadla Ravnås <[email protected]>
* Copyright (C) 2024 Francesco Tamagni <[email protected]>
*
* Licence: wxWindows Library Licence, Version 3.1
*/
Expand Down Expand Up @@ -30,6 +31,19 @@ gum_spinlock_acquire (GumSpinlock * spinlock)
;
}

gboolean
gum_spinlock_try_acquire (GumSpinlock * spinlock)
{
GumSpinlockImpl * self = (GumSpinlockImpl *) spinlock;

if (self->is_held)
return FALSE;

gum_spinlock_acquire (spinlock);

return TRUE;
}

void
gum_spinlock_release (GumSpinlock * spinlock)
{
Expand Down
20 changes: 20 additions & 0 deletions gum/gumcloak.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2017-2023 Ole André Vadla Ravnås <[email protected]>
* Copyright (C) 2024 Francesco Tamagni <[email protected]>
*
* Licence: wxWindows Library Licence, Version 3.1
*/
Expand Down Expand Up @@ -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;
}
6 changes: 6 additions & 0 deletions gum/gumcloak.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2017-2023 Ole André Vadla Ravnås <[email protected]>
* Copyright (C) 2024 Francesco Tamagni <[email protected]>
*
* Licence: wxWindows Library Licence, Version 3.1
*/
Expand All @@ -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);
Expand All @@ -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
21 changes: 21 additions & 0 deletions gum/guminterceptor.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright (C) 2008-2022 Ole André Vadla Ravnås <[email protected]>
* Copyright (C) 2008 Christian Berentsen <[email protected]>
* Copyright (C) 2024 Francesco Tamagni <[email protected]>
*
* Licence: wxWindows Library Licence, Version 3.1
*/
Expand Down Expand Up @@ -809,6 +810,26 @@ gum_interceptor_restore (GumInvocationState * state)
g_array_set_size (stack, old_depth);
}

void
gum_interceptor_with_lock_held (GumInterceptor * self,
GumInterceptorLockedFunc func,
gpointer user_data)
{
GUM_INTERCEPTOR_LOCK (self);
func (user_data);
GUM_INTERCEPTOR_UNLOCK (self);
}

gboolean
gum_interceptor_is_locked (GumInterceptor * self)
{
if (!g_rec_mutex_trylock (&self->mutex))
return TRUE;

GUM_INTERCEPTOR_UNLOCK (self);
return FALSE;
}

gpointer
_gum_interceptor_peek_top_caller_return_address (void)
{
Expand Down
6 changes: 6 additions & 0 deletions gum/guminterceptor.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright (C) 2008-2022 Ole André Vadla Ravnås <[email protected]>
* Copyright (C) 2008 Christian Berentsen <[email protected]>
* Copyright (C) 2024 Francesco Tamagni <[email protected]>
*
* Licence: wxWindows Library Licence, Version 3.1
*/
Expand All @@ -19,6 +20,7 @@ GUM_DECLARE_FINAL_TYPE (GumInterceptor, gum_interceptor, GUM, INTERCEPTOR,

typedef GArray GumInvocationStack;
typedef guint GumInvocationState;
typedef void (* GumInterceptorLockedFunc) (gpointer user_data);

typedef enum
{
Expand Down Expand Up @@ -76,6 +78,10 @@ GUM_API gpointer gum_invocation_stack_translate (GumInvocationStack * self,
GUM_API void gum_interceptor_save (GumInvocationState * state);
GUM_API void gum_interceptor_restore (GumInvocationState * state);

GUM_API void gum_interceptor_with_lock_held (GumInterceptor * self,
GumInterceptorLockedFunc func, gpointer user_data);
GUM_API gboolean gum_interceptor_is_locked (GumInterceptor * self);

G_END_DECLS

#endif
12 changes: 7 additions & 5 deletions gum/gumspinlock.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/*
* Copyright (C) 2010-2019 Ole André Vadla Ravnås <[email protected]>
* Copyright (C) 2010-2024 Ole André Vadla Ravnås <[email protected]>
* Copyright (C) 2024 Francesco Tamagni <[email protected]>
*
* Licence: wxWindows Library Licence, Version 3.1
*/

#ifndef __GUM_SPINLOCK_H__
#define __GUM_SPINLOCK_H__

#include <glib.h>
#include <gum/gumdefs.h>

#define GUM_SPINLOCK_INIT { NULL }

Expand All @@ -20,10 +21,11 @@ struct _GumSpinlock
gpointer data;
};

void gum_spinlock_init (GumSpinlock * spinlock);
GUM_API void gum_spinlock_init (GumSpinlock * spinlock);

void gum_spinlock_acquire (GumSpinlock * spinlock);
void gum_spinlock_release (GumSpinlock * spinlock);
GUM_API void gum_spinlock_acquire (GumSpinlock * spinlock);
GUM_API gboolean gum_spinlock_try_acquire (GumSpinlock * spinlock);
GUM_API void gum_spinlock_release (GumSpinlock * spinlock);

G_END_DECLS

Expand Down
9 changes: 9 additions & 0 deletions vapi/frida-gum-1.0.vapi
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ namespace Gum {

public void ignore_other_threads ();
public void unignore_other_threads ();

public void with_lock_held (Gum.Interceptor.LockedFunc func);
public bool is_locked ();

public delegate void LockedFunc ();
}

[CCode (type_cname = "GumInvocationListenerInterface")]
Expand Down Expand Up @@ -387,9 +392,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 {
Expand Down
Loading