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

Merged
AndyLatimer merged 1 commit from upstream-pr/221 into main 2026-11-25 00:19:15 +00:00
AndyLatimer commented 2026-11-24 01:35:03 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by AndyLatimer on 2026-11-24T01:35:03Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:issue:6572592876 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/221). Originally posted by [AndyLatimer](https://github.com/AndyLatimer) on 2026-11-24T01:35:03Z. --- 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)
jeremygold02 commented 2026-11-24 06:56:00 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by jeremygold02 on 2026-11-24T05:16:46Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7122797149 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/221#issuecomment-7122797149). Originally posted by [jeremygold02](https://github.com/jeremygold02) on 2026-11-24T05:16:46Z. --- 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.
mercuryVM commented 2026-11-24 09:04:57 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T09:04:57Z.

Review: APPROVED


Tested acdcc41 on Windows 11, RTX 5070, MSVC: cargo test --workspace --no-fail-fast passes (264 passed, 0 failed), and cargo test -p victory-loader --features diagnostics --lib passes (20), including another_threads_stack_unwinds_to_its_start and the new schedules_skip_times_out_of_range.

  • Moving SuspendThread ahead of the TEB query closes a real use-after-free: an OpenThread handle doesn't keep the TEB mapped. Every exit path after the suspend now resumes.
  • try_from_secs_f64 plus the NaN filter removes the panic on inf or 1e20, and the old quirk where NaN.max(0.0) meant "now". Negative times still mean "now", as documented.
  • The remaining risk you name (unwinding while the target holds the loader or function-table lock) is accurate, and is out of scope here.

No issues found.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:review:6036369960 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/221#pullrequestreview-6036369960). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T09:04:57Z. **Review:** APPROVED --- Tested acdcc41 on Windows 11, RTX 5070, MSVC: `cargo test --workspace --no-fail-fast` passes (264 passed, 0 failed), and `cargo test -p victory-loader --features diagnostics --lib` passes (20), including `another_threads_stack_unwinds_to_its_start` and the new `schedules_skip_times_out_of_range`. - Moving `SuspendThread` ahead of the TEB query closes a real use-after-free: an `OpenThread` handle doesn't keep the TEB mapped. Every exit path after the suspend now resumes. - `try_from_secs_f64` plus the NaN filter removes the panic on `inf` or `1e20`, and the old quirk where `NaN.max(0.0)` meant "now". Negative times still mean "now", as documented. - The remaining risk you name (unwinding while the target holds the loader or function-table lock) is accurate, and is out of scope here. No issues found. AI-assisted (Claude Code).
upstream-sync closed this pull request 2026-11-25 01:03:02 +00:00
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!221
No description provided.