Skip to content

runtime: synthetic multi-callback TLS callback-table regression tests - #8051

Closed
NethminaYasas wants to merge 2 commits into
ProjectLeonida:mainfrom
NethminaYasas:rt/tls-multi-callback-test
Closed

NethminaYasas wants to merge 2 commits into
ProjectLeonida:mainfrom
NethminaYasas:rt/tls-multi-callback-test

Conversation

@NethminaYasas

@NethminaYasas NethminaYasas commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Extends the existing Windows-only loader_tls CTest with three hand-built, fully synthetic PE images: (1) three distinguishable TLS callbacks in a zero-terminated table (asserting no execution on load()/dry-run, exact 1-2-3 order with mapped base, correct reason and null reserved for attach and detach); (2) the same image with the terminator removed (controlled std::runtime_error after all three entries, no out-of-bounds read); (3) a table with an out-of-image entry value (base + SizeOfImage + 0x100), asserting only the first callback runs and rva_in() rejects the second. One README sentence documents the contract. No production, CMake, TLS-slot or clone-management changes.

Fixture bytes are built from layout constants and hand-written x64 stubs by the test itself. The callback stubs store the 32-bit reason via EDX to match PIMAGE_TLS_CALLBACK. No game data, shaders, disassembly, strings or traces; no game binary opened or run; no REA used.

Tested on Windows x64, MSVC 19.44 (VS 2022), SDK 10.0.26100.0, CPU-only (GTX 1650 present but unused): loader_tls 1/1 passed, standalone 98 checks; full CTest 104 passed / 7 game-gated skips / 0 failed of 111 considered; python -I scripts/check_no_game_data.py exit 0; CodeRabbit deep review 0 findings. Not tested: GPU/Vulkan/D3D12 (no Vulkan headers), Wine, Linux/macOS, AMD RDNA. The fixtures do not exercise image relocation itself.

Overlap checked against #3790, #7503, #5233, #7423 — none adds this coverage. #3790 is a draft; re-check before merge. Claim: new item posted on #7599 (ID pending).

AI-assisted.

Add hand-built synthetic PE fixtures to loader_tls_test for three zero-terminated callbacks (order, reasons, base, dry-run) and for a missing terminator (bounded failure, no OOB read). Document the callback-table contract in runtime/README.md. No production changes.

@tupacfan04 tupacfan04 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 successful three-callback path is correct, but the failure coverage misses the case that matters and the hand-written stub has a real ABI bug.

  1. Failure case only covers “no terminator after all three.” The fixture places [cb1, cb2, cb3] in the last 24 bytes and omits the zero terminator; all three run, then require_range() throws (runtime/src/pe_loader.cpp:482-488). That never exercises the per-entry rva_in(m, fn, "a TLS callback") check. A table of [cb1, image_base + SizeOfImage + 0x100, cb3, 0] must run only cb1 and throw the bounds error; a regression that caught the per-entry error and continued would still pass the new test. Add that fixture (assert order == {1}, count == 1, controlled std::runtime_error). A callback that faults is a separate case: run_callbacks has no exception guard around callback(...).

  2. Reason is read as 64-bit. emit_callback_stub() stores rdx (48 89 90 disp32) and CallbackState.reason is uint64_t; PIMAGE_TLS_CALLBACK's second parameter is a 32-bit DWORD, so only EDX is defined in the x64 ABI. It passes on MSVC only because the loader zero-extends EDX; another compiler/optimization can leave garbage in the upper half. Store edx (89 90 disp32) and make reason/the comparison 32-bit.

  3. Relocation is claimed but not tested. The fixture has no .reloc and free_image_base() returns the preferred base on Windows, so neither AddressOfCallBacks nor the entries are relocated. Either add a relocatable/forced-relocation variant asserting the callbacks run at m.base, or drop “relocated” from the README claim.

  4. Minor: catch (const std::exception&) is too broad; check the std::runtime_error message (“runs past the image”) so an unrelated failure cannot pass.

Correct: successful order/count 1,2,3 once, mapped base, null reserved, attach/detach reasons; load() runs no callbacks; the zero terminator stops the table; the missing-terminator-after-three case throws cleanly; detach order is consistent with PE/Wine. Synthetic only; no game files. AI-assisted (OpenCode).

Extend loader_tls with a synthetic table containing an out-of-image callback entry, asserting rva_in() rejects it after the first callback. Store the callback reason as 32-bit EDX to match PIMAGE_TLS_CALLBACK, and narrow failure catches to std::runtime_error.
@NethminaYasas

Copy link
Copy Markdown
Contributor Author

Addressed the three approved findings in c68e5d84c:

  1. Added test_callback_entry_outside_image() with [cb1, base + SizeOfImage + 0x100, cb3, 0], asserting only cb1 runs and rva_in() throws std::runtime_error. This covers the per-entry value check at pe_loader.cpp:488, distinct from the range check at :484.
  2. The synthetic stub now stores EDX (89 90 disp32) into a uint32_t reason[3], matching PIMAGE_TLS_CALLBACK's DWORD parameter.
    4a. Both failure tests now catch std::runtime_error. I did not assert message text (explicit constraint for this change) — the exception type plus exact order/count assertions distinguish the failure.

On 3: the README wording describes the loader contract (entries are relocated virtual addresses), not test coverage; relocation itself is not exercised by these fixtures and is recorded as a limitation in the PR body.

Re-run: loader_tls 1/1, standalone 98 checks, full CTest 104 passed / 7 skipped / 0 failed, check_no_game_data.py exit 0, CodeRabbit deep review 0 findings. Pushed as c68e5d84c. AI-assisted; no game files used.

@optinium51-bot

optinium51-bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Tested c68e5d84c merged onto main f837d4fe9 (clean merge) on NVIDIA GeForce RTX 3060 (driver 616.92), Windows 11 26200, MSVC 14.52 (VS 2026), RelWithDebInfo + Ninja: pass.

No game files. AI-assisted (Claude Code).

@egexon

egexon commented Nov 23, 2026

Copy link
Copy Markdown

Tested c68e5d8 (merged onto main 867d922) on RTX 5070, NVIDIA 621.24, Windows 11 (build 26200), Vulkan validation layer on (LEONIDA_VK_VALIDATION=1, VK_VALIDATION_VALIDATE_SYNC=1): pass.

  • loader_tls: passes with this PR, and passes on main.

Ran the tests that belong to the test files this PR changes (ctest entries, or the test program directly when it is not registered), with no game files (LEONIDA_GAME_DIR unset). AI-assisted (Claude Code).

@NethminaYasas

Copy link
Copy Markdown
Contributor Author

thanks @egexon @optinium51-bot

@karearl

karearl commented Nov 23, 2026

Copy link
Copy Markdown

On main here d3d12_vulkan_device (tiles == 1, fixed by #8496/#7125) and d3d12_depth_resolve fail without the PR too; one-off failures in a full -j 8 run were rerun alone 3 times and passed.

AI-assisted (Claude Code).

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