Skip to content

Commit

Permalink
gumjs: Support returning value and binary data from RPC methods
Browse files Browse the repository at this point in the history
Co-authored-by: Håvard Sørbø <[email protected]>
  • Loading branch information
oleavr and hsorbo committed May 31, 2024
1 parent 52fa04d commit e4f72cf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
16 changes: 9 additions & 7 deletions bindings/gumjs/runtime/message-dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,15 @@ function MessageDispatcher() {
}
}

function reply(id, type, result, params) {
params = params || [];

if (result instanceof ArrayBuffer)
send(['frida:rpc', id, type, undefined].concat(params), result);
else
send(['frida:rpc', id, type, result].concat(params));
function reply(id, type, result, params = []) {
if (Array.isArray(result) && result.length === 2 && result[1] instanceof ArrayBuffer) {
const [value, data] = result;
send(['frida:rpc', id, type, value, ...params], data);
} else if (result instanceof ArrayBuffer) {
send(['frida:rpc', id, type, undefined, ...params], result);
} else {
send(['frida:rpc', id, type, result, ...params]);
}
}

function dispatchMessages() {
Expand Down
14 changes: 14 additions & 0 deletions tests/gumjs/script.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ TESTLIST_BEGIN (script)
TESTENTRY (method_can_return_null)
TESTENTRY (method_can_receive_binary_data)
TESTENTRY (method_can_return_binary_data)
TESTENTRY (method_can_return_value_and_binary_data)
TESTENTRY (method_list_can_be_queried)
TESTENTRY (calling_inexistent_method_should_throw_error)
TESTGROUP_END ()
Expand Down Expand Up @@ -6072,6 +6073,19 @@ TESTCASE (method_can_return_binary_data)
"59 6f");
}

TESTCASE (method_can_return_value_and_binary_data)
{
COMPILE_AND_LOAD_SCRIPT (
"rpc.exports.read = () => {"
"const buf = Memory.allocUtf8String(\"Yo\");"
"return [{meta: 'data'}, buf.readByteArray(2)];"
"};");
POST_MESSAGE ("[\"frida:rpc\",42,\"call\",\"read\",[]]");
EXPECT_SEND_MESSAGE_WITH_PAYLOAD_AND_DATA (
"[\"frida:rpc\",42,\"ok\",{\"meta\":\"data\"}]",
"59 6f");
}

TESTCASE (method_list_can_be_queried)
{
COMPILE_AND_LOAD_SCRIPT ("rpc.exports = { a() {}, b() {}, c() {} };");
Expand Down

0 comments on commit e4f72cf

Please sign in to comment.