Commit Graph

18 Commits

Author SHA1 Message Date
b17397f0dd tests: derive every test's scratch folder from test_temp_dir::temp_dir, not GetTempPath (TEST-TEMPDIR-STRIPPED-ENV)
With TMP, TEMP and USERPROFILE all unset, GetTempPathW returns C:\Windows\, and the throwing create_directories/copy_file/ofstream on C:\Windows\leonida_* aborts with 0xc0000409 before the test body runs. runtime/test/test_temp_dir.h grows one entry, testfiles::temp_dir(): LEONIDA_TEST_DIR when set, the current directory when the temp environment is stripped (Windows), else the standard temp resolution. Fifty-two test files and the tiny_game fixture that called GetTempPathA/W or temp_directory_path directly now use it; d3d12_queue_handover_test.cpp also checks the _sopen_s result in capture_stderr instead of silently returning empty stderr. One runtime source function needed the same fallback because tests exercise it: tls_host_clone_dir in runtime/src/pe_loader.cpp (the loader_tls tests). Rebased onto current main after its D3D-448 additions to d3d12_mesh_test.cpp (3-way; the mesh test keeps both). Verification on a Windows VM: stripped environment - the previously crashing d3d12 tests exit 77 (no usable Vulkan device) instead of aborting, file_system_test runs all 505 checks and loader_test all 95 with zero failures; normal environment - identical outcomes; LEONIDA_TEST_DIR honored under a fully stripped environment (the scratch folder observed mid-run inside it, cleaned up after the pass). The finding is whoalin1's (coordination issue #14, 02:37:44Z).

