Skip to content

Commit

Permalink
[WIP] Wire up more of the injectable test runner
Browse files Browse the repository at this point in the history
  • Loading branch information
oleavr committed Sep 20, 2023
1 parent 78fc865 commit 69d655a
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 42 deletions.
2 changes: 0 additions & 2 deletions tests/core/swiftapiresolver/loader.js

This file was deleted.

20 changes: 9 additions & 11 deletions tests/core/swiftapiresolver/run.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
import frida
from pathlib import Path
import sys
import time


def on_message(message, data):
print("on_message:", message)


runner_src_dir = Path(__file__).parent

runner_path = runner_src_dir.parent.parent.parent.parent / "build" / "tmp-macos-arm64" / "frida-gum" / "tests" / "core" / "swiftapiresolver" / "libtestswiftapiresolver.dylib"
print("runner_path:", runner_path)

device = frida.get_remote_device()

target = device.get_process("Xcode")

session = device.attach(target.pid)
session = device.attach("Xcode")

script = session.create_script((runner_src_dir / "runner.js").read_text(encoding="utf-8"))
script.on("message", on_message)
script.load()

state_buffer_location = script.exports.get_state_buffer_location()

device = frida.get_remote_device()
id = device.inject_library_file(target.pid, str(runner_path), "gum_swift_api_resolver_tests_main", state_buffer_location)
print("Injected:", id)
script.post({ "type": "start" }, runner_path.read_bytes())

sys.stdin.read()
print("Running...")
t1 = time.time()
num_matches = script.exports_sync.run("*!*")
t2 = time.time()
duration = int((t2 - t1) * 1000)
print(f"Got {num_matches} matches in {duration} ms.")
41 changes: 19 additions & 22 deletions tests/core/swiftapiresolver/runner.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,44 @@

#include "gum.h"

typedef guint FridaUnloadPolicy;

enum _FridaUnloadPolicy
{
FRIDA_UNLOAD_POLICY_IMMEDIATE,
FRIDA_UNLOAD_POLICY_RESIDENT,
FRIDA_UNLOAD_POLICY_DEFERRED,
};

static gboolean on_match (const GumApiDetails * details, gpointer user_data);

__attribute__ ((constructor)) static void
static GumApiResolver * resolver;

void
init (void)
{
gum_init_embedded ();

resolver = gum_api_resolver_make ("swift");
g_assert_nonnull (resolver);
}

void
gum_swift_api_resolver_tests_main (const gchar * agent_parameters,
FridaUnloadPolicy * unload_policy,
gpointer injector_state)
finalize (void)
{
*unload_policy = FRIDA_UNLOAD_POLICY_RESIDENT;
g_object_unref (resolver);

gum_deinit_embedded ();
}

void
gum_swift_api_resolver_tests_run (void)
guint
run (const gchar * query)
{
GumApiResolver * resolver;

resolver = gum_api_resolver_make ("swift");
g_assert_nonnull (resolver);
guint num_matches = 0;

gum_api_resolver_enumerate_matches (resolver, "*!*", on_match, NULL, NULL);
gum_api_resolver_enumerate_matches (resolver, query, on_match, &num_matches, NULL);

g_object_unref (resolver);
return num_matches;
}

static gboolean
on_match (const GumApiDetails * details,
gpointer user_data)
{
guint * num_matches = user_data;

(*num_matches)++;

return TRUE;
}
18 changes: 13 additions & 5 deletions tests/core/swiftapiresolver/runner.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
class Runner {
constructor() {
this.stateBuf = Memory.alloc(Process.pageSize);
this._cm = null;
this._run = null;
recv('start', this._onStart);
}

getStateBufferLocation() {
return this.stateBuf;
run(query) {
return this._run(Memory.allocUtf8String(query));
}

_onStart = (message, data) => {
this._cm = new CModule(data);
this._run = new NativeFunction(this._cm.run, 'uint', ['pointer'], { exceptions: 'propagate' });
};
}

const runner = new Runner();
globalThis.runner = runner;

rpc.exports = {
getStateBufferLocation: runner.getStateBufferLocation.bind(runner),
run(query) {
return runner.run(query);
}
};
4 changes: 3 additions & 1 deletion tests/core/swiftapiresolver/runner.symbols
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
_gum_swift_api_resolver_tests_run
_init
_finalize
_run
4 changes: 3 additions & 1 deletion tests/core/swiftapiresolver/runner.version
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
global:
gum_swift_api_resolver_tests_run;
init;
finalize;
run;

local:
*;
Expand Down

0 comments on commit 69d655a

Please sign in to comment.