Skip to content

Feat/tls slot0 stub - #9171

Closed
canalmaloqueru-design wants to merge 11 commits into
ProjectLeonida:mainfrom
canalmaloqueru-design:feat/tls-slot0-stub
Closed

canalmaloqueru-design wants to merge 11 commits into
ProjectLeonida:mainfrom
canalmaloqueru-design:feat/tls-slot0-stub

Conversation

@canalmaloqueru-design

Copy link
Copy Markdown

Claimed in the coordination issue as:

Depends on:

What and why

Tested

AI-assisted:

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.

@GonZiin

GonZiin commented Nov 23, 2026

Copy link
Copy Markdown

Review (AI-assisted, static: the Windows-only paths were not built or run on a GPU).

1. The "stub detected" test in ensure_tls_host matches almost any host EXE, not only the new stub (runtime/src/pe_loader.cpp, new block under if (slot == 0)).
The test is: the process EXE has a TLS directory, *AddressOfIndex == 0 and total size <= 512. The Windows loader gives index 0 to the first module with implicit TLS, which is the process EXE, so the index check is true for every EXE that has a TLS directory. A static-CRT test EXE (loader_test, pe_loader_test, test_pe_loader_bounds, all of which compile pe_loader.cpp in and call gta6rt_tls_host) has a small .tls section, so it passes the size check too. For those hosts g_tls_hosts[0].index is forced to 0 and the guest's TLS is swapped into the host's own slot 0, which is the collision the PR sets out to remove. The old gta6rt.exe build (scripts/build_runtime.ps1 cl.exe fallback, which this PR does not touch) would trigger it the same way. Nothing in the check tells the stub apart. Use a marker the stub exports or carries (for example an exported symbol or a named section checked in the EXE image), or pass the decision in explicitly from gta6rt_wmain.

