Skip to content

tests: fail when gta6rt.exe has TLS callbacks (TEST-GTA6RT-NO-TLS-CALLBACKS) - #2225

Open
MoonEyes wants to merge 4 commits into
ProjectLeonida:mainfrom
MoonEyes:test/gta6rt-no-tls-callbacks
Open

MoonEyes wants to merge 4 commits into
ProjectLeonida:mainfrom
MoonEyes:test/gta6rt-no-tls-callbacks

Conversation

@MoonEyes

Copy link
Copy Markdown
Collaborator

Claimed in the coordination issue as: TEST-GTA6RT-NO-TLS-CALLBACKS (#1608 (comment))

Depends on: #2195 (this test fails on current main by design and passes once #2195 is merged)

What and why

A thread_local with a destructor in gta6rt gives gta6rt.exe two TLS callbacks (__dyn_tls_init, __dyn_tls_dtor). The UCRT accepts only one _register_thread_local_exe_atexit_callback per process and the title registers its own, so the game exits at startup with 0xC0000409 (#2195, RT-XMEM-STARTUP-FASTFAIL). Nothing stopped that from coming back; this is the guard I suggested in my test report on #2195.

  • runtime/test/check_pe_tls_callbacks.py reads the PE32+ TLS directory of the file it is given, walks AddressOfCallBacks to its zero terminator, and exits 1 if any callback is listed. Standard library only.
  • runtime/CMakeLists.txt registers it as the CTest gta6rt_no_tls_callbacks on $<TARGET_FILE:gta6rt> (next to run_module_names).
  • tests/test_check_pe_tls_callbacks.py (stdlib, run by ci.yml as python -I) checks the parser on two in-memory PE32+ images, one with callbacks and one without, plus the exit codes for a non-PE and a missing file. It needs no MSVC and no game data.

Trivially destructible thread_local data adds no callback and stays allowed. The check is stricter than strictly needed: any future TLS callback in gta6rt.exe will fail it and have to be discussed.

CI note: this pull request's CI will be red on gta6rt_no_tls_callbacks until #2195 is merged, because main still has the two callbacks. I think it should wait for #2195 rather than land with WILL_FAIL, which someone has to remove by hand afterwards and which would hide the very regression it guards against. I will merge main in once #2195 lands.

Tested

  • Native Windows, MSVC 19.44, RelWithDebInfo, Ninja, target gta6rt only. ctest -R gta6rt_no_tls_callbacks:
  • python -I tests/test_check_pe_tls_callbacks.py: ok; pytest on the same file: 3 passed. tests/test_runner_calls_all_tests.py and tests/test_ci_registration.py: OK.
  • scripts/check_no_game_data.py on the three changed files: clean. git diff --check: clean.
  • The game was not started. Not tested: MinGW or clang-cl builds of gta6rt (they may list TLS callbacks of their own).

GPUs tested: n/a (the test reads the executable file and uses no GPU)

AI-assisted: yes (Claude Code, Claude Fable 5.2, wrote the test and ran it; a second agent reviewed it)

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • 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. (n/a: no translator rule)

🤖 Generated with Claude Code

…LBACKS)

A thread_local with a destructor gives gta6rt.exe __dyn_tls_init/__dyn_tls_dtor callbacks;
the title's own _register_thread_local_exe_atexit_callback then fast-fails (0xC0000409).
check_pe_tls_callbacks.py reads the PE TLS directory and fails on a non-empty list; a stdlib
test checks it on two in-memory PE32+ images.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@HasiKe

HasiKe commented Nov 26, 2026

Copy link
Copy Markdown

Test of this PR at head 74bab76 (RTX 2070 SUPER, NVIDIA 616.56, Windows 11 Pro 26300, MSVC (VS 18) RelWithDebInfo, own 1.0.2.1 package). AI-assisted.

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

The parser tests pass. The native CTest rejects a real gta6rt.exe with the two callbacks on main, and the same script accepts the separately built #2195 executable. I also checked test registration in cross-compiling mode. AI-assisted.

Comment thread runtime/CMakeLists.txt Outdated
# TEST-GTA6RT-NO-TLS-CALLBACKS: a thread_local with a destructor gives gta6rt.exe TLS callbacks, and the game
# then fast-fails (0xC0000409) in its own _register_thread_local_exe_atexit_callback (RT-XMEM-STARTUP-FASTFAIL).
if(Python3_FOUND)
add_test(NAME gta6rt_no_tls_callbacks COMMAND "${Python3_EXECUTABLE}" -I

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.

[P3] Register this check for cross-compiled Windows binaries too. I configured the same tree with CMake's cross-compiling mode enabled (CMAKE_SYSTEM_NAME=Windows) and ctest -N -R '^gta6rt_no_tls_callbacks$' listed zero tests, versus one in the native build. The checker runs host Python and only reads the target image. Moving this registration outside the guard for run_module_names, with host Python discovery, keeps the startup regression covered for cross-built binaries.

The checker only reads the PE file with the host's Python, so it does not need the
run_module_names guard (NOT CMAKE_CROSSCOMPILING). Move the registration out of it, with
its own host Python lookup. runtime/cmake/clang-cl-xwin.cmake builds the same MSVC ABI
and UCRT, so the same callbacks would appear; MinGW is not a supported gta6rt toolchain.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@MoonEyes

Copy link
Copy Markdown
Collaborator Author

@lixonik good catch, thanks. The checker only reads the PE with the host Python, so I moved the registration out of the run_module_names guard, with its own Python3 lookup (812d5cf).

Native configure still lists the test (ctest -N -R gta6rt_no_tls_callbacks shows 1). I could not try the cross mode here (no xwin toolchain), so a check with your CMAKE_SYSTEM_NAME=Windows setup would help.

clang-cl-xwin builds the same MSVC ABI and UCRT as the native build, so I did not limit it to if(MSVC); MinGW is not a gta6rt toolchain in CI.

AI-assisted: yes (Claude Code, Claude Fable 5.2)

@lixonik

lixonik commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Retested 812d5cfa6 with CMAKE_SYSTEM_NAME=Windows on the Windows host. CMake records CMAKE_CROSSCOMPILING=TRUE, and ctest -N -R ^gta6rt_no_tls_callbacks$ now lists one test. I ran that cross-configured CTest against the same two locally built PE files: it rejected the main binary with two callbacks and passed the #2195 binary. This verifies the registration fix in that CMake configuration. AI-assisted.

@qhukz13

qhukz13 commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

I ran this with #2195 merged on top of main 73516a290, on Windows 10 / MSVC 14.44 RelWithDebInfo. gta6rt_no_tls_callbacks fails there, on a build that is fixed:

FAIL: runtime\build\RelWithDebInfo\gta6rt.exe has 1 TLS callback(s) (RVAs 0x5b990). A thread_local with a destructor was linked in; ...

The callback at RVA 0x5b990 is std::sys::thread_local::guard::windows::tls_callback, Rust std's. dumpbin /disasm names it, and dumpbin /tls lists it alongside __dyn_tls_init and __dyn_tls_dtor on plain main. It comes from the XMA2 decoder, which runtime/CMakeLists.txt (around line 120) builds with cargo and links into gta6rt whenever cargo is on PATH. So on any machine with a Rust toolchain installed, the guard fails although the exe has none of the MSVC callbacks that cause the 0xC0000409. A build without cargo does not link the decoder and would not see it. The Python unit tests pass.

Two ways to keep the guard and drop the false alarm:

AI-assisted (Claude Code).

MoonEyes and others added 2 commits November 26, 2026 17:08
Link gta6rt with /MAP and read gta6rt.map to name each TLS callback.
Fail on the C++ runtime's __dyn_tls_init (tlsdyn.obj) and __dyn_tls_dtor (tlsdtor.obj), allow Rust std's thread-local callback, fail on the rest.
A callback the map does not name, a missing map or a stale map fails with a message.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@MoonEyes

Copy link
Copy Markdown
Collaborator Author

@qhukz13 thanks, you are right. gta6rt now links with /MAP, and the check uses gta6rt.map to name each TLS callback, following an /INCREMENTAL jump thunk if there is one. __dyn_tls_init (tlsdyn.obj) and __dyn_tls_dtor (tlsdtor.obj), or anything else from those two objects, fail the check. Rust std's thread-local callback passes. Any other callback fails, and so does a callback the map does not name.
I chose names over a count because a count of 1 lets a real __dyn_tls_dtor through whenever the Rust callback is missing. It also breaks when both decoders bring a copy of std. The map needs no extra tool on CI.
Without a map, an exe with callbacks fails, and the message says to link with /MAP or pass --map. A map with another timestamp or load address, or with no timestamp, also fails.
Tested: MSVC 19.44, Ninja, RelWithDebInfo, no cargo. The link writes gta6rt.map next to the exe, the exe has no callbacks and the test passes. I also ran the parser on that real map: it reads every symbol line and names a function through its incremental thunk.
Not tested: an exe that really has the Rust callback, the Visual Studio generator (where the .map lands), and lld-link. Could you rebuild with cargo on PATH and run ctest -R gta6rt_no_tls_callbacks -V? It should print allowed with the Rust tls_callback symbol and pass. If it prints no symbol at this address in the map or no linker map, please paste that output and the map lines around that RVA.
AI-assisted: yes (Claude Code. A sub-agent wrote the change; a separate agent reviewed it on a real linker map; Claude Fable 5.2 built and tested it.)

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

Rechecked the current head on Windows 11. The synthetic PE test script passed. I also ran the checker against two locally built gta6rt.exe files: it flagged two callbacks on the older build and accepted the newer build with zero callbacks. The CMake registration is outside the runnable-module guard. No new findings.

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.

4 participants