Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/boot.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ Read the startup trace first; the first event that is not the expected one is th
TLS index. The summary shows the address and phase of the last observed TLS callback
(`last_tls_callback` in JSON). The loader records it before invoking the callback, so the record gives no
completion result. The loader's own TLS is checked by `run_tiny_game_tls`.
An access violation in CRT startup before the first D3D12 call can be the TLS index instead: code built with
`/GA` reads its thread-local data at module index 0 without `_tls_index`, and index 0 belongs to `gta6rt.exe`
(stop 4 of [retail_boot_chain.md](retail_boot_chain.md)). `load()` rewrites those reads in a mapped executable
so that they reach the image's own block, and every other mapped module with a TLS directory has an index of
its own ([`runtime/README.md`](../runtime/README.md), "PE loader"). The stderr log has one
`[rt] loader: <image>: <N> index-0 TLS reads` line for the executable; `N` 0 on an image that faults there
means its reads have a shape the rewrite does not know. `LEONIDA_TLS_GA_PATCH=0` turns the rewrite off, to
see the stop without it. `loader_tls` checks both ways with a synthetic `/GA` executable mapped beside a DLL
that has TLS of its own, on the loading thread and on a new one, and `run_tiny_game_ga_tls` runs a `/GA`
build of the stand-in game through `gta6rt --run`. Not confirmed here on the retail executable: that needs
game files.
4. **`exception`** — code, `at`, access/address and the stack frames. The loader's vectored handler only records,
so the process behaviour is the same with or without the trace. When the report reaches `--limit`, it reserves
the final slot for the last observed exception. The trace also includes handled exceptions, so the last
Expand Down
18 changes: 13 additions & 5 deletions docs/boot_leads.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,19 @@ your own copy or with a synthetic test before you build on it, and record the re

