Skip to content

Runtime: give game threads the image's stack reserve (RT-119) - #1895

Open
opit80 wants to merge 3 commits into
ItsNotRexed:mainfrom
opit80:rt119-stack-reserve
Open

opit80 wants to merge 3 commits into
ItsNotRexed:mainfrom
opit80:rt119-stack-reserve

Conversation

@opit80

@opit80 opit80 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Claimed as RT-119 in the coordination issue. Finding by @dracbat.

What and why

The loader records the mapped PE32+ image's SizeOfStackReserve and SizeOfStackCommit. It configures the reserve before TLS callbacks or DllMain can create threads. The entry-point thread uses that reserve, with the previous 8 MiB fallback for an image that specifies zero.

CreateThread and _beginthreadex use the same reserve policy. A size of zero uses the mapped image's reserve. A nonzero commit gets the larger of the image reserve and the commit rounded up to 1 MiB. Both cases pass STACK_SIZE_PARAM_IS_A_RESERVATION and use the host's default initial commit. An explicit nonzero reservation is passed through. _beginthreadex retains the caller's other flags, including CREATE_SUSPENDED, and its unsigned stack argument caps the size the shim can pass. The import shims cover the kernel and CRT aliases listed in docs/runtime_imports.md.

Mapped-image reserves are unsupported for _beginthread at any size. Its public API accepts only a commit size, so passing the reserve would charge that much against the system commit limit for each thread. Substituting _beginthreadex would change the handle lifetime. The native CRT continues to handle _beginthread and _endthread, preserving automatic handle closure. These semantics are documented in the CRT reference and the Win32 stack reference.

SizeOfStackCommit is recorded for inspection. The Win32 APIs used here accept a single stack size. CreateRemoteThread(Ex) and threads created inside host DLLs continue to use the host policies. All fixtures are synthetic.

Tested

Windows 11 Pro build 26200, MSVC 19.44.35229, NVIDIA GeForce RTX 5070 Ti with driver 617.42. CMake/Ninja RelWithDebInfo, -j 4.

  • Zero new build warnings against clean upstream aa2988cc. Both builds emitted the same MSVC C4456 and GLSL precision warnings.
  • Both stack tests passed. The fixture measured 64 MiB for the entry-point thread and for zero and 64 KiB stack requests through CreateThread and _beginthreadex. The native 64 KiB _beginthread path measured 1 MiB and its _endthread closed the original handle. The shim test also checked explicit reservations, thread IDs, suspension, and 1 MiB rounding.
  • The negative control restored the old zero-only _beginthreadex logic. Both tests failed and the fixture measured 1 MiB for its 64 KiB _beginthreadex request. Both tests passed after restoring the change.
  • Full CTest on the final branch reported 146 passes, 9 skips, and 1 disabled test. Clean upstream reported 143 passes with the same skips and disabled test. Its d3d12_fence_timing run stalled and was stopped after 134 seconds. That test passed in 2.50 seconds on an isolated upstream retry.

Checklist

  • No game data, keys, or decryption.
  • Runtime-only change. Translator contract update is N/A.

AI-assisted.

Merge order: #2152 depends on this PR.

@Flyander Flyander left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One nonzero-commit path remains in the CRT wrappers. _beginthreadex only rewrites stack_size == 0; a nonzero size without STACK_SIZE_PARAM_IS_A_RESERVATION is the initial commit, and the reserve still comes from the host executable. _beginthread likewise only substitutes zero, so a nonzero request below the host reserve keeps the host's smaller reserve. Both can still create a mapped-game thread whose stack cannot grow to the game's SizeOfStackReserve.

The Windows CRT and thread-stack contracts confirm that a nonzero size without the reservation flag is a commit size, while zero uses the executable's default stack: https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/beginthread-beginthreadex?view=msvc-170 and https://learn.microsoft.com/en-us/windows/win32/procthread/thread-stack-size. Please apply the same reserve policy to nonzero _beginthreadex calls when the reservation flag is absent, and to nonzero _beginthread calls (which have no flag parameter), or document those paths as unsupported. A fixture case with a small nonzero CRT commit would catch the current gap.

seblessa added a commit to seblessa/Leonida that referenced this pull request Nov 22, 2026
Conflicts with upstream's API-set ExitProcess/GetCommandLine entries, the tiny_game_fixed fixture and the program-path set_command_line (ItsNotRexed#2861/ItsNotRexed#2661): kept both sides; the RT-119 CreateThread/_beginthread entries sit beside the EXIT_PROCESS/COMMAND_LINE macros, and main.cpp sets the stack reserve and the command line before initialise().

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@opit80
opit80 force-pushed the rt119-stack-reserve branch from 0af9b81 to 1bdfc69 Compare November 22, 2026 22:57
@opit80

opit80 commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

@Flyander Thanks for catching the nonzero commit case. _beginthreadex now shares the CreateThread reserve policy and preserves explicit reservations and CREATE_SUSPENDED. I documented _beginthread as unsupported for mapped-image reserves. Its native CRT path keeps _endthread handle closure without charging the whole reserve as commit. The fixture exercises a 64 KiB commit through both CRT starters, and reverting the _beginthreadex change makes the fixture fail.

Fixed in 1bdfc691; PR description updated. AI-assisted.

@evilclanker9000

Copy link
Copy Markdown
Contributor

