14 Commits
Author SHA1 Message Date
ItsNotRexedandClaude Opus 5.5 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
Rayhan RobinandDevin AI 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
kotorvrandClaude Opus 5.5 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
kotorvrandClaude Fable 5.2 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
kotorvrandClaude Fable 5.2 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
jaybeebabageyus 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
jaybeebabageyus 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
jaybeebabageyus 81d82bfcc4 runtime: support multiple simultaneous manually mapped static-TLS modules 2026-11-22 17:51:11 +01:00
jaybeebabageyus a28493ba8f runtime: support multiple simultaneous manually mapped static-TLS modules 2026-11-22 17:34:35 +01:00
omardev29 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
omardev29 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
omardev29 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
Ayoze Torres 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
Kam471 993a2cbf72 Runtime: install static TLS for mapped images 2026-11-21 23:29:24 +00:00