From dbe919344e88f3bcc27b21b6aaffcd3bc76e25e9 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 18 Dec 2023 17:11:31 +0000 Subject: [PATCH] Changes to use promise for async waits --- bindings/gumjs/gumquickprocess.c | 52 ++------------------------------ bindings/gumjs/gumv8process.cpp | 51 ++----------------------------- bindings/gumjs/runtime/core.js | 10 ++++++ tests/gumjs/script.c | 35 +++++++++++---------- 4 files changed, 35 insertions(+), 113 deletions(-) diff --git a/bindings/gumjs/gumquickprocess.c b/bindings/gumjs/gumquickprocess.c index e0b6599818..7a5f87a8f6 100644 --- a/bindings/gumjs/gumquickprocess.c +++ b/bindings/gumjs/gumquickprocess.c @@ -112,8 +112,7 @@ static gboolean gum_emit_range (const GumRangeDetails * details, GUMJS_DECLARE_FUNCTION (gumjs_process_enumerate_system_ranges) GUMJS_DECLARE_FUNCTION (gumjs_process_enumerate_malloc_ranges) GUMJS_DECLARE_FUNCTION (gumjs_process_set_exception_handler) -GUMJS_DECLARE_FUNCTION (gumjs_process_run_on_thread_sync) -GUMJS_DECLARE_FUNCTION (gumjs_process_run_on_thread_async) +GUMJS_DECLARE_FUNCTION (gumjs_process_run_on_thread) static GumQuickExceptionHandler * gum_quick_exception_handler_new ( JSValue callback, GumQuickCore * core); @@ -145,8 +144,7 @@ static const JSCFunctionListEntry gumjs_process_entries[] = JS_CFUNC_DEF ("_enumerateMallocRanges", 0, gumjs_process_enumerate_malloc_ranges), JS_CFUNC_DEF ("setExceptionHandler", 0, gumjs_process_set_exception_handler), - JS_CFUNC_DEF ("runOnThreadSync", 0, gumjs_process_run_on_thread_sync), - JS_CFUNC_DEF ("runOnThreadAsync", 0, gumjs_process_run_on_thread_async), + JS_CFUNC_DEF ("_runOnThread", 0, gumjs_process_run_on_thread), }; void @@ -638,51 +636,7 @@ gum_quick_exception_handler_on_exception (GumExceptionDetails * details, return handled; } -GUMJS_DEFINE_FUNCTION (gumjs_process_run_on_thread_sync) -{ - GumQuickScope scope = GUM_QUICK_SCOPE_INIT (core); - GumThreadId thread_id; - JSValue user_func; - GumQuickRunOnThreadContext sync_ctx; - GumStalker * stalker; - gboolean success; - - if (!_gum_quick_args_parse (args, "ZF", &thread_id, &user_func)) - return JS_EXCEPTION; - - if (thread_id == 0) - return JS_UNDEFINED; - - _gum_quick_scope_suspend (&scope); - - sync_ctx.core = core; - sync_ctx.scope = scope; - sync_ctx.user_func = user_func; - sync_ctx.sync = TRUE; - - stalker = gum_stalker_new (); - - success = gum_stalker_run_on_thread_sync (stalker, thread_id, - gum_js_process_run_cb, &sync_ctx); - _gum_quick_scope_resume (&scope); - - while (gum_stalker_garbage_collect (stalker)) - g_usleep (10000); - - g_object_unref (stalker); - - if (success) - { - return JS_UNDEFINED; - } - else - { - _gum_quick_throw_literal (ctx, "Failed to run on thread"); - return JS_EXCEPTION; - } -} - -GUMJS_DEFINE_FUNCTION (gumjs_process_run_on_thread_async) +GUMJS_DEFINE_FUNCTION (gumjs_process_run_on_thread) { GumQuickScope scope = GUM_QUICK_SCOPE_INIT (core); GumThreadId thread_id; diff --git a/bindings/gumjs/gumv8process.cpp b/bindings/gumjs/gumv8process.cpp index dcaba4f830..0b28cb2872 100644 --- a/bindings/gumjs/gumv8process.cpp +++ b/bindings/gumjs/gumv8process.cpp @@ -93,8 +93,7 @@ static gboolean gum_emit_range (const GumRangeDetails * details, GUMJS_DECLARE_FUNCTION (gumjs_process_enumerate_system_ranges) GUMJS_DECLARE_FUNCTION (gumjs_process_enumerate_malloc_ranges) GUMJS_DECLARE_FUNCTION (gumjs_process_set_exception_handler) -GUMJS_DECLARE_FUNCTION (gumjs_process_run_on_thread_sync) -GUMJS_DECLARE_FUNCTION (gumjs_process_run_on_thread_async) +GUMJS_DECLARE_FUNCTION (gumjs_process_run_on_thread) static GumV8ExceptionHandler * gum_v8_exception_handler_new ( Local callback, GumV8Core * core); @@ -128,9 +127,7 @@ static const GumV8Function gumjs_process_functions[] = { "enumerateSystemRanges", gumjs_process_enumerate_system_ranges }, { "_enumerateMallocRanges", gumjs_process_enumerate_malloc_ranges }, { "setExceptionHandler", gumjs_process_set_exception_handler }, - { "runOnThreadSync", gumjs_process_run_on_thread_sync }, - { "runOnThreadAsync", gumjs_process_run_on_thread_async }, - + { "_runOnThread", gumjs_process_run_on_thread }, { NULL, NULL } }; @@ -529,49 +526,7 @@ gum_v8_exception_handler_on_exception (GumExceptionDetails * details, return handled; } - -GUMJS_DEFINE_FUNCTION (gumjs_process_run_on_thread_sync) -{ - GumThreadId thread_id; - Local user_func; - GumV8RunOnThreadContext sync_ctx; - GumStalker * stalker; - gboolean success; - - auto isolate = core->isolate; - auto context = isolate->GetCurrentContext (); - - if (!_gum_v8_args_parse (args, "ZF", &thread_id, &user_func)) - return; - - if (thread_id == 0) - return; - - stalker = gum_stalker_new (); - - { - ScriptUnlocker unlocker (core); - sync_ctx.core = core; - sync_ctx.isolate = isolate; - sync_ctx.context = context; - sync_ctx.user_func = user_func; - - success = gum_stalker_run_on_thread_sync (stalker, thread_id, - gum_js_process_run_cb, &sync_ctx); - } - - while (gum_stalker_garbage_collect (stalker)) - g_usleep (10000); - - g_object_unref (stalker); - - if (success) - info.GetReturnValue ().Set (sync_ctx.ret.ToLocalChecked ()); - else - _gum_v8_throw_ascii_literal (isolate, "Failed to run on thread"); -} - -GUMJS_DEFINE_FUNCTION (gumjs_process_run_on_thread_async) +GUMJS_DEFINE_FUNCTION (gumjs_process_run_on_thread) { GumThreadId thread_id; Local user_func; diff --git a/bindings/gumjs/runtime/core.js b/bindings/gumjs/runtime/core.js index 90a03b8861..3aeac2019a 100644 --- a/bindings/gumjs/runtime/core.js +++ b/bindings/gumjs/runtime/core.js @@ -453,6 +453,16 @@ Object.defineProperties(Process, { return range; } }, + runOnThread: { + enumerable: true, + value: function (threadId, callback, data) { + return new Promise((resolve) => { + Process._runOnThread(threadId, function () { + resolve(callback(data)); + }); + }); + }, + }, }); if (Process.findRangeByAddress === undefined) { diff --git a/tests/gumjs/script.c b/tests/gumjs/script.c index 939d347205..bf67366272 100644 --- a/tests/gumjs/script.c +++ b/tests/gumjs/script.c @@ -5325,13 +5325,10 @@ TESTCASE (process_can_run_on_thread_sync) COMPILE_AND_LOAD_SCRIPT ( "const threads = Process.enumerateThreads();" "const thread = threads.find(t => t.id == " GUM_PTR_CONST ");" - "const data = 1338;" - "var out_val = 0;" - "const ret = Process.runOnThreadSync(thread.id, function (ctx) {" - " send (data);" - " out_val = 1339;" - "});" - "send (out_val)", + "Process.runOnThread(thread.id, function (ctx) {" + " send (ctx);" + " return 1339;" + "}, 1338).then((r) => send(r));", thread_id); EXPECT_SEND_MESSAGE_WITH ("1338"); @@ -5358,21 +5355,27 @@ TESTCASE (process_can_run_on_thread_async) "async function run () {" " const threads = Process.enumerateThreads();" " const thread = threads.find(t => t.id == " GUM_PTR_CONST ");" - " const data = 1338;" - " let res;" - " const prom = new Promise (function (resolve, reject) {" - " res = resolve;" - " });" - " const ret = Process.runOnThreadAsync(thread.id, function (ctx) {" - " send (data);" - " res();" + " let resolve;" + " const promise = new Promise ((r) => {" + " resolve = r;" " });" - " await prom;" + " let ret = Process.runOnThread(thread.id, function (ctx) {" + " send(ctx);" + " promise.then((r) => {send(r);});" + " return 1340;" + " }, 1337);" + " Thread.sleep(0.2);" + " send(1338);" + " resolve(1339);" + " send(await ret);" "};" "run();", thread_id); + EXPECT_SEND_MESSAGE_WITH ("1337"); EXPECT_SEND_MESSAGE_WITH ("1338"); + EXPECT_SEND_MESSAGE_WITH ("1339"); + EXPECT_SEND_MESSAGE_WITH ("1340"); done = TRUE; g_thread_join (thread);