Free fork of NotProton: Windows Steam games in Mac Steam, on CodeWeavers' Wine and Apple's Game Porting Toolkit
Swift apple-silicond3dmetaldxmtdxvkgame-porting-toolkitgptkmac-gamingmacosmetalfxrosetta-2steamsteam-playwinewinecx
test(sigdb): build the live fixtures from the installed Steam client
raymondchinse788cf58785b
diff --git a/Makefile b/Makefileindex 0998dcf..c80384b 100644--- a/Makefile+++ b/Makefile@@ -290,8 +290,14 @@ sigdb-fixtures:@if [ ! -d $(SIGDB_FIXTURES) ] || [ ! -f dylib/tests/anchorcheck.c ]; then \$(call SKIP,sigdb-fixtures,$(SIGDB_FIXTURES)); exit 0; fi; \$(MAKE) -s $(ANCHORCHECK) || exit 1; \- fail=0; n=0; \- for f in $(SIGDB_FIXTURES)/*.json; do \+ fail=0; n=0; live=$(OUT_DIR)/sigdb-fixtures-live; mkdir -p $$live || exit 1; \+ for t in $(SIGDB_FIXTURES)/live/*.json; do \+ out=$$($(ANCHORCHECK) --rebase "$$t" "$$live/$${t##*/}" 2>&1); rc=$$?; \+ if [ $$rc -ne 0 ]; then \+ echo "cannot build $$t against the installed client (rc=$$rc):"; \+ echo "$$out" | tail -1; exit 1; fi; \+ done; \+ for f in $(SIGDB_FIXTURES)/*.json $$live/*.json; do \n=$$((n+1)); \want=$$(awk -v name="$${f##*/}" '$$2 == name { print $$1 }' $(SIGDB_FIXTURES)/EXPECTED); \out=$$($(ANCHORCHECK) "$$f" 2>&1); rc=$$?; \diff --git a/dylib/tests/anchorcheck.c b/dylib/tests/anchorcheck.cindex cc341d5..b6bfdf0 100644--- a/dylib/tests/anchorcheck.c+++ b/dylib/tests/anchorcheck.c@@ -3,7 +3,10 @@// and bytes recorded when the database was cut. Loaded rather than on disk because dyld// rebases and ObjC rewrites selector slots. Other builds must land on the live addresses.// Usage: out/anchorcheck [path-to-sigdb.json ...]+// out/anchorcheck --rebase TEMPLATE OUT (see rebase_fixture)+#include "cJSON.h"+#include "../util/file.h"#include "../resolver/sigdb.h"#include "../resolver/resolver.h"#include "../resolver/anchor.h"@@ -396,6 +399,116 @@ static int report_cross_db(const dbcheck_t *dbs, int n) {return bad;}+// The first data slot holding `fn`, read after dyld has bound the image.+static uintptr_t data_slot_holding(const image_t *im, uintptr_t fn) {+ static const char *const segs[] = {"__DATA_CONST", "__AUTH_CONST", "__DATA"};+ for (size_t s = 0; s < sizeof(segs) / sizeof(segs[0]); s++) {+ uintptr_t base;+ size_t size;+ if (np_find_segment(im->mh, im->slide, segs[s], &base, &size) != 0) continue;+ for (size_t off = 0; off + sizeof(uintptr_t) <= size; off += sizeof(uintptr_t))+ if (*(const uintptr_t *)(base + off) == fn) return base + off;+ }+ return 0;+}++// The shortest exact run of bytes from `fn` that occurs nowhere else in __TEXT.+static int sole_pattern_at(const image_t *im, uintptr_t fn, char *out, size_t out_sz) {+ for (size_t len = 16; len <= 256 && len * 3 <= out_sz; len += 4) {+ if (fn + len > im->text + im->text_sz) return -1;+ size_t pos = 0;+ for (size_t i = 0; i < len; i++)+ pos += (size_t)snprintf(out + pos, out_sz - pos, i ? " %02X" : "%02X",+ ((const uint8_t *)fn)[i]);+ np_byte_pattern_t pat;+ if (np_compile_pattern(out, &pat) != 0) return -1;+ uintptr_t hit = np_scan_sole_match(im->text, im->text_sz, &pat);+ np_release_pattern(&pat);+ if (hit == fn) return 0;+ }+ return -1;+}++// Writes `tmpl` to `out` with the build-specific fields taken from the installed client,+// so a fixture that has to describe the running client keeps doing so after a Steam update.+// Each signature's anchor is resolved here; its result becomes func_addr_this_build. A+// signature marked "fixture_rebase": "vtable_slot" also gets its anchor swapped for a data+// slot holding that function and a byte pattern that matches only there.+static int rebase_fixture(const char *tmpl, const char *out) {+ np_sigdb_t db = {0};+ if (np_load_profile(tmpl, &db) != 0) {+ fprintf(stderr, "cannot load fixture template '%s'\n", tmpl);+ return 2;+ }+ size_t len = 0;+ uint8_t *text = np_read_whole_file(tmpl, &len);+ cJSON *root = text ? cJSON_ParseWithLength((const char *)text, len) : NULL;+ free(text);+ cJSON *arr = root ? cJSON_GetObjectItem(root, "signatures") : NULL;+ if (!arr || cJSON_GetArraySize(arr) != db.sig_count) {+ fprintf(stderr, "cannot parse fixture template '%s'\n", tmpl);+ cJSON_Delete(root);+ np_free_profile(&db);+ return 2;+ }++ int rc = 0;+ for (int i = 0; i < db.sig_count && !rc; i++) {+ np_sig_entry_t *sig = &db.signatures[i];+ cJSON *elem = cJSON_GetArrayItem(arr, i);+ const image_t *im = load_image(sig->module) == 0 ? image_for(sig->module) : NULL;+ uintptr_t fn = im ? np_locate_anchor(im->mh, im->slide, im->text, im->text_sz,+ &sig->anchor) : 0;+ if (!fn) {+ fprintf(stderr, "%s: the anchor of '%s' does not resolve in the installed "+ "client, so this fixture cannot be built here\n", tmpl, sig->name);+ rc = 3;+ break;+ }++ char hex[32];+ cJSON *mode = cJSON_GetObjectItem(elem, "fixture_rebase");+ if (cJSON_IsString(mode) && strcmp(mode->valuestring, "vtable_slot") == 0) {+ uintptr_t slot = data_slot_holding(im, fn);+ char pattern[sizeof(sig->aob_hex)];+ if (!slot || sole_pattern_at(im, fn, pattern, sizeof(pattern)) != 0) {+ fprintf(stderr, "%s: no data slot or unique byte pattern for '%s' in the "+ "installed client\n", tmpl, sig->name);+ rc = 3;+ break;+ }+ snprintf(hex, sizeof(hex), "0x%lx", (unsigned long)(slot - (uintptr_t)im->slide));+ cJSON *anchor = cJSON_CreateObject();+ cJSON_AddStringToObject(anchor, "kind", "vtable_slot");+ cJSON_AddStringToObject(anchor, "va", hex);+ cJSON_ReplaceItemInObject(elem, "anchor", anchor);+ cJSON_DeleteItemFromObject(elem, "aob_hex");+ cJSON_AddStringToObject(elem, "aob_hex", pattern);+ } else if (mode) {+ fprintf(stderr, "%s: unknown fixture_rebase for '%s'\n", tmpl, sig->name);+ rc = 2;+ break;+ }+ cJSON_DeleteItemFromObject(elem, "fixture_rebase");+ snprintf(hex, sizeof(hex), "0x%lx", (unsigned long)(fn - (uintptr_t)im->slide));+ cJSON_DeleteItemFromObject(elem, "func_addr_this_build");+ cJSON_AddStringToObject(elem, "func_addr_this_build", hex);+ }++ if (!rc) {+ char *json = cJSON_Print(root);+ FILE *fh = json ? fopen(out, "w") : NULL;+ if (!fh || fputs(json, fh) < 0 || fclose(fh) != 0) {+ fprintf(stderr, "cannot write '%s'\n", out);+ rc = 2;+ }+ free(json);+ }+ cJSON_Delete(root);+ np_free_profile(&db);+ return rc;+}+// Exit 1 is a database with problems, 2 one that could not be read, 3 this machine being// unable to answer. Callers feeding deliberately broken databases need those apart: a// rejected database is a result, an unloadable client image means nothing was checked.@@ -405,6 +518,11 @@ int main(int argc, char **argv) {if (load_image(NP_MODULE_DEFAULT) != 0) return 3;+ if (argc > 1 && strcmp(argv[1], "--rebase") == 0) {+ if (argc != 4) { fprintf(stderr, "usage: anchorcheck --rebase TEMPLATE OUT\n"); return 2; }+ return rebase_fixture(argv[2], argv[3]);+ }+char paths[MAX_DBS][512];int n = 0;for (int i = 1; i < argc && n < MAX_DBS; i++)diff --git a/dylib/tests/sigdb-fixtures/EXPECTED b/dylib/tests/sigdb-fixtures/EXPECTEDindex 5ebc2ad..9f8f47c 100644--- a/dylib/tests/sigdb-fixtures/EXPECTED+++ b/dylib/tests/sigdb-fixtures/EXPECTED@@ -3,9 +3,9 @@# rejected before it could be read, 1 is one read and reported as broken, 0 is# one read with nothing worth reporting.#-# The fixtures that claim the build of the client installed on the machine are-# judged against that client, so a client update can move them. The failure-# names the fixture and both statuses when that happens.+# The fixtures in live/ have to describe the client installed on the machine.+# They are templates: anchorcheck --rebase fills in the addresses and patterns+# from that client, so a Steam update cannot move their status.1 all_deprecated.json0 array_root.jsondiff --git a/dylib/tests/sigdb-fixtures/all_deprecated.json b/dylib/tests/sigdb-fixtures/live/all_deprecated.jsonsimilarity index 89%rename from dylib/tests/sigdb-fixtures/all_deprecated.jsonrename to dylib/tests/sigdb-fixtures/live/all_deprecated.jsonindex 34aac9e..1d6fb1a 100644--- a/dylib/tests/sigdb-fixtures/all_deprecated.json+++ b/dylib/tests/sigdb-fixtures/live/all_deprecated.json@@ -9,7 +9,6 @@{"name": "CCompatManager::Init","aob_hex": "EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE",- "func_addr_this_build": "0x690f6c","anchor": {"kind": "string","value": "STEAM_COMPAT_TOOL_MAPPINGS"@@ -19,7 +18,6 @@{"name": "CCompatManager::BIsCompatibilityToolEnabled","aob_hex": "EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE",- "func_addr_this_build": "0x6962f0","anchor": {"kind": "string","value": "CCompatManager::BIsCompatibilityToolEnabled"@@ -29,7 +27,6 @@{"name": "CCompatManager::YldRegisterTool","aob_hex": "EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE",- "func_addr_this_build": "0x699f00","match_offset": 168,"anchor": {"kind": "string",@@ -40,7 +37,6 @@{"name": "CCompatManager::GetOSListOverrideForApp.oslist_gate","match_offset": -8,- "func_addr_this_build": "0x695ed0","aob_hex": "EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE","anchor": {"kind": "string",@@ -53,7 +49,6 @@{"name": "CCompatManager::ResolveCompatToolForApp.local_redirect","match_offset": -8,- "func_addr_this_build": "0x695fc4","aob_hex": "EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE","anchor": {"kind": "string",@@ -65,7 +60,6 @@{"name": "CAppManager::TerminateProcessByPid","aob_hex": "EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE",- "func_addr_this_build": "0xd6f28","anchor": {"kind": "string","value": "terminate",@@ -77,7 +71,6 @@{"name": "CCompatManager::FindToolForTargetApp","aob_hex": "EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE",- "func_addr_this_build": "0x69577c","anchor": {"kind": "string","value": "FindToolForTargetApp matched with itself - ignoring: %u %s"@@ -87,7 +80,6 @@{"name": "CCompatManager::GetValidPlatforms","aob_hex": "EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE EE",- "func_addr_this_build": "0x69457c","anchor": {"kind": "string","value": "AppID %u does not advertise linux as a supported platform, but a linux specific depot is present.\n"@@ -95,4 +87,4 @@"deprecated": true}]-}\ No newline at end of file+}diff --git a/dylib/tests/sigdb-fixtures/live/vtable_va_real.json b/dylib/tests/sigdb-fixtures/live/vtable_va_real.jsonnew file mode 100644index 0000000..8cec486--- /dev/null+++ b/dylib/tests/sigdb-fixtures/live/vtable_va_real.json@@ -0,0 +1,18 @@+{+ "schema_version": 3,+ "profile_version": 1,+ "arch": "arm64",+ "platform": "macos",+ "steam_build": 0,+ "steam_build_date": "",+ "signatures": [+ {+ "name": "CCompatManager::Init",+ "fixture_rebase": "vtable_slot",+ "anchor": {+ "kind": "string",+ "value": "STEAM_COMPAT_TOOL_MAPPINGS"+ }+ }+ ]+}diff --git a/dylib/tests/sigdb-fixtures/vtable_va_real.json b/dylib/tests/sigdb-fixtures/vtable_va_real.jsondeleted file mode 100644index 09649a4..0000000--- a/dylib/tests/sigdb-fixtures/vtable_va_real.json+++ /dev/null@@ -1,19 +0,0 @@-{- "schema_version": 3,- "profile_version": 1,- "arch": "arm64",- "platform": "macos",- "steam_build": 0,- "steam_build_date": "",- "signatures": [- {- "name": "CCompatManager::Init",- "aob_hex": "?? ?? ?? D1 FC 6F 0B A9 FA 67 0C A9 F8 5F 0D A9 F6 57 0E A9 F4 4F 0F A9 FD 7B 10 A9 FD 03 04 91 F4 03 01 AA F3 03 00 AA ?? ?? ?? ?? 08 A9 40 F9 08 01 40 F9 ?? ?? ?? ?? 29 01 28 91 A8 03 1A F8 09 00 00 F9 F9 03 00 AA 3F 8F 00 F8 1F 08 00 F9",- "func_addr_this_build": "0x690f6c",- "anchor": {- "kind": "vtable_slot",- "va": "0x167c2b8"- }- }- ]-}\ No newline at end of file