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>
- 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.
@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.

@Vonsek

Vonsek commented Nov 23, 2026

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.

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.

3 participants