Loader: suspend a thread before reading its TEB; skip bad dump times #221

Manually merged
github merged 1 commit from gh-pr/221/head into gh-pr/221/base 2026-11-26 03:55:25 +00:00
Owner

@⁠AndyLatimer on GitHub · 2026-11-24 01:35 UTC · original
AndyLatimer:fix/loader-stack-dump-safety → main · +43 −15 · 1 files · 1 commits · merged 2026-11-25 00:19 UTC

Two fixes in the scheduled stack dumps (crates/victory-loader/src/stacks.rs, VICTORY_STACKS, diagnostics builds).

TEB read of a running thread: frames_of asked NtQueryInformationThread for the thread's TEB and read the stack bounds from it, then called SuspendThread. Our OpenThread handle keeps only the thread object alive, not its TEB. A title thread that exits between the query and the read frees its TEB, and the read faults: a crash caused by the diagnostics feature itself. The thread is now suspended before the query, and resumed on every path out (query failure included). The SAFETY comment now matches.

Dump times that panic: each time went through Duration::from_secs_f64(t.max(0.0)), which panics for inf, 1e20 and other times no Duration can hold. That kills the dump thread, and log_crashes_to's hook reports it as a crash. Parsing moved into schedule, which uses Duration::try_from_secs_f64, leaves out NaN (NaN.max(0.0) is 0, so it used to mean "now"), and sorts the Durations. Negative times still mean now.

Not changed: the unwind still runs while the target thread is suspended. If that thread holds ntdll's function-table lock (a DLL load at that moment), RtlLookupFunctionEntry can block. Avoiding that would need unwinding over a copy of the stack after resuming it, which is a bigger change for another PR.

Testing

  • cargo test -p victory-loader --features diagnostics: 20 + 2 passed. Includes the existing another_threads_stack_unwinds_to_its_start, which goes through the reordered frames_of, and 1 new test, schedules_skip_times_out_of_range.
  • cargo test --workspace: 264 passed, 4 ignored (need external files).
  • cargo clippy -p victory-loader --features diagnostics --all-targets: clean.
  • cargo xtask policy: pass. rustfmt --check on stacks.rs: clean.
  • Synthetic tests only (not run against a title).

🤖 Generated with Claude Code

**@⁠AndyLatimer** on GitHub · 2026-11-24 01:35 UTC · [original](https://github.com/Marvisak/VIctory/pull/221) `AndyLatimer:fix/loader-stack-dump-safety` → `main` · +43 −15 · 1 files · 1 commits · merged 2026-11-25 00:19 UTC Two fixes in the scheduled stack dumps (`crates/victory-loader/src/stacks.rs`, `VICTORY_STACKS`, diagnostics builds). **TEB read of a running thread:** `frames_of` asked `NtQueryInformationThread` for the thread's TEB and read the stack bounds from it, then called `SuspendThread`. Our `OpenThread` handle keeps only the thread object alive, not its TEB. A title thread that exits between the query and the read frees its TEB, and the read faults: a crash caused by the diagnostics feature itself. The thread is now suspended before the query, and resumed on every path out (query failure included). The SAFETY comment now matches. **Dump times that panic:** each time went through `Duration::from_secs_f64(t.max(0.0))`, which panics for `inf`, `1e20` and other times no `Duration` can hold. That kills the dump thread, and `log_crashes_to`'s hook reports it as a crash. Parsing moved into `schedule`, which uses `Duration::try_from_secs_f64`, leaves out NaN (`NaN.max(0.0)` is 0, so it used to mean "now"), and sorts the `Duration`s. Negative times still mean now. Not changed: the unwind still runs while the target thread is suspended. If that thread holds ntdll's function-table lock (a DLL load at that moment), `RtlLookupFunctionEntry` can block. Avoiding that would need unwinding over a copy of the stack after resuming it, which is a bigger change for another PR. ## Testing - `cargo test -p victory-loader --features diagnostics`: 20 + 2 passed. Includes the existing `another_threads_stack_unwinds_to_its_start`, which goes through the reordered `frames_of`, and 1 new test, `schedules_skip_times_out_of_range`. - `cargo test --workspace`: 264 passed, 4 ignored (need external files). - `cargo clippy -p victory-loader --features diagnostics --all-targets`: clean. - `cargo xtask policy`: pass. `rustfmt --check` on `stacks.rs`: clean. - Synthetic tests only (not run against a title). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Stack dumps (VICTORY_STACKS, diagnostics builds) read another thread's
TEB right after NtQueryInformationThread and suspended it only later: a
thread exiting in between frees its TEB, and the read faults. The
thread is now suspended first and resumed on every way out.

The dump times went through Duration::from_secs_f64, which panics on
`inf` or `1e20`, killing the dump thread (the crash hook logs it as a
crash). They are parsed by `schedule` now, which leaves out NaN and
times no Duration holds. 1 new test.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:55:24 +00:00
github manually merged commit acdcc41687 into gh-pr/221/base 2026-11-26 03:55:25 +00:00
Author
Owner

@⁠jeremygold02 on GitHub · 2026-11-24 05:16 UTC · original

Thanks @⁠AndyLatimer for guarding the diagnostic stack-dump schedule against unusable timeout values.

I tested commit acdcc41687be2ecd2bf2efc43ab7449329483382 with the feature that enables this module:

cargo test --locked -p victory-loader --features diagnostics --lib schedules_skip_times_out_of_range -- --test-threads=1

The targeted test passed (1 passed, 0 failed; 19 filtered). schedules_skip_times_out_of_range checks that a mixed schedule keeps valid 5- and 30-second entries, skips malformed, negative, infinite, NaN, and extremely large values, and returns an empty schedule for empty input.

The same test filter without --features diagnostics selected zero tests because this module is feature-gated. The suspend-before-TEB-read path compiles with diagnostics enabled, but this test does not exercise its thread-suspension behavior or races in a live stack dump.

**@⁠jeremygold02** on GitHub · 2026-11-24 05:16 UTC · [original](https://github.com/Marvisak/VIctory/pull/221#issuecomment-7122797149) Thanks @⁠AndyLatimer for guarding the diagnostic stack-dump schedule against unusable timeout values. I tested commit `acdcc41687be2ecd2bf2efc43ab7449329483382` with the feature that enables this module: ``` cargo test --locked -p victory-loader --features diagnostics --lib schedules_skip_times_out_of_range -- --test-threads=1 ``` The targeted test passed (1 passed, 0 failed; 19 filtered). [`schedules_skip_times_out_of_range`](https://github.com/Marvisak/VIctory/blob/acdcc41687be2ecd2bf2efc43ab7449329483382/crates/victory-loader/src/stacks.rs#L242) checks that a mixed schedule keeps valid 5- and 30-second entries, skips malformed, negative, infinite, NaN, and extremely large values, and returns an empty schedule for empty input. The same test filter without `--features diagnostics` selected zero tests because this module is feature-gated. The suspend-before-TEB-read path compiles with diagnostics enabled, but this test does not exercise its thread-suspension behavior or races in a live stack dump.
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!221
No description provided.