From bdddf3633d50dff8d8f3c399d40c6ed70a2f611b Mon Sep 17 00:00:00 2001 From: Sai Cao <1665673333@qq.com> Date: Thu, 22 Feb 2024 19:11:15 +0800 Subject: [PATCH 1/3] Fix: Stalker disrupts exclusive access by emitting a block event. Stalker generates the block event even when those blocks have exclusive access. This may disrupt the exclusive access when a user handles the block event. It seems that the default stalker event sinker uses spinlocks, which can starve the following thread. --- gum/backend-arm64/gumstalker-arm64.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gum/backend-arm64/gumstalker-arm64.c b/gum/backend-arm64/gumstalker-arm64.c index b734f923d..381409cbe 100644 --- a/gum/backend-arm64/gumstalker-arm64.c +++ b/gum/backend-arm64/gumstalker-arm64.c @@ -2831,7 +2831,7 @@ gum_stalker_iterator_next (GumStalkerIterator * self, self->generator_context->instruction = instruction; - if (is_first_instruction && (self->exec_context->sink_mask & GUM_BLOCK) != 0) + if (is_first_instruction && (self->exec_context->sink_mask & GUM_BLOCK) != 0 && (self->exec_block->flags&GUM_EXEC_BLOCK_USES_EXCLUSIVE_ACCESS)==0) { gum_exec_block_write_block_event_code (self->exec_block, gc, GUM_CODE_INTERRUPTIBLE); @@ -5445,7 +5445,7 @@ gum_exec_block_write_exec_event_code (GumExecBlock * block, GumCodeContext cc) { gum_exec_block_open_prolog (block, GUM_PROLOG_FULL, gc, gc->code_writer); - + gum_arm64_writer_put_call_address_with_arguments (gc->code_writer, GUM_ADDRESS (gum_exec_ctx_emit_exec_event), 3, GUM_ARG_ADDRESS, GUM_ADDRESS (block->ctx), From b308f84961906d7205d3877aeb8bab12cb94e7d7 Mon Sep 17 00:00:00 2001 From: Sai Cao <1665673333@qq.com> Date: Fri, 23 Feb 2024 09:28:11 +0800 Subject: [PATCH 2/3] Fix code style --- gum/backend-arm64/gumstalker-arm64.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/gum/backend-arm64/gumstalker-arm64.c b/gum/backend-arm64/gumstalker-arm64.c index 381409cbe..54e860eae 100644 --- a/gum/backend-arm64/gumstalker-arm64.c +++ b/gum/backend-arm64/gumstalker-arm64.c @@ -2831,7 +2831,9 @@ gum_stalker_iterator_next (GumStalkerIterator * self, self->generator_context->instruction = instruction; - if (is_first_instruction && (self->exec_context->sink_mask & GUM_BLOCK) != 0 && (self->exec_block->flags&GUM_EXEC_BLOCK_USES_EXCLUSIVE_ACCESS)==0) + if (is_first_instruction && + (self->exec_context->sink_mask & GUM_BLOCK) != 0 && + (self->exec_block->flags&GUM_EXEC_BLOCK_USES_EXCLUSIVE_ACCESS)==0) { gum_exec_block_write_block_event_code (self->exec_block, gc, GUM_CODE_INTERRUPTIBLE); @@ -5445,7 +5447,6 @@ gum_exec_block_write_exec_event_code (GumExecBlock * block, GumCodeContext cc) { gum_exec_block_open_prolog (block, GUM_PROLOG_FULL, gc, gc->code_writer); - gum_arm64_writer_put_call_address_with_arguments (gc->code_writer, GUM_ADDRESS (gum_exec_ctx_emit_exec_event), 3, GUM_ARG_ADDRESS, GUM_ADDRESS (block->ctx), From 58ac4a1beb07de7b75a653d5e1741e1865ad3999 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20Andr=C3=A9=20Vadla=20Ravn=C3=A5s?= Date: Fri, 23 Feb 2024 23:34:43 +0100 Subject: [PATCH 3/3] Apply some tweaks --- gum/backend-arm64/gumstalker-arm64.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/gum/backend-arm64/gumstalker-arm64.c b/gum/backend-arm64/gumstalker-arm64.c index 54e860eae..f854a7afb 100644 --- a/gum/backend-arm64/gumstalker-arm64.c +++ b/gum/backend-arm64/gumstalker-arm64.c @@ -5,6 +5,7 @@ * Copyright (C) 2023 Håvard Sørbø * Copyright (C) 2024 Francesco Tamagni * Copyright (C) 2024 Alex Soler + * Copyright (C) 2024 Sai Cao <1665673333@qq.com> * * Licence: wxWindows Library Licence, Version 3.1 */ @@ -2833,7 +2834,7 @@ gum_stalker_iterator_next (GumStalkerIterator * self, if (is_first_instruction && (self->exec_context->sink_mask & GUM_BLOCK) != 0 && - (self->exec_block->flags&GUM_EXEC_BLOCK_USES_EXCLUSIVE_ACCESS)==0) + (self->exec_block->flags & GUM_EXEC_BLOCK_USES_EXCLUSIVE_ACCESS) == 0) { gum_exec_block_write_block_event_code (self->exec_block, gc, GUM_CODE_INTERRUPTIBLE); @@ -5447,6 +5448,7 @@ gum_exec_block_write_exec_event_code (GumExecBlock * block, GumCodeContext cc) { gum_exec_block_open_prolog (block, GUM_PROLOG_FULL, gc, gc->code_writer); + gum_arm64_writer_put_call_address_with_arguments (gc->code_writer, GUM_ADDRESS (gum_exec_ctx_emit_exec_event), 3, GUM_ARG_ADDRESS, GUM_ADDRESS (block->ctx),