loader/stacks: suspend target thread before querying and reading its TEB stack bounds #586

Open
AscilCH wants to merge 1 commit from upstream-pr/586 into main
AscilCH commented 2026-11-25 02:35:36 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by AscilCH on 2026-11-25T02:35:36Z.


Problem

In crates/victory-loader/src/stacks.rs, frames_of() queried NtQueryInformationThread(ThreadBasicInformation) and directly dereferenced info.teb.add(1) / info.teb.add(2) (StackBase / StackLimit) while the target thread was still running, before calling SuspendThread(thread).

  1. If a transient worker thread exited between OpenThread/NtQueryInformationThread and reading info.teb, Windows unmapped its TEB and frames_of() crashed with STATUS_ACCESS_VIOLATION.
  2. If the thread grew its stack between reading StackLimit and SuspendThread, unwind_into received a stale low bound and truncated the stack trace.

Fix

  • Call SuspendThread(thread) first in frames_of(), then query NtQueryInformationThread(ThreadBasicInformation) while suspended, verify status == 0 && info.exit_status == STATUS_PENDING && !info.teb.is_null(), check low <= high, unwind, and always call ResumeThread(thread).
  • Add synthetic unit test frames_of_handles_exited_thread_safely.

Verification

  • cargo test -p victory-loader --all-features --locked stacks::tests: 2 passed, 0 failed
  • cargo test -p victory-loader --locked: 24 unit + 3 integration passed, 0 failed
  • cargo xtask policy: 169 files ok
  • cargo clippy -p victory-loader --all-targets --locked -- -D warnings: clean
  • cargo clippy -p victory-loader --all-targets --all-features --locked -- -D warnings: clean

(AI-assisted; synthetic unit tests only, no game data.)

<!-- greyfinch-sync:fxceless/victory-fork:issue:6595880562 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/586). Originally posted by [AscilCH](https://github.com/AscilCH) on 2026-11-25T02:35:36Z. --- ## Problem In `crates/victory-loader/src/stacks.rs`, `frames_of()` queried `NtQueryInformationThread(ThreadBasicInformation)` and directly dereferenced `info.teb.add(1)` / `info.teb.add(2)` (`StackBase` / `StackLimit`) **while the target thread was still running**, before calling `SuspendThread(thread)`. 1. If a transient worker thread exited between `OpenThread`/`NtQueryInformationThread` and reading `info.teb`, Windows unmapped its TEB and `frames_of()` crashed with `STATUS_ACCESS_VIOLATION`. 2. If the thread grew its stack between reading `StackLimit` and `SuspendThread`, `unwind_into` received a stale `low` bound and truncated the stack trace. ## Fix - Call `SuspendThread(thread)` first in `frames_of()`, then query `NtQueryInformationThread(ThreadBasicInformation)` while suspended, verify `status == 0 && info.exit_status == STATUS_PENDING && !info.teb.is_null()`, check `low <= high`, unwind, and always call `ResumeThread(thread)`. - Add synthetic unit test `frames_of_handles_exited_thread_safely`. ## Verification - `cargo test -p victory-loader --all-features --locked stacks::tests`: 2 passed, 0 failed - `cargo test -p victory-loader --locked`: 24 unit + 3 integration passed, 0 failed - `cargo xtask policy`: 169 files ok - `cargo clippy -p victory-loader --all-targets --locked -- -D warnings`: clean - `cargo clippy -p victory-loader --all-targets --all-features --locked -- -D warnings`: clean *(AI-assisted; synthetic unit tests only, no game data.)*
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/586:upstream-pr/586'
git switch 'upstream-pr/586'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/586'
git switch 'upstream-pr/586'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/586'
git switch 'upstream-pr/586'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/586'
git switch 'main'
git merge --squash 'upstream-pr/586'
git switch 'main'
git merge --ff-only 'upstream-pr/586'
git switch 'main'
git merge 'upstream-pr/586'
git push origin 'main'
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
fxceless/victory-fork!586
No description provided.