Skip to content

Free fork of NotProton: Windows Steam games in Mac Steam, on CodeWeavers' Wine and Apple's Game Porting Toolkit

fix(dylib): keep prefix backups and never guess a Steam address

raymondchins308230b6e2a8
diff --git a/dylib/feats/compatsvc.c b/dylib/feats/compatsvc.c
index 9d71504..8179baf 100644
--- a/dylib/feats/compatsvc.c
+++ b/dylib/feats/compatsvc.c
@@ -406,7 +406,9 @@ static int run_detached(char *const argv[]) {
#define REBUILD_SCRIPT \
"log=$1; shift; \"$0\" --rebuild-prefix \"$@\" >> \"$log\" 2>&1 < /dev/null &"
-static int start_rebuild(uintptr_t request, uintptr_t response, int keep_backup) {
+// Any SteamUI page can call these routes, so a route never discards the original prefix:
+// saves outside drive_c/users (registry, game folder) would go with it.
+static int start_rebuild(uintptr_t request, uintptr_t response) {
uint32_t appid = request_appid(request);
np_prefix_state_t st;
if (!read_state(appid, &st)) return RESULT_FAIL;
@@ -437,8 +439,7 @@ static int start_rebuild(uintptr_t request, uintptr_t response, int keep_backup)
&& ftruncate(fd, 0) == 0 && write(fd, "setup\n0\n", 8) == 8;
if (fd >= 0) close(fd);
char *argv[] = { "/bin/sh", "-c", REBUILD_SCRIPT, st.app, log, st.compatdata,
- st.tool, st.tool_build, st.tool_flavor,
- keep_backup ? "keep-backup" : "discard-backup", NULL };
+ st.tool, st.tool_build, st.tool_flavor, "keep-backup", NULL };
if (!recorded) {
refused = "spawn";
} else if (!run_detached(argv)) {
@@ -450,8 +451,7 @@ static int start_rebuild(uintptr_t request, uintptr_t response, int keep_backup)
if (refused)
NP_WARN("compatsvc: app %u was not rebuilt (%s)", appid, refused);
else
- NP_LOG("compatsvc: rebuilding app %u for %s, %s the original", appid, st.tool,
- keep_backup ? "keeping" : "discarding");
+ NP_LOG("compatsvc: rebuilding app %u for %s, keeping the original", appid, st.tool);
char json[64];
snprintf(json, sizeof json, refused ? "{\"error\":\"%s\"}" : "{\"started\":true}",
@@ -460,11 +460,11 @@ static int start_rebuild(uintptr_t request, uintptr_t response, int keep_backup)
}
static int rebuild_prefix(uintptr_t request, uintptr_t response) {
- return start_rebuild(request, response, 0);
+ return start_rebuild(request, response);
}
static int backup_and_rebuild_prefix(uintptr_t request, uintptr_t response) {
- return start_rebuild(request, response, 1);
+ return start_rebuild(request, response);
}
static int open_prefix_log(uintptr_t request, uintptr_t response) {
diff --git a/dylib/hooks/hooks.c b/dylib/hooks/hooks.c
index a306215..f02fc7a 100644
--- a/dylib/hooks/hooks.c
+++ b/dylib/hooks/hooks.c
@@ -12,6 +12,10 @@
extern int DobbyHook(void *address, void *replace_call, void **origin_call);
typedef void (*np_prehook_fn)(void *address, void *ctx);
extern int DobbyInstrument(void *address, np_prehook_fn pre_handler);
+extern int DobbyCodePatch(void *address, uint8_t *buffer, uint32_t buffer_size);
+
+// The longest trampoline Dobby writes on arm64: LDR, BR and an 8-byte literal.
+#define NP_TRAMPOLINE_MAX 16
int np_hooks_env_lists_label(const char *var, const char *label) {
const char *list = getenv(var);
@@ -66,6 +70,9 @@ static int np_apply_entry(np_resolve_result_t *resolved, const char *group,
return 0;
}
+ uint8_t before[NP_TRAMPOLINE_MAX];
+ memcpy(before, (const void *)addr, sizeof(before));
+
int prehook = (e->mode == NP_PATCH_PREHOOK);
int rc = prehook ? DobbyInstrument((void *)addr, (np_prehook_fn)e->entry)
: DobbyHook((void *)addr, e->entry, e->trampoline);
@@ -75,11 +82,17 @@ static int np_apply_entry(np_resolve_result_t *resolved, const char *group,
return 1;
}
- // Dobby patches the target before reporting the error and never registers the
- // entry, so the patch cannot be lifted.
- NP_ERR("[%s] %s: %s failed (ret=%d) @ %p, target may stay patched with no original; "
- "the hook degrades instead of calling it",
- group, e->label, prehook ? "DobbyInstrument" : "DobbyHook", rc, (void *)addr);
+ // Dobby writes the trampoline even when relocating the original failed, then
+ // returns an error with no original and no registered entry. Put the bytes back
+ // so Steam runs its own code; if that fails the hook degrades instead.
+ int patched = memcmp((const void *)addr, before, sizeof(before)) != 0;
+ int restored = patched && DobbyCodePatch((void *)addr, before, sizeof(before)) == 0
+ && memcmp((const void *)addr, before, sizeof(before)) == 0;
+ NP_ERR("[%s] %s: %s failed (ret=%d) @ %p, %s", group, e->label,
+ prehook ? "DobbyInstrument" : "DobbyHook", rc, (void *)addr,
+ !patched ? "target untouched"
+ : restored ? "original bytes restored"
+ : "target stays patched with no original, the hook degrades instead");
return 0;
}
diff --git a/dylib/resolver/resolver.c b/dylib/resolver/resolver.c
index 7a50a0a..5fdb0ee 100644
--- a/dylib/resolver/resolver.c
+++ b/dylib/resolver/resolver.c
@@ -139,10 +139,18 @@ static uintptr_t try_anchor(np_sig_entry_t *sig,
return alt;
}
- if (alt && alt != addr)
+ if (alt && alt != addr) {
+ // Two sources naming different function entries means at least one is wrong,
+ // and nothing here says which, so the feature stays off.
+ if (anchor_targets_function_entry(sig->anchor.kind)) {
+ NP_WARN("resolver: '%s' anchor 0x%lx disagrees with byte pattern 0x%lx; "
+ "rejecting", sig->name, (unsigned long)addr, (unsigned long)alt);
+ return 0;
+ }
NP_WARN("resolver: '%s' anchor 0x%lx disagrees with byte pattern 0x%lx; "
"keeping the anchor", sig->name, (unsigned long)addr,
(unsigned long)alt);
+ }
NP_LOG("resolver: '%s' -> 0x%lx [anchor]", sig->name, (unsigned long)addr);
return addr;
diff --git a/dylib/tests/callscheck.c b/dylib/tests/callscheck.c
index 5ba48a9..150c770 100644
--- a/dylib/tests/callscheck.c
+++ b/dylib/tests/callscheck.c
@@ -1,4 +1,6 @@
#include "resolver/anchor.h"
+#include "resolver/resolver.h"
+#include "resolver/sigdb.h"
#include <mach-o/loader.h>
#include <mach-o/nlist.h>
#include <stdio.h>
@@ -142,6 +144,34 @@ static void check_string(const char *label, int hops, int tail, uintptr_t expect
(unsigned long)expected, (unsigned long)got);
}
+// The whole resolver on the image: a function-entry anchor and a byte pattern that name
+// different entries leave the signature unresolved instead of trusting either one.
+static void check_resolved(const char *label, const char *aob, int32_t offset,
+ uintptr_t expected) {
+ static np_sig_entry_t sig;
+ memset(&sig, 0, sizeof(sig));
+ strcpy(sig.name, "Fixture::Entry");
+ strcpy(sig.module, "fixture.dylib");
+ strcpy(sig.aob_hex, aob);
+ sig.match_offset = offset;
+ sig.anchor = (np_anchor_t){.kind = NP_MATCH_CALLS, .call_count = 2,
+ .calls_exact = 1, .no_data_refs = 1};
+ strcpy(sig.anchor.calls[0], "_first");
+ strcpy(sig.anchor.calls[1], "_second");
+ np_sigdb_t db = {.signatures = &sig, .sig_count = 1};
+ np_resolve_result_t res = {0};
+ uintptr_t base = (uintptr_t)image;
+ np_resolve_signatures((void *)image, (intptr_t)base, &db, "fixture.dylib", &res);
+ uintptr_t got = np_lookup_address(&res, "Fixture::Entry");
+ np_free_resolution(&res);
+ if (got) got -= base;
+ cases++;
+ if (got == expected) return;
+ failures++;
+ printf("FAIL %s: expected 0x%lx, got 0x%lx\n", label,
+ (unsigned long)expected, (unsigned long)got);
+}
+
int main(void) {
reset();
check("exact match spanning more than 0x400 bytes", 1, 0x400);
@@ -264,6 +294,13 @@ int main(void) {
call(0x990, 0x900);
check_string("caller in the unbounded last function", 1, 0, 0);
+ reset();
+ check_resolved("anchor without a byte pattern", "", 0, 0x400);
+ check_resolved("anchor and byte pattern agree", "F8040094FA040094", 0x20, 0x400);
+ word(0x884, 0xAABBCCDD);
+ check_resolved("anchor and byte pattern name different entries",
+ "FF4300D1DDCCBBAA", 0, 0);
+
if (failures) {
printf("%d of %d anchor cases failed\n", failures, cases);
return 1;
diff --git a/dylib/tests/compatsvc-check.c b/dylib/tests/compatsvc-check.c
index bbad704..ae6dfc5 100644
--- a/dylib/tests/compatsvc-check.c
+++ b/dylib/tests/compatsvc-check.c
@@ -817,8 +817,8 @@ static void rebuild_route_cases(void) {
ready_to_rebuild();
rebuild_prefix(build_request(REQ_HAS_APPID, 480), (uintptr_t)response_buf);
- check(spawned_count == 10 && strcmp(spawned[9], "discard-backup") == 0,
- "a plain rebuild discards the original");
+ check(spawned_count == 10 && strcmp(spawned[9], "keep-backup") == 0,
+ "a plain rebuild from a page still keeps the original");
ready_to_rebuild();
spawn_result = 1;