Skip to content

startup_trace: LEONIDA_SKIP_INT3 works on title threads and with Wine's breakpoint address - #7824

Open
aslan2142 wants to merge 1 commit into
ItsNotRexed:mainfrom
aslan2142:skip-int3-wine
Open

aslan2142 wants to merge 1 commit into
ItsNotRexed:mainfrom
aslan2142:skip-int3-wine

Conversation

@aslan2142

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue: #7599 (comment) (finding by Spacecer2: #7599 (comment)).

LEONIDA_SKIP_INT3 never took effect in Spacecer2's Wine boot. The measurements are theirs (Wine 11.22, main 1ce6eb5 plus several PRs, #4313 applied); this PR is the fix they described, with tests.

The four bugs (Spacecer2's measurements)

  1. The flag never turned on. skip_int3 read LEONIDA_SKIP_INT3 into a block-scope static const bool. Its first use is on the title's thread, and there the MSVC thread-safe-static guard passed while the object was still zero. So skip stayed false and every guest int3 stayed fatal.
  2. Two more block-scope statics in the handler: skip_int3's static std::set<const void*> seen, and the trace lock from detail::mutex().
  3. The breakpoint address. Their Wine run reported ExceptionAddress and Rip one byte past the 0xCC, so the Rip == at test rejected every breakpoint.
  4. A null pointer. They saw a null+8 write inside skip_int3 and traced it to a null EXCEPTION_POINTERS.

The change

  • runtime/shims/startup_trace.h:
    • startup_trace::open() reads LEONIDA_SKIP_INT3 into a constinit std::atomic<bool>. That runs on the thread that opens the trace, before any guest code.
    • The trace lock (detail::mutex()) is a constinit SRW lock. It has no constructor or destructor, so it stays usable after static destruction, as the old leaked new std::mutex was. Callers are unchanged (std::lock_guard lock(detail::mutex())).
    • The logged int3 sites are a constinit open-addressing table of 4096 atomics. It needs no constructor, lock or allocation inside the handler. If it fills up, a site is logged again rather than lost.
    • The int3 is found from Rip first (the Windows form, unchanged), then Rip - 1. ExceptionAddress must be on that byte or one past it. Rip always ends at int3 + 1. Each byte is checked with VirtualQuery before it is read.
    • skip_int3 returns false for a null EXCEPTION_POINTERS, ExceptionRecord or ContextRecord. The handler already rejected a null pointer or record before calling it.
  • runtime/shims/xgameruntime_offline.cpp: LEONIDA_LOG_GAME_EVENTS (XGameEventWrite) and LEONIDA_HDR (XDisplayTryEnableHdrMode) were the other LEONIDA_* flags read into block-scope statics on title-thread paths. They are now cached in namespace-scope std::atomic<int> (-1 = not read yet), the pattern registry.cpp uses for LEONIDA_UNIMPLEMENTED.
  • Docs: docs/boot.md and runtime/README.md say that the flag is read when the trace opens.

Tests

int3_skip_test now runs each case in a child process of its own, so a crash fails only that case and each case gets its own environment:

Case What it checks main This branch
real real __debugbreak() at two sites, one hit twice: execution continues, 2 events pass pass
windows synthetic EXCEPTION_POINTERS with Rip == at on a 0xCC buffer: Rip == at + 1, one event naming at; not skipped when there is no 0xCC or ExceptionAddress is elsewhere pass pass
wine ExceptionAddress == Rip == at + 1, and ExceptionAddress == at with Rip == at + 1: both end at at + 1, one event naming at fail (not skipped) pass
null null EXCEPTION_POINTERS, null record, null context: false, no crash fail (0xc0000005 in skip_int3) pass
init variable set, trace opened, variable removed: the flag reads on and an int3 is still skipped fail (not skipped) pass
off without the variable nothing is skipped pass pass
  • Build and run: clang for x86_64-w64-mingw32 (libstdc++), run under Wine 11.22 (Staging) on Linux. For main (c1392d06f) the test was built against main's header, without the two checks that read the new skip_int3_on flag, which doesn't exist there.
  • Other tests (same mingw + Wine setup):
    • xgameruntime_offline_test passes 44/44, also with LEONIDA_HDR=1 LEONIDA_LOG_GAME_EVENTS=1.
    • startup_trace_test (7 of 26) and file_system_test (6 of 446) have the same failures on main and on this branch. They are failures of the mingw/Wine setup, not of this change.
    • xmem.cpp compiles with the new header.
  • Not tested:
    • Not built with MSVC: CI's /W4 /permissive- build is the check for that.
    • Not tested on native Windows.
    • Not tested with a real boot: no retail code was run.

What I could and could not reproduce here

Simple probes, Wine 11.22 (Staging):

  • Breakpoint address: a plain 0xCC is reported the Windows way here (ExceptionAddress == Rip == the 0xCC), on the main thread and on a new thread. Only CD 03 (int 3, still not handled) and 90 CC put both one past the instruction start. So I couldn't reproduce bug 3 with a plain int3. The fix accepts both forms and keeps the Windows one first.
  • Null EXCEPTION_POINTERS: never seen in those probes. A likely alternative cause of the null+8 access: an MSVC std::set whose constructor never ran has a null head node, and its parent pointer is at offset 8. That fits bug 2. The null checks stay either way.
  • Guest-thread statics and Loader: the mapped game executable owns module TLS index 0 (RT-259) #4313: mingw's libstdc++ guards statics with __cxa_guard_acquire, not a TLS epoch, so bug 1 can't be reproduced without an MSVC build. The symptom fits an MSVC _Init_thread_epoch that reads 0 instead of INT_MIN through gta6rt's _tls_index on the title's thread. That would mean gta6rt's TLS block on that thread is zero-filled or belongs to someone else.
    • I checked one premise of Loader: the mapped game executable owns module TLS index 0 (RT-259) #4313 under Wine: a LoadLibrary'd DLL whose TLS template is patched after loading. Wine copies the patched template for threads created afterwards, as Windows does. So the home-clone copy itself looks fine on Wine.
    • The cause is still open. A next check is to read _Init_thread_epoch through _tls_index on a title-created thread in an MSVC build under Wine.
    • Either way, code the guest can reach shouldn't rely on block-scope statics or thread_local.

Related pull requests

Thanks to Spacecer2 for the measurements and the fix this follows.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • Claimed in the coordination issue.
  • Tests fail on main and pass with the change (mingw + Wine 11.22; MSVC is left to CI).
  • A new translator rule is in the contract table of docs/design.md. (Not applicable: no translator change.)

AI-assisted (Claude Code, Claude Opus 5.5).

…'s breakpoint address

Spacecer2 measured under Wine 11.22 (ItsNotRexed#7599) that LEONIDA_SKIP_INT3 never took
effect: the flag was a block-scope static first read on a title thread, whose
MSVC thread-safe-static guard passed with the object still zero, and the
handler's int3 set and the trace lock had the same shape.

- The flag is read by startup_trace::open(), on the thread that opens the trace,
  into a constant-initialized atomic.
- The trace lock is a constant-initialized SRW lock (no constructor, never
  destroyed), and the logged int3 sites are a constant-initialized table of
  atomics: the handler touches no block-scope static.
- The int3 is found from Rip (Windows) or Rip - 1 (Spacecer2's Wine run), and
  Rip always ends one byte past the 0xCC.
- skip_int3 returns false for a null EXCEPTION_POINTERS, record or context.
- LEONIDA_LOG_GAME_EVENTS and LEONIDA_HDR, read on title threads, are cached in
  namespace-scope atomics instead of block-scope statics.

int3_skip_test runs each case in a child process: real int3s, the Windows and
the Wine breakpoint forms, the null pointers, the flag read at open() and the
flag off. The wine, null and init cases fail on main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AirKyzzZ

Copy link
Copy Markdown
Contributor

Tested 269ec0aea on RTX 2060 (NVIDIA 566.14), Windows 10 22H2 (native, not Wine), clang-cl 23.1.6 + lld-link (MSVC 14.44 STL), SDK 10.0.26100: pass

Built int3_skip_test at the head as is, ctest -R "^int3_skip$": 1/1 pass. Without the fix: with runtime/shims/startup_trace.h and runtime/shims/xgameruntime_offline.cpp reverted to the merge base c1392d06f, the test does not compile (it reads detail::skip_int3_on, which only the PR adds), so I replaced those two reads with true and ran it again. It then fails case wine (FAIL Rip one byte past the 0xCC: skipped, FAIL ExceptionAddress on the 0xCC, Rip past it: skipped), case null (exits with 0xc0000005) and case init (FAIL an int3 after the variable is removed is still skipped); case windows passes on both. No game files. AI-assisted (Claude Code).

@MatyEjsr

Copy link
Copy Markdown

AI-assisted review and native Windows / MSVC verification (Gemini).

PASS (MSVC /W4 /permissive- build & native Windows verification) on commit 269ec0aeae0e3b9b4aec55e81fec00df57d68c7d, with two minor code-review notes below.

Environment

  • OS: Windows 11 x64 (build 26200)
  • Compiler / Build: MSVC 19.44.35230 (VS 2022 Build Tools), CMake + Ninja (RelWithDebInfo, /std:c++20 /W4 /permissive-, 8 parallel jobs via scripts/build_runtime.ps1 -jobs 8)
  • SDKs: Windows SDK 10.0.26100.0, Vulkan SDK 1.4.363.0
  • GPU / Driver: NVIDIA GeForce RTX 3090, driver 617.42
  • Inputs: Synthetic test fixtures only (no game data, no keys).

Verification Results

  1. Full MSVC runtime build (356/356 targets): Succeeded cleanly with 0 warnings and 0 errors in all touched files (runtime/shims/startup_trace.h, runtime/shims/xgameruntime_offline.cpp, runtime/test/int3_skip_test.cpp) and across all targets including startup_trace.h (gta6rt.exe, loader/shim/runtime tests). Confirmed constinit std::atomic<bool>, constinit Lock (SRWLOCK_INIT), and constinit std::atomic<const void*> int3_sites[4096] compile cleanly under MSVC /std:c++20 /W4 /permissive-.
  2. Direct native Windows test runs:
    • int3_skip_test.exe: all checks passed (exit 0) across all 6 child-process cases (real, windows, wine, null, init, off).
    • startup_trace_test.exe: 26/26 checks passed (exit 0).
    • xgameruntime_offline_test.exe: 44/44 checks passed with default environment, and 44/44 checks passed with LEONIDA_HDR=1 and LEONIDA_LOG_GAME_EVENTS=1 (confirmed HDR enabled and per-event logging emitted via env_is_one).
  3. Focused CTest subset (int3_skip, startup_trace, xgameruntime*, file_system, xmem, loader*, pe_loader*, unimplemented_policy, dll_thread_calls, module_names, run_tiny_game*, gta6rt*): 33/33 passed (0.84 s).
  4. Full runtime CTest (ctest -j 8 --output-on-failure on RTX 3090): 269/272 passed (11 skipped for retail/optional inputs, 1 disabled for absent SDL3). The 3 failures (d3d12_vulkan_device sparse tiling tiles == 1, d3d12_indirect_dispatch_count, and d3d12_depth_resolve) are unrelated D3D12/Vulkan layer failures that do not touch startup_trace.h or xgameruntime_offline.cpp.
  5. Hygiene / repo checks:
    • python -I scripts/check_no_game_data.py on all 6 changed files: exit 0.
    • git diff --check HEAD~1..HEAD: clean.
    • python -I tests/test_ctest_registration.py: 4/4 passed.

Code Review Notes (Non-Blocking)

  1. Adjacent 0xCC bytes in int3_from() (runtime/shims/startup_trace.h:239-247): When ExceptionAddress == at and Rip == at + 1 (the mixed form tested in case_wine) and the byte at at + 1 also happens to be 0xCC (common in MSVC 0xCC padding), if (readable_byte(rip) && *rip == 0xCC) selects at = rip (at_real + 1) and skips the else if branch; the subsequent check address != at && address != at + 1 then fails (at_real != at_real + 1 and at_real != at_real + 2) and returns nullptr instead of skipping. Falling through to check rip - 1 when at == rip fails the address check (or checking (rip, address) pairs directly) would avoid missing that case.
  2. Unused include: #include <set> at runtime/shims/startup_trace.h:24 is now unused after replacing std::set<const void*> with the int3_sites atomic table.
  3. Overlap note: This overlaps with open PR startup trace: LEONIDA_SKIP_INT3 honours an int3 on a loader-created thread #7723 on startup_trace.h / int3_skip_test.cpp; this PR avoids static-destructor hazards during late VEH teardown by using a constinit SRWLOCK wrapper and lock-free constinit atomic table, and also covers the two xgameruntime_offline.cpp block-scope statics.

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.

3 participants