Skip to content

Commit

Permalink
Changes to use promise for async waits
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Dec 18, 2023
1 parent 7fe6a04 commit dbe9193
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 113 deletions.
52 changes: 3 additions & 49 deletions bindings/gumjs/gumquickprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
51 changes: 3 additions & 48 deletions bindings/gumjs/gumv8process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Function> callback, GumV8Core * core);
Expand Down Expand Up @@ -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 }
};

Expand Down Expand Up @@ -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<Function> 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<Function> user_func;
Expand Down
10 changes: 10 additions & 0 deletions bindings/gumjs/runtime/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
35 changes: 19 additions & 16 deletions tests/gumjs/script.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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);
Expand Down

0 comments on commit dbe9193

Please sign in to comment.