Commit Graph

5 Commits

Author SHA1 Message Date
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