Skip to content

macos_host: run gta6rt natively on macOS under Rosetta 2, no Wine - #7446

Draft
adrbn wants to merge 16 commits into
ItsNotRexed:mainfrom
adrbn:mac-native-host
Draft

adrbn wants to merge 16 commits into
ItsNotRexed:mainfrom
adrbn:mac-native-host

Conversation

@adrbn

@adrbn adrbn commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: new item, claim

What and why

A native macOS host for gta6rt, with no Wine. runtime/macos_host is a small x86-64 Mach-O that maps the clang-cl cross-built gta6rt.exe (and the PE images it loads) and runs it under Rosetta 2 on Apple Silicon. The Windows API surface gta6rt and a title reach is answered on POSIX and Cocoa: kernel32 (files, threads, events, mutexes, TLS, the thread pool, VirtualAlloc with real reserve/commit and placeholders), the UCRT pieces that differ from the host libc (32-bit long, UTF-16 wchar_t, the MS rand, wide printf), SEH and C++ exceptions with a multi-module unwinder, per-module static TLS, Vulkan on MoltenVK with the Win32 window as an NSWindow, and a GameInput stub with no device.

Everything is under runtime/macos_host/; nothing else in the tree changes, so it builds on no PR and conflicts with none.

Two things specific to Rosetta:

  • Rosetta 2 raises SIGILL on ADX, SHA, RDSEED, CLWB, SSE4a and CLZERO. The host patches the instructions it meets on first fault (a jump to an emulation stub) and emulates clzero. scan_exe.sh counts them in an image without quoting it.
  • The retail title reads its TLS from module index 0 (the /GA assumption that Loader: the mapped game executable owns module TLS index 0 (RT-259) #4313 fixes in the Windows loader). Here the host's own loader gives index 0 to the TLS host and index 1 to gta6rt.exe, so the two fixes don't touch each other.

To build and run (Homebrew llvm, lld, molten-vk):

cmake -S runtime -B runtime/build/windows-mac-mt -G Ninja \
  -DCMAKE_TOOLCHAIN_FILE=runtime/cmake/clang-cl-xwin.cmake -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded
ninja -C runtime/build/windows-mac-mt gta6rt
sh runtime/macos_host/build.sh
cd runtime/build/windows-mac-mt && ../../macos_host/build/macos_host gta6rt.exe --run <exe> [--game-dir DIR --overlay DIR]

Tested

Apple M1, 16 GB, macOS 27.0.1, Rosetta 2, MoltenVK 1.4.2 on Metal. gta6rt built with clang-cl and the static CRT (runtime/build/windows-mac-mt).

AI-assisted: yes

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (No translator change.)

adrbn added 16 commits November 23, 2026 01:26
…tatic TLS)

Split the host into host.cpp (PE loader, TEB, static TLS and TLS callbacks,
threads), kernel32.cpp and crt.cpp (UCRT, vcruntime140, msvcp140 std::thread,
xgameruntime). build.sh builds tiny_game.exe with xwin when present.

Tested on an M1 (macOS 27.0.1): guest.exe passes; tiny_game.exe passes
'hello 42', 'hello 42 tls', and 'exit-code' returns 42. Not done: eh
(needs .pdata unwinding), game-file (GAME: path mapping).
_CxxThrowException captures the thrower's registers in an assembly stub; the
host unwinds guest frames with the image's .pdata codes, reads the
__CxxFrameHandler3 tables, runs destructor funclets, copies the catch object,
calls the catch funclet and resumes at its continuation.

