From e99fd4b97ce8238c5028e5e32155669ddbf7be02 Mon Sep 17 00:00:00 2001 From: Lucas Koplin Date: Mon, 23 Nov 2026 18:04:37 -0300 Subject: [PATCH 1/2] feat(macos_host): winmm, ole32, setupapi, gdi32, shell32 and oleaut32 imports SDL3 needs gta6rt.exe built with SDL3 (the GameInput shim's controller backend) imports these; the gameinput ctest stopped at winmm!timeBeginPeriod. COM, the device tree and GDI answer as a machine without the class, device or screen DC; CommandLineToArgvW follows Windows' quoting rules and CoWaitForMultipleHandles reuses WaitForMultipleObjects. guest/shell_probe.c checks them. Co-Authored-By: Claude Opus 5.5 --- runtime/macos_host/build.sh | 5 +- runtime/macos_host/guest/shell_probe.c | 73 ++++++++++++++ runtime/macos_host/host.cpp | 2 +- runtime/macos_host/host.h | 1 + runtime/macos_host/kernel32_crt.cpp | 10 +- runtime/macos_host/win32_misc.cpp | 126 +++++++++++++++++++++++++ 6 files changed, 214 insertions(+), 3 deletions(-) create mode 100644 runtime/macos_host/guest/shell_probe.c create mode 100644 runtime/macos_host/win32_misc.cpp diff --git a/runtime/macos_host/build.sh b/runtime/macos_host/build.sh index 6a74368a79..bf3e6c2d9f 100755 --- a/runtime/macos_host/build.sh +++ b/runtime/macos_host/build.sh @@ -15,7 +15,7 @@ mkdir -p "$OUT" "$OUT/guest.obj" "$OUT/kernel32.lib" /out:"$OUT/guest_fixed.exe" VKH=${VKH:-/opt/homebrew/opt/vulkan-headers/include} MVK=${MVK:-$HOME/.local/share/moltenvk/MoltenVK/MoltenVK/dynamic/dylib/macOS} # universal MoltenVK release -/usr/bin/clang++ -arch x86_64 -std=c++17 -O1 -Wall -Wextra -I"$VKH" host.cpp kernel32.cpp kernel32_crt.cpp kernel32_file.cpp d3d12_serialize.cpp seh.cpp kernel32_loader.cpp threadpool.cpp crt_lib.cpp crt.cpp eh.cpp illegal.cpp vulkan.cpp \ +/usr/bin/clang++ -arch x86_64 -std=c++17 -O1 -Wall -Wextra -I"$VKH" host.cpp kernel32.cpp kernel32_crt.cpp kernel32_file.cpp d3d12_serialize.cpp seh.cpp kernel32_loader.cpp threadpool.cpp crt_lib.cpp win32_misc.cpp crt.cpp eh.cpp illegal.cpp vulkan.cpp \ window.mm -framework Cocoa -framework QuartzCore -Wl,-rpath,"$MVK" -o "$OUT/macos_host" /usr/bin/clang++ -arch x86_64 -std=c++17 -O1 -Wall illegal_probe.cpp illegal.cpp -o "$OUT/illegal_probe" # run it: all ok @@ -45,3 +45,6 @@ $CL /c guest/pool_probe.c /Fo"$OUT/pool_probe.obj" # run it: exit code 0 "$LLD/lld-link" /nologo /winsysroot:"$XWIN_DIR" "$OUT/pool_probe.obj" /out:"$OUT/pool_probe.exe" $CL /c guest/crt_probe.c /Fo"$OUT/crt_probe.obj" # run it: exit code 0 "$LLD/lld-link" /nologo /winsysroot:"$XWIN_DIR" "$OUT/crt_probe.obj" /out:"$OUT/crt_probe.exe" +$CL /c guest/shell_probe.c /Fo"$OUT/shell_probe.obj" # run it: exit code 0 +"$LLD/lld-link" /nologo /winsysroot:"$XWIN_DIR" "$OUT/shell_probe.obj" shell32.lib ole32.lib oleaut32.lib winmm.lib \ + setupapi.lib /out:"$OUT/shell_probe.exe" diff --git a/runtime/macos_host/guest/shell_probe.c b/runtime/macos_host/guest/shell_probe.c new file mode 100644 index 0000000000..9d1430f368 --- /dev/null +++ b/runtime/macos_host/guest/shell_probe.c @@ -0,0 +1,73 @@ +// The winmm, ole32, setupapi, shell32 and oleaut32 imports an SDL3 build of gta6rt.exe adds. Exit code 0, or the +// number of the first check that failed. +#include +#include +#include +#include +#include +#include +#include + +// argv[0] is "p", then the expected arguments. NULL-terminated. +static int args_are(const wchar_t* line, const wchar_t* const* want) { + int n = 0; + wchar_t** argv = CommandLineToArgvW(line, &n); + if (!argv) return 0; + int ok = 1, i = 0; + for (; want[i]; ++i) ok = ok && i < n && wcscmp(argv[i], want[i]) == 0; + ok = ok && n == i && argv[n] == NULL; + return LocalFree(argv) == NULL && ok; +} + +int main(void) { + // Microsoft's CommandLineToArgvW examples, behind a program name. + static const wchar_t* const c1[] = {L"p", L"abc", L"d", L"e", NULL}; + static const wchar_t* const c2[] = {L"p", L"a\\\\b", L"de fg", L"h", NULL}; + static const wchar_t* const c3[] = {L"p", L"a\\\"b", L"c", L"d", NULL}; + static const wchar_t* const c4[] = {L"p", L"a\\\\b c", L"d", L"e", NULL}; + static const wchar_t* const c5[] = {L"p", L"ab\" c d", NULL}; + if (!args_are(L"p \"abc\" d e", c1)) return 1; + if (!args_are(L"p a\\\\b d\"e f\"g h", c2)) return 2; + if (!args_are(L"p a\\\\\\\"b c d", c3)) return 3; + if (!args_are(L"p a\\\\\\\\\"b c\" d e", c4)) return 4; + if (!args_are(L"p a\"b\"\" c d", c5)) return 5; + // The program name takes no escapes: a quoted one ends at the next quote, a trailing backslash included. + static const wchar_t* const c6[] = {L"C:\\a b\\", L"x", NULL}; + if (!args_are(L"\"C:\\a b\\\" x", c6)) return 6; + // An empty line gives the module's path, as on Windows. + int n = 0; + wchar_t** argv = CommandLineToArgvW(L"", &n); + if (!argv || n != 1 || !argv[0][0] || argv[1]) return 7; + LocalFree(argv); + + HANDLE set = CreateEventW(NULL, TRUE, TRUE, NULL), unset = CreateEventW(NULL, TRUE, FALSE, NULL); + HANDLE both[2] = {unset, set}; + DWORD index = 99; + if (CoWaitForMultipleHandles(0, 0, 2, both, &index) != S_OK || index != 1) return 8; + if (CoWaitForMultipleHandles(0, 10, 1, &unset, &index) != RPC_S_CALLPENDING) return 9; + if (CoWaitForMultipleHandles(COWAIT_WAITALL, 10, 2, both, &index) != RPC_S_CALLPENDING) return 10; + + if (timeBeginPeriod(1) != TIMERR_NOERROR || timeEndPeriod(1) != TIMERR_NOERROR) return 11; + + if (CoInitializeEx(NULL, COINIT_MULTITHREADED) != S_OK) return 12; + APTTYPE type = APTTYPE_STA; + APTTYPEQUALIFIER qualifier = APTTYPEQUALIFIER_IMPLICIT_MTA; + if (CoGetApartmentType(&type, &qualifier) != S_OK || type != APTTYPE_MTA || qualifier != APTTYPEQUALIFIER_NONE) return 13; + static const GUID nothing = {0x12345678, 0x1234, 0x1234, {1, 2, 3, 4, 5, 6, 7, 8}}; + void* object = (void*)1; + if (CoCreateInstance(¬hing, NULL, CLSCTX_ALL, ¬hing, &object) != REGDB_E_CLASSNOTREG || object) return 14; + CoUninitialize(); + SysFreeString(NULL); + + // SDL builds its pref path by appending "\\" parts, which the host maps to '/'. + wchar_t path[MAX_PATH] = {0}; + if (SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, 0, path) != S_OK || !path[0]) return 15; + wcscat_s(path, MAX_PATH, L"\\"); + DWORD attributes = GetFileAttributesW(path); + if (attributes == INVALID_FILE_ATTRIBUTES || !(attributes & FILE_ATTRIBUTE_DIRECTORY)) return 16; + if ((INT_PTR)ShellExecuteW(NULL, L"open", L"x", NULL, NULL, SW_SHOW) > 32) return 17; + + DEVINST dev = 0; + if (CM_Locate_DevNodeA(&dev, NULL, 0) != CR_NO_SUCH_DEVNODE) return 18; + return 0; +} diff --git a/runtime/macos_host/host.cpp b/runtime/macos_host/host.cpp index 89d6140b4e..5c6a270230 100644 --- a/runtime/macos_host/host.cpp +++ b/runtime/macos_host/host.cpp @@ -621,7 +621,7 @@ int main(int argc, char** argv) { execv(self, argv); // falls through on failure: the guest still runs, without AVX in CPUID } } - for (auto* list : {&kernel32_exports, &crt_exports, &eh_exports, &vulkan_exports, &user32_exports, &kernel32_crt_exports, &kernel32_file_exports, &d3d12_serialize_exports, &seh_exports, &kernel32_loader_exports, &threadpool_exports, &crt_lib_exports}) + for (auto* list : {&kernel32_exports, &crt_exports, &eh_exports, &vulkan_exports, &user32_exports, &kernel32_crt_exports, &kernel32_file_exports, &d3d12_serialize_exports, &seh_exports, &kernel32_loader_exports, &threadpool_exports, &crt_lib_exports, &win32_misc_exports}) for (auto& [name, fn] : (*list)()) g_exports[name] = fn; char real[PATH_MAX]; diff --git a/runtime/macos_host/host.h b/runtime/macos_host/host.h index 3884121578..a1ace980d5 100644 --- a/runtime/macos_host/host.h +++ b/runtime/macos_host/host.h @@ -130,6 +130,7 @@ Exports seh_exports(); Exports kernel32_loader_exports(); Exports threadpool_exports(); Exports crt_lib_exports(); +Exports win32_misc_exports(); uint32_t wait_object(void* h, uint32_t ms); // WaitForSingleObject from host code } // namespace host diff --git a/runtime/macos_host/kernel32_crt.cpp b/runtime/macos_host/kernel32_crt.cpp index d832e6bf10..73cbefd952 100644 --- a/runtime/macos_host/kernel32_crt.cpp +++ b/runtime/macos_host/kernel32_crt.cpp @@ -428,6 +428,14 @@ uint32_t WaitForMultipleObjects(uint32_t n, HANDLE* hs, int32_t all, uint32_t ms usleep(1000); } } +// ole32's wait: the same, with a timeout as RPC_S_CALLPENDING. ponytail: no COM messages to pump, flags past +// COWAIT_WAITALL are ignored. +int32_t CoWaitForMultipleHandles(uint32_t flags, uint32_t ms, uint32_t n, HANDLE* hs, uint32_t* index) { + uint32_t r = WaitForMultipleObjects(n, hs, flags & 1, ms); + if (r == 0x102) return static_cast(0x80010115); // WAIT_TIMEOUT -> RPC_S_CALLPENDING + *index = r; + return 0; +} uint32_t WaitForSingleObjectEx(HANDLE h, uint32_t ms, int32_t) { auto* o = static_cast(h); if (o->kind == Object::Thread) { @@ -802,7 +810,7 @@ Exports kernel32_crt_exports() { K(GetSystemInfo), K(GetNativeSystemInfo), K(GetCurrentProcessorNumber), K(FlushProcessWriteBuffers), K(GetCurrentThread), K(DuplicateHandle), K(SwitchToThread), K(AddVectoredExceptionHandler), K(EncodePointer), K(CreateEventW), K(CreateEventA), K(CreateEventExW), K(CreateSemaphoreW), K(CreateSemaphoreExW), K(SetEvent), K(ResetEvent), K(AreFileApisANSI), K(FormatMessageA), K(LocalFree), K(GetTempPathW), - EXPORT("ntdll.dll", wine_get_version), EXPORT("dxgi.dll", CreateDXGIFactory1), EXPORT_AS("d3d12.dll", "#102", D3D12GetDebugInterface), EXPORT_AS("gameinput.dll", "#1", GameInputCreate), K(CreateMutexA), K(ReleaseMutex), K(InitializeCriticalSection), EXPORT_AS("kernel32.dll", "SetThreadPriority", set_ok), EXPORT_AS("kernel32.dll", "SetProcessPriorityBoost", set_ok), EXPORT_AS("kernel32.dll", "GetThreadPriority", thread_priority), EXPORT_AS("kernel32.dll", "ResumeThread", resume_thread), EXPORT_AS("kernel32.dll", "SuspendThread", suspend_thread), K(GetThreadGroupAffinity), K(SetThreadGroupAffinity), K(GetProcessId), K(OpenProcess), K(OpenThread), K(GetUserDefaultGeoName), EXPORT_AS("api-ms-win-core-psm-appnotify-l1-1-0.dll", "RegisterAppStateChangeNotification", register_app_notification), EXPORT_AS("api-ms-win-core-psm-appnotify-l1-1-0.dll", "UnregisterAppStateChangeNotification", unregister_app_notification), EXPORT_AS("api-ms-win-core-psm-appnotify-l1-1-1.dll", "RegisterAppConstrainedChangeNotification", register_app_notification), EXPORT_AS("api-ms-win-core-psm-appnotify-l1-1-1.dll", "UnregisterAppConstrainedChangeNotification", unregister_app_notification), EXPORT_AS("msvcp140.dll", "?_Random_device@std@@YAIXZ", random_device), EXPORT("gameinput.dll", GameInputCreate), K(ReleaseSemaphore), K(WaitForSingleObjectEx), K(WaitForSingleObject), K(WaitForMultipleObjects), K(ExitThread), K(FreeLibraryAndExitThread), + EXPORT("ntdll.dll", wine_get_version), EXPORT("dxgi.dll", CreateDXGIFactory1), EXPORT_AS("d3d12.dll", "#102", D3D12GetDebugInterface), EXPORT_AS("gameinput.dll", "#1", GameInputCreate), K(CreateMutexA), K(ReleaseMutex), K(InitializeCriticalSection), EXPORT_AS("kernel32.dll", "SetThreadPriority", set_ok), EXPORT_AS("kernel32.dll", "SetProcessPriorityBoost", set_ok), EXPORT_AS("kernel32.dll", "GetThreadPriority", thread_priority), EXPORT_AS("kernel32.dll", "ResumeThread", resume_thread), EXPORT_AS("kernel32.dll", "SuspendThread", suspend_thread), K(GetThreadGroupAffinity), K(SetThreadGroupAffinity), K(GetProcessId), K(OpenProcess), K(OpenThread), K(GetUserDefaultGeoName), EXPORT_AS("api-ms-win-core-psm-appnotify-l1-1-0.dll", "RegisterAppStateChangeNotification", register_app_notification), EXPORT_AS("api-ms-win-core-psm-appnotify-l1-1-0.dll", "UnregisterAppStateChangeNotification", unregister_app_notification), EXPORT_AS("api-ms-win-core-psm-appnotify-l1-1-1.dll", "RegisterAppConstrainedChangeNotification", register_app_notification), EXPORT_AS("api-ms-win-core-psm-appnotify-l1-1-1.dll", "UnregisterAppConstrainedChangeNotification", unregister_app_notification), EXPORT_AS("msvcp140.dll", "?_Random_device@std@@YAIXZ", random_device), EXPORT("gameinput.dll", GameInputCreate), K(ReleaseSemaphore), K(WaitForSingleObjectEx), K(WaitForSingleObject), K(WaitForMultipleObjects), EXPORT("ole32.dll", CoWaitForMultipleHandles), K(ExitThread), K(FreeLibraryAndExitThread), K(GetExitCodeThread), EXPORT("advapi32.dll", OpenProcessToken), EXPORT("advapi32.dll", OpenThreadToken), K(DecodePointer), }; } diff --git a/runtime/macos_host/win32_misc.cpp b/runtime/macos_host/win32_misc.cpp new file mode 100644 index 0000000000..07ad25f4c4 --- /dev/null +++ b/runtime/macos_host/win32_misc.cpp @@ -0,0 +1,126 @@ +// The winmm, ole32, setupapi, gdi32, shell32 and oleaut32 calls SDL3 (the GameInput shim's controller backend) adds +// to gta6rt.exe. There is no COM, device tree or GDI here, so those answer as a Windows machine without the class, +// the device or a screen DC would; CommandLineToArgvW is real. CoWaitForMultipleHandles is in kernel32_crt.cpp, next to +// the wait it uses. +#include "host.h" + +#include +#include +#include + +namespace host { +using HANDLE = void*; +namespace { + +uint32_t timeBeginPeriod(uint32_t) { return 0; } // TIMERR_NOERROR: the host's sleeps are already fine-grained +uint32_t timeEndPeriod(uint32_t) { return 0; } + +int32_t CoInitializeEx(void*, uint32_t) { return 0; } // S_OK +void CoUninitialize() {} +int32_t CoGetApartmentType(int32_t* type, int32_t* qualifier) { + *type = 1; // APTTYPE_MTA + *qualifier = 0; // APTTYPEQUALIFIER_NONE + return 0; +} +int32_t CoCreateInstance(const void*, void*, uint32_t, const void*, void** out) { + if (out) *out = nullptr; + return static_cast(0x80040154); // REGDB_E_CLASSNOTREG +} +void SysFreeString(void*) {} // ponytail: no BSTR can come from here, CoCreateInstance never succeeds + +constexpr uint32_t kNoSuchDevnode = 0x0D; // CR_NO_SUCH_DEVNODE +uint32_t CM_Locate_DevNodeA(uint32_t*, const char*, uint32_t) { return kNoSuchDevnode; } +uint32_t CM_Get_Parent(uint32_t*, uint32_t, uint32_t) { return kNoSuchDevnode; } +uint32_t CM_Get_Device_IDA(uint32_t, char*, uint32_t, uint32_t) { return kNoSuchDevnode; } + +// SDL draws cursors with these; without a screen DC they fail and SDL keeps the system cursor. +HANDLE CreateBitmap(int32_t, int32_t, uint32_t, uint32_t, const void*) { return nullptr; } +HANDLE CreateCompatibleDC(HANDLE) { return nullptr; } +HANDLE CreateDIBSection(HANDLE, const void*, uint32_t, void** bits, HANDLE, uint32_t) { + if (bits) *bits = nullptr; + return nullptr; +} +int32_t DeleteDC(HANDLE) { return 0; } +int32_t DeleteObject(HANDLE) { return 0; } +HANDLE SelectObject(HANDLE, HANDLE) { return nullptr; } +uint32_t SetPixel(HANDLE, int32_t, int32_t, uint32_t) { return 0xFFFFFFFFu; } // CLR_INVALID + +HANDLE ShellExecuteW(HANDLE, const char16_t*, const char16_t*, const char16_t*, const char16_t*, int32_t) { + return reinterpret_cast(2); // SE_ERR_FNF: a value of 32 or less is a failure +} +// ponytail: every folder is Application Support; SDL only asks for CSIDL_APPDATA (its pref path). +int32_t SHGetFolderPathW(HANDLE, int32_t, HANDLE, uint32_t, char16_t* out) { + const char* home = std::getenv("HOME"); + std::u16string path = widen((std::string(home ? home : "") + "/Library/Application Support").c_str()); + if (!home || path.size() >= 260) return static_cast(0x80004005); // E_FAIL; out is MAX_PATH + std::memcpy(out, path.c_str(), (path.size() + 1) * 2); + return 0; +} + +// Windows' rules: the program name runs to the next quote if it starts with one, else to a blank, with no escapes. +// After it, 2n backslashes and a quote give n backslashes and toggle quoting, 2n+1 give n and a literal quote, "" inside +// quotes is a literal quote, and other backslashes are literal. One block the caller frees with LocalFree. +char16_t** CommandLineToArgvW(const char16_t* line, int32_t* count) { + auto blank = [](char16_t c) { return c == u' ' || c == u'\t'; }; + std::vector args(1); + const char16_t* p = line; + if (!*p) args[0] = widen(g_exe_path.c_str()); + else if (*p == u'"') { + for (++p; *p && *p != u'"';) args[0] += *p++; + if (*p) ++p; + } else + while (*p && !blank(*p)) args[0] += *p++; + for (;;) { + while (blank(*p)) ++p; + if (!*p) break; + std::u16string a; + bool quoted = false; + while (*p && (quoted || !blank(*p))) { + if (*p == u'\\') { + size_t n = 0; + for (; *p == u'\\'; ++p) ++n; + if (*p != u'"') a.append(n, u'\\'); + else { + a.append(n / 2, u'\\'); + if (n % 2) a += *p++; + } + } else if (*p == u'"') { + if (quoted && p[1] == u'"') a += u'"', p += 2; + else quoted = !quoted, ++p; + } else + a += *p++; + } + args.push_back(a); + } + size_t bytes = (args.size() + 1) * sizeof(char16_t*); + for (auto& a : args) bytes += (a.size() + 1) * sizeof(char16_t); + auto** argv = static_cast(std::malloc(bytes)); + auto* text = reinterpret_cast(argv + args.size() + 1); + for (size_t i = 0; i < args.size(); ++i) { + argv[i] = text; + std::memcpy(text, args[i].c_str(), (args[i].size() + 1) * sizeof(char16_t)); + text += args[i].size() + 1; + } + argv[args.size()] = nullptr; + *count = static_cast(args.size()); + return argv; +} + +} // namespace + +Exports win32_misc_exports() { + return { + EXPORT("winmm.dll", timeBeginPeriod), EXPORT("winmm.dll", timeEndPeriod), + EXPORT("ole32.dll", CoInitializeEx), EXPORT("ole32.dll", CoUninitialize), EXPORT("ole32.dll", CoGetApartmentType), + EXPORT("ole32.dll", CoCreateInstance), EXPORT_AS("oleaut32.dll", "#6", SysFreeString), + EXPORT("setupapi.dll", CM_Locate_DevNodeA), EXPORT("setupapi.dll", CM_Get_Parent), + EXPORT("setupapi.dll", CM_Get_Device_IDA), + EXPORT("gdi32.dll", CreateBitmap), EXPORT("gdi32.dll", CreateCompatibleDC), EXPORT("gdi32.dll", CreateDIBSection), + EXPORT("gdi32.dll", DeleteDC), EXPORT("gdi32.dll", DeleteObject), EXPORT("gdi32.dll", SelectObject), + EXPORT("gdi32.dll", SetPixel), + EXPORT("shell32.dll", ShellExecuteW), EXPORT("shell32.dll", SHGetFolderPathW), + EXPORT("shell32.dll", CommandLineToArgvW), + }; +} + +} // namespace host From d4da9dcccf7641c06ec2e6e887cc2956f3bf409a Mon Sep 17 00:00:00 2001 From: Lucas Koplin Date: Mon, 23 Nov 2026 18:06:28 -0300 Subject: [PATCH 2/2] fix(macos_host): GetModuleFileName reports a path with '\' separators SDL's GetExeName and GetBasePath split the module path at its last '\', so the host's POSIX path made SDL_Init crash in strlen(NULL). The path still opens: to_host_path maps '\' back to '/'. shell_probe checks 19-22. Co-Authored-By: Claude Opus 5.5 --- runtime/macos_host/guest/shell_probe.c | 12 ++++++++++++ runtime/macos_host/host.cpp | 7 +++++++ runtime/macos_host/host.h | 1 + runtime/macos_host/kernel32_crt.cpp | 7 ++++--- runtime/macos_host/win32_misc.cpp | 2 +- 5 files changed, 25 insertions(+), 4 deletions(-) diff --git a/runtime/macos_host/guest/shell_probe.c b/runtime/macos_host/guest/shell_probe.c index 9d1430f368..04f4f49573 100644 --- a/runtime/macos_host/guest/shell_probe.c +++ b/runtime/macos_host/guest/shell_probe.c @@ -19,6 +19,9 @@ static int args_are(const wchar_t* line, const wchar_t* const* want) { return LocalFree(argv) == NULL && ok; } +static int has_w(const wchar_t* s, wchar_t c) { for (; *s; ++s) if (*s == c) return 1; return 0; } +static int has_a(const char* s, char c) { for (; *s; ++s) if (*s == c) return 1; return 0; } + int main(void) { // Microsoft's CommandLineToArgvW examples, behind a program name. static const wchar_t* const c1[] = {L"p", L"abc", L"d", L"e", NULL}; @@ -69,5 +72,14 @@ int main(void) { DEVINST dev = 0; if (CM_Locate_DevNodeA(&dev, NULL, 0) != CR_NO_SUCH_DEVNODE) return 18; + + // SDL's GetExeName and GetBasePath split the module path at its last '\\' (a POSIX path crashed them in strlen), + // and the path still has to open. + wchar_t module[MAX_PATH]; + char module_a[MAX_PATH]; + if (!GetModuleFileNameW(NULL, module, MAX_PATH) || !has_w(module, L'\\') || has_w(module, L'/')) return 19; + if (GetFileAttributesW(module) == INVALID_FILE_ATTRIBUTES) return 20; + if (!GetModuleFileNameA(NULL, module_a, MAX_PATH) || !has_a(module_a, '\\') || has_a(module_a, '/')) return 21; + if (!args_are(L"", (const wchar_t* const[]){module, NULL})) return 22; // an empty line's argv[0] is the same path return 0; } diff --git a/runtime/macos_host/host.cpp b/runtime/macos_host/host.cpp index 5c6a270230..744fd85bc8 100644 --- a/runtime/macos_host/host.cpp +++ b/runtime/macos_host/host.cpp @@ -99,6 +99,13 @@ uint32_t thread_id() { std::string g_game_dir; // absolute folder of the game image, where GAME:\ points std::string g_exe_path; +// Callers split a module path at its last '\\' (SDL's GetExeName crashed on a POSIX one); to_host_path maps it back. +std::string exe_windows_path() { + std::string s = g_exe_path; + for (auto& c : s) + if (c == '/') c = '\\'; + return s; +} // API sets are names for functions that live in one real DLL: api-ms-win-core-* in kernel32, api-ms-win-crt-* in // the UCRT, whose functions the host registers under whichever api-ms-win-crt-* set first needed them. diff --git a/runtime/macos_host/host.h b/runtime/macos_host/host.h index a1ace980d5..e3881491fe 100644 --- a/runtime/macos_host/host.h +++ b/runtime/macos_host/host.h @@ -39,6 +39,7 @@ void set_last_error(uint32_t e); std::string to_host_path(const char* windows_path); [[noreturn]] void fail(const std::string& why); extern std::string g_exe_path; // host path of the game image +std::string exe_windows_path(); // the same with '\\' separators, as GetModuleFileName reports it std::string narrow(const char16_t* s, int64_t n = -1); // UTF-16 -> UTF-8 std::u16string widen(const char* s, int64_t n = -1); // UTF-8 -> UTF-16 void* find_export(const std::string& key); // "dll!name" (dll lowercase) -> entry, or nullptr diff --git a/runtime/macos_host/kernel32_crt.cpp b/runtime/macos_host/kernel32_crt.cpp index 73cbefd952..1437f60e13 100644 --- a/runtime/macos_host/kernel32_crt.cpp +++ b/runtime/macos_host/kernel32_crt.cpp @@ -158,15 +158,16 @@ int GetModuleHandleExW(uint32_t flags, const char16_t* name, HANDLE* out) { return *out != nullptr; } uint32_t GetModuleFileNameW(HANDLE, char16_t* buf, uint32_t n) { - std::u16string s = widen(g_exe_path.c_str()); + std::u16string s = widen(exe_windows_path().c_str()); uint32_t len = static_cast(std::min(s.size(), n ? n - 1 : 0)); std::memcpy(buf, s.data(), len * 2); if (n) buf[len] = 0; return len; } uint32_t GetModuleFileNameA(HANDLE, char* buf, uint32_t n) { - uint32_t len = static_cast(std::min(g_exe_path.size(), n ? n - 1 : 0)); - std::memcpy(buf, g_exe_path.data(), len); + std::string s = exe_windows_path(); + uint32_t len = static_cast(std::min(s.size(), n ? n - 1 : 0)); + std::memcpy(buf, s.data(), len); if (n) buf[len] = 0; return len; } diff --git a/runtime/macos_host/win32_misc.cpp b/runtime/macos_host/win32_misc.cpp index 07ad25f4c4..d187573b7f 100644 --- a/runtime/macos_host/win32_misc.cpp +++ b/runtime/macos_host/win32_misc.cpp @@ -64,7 +64,7 @@ char16_t** CommandLineToArgvW(const char16_t* line, int32_t* count) { auto blank = [](char16_t c) { return c == u' ' || c == u'\t'; }; std::vector args(1); const char16_t* p = line; - if (!*p) args[0] = widen(g_exe_path.c_str()); + if (!*p) args[0] = widen(exe_windows_path().c_str()); else if (*p == u'"') { for (++p; *p && *p != u'"';) args[0] += *p++; if (*p) ++p;