- **Finding:** code built with `/GA` reads thread-local storage straight from `ThreadLocalStoragePointer[0]` and
assumes the executable's own TLS block sits at index 0. Static TLS has to be set up per mapped module, not once.
- **Where:** the retail run on `main` ends in CRT startup with an access violation and 0 D3D12 calls, "the stop-4
shape (TLS from module index 0)" ([retail_boot_intel_host.md §1](retail_boot_intel_host.md)). The loader already
keeps one TLS host slot per mapped module (`runtime/src/pe_loader.cpp`, `runtime/src/tls_host.cpp`), and the
integration tree carries a fix for stop 4 that `main` does not
([retail_boot_chain.md](retail_boot_chain.md), row 4).
- **Where:** a retail run on `main` at `867d922f` ended in CRT startup with an access violation and 0 D3D12 calls, "the
stop-4 shape (TLS from module index 0)" ([retail_boot_intel_host.md §1](retail_boot_intel_host.md)). The loader keeps
one TLS host slot per mapped module (`runtime/src/pe_loader.cpp`, `runtime/src/tls_host.cpp`). The integration tree
clears stop 4 with #4313 ([retail_boot_chain.md](retail_boot_chain.md), row 4).
- **Status:** answered on `main` by `b7a3fed28` and `4710549af`, which the measurement above predates; the retail
confirmation is still pending. `load()` rewrites the executable's `mov r64, gs:[58h]` reads so that they reach a
per-thread view with the image's own block at `[0]`, and each other mapped module with a TLS directory has an index of
its own. `loader_tls` checks it with a `/GA` executable beside a DLL with TLS of its own, on the loading thread and on
a new one, and shows the stop with `LEONIDA_TLS_GA_PATCH=0`; `run_tiny_game_ga_tls` runs a `/GA` build of the stand-in
game through `gta6rt --run`. #4313, which #321 and #64 carry, is an alternative design, not a missing fix: the
runtime's own EXE gives up index 0 at process start, so an index-0 read of any shape works without a rewrite. What is
left of this lead is the retail `--run` on current `main` and, if it still stops here, the read shape the rewrite does
not know.
- **Implement:**
1. Find the stop-4 fix on the integration tree and compare it with `Loader::initialize_tls` on `main`.
2. Make the executable's slot resolve at index 0 of the thread's TLS array, and give every other mapped module that
Expand Down
2 changes: 1 addition & 1 deletion docs/retail_boot_chain.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ stand-ins named below (not pull requests yet). Own 1.0.2.1 copy, Windows 11, RTX
| 1 | The first `XMemVirtualAlloc` calls: two 64 KiB reserve forms, then 2 MiB pages | #6709, #4531 |
| 2 | The 2 MiB reserve+commit calls carry attribute words the shim refuses, and `PAGE_GRAPHICS_*` bits (0x800..0x40000) on top of `PAGE_READWRITE` | Local: accept the attribute word, drop the graphics bits and keep the CPU protection. All seven startup allocations then succeed. #6843 measures the attribute words; no PR changes the check yet |
| 3 | `xgameruntime.lib`'s thunk treats a missing `XErrorReport` as fatal | #6582 |
| 4 | Access violation in the title's allocator: its `/GA` code reads its TLS from module index 0 | #4313 |
| 4 | Access violation in the title's allocator: its `/GA` code reads its TLS from module index 0 | #4313 in the measured tree: the runtime's own EXE gives up index 0 at process start. `main` has a different fix since `b7a3fed28` (with `4710549af` for threads that were already running): the loader rewrites the executable's index-0 reads so that they reach the image's own block ([boot.md](boot.md) §3, step 3). Checked on `main` with a synthetic `/GA` executable beside a DLL with TLS of its own (`loader_tls`) and a `/GA` build of the stand-in game under `gta6rt --run` (`run_tiny_game_ga_tls`); the retail run was not repeated for this note, it needs game files |
| 4a | Wine only, not reached on Windows: `GameInput.dll` ordinal 1, the executable's `GameInputCreate` import (by ordinal, not by name), which the loader looks up as `gameinput.dll!#1`. When `gta6rt` doesn't serve that name, the import binds to the host's `GameInput.dll`. On Windows the installed one answers; under Wine it is Wine's builtin stub, and the title reads through a null pointer right after the call. Measured by hcristosm after stop 4 with stop 5's fix (old upstream PR 4348) merged, so its order against stop 5 is not measured. #127 reproduced it on Wine 11.21 staging: the title's own `int3` 0.65 s after start, before any D3D12 call | #127 (the `#1` alias in the shim table, `runtime/shims/gameinput.cpp`), but only in a `gta6rt` built with the GameInput shim, that is with SDL3: an installed SDL3 3.2 or newer that CMake finds, `-DLEONIDA_SDL3_FETCH=ON` (`scripts/build_runtime.ps1 -FetchSdl3`) or `-DFETCHCONTENT_SOURCE_DIR_SDL3`. A build without SDL3, one with `-DLEONIDA_SDL3=OFF`, and `build_runtime.ps1`'s `cl.exe` fallback have no `GameInputCreate` at all ([gameinput.md](gameinput.md#building)), so they still stop here under Wine. With the shim and the alias, the Wine run went on to stop 6 in hcristosm's measurement |
| 5 | Fatal assert when `DStorageGetFactory` returns null | #4348 |
| 6 | `XMemAllocatePhysicalPages` unimplemented; the title asserts on FALSE | Local stand-in, §2 |
Expand Down
14 changes: 14 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,17 @@ if(BUILD_TESTING)
set_target_properties(tiny_game_fixed PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/fixtures/game/$<CONFIG>"
)
# The same game built with /GA, as a title's executable is: its thread_local reads go to module TLS index 0
# without _tls_index, and under gta6rt index 0 is gta6rt.exe's own (docs/boot_leads.md, BL-1).
add_executable(tiny_game_ga test/fixtures/tiny_game.cpp)
target_link_libraries(tiny_game_ga PRIVATE xgameruntime_stub)
target_compile_definitions(tiny_game_ga PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX _CRT_SECURE_NO_WARNINGS)
target_compile_options(tiny_game_ga PRIVATE /W4 /permissive- /utf-8 /EHsc /GA)
# /GA can remove the _tls_index reference that pulls in the CRT TLS directory.
target_link_options(tiny_game_ga PRIVATE /INCLUDE:_tls_used)
set_target_properties(tiny_game_ga PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/fixtures/game/$<CONFIG>"
)
add_executable(tiny_game_exit_code_test test/tiny_game_exit_code_test.cpp)
target_compile_definitions(tiny_game_exit_code_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
target_compile_options(tiny_game_exit_code_test PRIVATE /W4 /permissive- /utf-8)
Expand Down Expand Up @@ -1384,6 +1395,9 @@ if(BUILD_TESTING)
# The opt-in checks, one test each: thread_local on the game's threads, a C++ exception caught inside the image.
add_test(NAME run_tiny_game_tls COMMAND gta6rt --run $<TARGET_FILE:tiny_game> -- hello 42 tls)
add_test(NAME run_tiny_game_eh COMMAND gta6rt --run $<TARGET_FILE:tiny_game> -- hello 42 eh)
# The /GA build of the same game: thread_local on the entry thread and on a thread it starts, through index-0
# reads, while index 0 holds gta6rt.exe's own TLS.
add_test(NAME run_tiny_game_ga_tls COMMAND gta6rt --run $<TARGET_FILE:tiny_game_ga> -- hello 42 tls)
# BUILD-1457: the stack a game thread gets. gta6rt_stack reads SizeOfStackReserve from the built loader (nothing
# is run); run_tiny_game_stack asks Windows, on the entry thread and on a thread created with dwStackSize 0.
add_test(NAME gta6rt_stack COMMAND "${CMAKE_COMMAND}"
Expand Down
3 changes: 3 additions & 0 deletions runtime/test/fixtures/tiny_ga_tls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ __declspec(thread) int g_ga_tls_value = 7;

extern "C" __declspec(dllexport) int ga_tls_get() { return g_ga_tls_value; }
extern "C" __declspec(dllexport) void ga_tls_set(int value) { g_ga_tls_value = value; }
// Where this code looks for the variable on the calling thread: ThreadLocalStoragePointer[0] plus the variable's
// offset, with no read of the variable itself. The test uses it to tell whose block index 0 resolved to.
extern "C" __declspec(dllexport) void* ga_tls_address() { return &g_ga_tls_value; }

int main() {
return 0;
Expand Down
2 changes: 2 additions & 0 deletions runtime/test/fixtures/tiny_game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
// calls exit() with 0 (42 with `exit-code`) when everything held. Two checks are opt-in, by argument (CTest's
// run_tiny_game_tls and run_tiny_game_eh): `tls`, thread_local on the game's own threads, which needs the loader's
// per-thread TLS (gta6rt starts main on a separate thread); and `eh`, a C++ exception caught inside the image.
// run_tiny_game_ga_tls runs `tls` on tiny_game_ga, this file built with /GA: its thread_local reads then take module
// TLS index 0 without _tls_index, as a title's executable does, and under gta6rt that index is gta6rt.exe's own.
// A third, `game-file` (run_tiny_game_dir), reads GAME:\tiny_game_probe.txt after moving the current directory.
// A fourth, `stack` (run_tiny_game_stack), checks that the entry thread and a thread created with dwStackSize 0
// both have 8 MB of stack: a driver compiles shaders on the game's threads (BUILD-1457). Only under gta6rt: run
Expand Down
Loading
Loading