Tested 1bdfc69 on Windows 11 (MSVC RelWithDebInfo): full runtime build, then ctest for the shim/loader tests (loader, pe_loader, loader_tls, dll_thread_calls, unimplemented_policy, startup_trace, xmem, process_cmdline, file_system, winhttp_offline, xgameruntime, run_tiny_*, allocator_proxy) — 28 of 28 passed. AI-assisted.

- CreateThread with a nonzero size and no STACK_SIZE_PARAM_IS_A_RESERVATION now gets the image reserve (raised to a 1 MiB-rounded commit if larger) with the flag; an explicit reservation is left untouched.
- _beginthread gets the same size-0 substitution as _beginthreadex and is registered under the UCRT and msvcrt import names.
- Extend CreateThread registration to the processthreads API-set variants and document the import scope and the commit-size trade-off.
- Tests cover both new paths and check that the nonzero reserve stays at the image size.
@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested 01f02d1 on NVIDIA GeForce RTX 4080, driver 617.42, Windows 11 (clang-cl cross-build): stack_reserve_shim_test exits 0 and ends with "stack_shim: OK". It prints "size-0 CreateThread stack = 16 MiB" ("PASS size 0 gets the game image's reserve"), "flagged CreateThread stack = 1 MiB" ("PASS an explicit reservation is left alone"), "_beginthreadex stack = 17 MiB" and "PASS _beginthread stays in the native CRT". The gta6rt-based run_stack_reserve entry was not run here, so the 64 MiB entry-point and native _beginthread measurements are not covered.

AI-assisted.

@Roberto0953

Roberto0953 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Tested 01f02d16 (up to date with main 3faac557) on Windows 11 Pro 26300, MSVC 14.44, Ninja RelWithDebInfo: pass, and it fixes one case of an independent loader comparison.

The PR's own tests: ctest -R stack_reserve: 2/2 passed (stack_reserve_shim, run_stack_reserve). Build of gta6rt, stack_reserve_fixture and stack_reserve_shim_test: 0 warnings.

Independent check. We keep 16 small synthetic PEs (our own code, no game files) and run each natively, then under gta6rt --run, comparing the exit code and the guest's stdout with the Windows loader's.

Build Cases equal to the Windows loader
main 3faac557 5 of 16
main + this PR 6 of 16
  • The case that changes: a static-CRT EXE that prints, among other things, the stack reserve of its main thread (GetCurrentThreadStackLimits). On main the size printed under gta6rt differs from the native run; with this PR the output is the same.
  • No regression: the other 15 cases give the same verdict in both builds.
  • End-to-end fixture unchanged: a synthetic Xbox-style title run through gta6rt (device proxy, translated VS/PS/CS, readback) still matches its CPU reference: 16,384 of 16,384 pixels and 1,024 of 1,024 compute texels on an RTX 3080 (617.42), 0 validation messages.

The same comparison on #2669 also went from 5 to 6 (the TLS/thread-events case), so the two together would give 7 of 16; I did not run them merged.

AI-assisted (Claude Code); run on this machine.

seblessa added a commit to seblessa/Leonida that referenced this pull request Nov 23, 2026
Conflicts with ItsNotRexed#2861 (CRT program-path imports) in runtime/src/main.cpp and runtime/shims/process.cpp: kept both. main.cpp keeps main's mod hooks and ItsNotRexed#2861's set_command_line(..., image_path), then calls ItsNotRexed#1895's set_thread_stack; process.cpp takes the union of the includes and puts set_thread_stack before ItsNotRexed#2861's set_command_line.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
seblessa added a commit to seblessa/Leonida that referenced this pull request Nov 23, 2026
…est_docs_cli_flags

FORK.md: boot-prep is upstream main 3faac55 + ItsNotRexed#2861, ItsNotRexed#1895, fork/first-boot and
gdk-offline-classes (ItsNotRexed#3476, after its merge of main). ItsNotRexed#2218, ItsNotRexed#2784, ItsNotRexed#2594, ItsNotRexed#2839,
ItsNotRexed#2954, ItsNotRexed#2957, ItsNotRexed#2750, ItsNotRexed#2833 and our ItsNotRexed#3455 are upstream now; fork/first-boot and
gdk-runtime-calls are gone, so the fork-only files are carried as a patch when
boot-prep is rebuilt. gta6rt now seeds its VkPipelineCache from the
pipeline_cache.bin beside it when the header matches the device.

tests/test_docs_cli_flags.py failed on boot-prep only: FORK.md, merged from
fork/first-boot, quotes git commands, and the test wants every --flag in a
Markdown page to appear in a source file. git's flags join FOREIGN, next to gh's
and pip's (fork-only line).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested 01f02d1 on Intel UHD 770, driver 32.0.101.7088: after a fresh configure, gta6rt, stack_reserve_fixture and stack_reserve_shim_test build, and ctest -R stack_reserve passes 2 of 2 (stack_reserve_shim, Test #38, and run_stack_reserve, Test #58). run_stack_reserve prints "stack_fixture: PASS the entry point runs with the game image's stack reserve" at size=64 MiB, PASS for a size-0 CreateThread and for a nonzero _beginthreadex commit at 64 MiB, "PASS a nonzero _beginthread commit uses the host stack policy" at size=1 MiB and "PASS native _endthread terminated and closed its original handle", then "stack_fixture: OK" and "game exited with code 0". The shim test's own pass lines were not captured, only its exit 0, and I did not run main.

AI-assisted.

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.

5 participants