From 3d0d2812c7ab80e57bfa35d8305759ccc7bd7439 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20Andr=C3=A9=20Vadla=20Ravn=C3=A5s?= Date: Thu, 18 Jul 2024 14:19:42 +0200 Subject: [PATCH] spinlock: Consolidate into a single implementation Using GLib's atomic primitives. --- gum/backend-arm/gumspinlock-arm.c | 62 ------------------- gum/backend-arm64/gumspinlock-arm64.c | 53 ---------------- gum/backend-mips/gumspinlock-mips.c | 53 ---------------- gum/backend-x86/gumspinlock-ia32-msc.c | 61 ------------------ gum/backend-x86/gumspinlock-x86.c | 53 ---------------- .../gumspinlock-amd64-msc.c => gumspinlock.c} | 0 gum/meson.build | 17 +---- 7 files changed, 3 insertions(+), 296 deletions(-) delete mode 100644 gum/backend-arm/gumspinlock-arm.c delete mode 100644 gum/backend-arm64/gumspinlock-arm64.c delete mode 100644 gum/backend-mips/gumspinlock-mips.c delete mode 100644 gum/backend-x86/gumspinlock-ia32-msc.c delete mode 100644 gum/backend-x86/gumspinlock-x86.c rename gum/{backend-x86/gumspinlock-amd64-msc.c => gumspinlock.c} (100%) diff --git a/gum/backend-arm/gumspinlock-arm.c b/gum/backend-arm/gumspinlock-arm.c deleted file mode 100644 index 59802b76c..000000000 --- a/gum/backend-arm/gumspinlock-arm.c +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright (C) 2010-2021 Ole André Vadla Ravnås - * Copyright (C) 2024 Francesco Tamagni - * - * Licence: wxWindows Library Licence, Version 3.1 - */ - -#include "gumspinlock.h" - -#include "gumlibc.h" - -typedef struct _GumSpinlockImpl GumSpinlockImpl; - -struct _GumSpinlockImpl -{ - volatile int is_held; -}; - -void -gum_spinlock_init (GumSpinlock * spinlock) -{ - gum_memset (spinlock, 0, sizeof (GumSpinlock)); -} - -void -gum_spinlock_acquire (GumSpinlock * spinlock) -{ - GumSpinlockImpl * self = (GumSpinlockImpl *) spinlock; - -#ifdef HAVE_SYNC_LOCK - while (__sync_lock_test_and_set (&self->is_held, 1)) - ; -#else - while (!g_atomic_int_compare_and_exchange (&self->is_held, FALSE, TRUE)) - ; -#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) -{ - GumSpinlockImpl * self = (GumSpinlockImpl *) spinlock; - -#ifdef HAVE_SYNC_LOCK - __sync_lock_release (&self->is_held); -#else - g_atomic_int_set (&self->is_held, FALSE); -#endif -} diff --git a/gum/backend-arm64/gumspinlock-arm64.c b/gum/backend-arm64/gumspinlock-arm64.c deleted file mode 100644 index 5a1ba4c9e..000000000 --- a/gum/backend-arm64/gumspinlock-arm64.c +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (C) 2014-2019 Ole André Vadla Ravnås - * Copyright (C) 2024 Francesco Tamagni - * - * Licence: wxWindows Library Licence, Version 3.1 - */ - -#include "gumspinlock.h" - -#include "gumlibc.h" - -typedef struct _GumSpinlockImpl GumSpinlockImpl; - -struct _GumSpinlockImpl -{ - volatile int is_held; -}; - -void -gum_spinlock_init (GumSpinlock * spinlock) -{ - gum_memset (spinlock, 0, sizeof (GumSpinlock)); -} - -void -gum_spinlock_acquire (GumSpinlock * spinlock) -{ - GumSpinlockImpl * self = (GumSpinlockImpl *) spinlock; - - while (__sync_lock_test_and_set (&self->is_held, 1)) - ; -} - -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) -{ - GumSpinlockImpl * self = (GumSpinlockImpl *) spinlock; - - __sync_lock_release (&self->is_held); -} diff --git a/gum/backend-mips/gumspinlock-mips.c b/gum/backend-mips/gumspinlock-mips.c deleted file mode 100644 index 5a1ba4c9e..000000000 --- a/gum/backend-mips/gumspinlock-mips.c +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (C) 2014-2019 Ole André Vadla Ravnås - * Copyright (C) 2024 Francesco Tamagni - * - * Licence: wxWindows Library Licence, Version 3.1 - */ - -#include "gumspinlock.h" - -#include "gumlibc.h" - -typedef struct _GumSpinlockImpl GumSpinlockImpl; - -struct _GumSpinlockImpl -{ - volatile int is_held; -}; - -void -gum_spinlock_init (GumSpinlock * spinlock) -{ - gum_memset (spinlock, 0, sizeof (GumSpinlock)); -} - -void -gum_spinlock_acquire (GumSpinlock * spinlock) -{ - GumSpinlockImpl * self = (GumSpinlockImpl *) spinlock; - - while (__sync_lock_test_and_set (&self->is_held, 1)) - ; -} - -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) -{ - GumSpinlockImpl * self = (GumSpinlockImpl *) spinlock; - - __sync_lock_release (&self->is_held); -} diff --git a/gum/backend-x86/gumspinlock-ia32-msc.c b/gum/backend-x86/gumspinlock-ia32-msc.c deleted file mode 100644 index 912a63255..000000000 --- a/gum/backend-x86/gumspinlock-ia32-msc.c +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (C) 2010-2019 Ole André Vadla Ravnås - * Copyright (C) 2024 Francesco Tamagni - * - * Licence: wxWindows Library Licence, Version 3.1 - */ - -#include "gumspinlock.h" - -#include "gumlibc.h" - -void -gum_spinlock_init (GumSpinlock * spinlock) -{ - gum_memset (spinlock, 0, sizeof (GumSpinlock)); -} - -__declspec (naked) void -gum_spinlock_acquire (GumSpinlock * spinlock) -{ - __asm - { - mov ecx, [esp + 4]; - mov edx, 1; - -try_again: - xor eax, eax; - lock cmpxchg [ecx], edx; - jz beach; - - pause; - jmp try_again; - -beach: - ret; - } -} - -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) -{ - __asm - { - mov ecx, [esp + 4]; - mov [ecx], 0; - ret; - } -} diff --git a/gum/backend-x86/gumspinlock-x86.c b/gum/backend-x86/gumspinlock-x86.c deleted file mode 100644 index ac882e4f4..000000000 --- a/gum/backend-x86/gumspinlock-x86.c +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (C) 2010-2019 Ole André Vadla Ravnås - * Copyright (C) 2024 Francesco Tamagni - * - * Licence: wxWindows Library Licence, Version 3.1 - */ - -#include "gumspinlock.h" - -#include "gumlibc.h" - -typedef struct _GumSpinlockImpl GumSpinlockImpl; - -struct _GumSpinlockImpl -{ - volatile int is_held; -}; - -void -gum_spinlock_init (GumSpinlock * spinlock) -{ - gum_memset (spinlock, 0, sizeof (GumSpinlock)); -} - -void -gum_spinlock_acquire (GumSpinlock * spinlock) -{ - GumSpinlockImpl * self = (GumSpinlockImpl *) spinlock; - - while (__sync_lock_test_and_set (&self->is_held, 1)) - ; -} - -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) -{ - GumSpinlockImpl * self = (GumSpinlockImpl *) spinlock; - - __sync_lock_release (&self->is_held); -} diff --git a/gum/backend-x86/gumspinlock-amd64-msc.c b/gum/gumspinlock.c similarity index 100% rename from gum/backend-x86/gumspinlock-amd64-msc.c rename to gum/gumspinlock.c diff --git a/gum/meson.build b/gum/meson.build index 04e8d29f5..825a57b07 100644 --- a/gum/meson.build +++ b/gum/meson.build @@ -67,6 +67,7 @@ gum_sources = [ 'gumprintf.c', 'gumprocess.c', 'gumreturnaddress.c', + 'gumspinlock.c', 'gumstalker.c', 'gumswiftapiresolver.c', 'arch-x86' / 'gumx86writer.c', @@ -212,17 +213,8 @@ if host_arch in ['x86', 'x86_64'] 'backend-x86' / 'guminterceptor-x86.c', 'backend-x86' / 'gumstalker-x86.c', ] - if cc.get_argument_syntax() == 'msvc' - if host_arch == 'x86' - gum_sources += 'backend-x86' / 'gumspinlock-ia32-msc.c' - elif host_arch == 'x86_64' - gum_sources += 'backend-x86' / 'gumspinlock-amd64-msc.c' - endif - else - gum_sources += [ - 'backend-x86' / 'gumspinlock-x86.c', - 'backend-x86' / 'gumstalker-x86-glue.S', - ] + if cc.get_argument_syntax() != 'msvc' + gum_sources += 'backend-x86' / 'gumstalker-x86-glue.S' endif endif @@ -245,7 +237,6 @@ if host_arch == 'arm' 'arch-arm' / 'gumthumbreader.c', 'backend-arm' / 'gumcpucontext-arm.c', 'backend-arm' / 'guminterceptor-arm.c', - 'backend-arm' / 'gumspinlock-arm.c', 'backend-arm' / 'gumstalker-arm.c', 'backend-arm' / 'gumstalker-arm-glue.S', ] @@ -266,7 +257,6 @@ if host_arch == 'arm64' 'backend-arm64' / 'gumcpucontext-arm64.c', 'backend-arm64' / 'guminterceptor-arm64.c', 'backend-arm64' / 'guminterceptor-arm64-glue.S', - 'backend-arm64' / 'gumspinlock-arm64.c', 'backend-arm64' / 'gumstalker-arm64.c', 'backend-arm64' / 'gumstalker-arm64-glue.S', 'backend-arm64' / 'memcpy-advsimd.S', @@ -287,7 +277,6 @@ if host_arch in ['mips', 'mips64'] 'arch-mips' / 'gummipsreader.c', 'backend-mips' / 'gumcpucontext-mips.c', 'backend-mips' / 'guminterceptor-mips.c', - 'backend-mips' / 'gumspinlock-mips.c', 'backend-mips' / 'gumstalker-mips.c', ] endif