Tested on an M1 (macOS 27.0.1): tiny_game passes 'hello 42 eh' and 'tls eh';
the new guest/eh_probe.exe throws through 4 frames (4 destructors run),
catches by value and with catch(...). Not handled: rethrow, nested
exceptions, __finally, std::current_exception.
GAME:\ paths resolve to the image's real folder, whatever the current
directory; the image is loaded by its real path (argv[1] was overwritten by
the guest's argv[0] before loading, which broke runs from another folder).

Tested on an M1: tiny_game passes 'hello 42 tls eh game-file' run from
another directory; exit-code, eh_probe and guest.exe still pass.
… as Cocoa

vulkan-1.dll's vkGetInstanceProcAddr hands the guest MoltenVK functions behind
generated stubs into one MS-to-SysV converter that also switches gs; the
float-parameter commands get typed thunks. VK_KHR_win32_surface is answered
with VK_EXT_metal_surface on the window's CAMetalLayer. window.mm covers the
user32 calls of one window and its message pump. Needs the universal (x86_64)
MoltenVK from the KhronosGroup release, since the host runs under Rosetta.

Tested on an M1 (macOS 27.0.1): guest/vk_clear.exe (clang-cl + xwin) opens a
640x360 window, presents 180 cleared frames on a 1280x720 swapchain and reads
back the last frame's pixel (254,127,1 as cleared). tiny_game, eh_probe and
guest.exe still pass.
Static-CRT test builds load with three system DLLs; missing imports become
named traps; adds locks, init-once, events, semaphores, modules, text
conversion, ExitThread, and a fault report with symbols.
…for the static-CRT D3D12 tests

RaiseException, RtlUnwindEx (with unwind consolidation), RtlCaptureContext, RtlLookupFunctionEntry and
RtlVirtualUnwind on the existing .pdata unwinder, so /MT C++ exceptions are caught; eh_probe also builds with /MT.
std::filesystem calls (attributes, directories, FindFirstFile, disposition deletes), DuplicateHandle,
FormatMessageA and D3D12SerializeVersionedRootSignature (DXBC + RTS0). 35 of the 57 d3d12 tests pass under
MoltenVK on an M1, 14 skip on missing Apple GPU features.
… Retina pixels, fault reports

- Re-exec under Rosetta with ROSETTA_ADVERTISE_AVX=1 so CPUID reports AVX/AVX2/FMA (guest/cpuid_probe.c checks it)
- user32 window calls d3d12_present_test needs; Win32 sizes are pixels, not Retina points
- Bind ordinal imports (d3d12.lib's ItsNotRexed#101/ItsNotRexed#102) and stub D3D12GetDebugInterface
- Fault handler restores the host gs before printing, so a guest crash no longer deadlocks in fprintf
- d3d12 sweep: 36 pass, 15 skip, 6 fail (WARP/DXGI references, Metal sampler LOD bias, Metal NaN clears)
Load PE DLLs from disk with per-module TLS (gta6rt_tls_host.dll), RtlAddFunctionTable and a multi-module
unwinder, RtlPcToFileHeader for any module, VirtualAlloc reserve/commit at fixed addresses, VirtualQuery,
WaitForMultipleObjects. tiny_game passes under gta6rt --run with tls, eh, game-file and exit-code.
scan_exe.sh lists a game image's unanswered imports and the instructions Rosetta 2 cannot run.
… under gta6rt

- illegal.cpp: on the first SIGILL at an adcx/adox/insertq site, patch a jmp to a
  stub built from instructions Rosetta runs; clwb becomes NOPs, clzero is emulated.
  illegal_probe checks each against x86 semantics (incl. rsp- and rip-relative).
- TLS: EXE code may read its TLS at index 0 without loading _tls_index. Under
  gta6rt the game takes gta6rt_tls_host.dll's index, so that DLL now gets 0 and
  gta6rt.exe moves to 1. Before, the game wrote over gta6rt's thread_locals.
- API sets: api-ms-win-core-* resolve to kernel32 exports, api-ms-win-crt-* to
  any UCRT set the host registers.
- Handles to host-provided DLLs point at an empty PE header, so pe_loader's
  RtlPcToFileHeader redirect can walk them.
- Crash report: SIGTRAP, registers, and return addresses found on the stack.
…++ runtime DLLs

- VirtualAlloc2 / MapViewOfFile3 / UnmapViewOfFile2 / CreateFileMappingW on mmap and shm: placeholder
  reserve, split, replace, section views, and VirtualQuery reports a placeholder as MEM_RESERVE. This is
  what xmem's physical pages and 2 MiB pools need.
- The Windows thread pool (work, timers, waits, TrySubmitThreadpoolCallback) on guest threads.
- GetLogicalProcessorInformation from sysctl; CreateEventA.
- advapi32 RegOpenKey(Ex)W / RegQueryValueExW answer ERROR_FILE_NOT_FOUND: the unimplemented policy's 0
  reads as success with an empty value.
- LEONIDA_DLL_DIR: real PE DLLs from that folder (for example the x64 vcruntime140 / vcruntime140_1 of
  Microsoft's VC++ redistributable) win over the host stand-ins; plus the CRT entries vcruntime140 imports.
- guest/pool_probe.c covers the placeholder, thread pool and processor paths (exit code 0).
… with no devices, ordinal GetProcAddress

- crt_lib.cpp: the rest of the UCRT a retail title imports (math, ctype, narrow and UTF-16 strings, 32-bit long
  conversions, 64-bit time, stdio seeks, qsort/bsearch with guest comparators, the MS rand, errno, localeconv)
- wide __stdio_common_vswprintf family on the existing formatter (%s is wchar_t* in a wide format)
- GameInputCreate (GameInput.dll ordinal 1): an IGameInput that never reports a device
- GetProcAddress by ordinal for host DLLs; mutexes, InitializeCriticalSection, thread priority/affinity hints,
  app-state notifications that never fire, std::_Random_device
- crash report dumps the first words behind rbx/rcx/rsi/rdi
- guest/crt_probe.c checks the Windows-specific behaviour (exit code 0)
…tFileSizeEx/GetFileSize/GetFileTime, OpenProcess

The DirectStorage shim reads at an OVERLAPPED offset; the host read from the
file position instead. With GetFileSizeEx missing, a title saw every archive
as empty. crt_probe checks both.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant