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
Fix races in the default compatibility tool picker
NotProtonNota056d7edd32f
diff --git a/dylib/feats/compat.c b/dylib/feats/compat.cindex a26d2a7..23af056 100644--- a/dylib/feats/compat.c+++ b/dylib/feats/compat.c@@ -4,6 +4,7 @@#include "../util/file.h"#include <dirent.h>+#include <pthread.h>#include <stdint.h>#include <stdio.h>#include <stdlib.h>@@ -413,11 +414,7 @@ static int tools_dir_path(char *out, size_t size) {return 0;}-static void load_tool_list_once(void) {- static int loaded = 0;- if (loaded) return;- loaded = 1;-+static void load_tool_list(void) {const char *home = np_home_dir();char tools_dir[512];if (!home || tools_dir_path(tools_dir, sizeof(tools_dir)) != 0) return;@@ -427,6 +424,11 @@ static void load_tool_list_once(void) {np_compat_load_tool_list(path, tools_dir);}+static void load_tool_list_once(void) {+ static pthread_once_t once = PTHREAD_ONCE_INIT;+ pthread_once(&once, load_tool_list);+}+uint32_t np_compat_manager_tools_max(void) {load_tool_list_once();return COMPAT_MANAGER_TOOLS_HEADROOM + (uint32_t)g_tool_count;diff --git a/dylib/feats/webpatch.c b/dylib/feats/webpatch.cindex a9dc607..5312eef 100644--- a/dylib/feats/webpatch.c+++ b/dylib/feats/webpatch.c@@ -18,7 +18,7 @@// Stands for the name set by np_webpatch_set_fallback_tool.#define NP_FALLBACK_TOOL "\021"-static char g_fallback_tool[128];+static _Thread_local char g_fallback_tool[128];typedef struct {const char *find;diff --git a/dylib/feats/webpatch.h b/dylib/feats/webpatch.hindex 3d83f99..2d526cb 100644--- a/dylib/feats/webpatch.h+++ b/dylib/feats/webpatch.h@@ -7,8 +7,8 @@int np_webpatch_should_patch(const char *path);-// The tool the default tool picker shows before a default has been saved. Names with-// characters outside letters, digits, '_', '$', '.' and '-' are dropped.+// Sets the default picker fallback for this thread. Names with characters outside+// letters, digits, '_', '$', '.' and '-' are dropped.void np_webpatch_set_fallback_tool(const char *name);// The compat UIs, named so a caller can compare against the table rather than a literaldiff --git a/dylib/tests/compatcheck.c b/dylib/tests/compatcheck.cindex 5ed5e65..e1e497d 100644--- a/dylib/tests/compatcheck.c+++ b/dylib/tests/compatcheck.c@@ -4,6 +4,11 @@#include "../feats/compat.c"#include <stdio.h>+#include <pthread.h>+#include <stdatomic.h>++static atomic_int home_calls;+static atomic_int home_ready;// Silent, because half of these checks drive paths whose whole job is to refuse and log.int np_log_level = -1;@@ -15,6 +20,10 @@ int np_log_first_hit(const void *anchor, unsigned long tag) {}const char *np_home_dir(void) {+ if (atomic_fetch_add(&home_calls, 1) == 0) {+ usleep(50000);+ atomic_store(&home_ready, 1);+ }return "/nonexistent";}@@ -27,6 +36,30 @@ static void check(int ok, const char *what) {}}+static void *first_fallback(void *arg) {+ int *ok = arg;+ const char *name = np_compat_fallback_tool_name();+ *ok = atomic_load(&home_ready) && strcmp(name, "notproton") == 0;+ return NULL;+}++static void concurrent_fallback_cases(void) {+ pthread_t threads[16];+ int results[16] = {0};+ size_t started = 0;+ for (; started < 16; started++) {+ if (pthread_create(&threads[started], NULL, first_fallback, &results[started]) != 0) {+ check(0, "the fallback reader thread starts");+ break;+ }+ }+ for (size_t i = 0; i < started; i++) {+ pthread_join(threads[i], NULL);+ check(results[i], "every first fallback read waits for initialization");+ }+ check(atomic_load(&home_calls) == 2, "the tool list is initialized once");+}+// A64 encodings the probes look for.#define NOP 0xD503201Fustatic uint32_t ldr32_x0(uint32_t off, uint32_t rt) { return 0xB9400000u | ((off / 4u) << 10) | rt; }@@ -243,6 +276,8 @@ static void tool_list_cases(void) {unlink(path);check(kept == 40 && strcmp(g_tools[39].name, "notproton-39") == 0&& strcmp(g_tools[0].dir, "/tools/notproton-0") == 0, "a long list keeps every tool");+ check(strcmp(np_compat_fallback_tool_name(), "notproton-0") == 0,+ "the fallback is the first configured tool");check(np_compat_load_tool_list("out/compatcheck-missing", "/tools") == 0&& !g_tool_list_present, "a missing list keeps no tools and says so");@@ -402,6 +437,7 @@ static void installed_fn_cases(void) {}int main(void) {+ concurrent_fallback_cases();probe_cases();oslist_cases();enabled_cases();diff --git a/dylib/tests/gatecheck.c b/dylib/tests/gatecheck.cindex 5f67321..d3ed25a 100644--- a/dylib/tests/gatecheck.c+++ b/dylib/tests/gatecheck.c@@ -4,6 +4,8 @@#include <stdarg.h>#include <stdio.h>+#include <pthread.h>+#include <stdatomic.h>// Satisfies the log macros without pulling in log.c, which reaches for the// support directory this tool has no use for. Drift reasons land on stderr.@@ -22,6 +24,45 @@ static void wrong(const char *fmt, ...) {g_wrong = 1;}+static void *rewrite_fallback(void *arg) {+ atomic_int *stop = arg;+ while (!atomic_load(stop))+ np_webpatch_set_fallback_tool("notproton-fex");+ return NULL;+}++static void check_fallback(void) {+ static const char source[] =+ "return t?(0,i.jsx)(m.B,{feature:p.OK,label:(0,l.we)"+ "(\"#Settings_SteamPlay_DefaultTool\"),rgOptions:A,disabled:0==A.length,"+ "selectedOption:t,onChange:e=>g(e.data)}):null";+ np_webpatch_set_fallback_tool("notproton-fex");+ atomic_int stop = 0;+ pthread_t writer;+ if (pthread_create(&writer, NULL, rewrite_fallback, &stop) != 0) {+ wrong("the fallback writer thread did not start");+ return;+ }+ int saved = np_log_level;+ np_log_level = -1;+ for (int i = 0; i < 10000; i++) {+ size_t length = 0;+ char *out = np_webpatch_transform((const uint8_t *)source, sizeof(source) - 1,+ &length, NULL);+ int ok = out && !memchr(out, '\0', length)+ && strstr(out, "A.some(e=>e.data===\"notproton-fex\")")+ && strstr(out, "selectedOption:t||\"notproton-fex\"");+ free(out);+ if (!ok) {+ wrong("concurrent fallback writes changed a chunk's tool name");+ break;+ }+ }+ atomic_store(&stop, 1);+ pthread_join(writer, NULL);+ np_log_level = saved;+}+// Invariants the transform relies on but cannot check at run time. A replacement still// holding its own anchor would be found again next pass, and an anchor inside another// anchor would make the result depend on table order rather than on the chunk.@@ -213,7 +254,7 @@ int main(int argc, char **argv) {if (argc == 4 && strcmp(argv[1], "--extract") == 0)return extract(argv[2], argv[3]);- if (argc < 2) { selfcheck(); return g_wrong; }+ if (argc < 2) { selfcheck(); check_fallback(); return g_wrong; }np_webpatch_set_fallback_tool("notproton-fex");