2. The override leaves the helper's real slot unused but still hooked. load_tls_host_image(primary, true) has already registered on_host_thread_event with the helper DLL, and the helper's DLL block sits at its own index N. After index = 0 the swap/restore bookkeeping works on slot 0 (the stub's block pointer) instead. Please say in the comment (and ideally test) that restore_thread_slot puts the stub's block back on thread exit, since it now saves and restores a block that does not belong to the helper.

3. xbox_console_stubs.cpp duplicates work that exists or is claimed elsewhere, and half of it is dead code.

  • runtime/shims/xg_xs.cpp already registers xg_xs.dll!XGCreateTextureComputer and XGSuggestSwizzleMode, with signatures measured from the call sites (the second is HRESULT f(desc, 0, uint32_t*), not the one-argument uint32_t form used here). The two xg_xs stubs in this PR are not in its SHIM_TABLE, so they are unreferenced functions in an anonymous namespace (MSVC C4505 at /W4) and carry the wrong signature.
  • runtime: add initial bring-up shims for xcrdapi, xg_xs, chad, xapu, a… #9168 (same author) adds the same stubs for the same DLLs, and DStorageGetFactory has an open takeover PR (runtime: console DirectStorage (DStorageGetFactory) with file-to-memory reads (takeover of #4348) #8461, named in docs(audio): point DStorageGetFactory to open takeover PR #8461 #9193). I could not find claims for these in the coordination issue; please post one and say which PR is meant to land.
  • The stubs return S_OK for XDspSubmitCommand, XApuEnqueueCommand and others. docs/audio_hal_exe_calls.md measures the call-site shapes and says four of these imports branch on a negative result (AcpHalCreate, AcpHalAllocateShapeContexts, ApuCreateHeap, XApuDequeueResult). The LEONIDA_UNIMPLEMENTED policy already models "unimplemented import" (unimplemented_policy_test.cpp); a stub that returns success hides the same gap from that policy. If success is intended, cite the call site that shows the caller needs it.
  • ApuAlloc_stub reports apu_address as the low 32 bits of a host pointer. That is only a unique token if nothing ever maps it back, and ApuMapApuAddress_stub returns nullptr.

4. Packaging. The PR splits gta6rt.exe into a stub plus gta6rt_core.dll but scripts/build_runtime.ps1 still builds the single exe, the /BASE, /STACK and BUILD-1457 comments were deleted from CMakeLists.txt, and the dependency search for the DLL (LoadLibraryW on a full path, no LOAD_LIBRARY_SEARCH_* flags) is wider than the one load_tls_host_image deliberately restricts to System32. Please keep the two build paths in step (the old comment says to) and restrict the search the same way.

The PR description is still the template: no claim item, no "Tested", no AI-assisted line. Two unrelated changes (TLS anchor, console stubs) would be easier to review and land as two PRs.

@BatSmacker84

Copy link
Copy Markdown

Tested 9edab782e merged onto main 5d74c2723 (clean merge) on Windows 11 26200, MSVC 19.44 RelWithDebInfo: build fails.

  • gta6rt_core doesn't compile: MSVC C2084 (function already has a body) for XErrorReport at xgameruntime.cpp:265. main now defines it too, so the branch needs a rebase that drops one of the two definitions.

AI-assisted: Codex GPT-6.2 Sol ran the tests, Claude Opus 5.5 reviewed the report. No game files used.

@BatSmacker84

Copy link
Copy Markdown

Re-tested the new head d253b9517 merged onto main 5d74c2723: the runtime build still fails with MSVC C2084, duplicate XErrorReport at xgameruntime.cpp:265 (main defines it at line 255). main builds. The seven standalone loader, TLS, DLL-callback and proxy ctests pass on both.

AI-assisted: Codex GPT-6.2 Sol ran the tests, Claude Opus 5.5 reviewed the report. No game files used.

@sgonzalez-glitch

Copy link
Copy Markdown

Tested on NVIDIA GeForce RTX 2080, driver 621.24 (32.0.16.2124), Windows 11, MSVC 19.44 RelWithDebInfo. Two things:

  1. It no longer builds on current main. Merged onto main 6312c057f, runtime/shims/xgameruntime.cpp fails with error C2084: function ... XErrorReport(HRESULT,const char *) already has a body. Main got the same flat XErrorReport export in 6312c057f ("export XErrorReport, which XGameRuntimeInitialize requires"), so the copy from dd38f2eec needs dropping (still there at d253b9517) in a rebase (the merge itself has no textual conflict, which hides it).

  2. xs_proxy_mock fails from 9edab782e on, still at the current head d253b9517 (on its own base 867d922f0, every run): xs_proxy_mock line 721: mock->args[8][0] == word(&kQueueDesc), right after device slot 48 routed to CreateCommandQueueX. At the previous head dd38f2eec it passed, so it comes from 9edab782e ("add Xbox ABI device slots 26, 48, 73 (list proxy), and 133 stand-in"): the test still expects the slot 48 call to reach the PC CreateCommandQueue with the description as its first argument.

Otherwise, at 9edab782e (full run; d253b9517 was only rebuilt for xs_proxy_mock): loader, loader_tls, run_tiny_game_tls, run_tiny_gdk_load, gta6rt_stack, device_proxy, queue_proxy and xgameruntime pass with the new stub EXE + gta6rt_core.dll split. Full ctest 260/264, 11 skipped (no game data); the other 3 failures are the ones main has here (d3d12_vulkan_device tiles == 1 → #7125, d3d12_indirect_rt → #6010, d3d12_depth_resolve → #7905 / #7930 / #7985). At dd38f2eec the full run matched main apart from the known crt_timed_wait flake (#7686 / #8184). Synthetic test images only: nothing was run against, or checked with, a retail executable.

AI-assisted: yes

@icecreammule277-dot

Copy link
Copy Markdown
Contributor

I am deliberately not re-running the build or the ctest sweep — BatSmacker84 and sgonzalez-glitch have that covered, and GonZiin has the static review. Two things I can add: an independent confirmation, and one question I did not see asked.

Confirmed (build only, my clone): merging this head onto main 6312c057f and building gta6rt_core fails exactly as reported — error C2084: function 'anonymous-namespace'::XErrorReport(HRESULT,const char *) already has a body at runtime/shims/xgameruntime.cpp(265), previous definition at :255. I stopped there rather than compile the rest.

The question: main already fixed this PR's headline bug, by a different mechanism, after this branch was cut — so the rebase has a decision in it, not just a conflict to drop.

The TLS-anchor half of this PR exists to give the retail /GA executable its own TLS at index 0, for the boot AV at exe+0x104075. main now solves the same problem in b7a3fed28 ("pe_loader: give a /GA executable its own TLS at index 0 (retail boot AV at exe+0x104075)", 2026-11-23T20:34:37Z): it rewrites the executable's mov r64, gs:[58h] TLS reads and backs them with a TlsAlloc slot, switched off by LEONIDA_TLS_GA_PATCH=0. This branch was cut at 867d922f0 (18:54:54Z, before that), and git merge-base --is-ancestor b7a3fed28 pr9171 is false, so the head aab407396 (21:45:34Z) still carries the older approach on the older base.

That matters because the two interact rather than merely overlapping: main's rewrite deliberately leaves alone any image whose tls_index is already 0 — and arranging for index 0 to be the primary mapped executable's is precisely what this PR's anchor does. So after the mechanical rebase there is a real choice: is the anchor still wanted (and then which mechanism is authoritative for /GA images), or is b7a3fed28 sufficient and the anchor should be dropped? A reviewer can't tell from the code which is intended, and the PR description (still the template) doesn't say. Making that explicit in the rebase is probably worth more than the two-line fix for XErrorReport.

I did not use a retail executable or any game files here — build only. AI-assisted (OpenCode).

@BatSmacker84

Copy link
Copy Markdown

Still a build failure at 9353e89c3. The merge of main brought main's XErrorReport export in alongside the PR's own, so the head itself now has two definitions of HRESULT WINAPI XErrorReport(HRESULT, const char*) in runtime/shims/xgameruntime.cpp (lines 255 and 265), which MSVC rejects with C2084. Dropping the PR's copy and keeping main's should fix it. Read from the source, not rebuilt; I'll run the tests once a head has a single definition.

AI-assisted.

@icecreammule277-dot

Copy link
Copy Markdown
Contributor

One thing to add while rebasing: your slot-26 work is not speculative any more — it fixes a crash that is now measured end to end, and it is the next blocker on the real title.

I ran the retail 1.0.2.1 executable through gta6rt --run with the live DirectStorage PR (#8461, the takeover of the closed #4348) merged onto main 5d74c2723. That clears the storage stop and takes the run into device creation, where it stops at exactly the case this PR's 9edab782e handles:

exception 0xc0000005 at host:gta6rt.exe+0xda238   (= rt::d3d12_xs::proxy_slot<26>+0x2c8)
call qword ptr [r10 + 0xd0]        ; r10 = [r14 + 0x10] == 0
stack: game_scarlett_master.exe+0x2478713, +0x19f4f54, +0x580437e, ...

0xd0 is 26 * 8. main's generic proxy_slot<26> takes the register that actually holds the return buffer as self, reads [self+0x10] as the device pointer (0 in a freshly zeroed D3D12_HEAP_PROPERTIES), and calls its vtable slot 26. That is the swapped-argument shape, and proxy_get_custom_heap_properties — the (return_buffer, this, node_mask, heap_type) detection via is_device_proxy — is exactly the right fix. Reproducible with the RTSS/OBS layers explicitly disabled.

So this is the one commit worth keeping when the C2084 duplicate-XErrorReport conflict is resolved: it moves the boot further than anything else in the stack. If the full rebase is slow, lifting just the slot-26 handler would still be worth landing on its own.

Context for the record: retail boot result and the live board #9677. No game bytes were used or quoted. AI-assisted (OpenCode).

@icecreammule277-dot

Copy link
Copy Markdown
Contributor

Local validation of this commit against the real title — it takes the retail boot from stop 7 to stop 13, and it is the highest-value unmerged change I have seen for the boot.

Setup: own 1.0.2.1 copy, verified untouched after every run (its 108 manifest files hash to 223,880,932,870 bytes and the executable to e95020b2…; 0 files changed), Windows 11 26300, MSVC 19.52, Vulkan SDK 1.4.363.0, RX 9070 XT, LEONIDA_D3D12=vulkan, --overlay, RTSS/OBS layers disabled. No game bytes are quoted below.

9edab782e cherry-picks onto main 5d74c2723 + #8461 cleanly — no conflicts, one file (runtime/shims/d3d12_xs_device.cpp). main + #8461 (077606cb4) already reaches stop 7; each addition moves the first failure further:

tree first failure
main + #8461 0xc0000005 in rt::d3d12_xs::proxy_slot<26> (stop 7)
+ the slot-26 handler only 0x80000003 at +0x2478866 (stop 8)
+ the slot-48 Flags mask only 0x80000003 at +0x2478b7b (stop 9)
+ this commit (slots 26/48/73/133) 0x80000003 at +0x1cf336c (stop 13)

At the last row the D3D12 trace holds 1,188 calls: 1 device, 5 command queues, 5 fences, 7 command allocators, 586 CreateCommandList, 581 Close, 1 committed resource (the slot-133 stand-in), 1 descriptor heap, and XboxIDXGIDevice::GetAdapter. retail_boot_chain.md describes stop 13 with 1,185 calls and the same mix, so this commit reproduces the documented stop-13 conditions on hardware. The three intermediate rows also show each handler is load-bearing on its own: slot 26 clears stop 7, the slot-48 Flags mask clears the compute-queue refusal, and the slot-73 E_NOINTERFACE fallback clears the CreateCommandList failure.

Two notes:

  • main's slot-48 route currently hands the console desc straight to the PC device with no mask, so a D3D12_COMMAND_LIST_TYPE_COMPUTE queue is refused at d3d12_vulkan_device.cpp:5443; your Flags mask is exactly the §3.2 fix and is why the queue creation succeeds after the cherry-pick.
  • The slot-73 fallback also resolves the console command-list IID that §3.3 records as unnamed (docs: record the four console command-list IIDs as not exported (D3D-369) #3520) — the shim's own log names it at run time. I am not printing the GUID here, to stay inside retail_boot_chain.md's "no IID values" rule.

I did not run the list-proxy path against run_tiny_frame, which is why #2490 was reverted; that is a CPU check for the test suite, not something a boot proves.

Recommendation: rebasing to land 9edab782e — even alone, separated from the TLS-stub split and the duplicate XErrorReport — is worth more than anything else in the stack.

No game data was used or quoted. AI-assisted (OpenCode).

@icecreammule277-dot

Copy link
Copy Markdown
Contributor

The one objection this PR needs answered is the one that sank its predecessor: #2490's console list proxy was reverted because it broke run_tiny_frame. I ran that test against this commit.

run_tiny_frame still passes. main 5d74c2723 with 9edab782e cherry-picked (one file, no conflicts), gta6rt rebuilt, GPU present (RX 9070 XT, LEONIDA_D3D12=vulkan):

main,                       run_tiny_frame: OK   exit 0
main + 9edab782e,           run_tiny_frame: OK   exit 0
main + 9edab782e,           run_tiny_frame_queue_test_settings: OK   exit 0

So the revert's failure mode does not reproduce, and there is no PC-path regression.

And it cannot, by construction — worth saying because it is the reason the objection is gone, not luck. #2490's proxy wrapped lists coming out of device slot 12 (CreateCommandList), which is why the PC-shaped fixture's list read console slot 48 as IASetVertexBuffers and Close failed. In this commit the proxy is created in exactly one place — proxy_create_command_list_x (slot 73, the console CreateCommandListX), and only on the E_NOINTERFACE fallback for the console IID — and no device slot 12 handler is touched. The console path and the PC path are separated, so a PC fixture never sees the proxy.

One honest limit of this check: because run_tiny_frame is a PC-shaped fixture, it proves the PC path is untouched and does not exercise the console list proxy itself. The proxy is exercised in the boot: with this commit, the retail run creates 581 command lists back to back and closes every one (ID3D12GraphicsCommandList::Close ok ×581, CreateCommandList ×586), all through the slot-73 console-IID fallback and the ListProxy. So proxy correctness is shown there; run_tiny_frame shows it does not cost the PC path anything.

That closes the "please rebase and split" reason for the revert: no pe_loader.cpp change and no committed conflict markers are involved here either.

Tree restored to main afterwards; run_tiny_frame re-confirmed green on the restored build. No game data used. AI-assisted (OpenCode).

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.

6 participants