AI-assisted.
2026-11-28 19:01:06 +00:00
b2360deeb6 loader_tls: a /GA executable beside a second TLS module, on the loading thread and on a new one (BL-1)
BL-1 (docs/boot_leads.md) says code built with /GA reads its TLS at module index 0 and that main stops there.
main already carries a fix (b7a3fed28 and 4710549af: load() rewrites the executable's gs:[58h] reads to a
per-thread view with the image's block at [0]), but no test maps a /GA executable and a second TLS module at
the same time, and no test shows that the fixture's code really has the index-0 shape: the CRT objects linked
into it read gs:[58h] through _tls_index, so "some reads were rewritten" holds for a non-/GA build too.

tiny_ga_tls gets ga_tls_address(), which returns where its code looks for its variable without touching it.
The new case uses it three ways. With LEONIDA_TLS_GA_PATCH=0 the address lies in this test EXE's own block at
index 0: the stop-4 shape, shown without reading or writing another module's TLS. By default it lies in the
fixture's own block, on the loading thread and on a thread started after load(), while tiny_tls.dll mapped
beside it keeps a block at its own index, gets its thread attach and detach, and this EXE's block and value
stay at index 0 of each thread's real TLS array. ~Loader takes the view out again.

No loader change: the case passes on main as it is, which is the finding. pe_loader.cpp and tls_host.cpp are
untouched.

Tested on Windows 11, MSVC 19.44 (VS 2022 Build Tools), Ninja, RelWithDebInfo, at main 8f05e09e2:
loader_tls_test OK (118 checks, 95 before), 40 runs in a row; with LEONIDA_TLS_GA_PATCH=0 set for the whole
process, 8 checks fail, as they should (7 existing ones and "by default load() rewrites the EXE's index-0
TLS reads"). MSVC emits the index-0 read for ga_tls_address at /Od and at /O2 (checked in the listing). Full
ctest: 304 of 306, the same two failures as main (d3d12_execute_indirect_validation, d3d12_depth_resolve).
Not run on the retail executable: that needs game files.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SR1Fv7eQNgZjM6miWPKjHf
2026-11-24 10:24:51 -05:00
1052aad6c1 runtime: retain the TLS directory in the /GA test fixture
Force the CRT TLS directory into tiny_ga_tls when /GA removes the last _tls_index reference. Reject the unassigned TLS index in the test so missing metadata fails the index check.
2026-11-24 08:34:03 -05:00
4710549aff pe_loader: a thread that existed before load() gets the /GA TLS view too (EGEN-GA-TLS-EXISTING-THREAD)
load() rewrites a /GA executable's `mov r64, gs:[58h]` reads to a TlsAlloc slot whose value is a per-thread
view with the image's block at [0] (pe_loader.h, tls_ga_slot). install_thread_block creates that view for
the loading thread and for threads started after load(), but install_on_existing_threads gave the threads
that were already running only the block in TEB->ThreadLocalStoragePointer[tls_index]. A rewritten index-0
read on such a thread then loaded null and dereferenced it: the task queue runs a title's callbacks on pool
workers that predate the load, so this is the same AV the main thread had at exe+0x104075, moved to a worker.

Each existing thread now also gets a view of its own in TEB->TlsSlots[ga_slot], and ~Loader, the thread's
detach event and a reused thread id free it with the block.

Tested: runtime/test/loader_tls_test.cpp maps a new handmade /GA EXE fixture (tiny_ga_tls) and reads its
__declspec(thread) value from the loading thread, from a thread that existed before load() and from a later
one, then again after a second load(). Without the pe_loader change that test dies with 0xC0000005 on the
existing thread; with it all 95 checks pass. ctest -R loader: 4/4 passed (Windows 11, MSVC 19.44,
RelWithDebInfo, no GPU needed).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 22:13:50 +01:00
80469257a4 Merge pull request #4091 from Rayhan Robin
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 23:11:11 -05:00
e9fe34851a runtime: give loader_tls's purge test suspended, console-less children
test_purge_ownership_scoping spawned cmd.exe with CREATE_NO_WINDOW for its
dead and live owner PIDs. Under Wine each console child starts a headless
conhost.exe, which inherits the runner's output pipe and can outlive a
terminated child, so ctest waits on the pipe after the test printed OK.

The children are now copies of the test executable started with
CREATE_SUSPENDED | DETACHED_PROCESS: they never run, have no console, and
still give one dead PID (terminated and waited) and one live PID.

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-11-22 23:23:30 +00:00
2fb013cd9b Merge origin/main into tls-existing-threads
Conflicts with #2070's later round (TLS generations, per-slot thread-event
errors, clone purge) in pe_loader.cpp/.h and loader_tls_test.cpp:
- install_thread_block keeps main's generation check and this branch's
  existing-thread check.
- on_host_thread_event keeps main's per-slot try/catch and calls this
  branch's existing-thread hooks first, each in its own try/catch.
- ~Loader resets the slot generation and removes the existing-thread blocks.
- The existing-thread records carry the module's TLS generation too.
- Both sides' tests stay. test_orphan_slot_generation_reuse now expects
  one DLL_THREAD_DETACH, not none: the worker was running when the second
  Loader mapped the module, so it gets that module's copy and its detach,
  as Windows does; a second detach would mean the orphaned record was taken
  for the new module's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 22:19:40 +00:00
292c917822 loader: harden the TLS swap on running threads
- After writing a thread's slot, read its array pointer again and repeat on
  the new array if Windows replaced it; if the block is not in the current
  array after four attempts, take it back and log.
- Read and write another thread's TEB and TLS array inside __try, right
  after the thread was seen running, so a TEB freed by TerminateThread is a
  skipped thread.
- Bound the list of ended thread ids.

loader_tls_test: a thread terminated while it holds a block, and a load()
with a dead thread still open.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-22 21:12:56 +00:00
b418319ae9 loader: static TLS for threads that were running before load()
load() installed the image's TLS block on its caller, and the helper DLL's
DLL_THREAD_ATTACH covers threads started later. A thread that was already
running kept the helper's one-byte block in the reserved slot, so mapped
__declspec(thread) code on it read and wrote past it. The process thread
pool's workers are such threads, and the task queue runs a title's
callbacks on them.

load() now gives every other running thread its own copy of the template
through that thread's TEB, as Windows does for a DLL it loads itself: a
block, no DLL_THREAD_ATTACH. The block comes out in the thread's own
DLL_THREAD_DETACH (after the module's TLS callbacks) or in ~Loader.

loader_tls_test: a thread and a pool worker that exist before load(),
across two mappings of the fixture.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-22 20:40:54 +00:00
4edfb2418d runtime: make the purge-ownership test deterministic
The dead-owner case relied on the spawned child's PID staying free until the
sweep ran, which is not guaranteed on a busy machine; a reused PID turned the
check into a SKIP. The deletable stand-in now uses this process's own PID
instead: the sweep runs before this process has created any clone, so a file
named with the current PID is always a leftover of a dead run there, and the
live-PID and handle-held skip cases keep their own deterministic stand-ins.
2026-11-22 21:02:58 +01:00
1188e6406e runtime: scope the TLS clone purge to ownerless files, clean clones before ExitProcess, per-slot thread-event errors
The startup purge deleted every leonida_tls_host_*.dll in the temp directory, so it could remove the
clone of a concurrent process that had copied its helper but not reached LoadLibraryExW yet. Clones
are now copied to a staging name and renamed into place while the copy step's guard handle is open, so
a final-name clone never exists without a handle, and the purge skips files that fail an exclusive
open or whose owner process (from the file name) is still alive.

std::atexit cleanup never ran on the --run path because main ends with ExitProcess; the loader now
exposes cleanup_tls_clones() and main calls it before ExitProcess (atexit still covers normal exits).

on_host_thread_event wrapped the whole slot loop in one try/catch, so one throwing slot killed the
events for every other module. The catch is now per slot, and the detach path releases the slot even
when the callbacks threw, instead of leaking the block to the OS loader.

README and the initialize_tls comment no longer claim only one mapped TLS module can be live; the
loader_tls_test gained purge-ownership and explicit-cleanup checks.
2026-11-22 20:54:47 +01:00
81d82bfcc4 runtime: support multiple simultaneous manually mapped static-TLS modules 2026-11-22 17:51:11 +01:00
a28493ba8f runtime: support multiple simultaneous manually mapped static-TLS modules 2026-11-22 17:34:35 +01:00
c8c640059d loader_tls_test: the thread that outlives its Loader belongs to an initialised module
With initialise() run, the module's TLS callbacks take thread events, so the
thread that ends after ~Loader has delivered the process detach and unmapped
the image checks that no DLL_THREAD_DETACH callback runs into freed memory.
2026-11-22 12:29:52 +01:00
21b920bfb0 loader_tls_test: thread events after initialise() and a thread that outlives its Loader
tiny_tls counts DLL_THREAD_ATTACH/DETACH only for a thread the test names, so
threads the process starts for itself can't change the counts; the test starts
that thread suspended and names it before it runs. Checks: one attach before the
thread's code runs, with the template value already readable in the callback;
one detach when it ends; and a thread that is still running when its Loader is
destroyed gets its copy freed when it ends, with the process carrying on.
2026-11-22 12:29:52 +01:00
e5d44d4b0f Loader: give every thread started after load() its own TLS copy
Until now only the thread that called load() had the mapped image's TLS; any
other thread read gta6rt_tls_host.dll's block through _tls_index, so a
thread_local on the game's own threads (gta6rt runs the entry point on a new
thread) started from the wrong data: tiny_game's thread_local check fails on
main with LEONIDA_RUN_TLS=1.

The loader now registers for the helper's thread events. On DLL_THREAD_ATTACH
the new thread gets a fresh copy of the template in the reserved slot; on
DLL_THREAD_DETACH the helper's own block goes back before the Windows loader
frees the array. Once initialise() has run the module, the events also run its
TLS callbacks (as Windows does: thread events only after DLL_PROCESS_ATTACH),
with the copy already in place.

An SRW lock orders the owner's publication, its initialised flag and its
retirement in ~Loader against the events, which run under the loader lock; the
exclusive sections only write fields. ~Loader retires the owner before
uninstalling, so a thread that outlives its Loader frees its copy from its own
record without touching the freed Module. Module::tls_block stays the loading
thread's copy, now set by initialize_tls only. install_tls_for_thread remains
for threads that existed before load(); on a thread that already has the copy
it is a no-op, which the updated worker check shows.
2026-11-22 12:29:51 +01:00
42e0b56c8d runtime: keep the loader off the default EXE base; loader_tls copies with CopyFileW
Under Wine the host EXE stays at its preferred base, so gta6rt and
loader_test at 0x140000000 took the address a mapped EXE without
relocations needs (loader_test: "preferred base is taken"). Link them at
0x10000000000; Windows relocates them with ASLR either way.

loader_tls_test copied its fixture with std::filesystem::copy_file,
which MSVC implements with CopyFile2, missing from Wine 9.
2026-11-22 02:25:15 +00:00
993a2cbf72 Runtime: install static TLS for mapped images 2026-11-21 23:29:24 +00:00