loader: bind 8 TEB-only/constant kernel functions with no bridge swap #609

Open
AnastasisKon wants to merge 3 commits from upstream-pr/609 into main
AnastasisKon commented 2026-11-28 12:18:42 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by AnastasisKon on 2026-11-28T12:18:42Z.


Refs #179

What

Every host call pays a ~370 ns GS swap each way (docs/native-host.md): at 100,000 host calls per frame that is ~37 ms — a third of a 4K120 frame budget on its own. Four functions the title calls constantly touch only the TEB at the caller's GS base, so they do not need the swap at all.

Eight functions the title calls constantly are now small functions of their own in teb.rs (_victory_*), bound with the existing declare_direct mechanism so the binder hands the title the raw address and a call costs the call itself: GetLastError/SetLastError (gs:[0x68]), TlsGetValue/TlsSetValue (gs:[0x1480 + 8*index]), GetCurrentThreadId/GetCurrentProcessId (gs:[0x48]/gs:[0x40] — the ids the TEB was made with), and GetCurrentProcess/GetCurrentThread (the constant pseudo-handles -1/-2). lookup intercepts all eight before the module tables (so system::lookup can't win for the pseudo-handles) and only for kernel-ish DLLs (kernel32/kernelbase/ntdll/vcruntime140/api-ms-win-core-*); the host-side extern "system" functions are unchanged. TlsAlloc/TlsFree stay bridged — they take the loader's global TLS-module lock.

Semantics mirror the bridged implementations exactly: TlsGetValue/TlsSetValue with index >= 64 set LastError to 87 (ERROR_INVALID_PARAMETER) and fail; success clears LastError, as the bridged versions do. The id functions return exactly what the TEB was populated with (victory_platform::thread::current_id() / std::process::id()).

Correctness

  • An execution test under Rosetta points GS at a fake TEB via the same thread_fast_set_cthread_self machdep call the bridge uses (drop-guard restores the real host GS base via host_tsd_base), then checks: GetLastError returns the value at fake+0x68; SetLastError writes it; TlsGetValue(3) reads slot 3; TlsSetValue(4, v) writes slot 4 and returns 1; TlsGetValue(64) returns null and sets LastError 87; GetCurrentThreadId/GetCurrentProcessId read fake+0x48/fake+0x40; GetCurrentProcess/GetCurrentThread return -1/-2.
  • A registry test checks lookup returns the direct addresses for all eight names across kernel32/kernelbase/ntdll/api-ms-win-* and that stub_for gives them no stub.

Testing

  • cargo test --target x86_64-apple-darwin -p victory-loader -p victory-hle (Rosetta) — all pass incl. both new tests (one unrelated test flaked once in a full run, not reproducible in 3 reruns; the Rosetta flake pattern is known on clean main)
  • cargo test -p victory-hle -p victory -p victory-gpu -p xtask (host arm64) — all pass
  • cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings — clean
  • cargo xtask policy — 404 files ok

Checklist

  • One change, one PR
  • Branch vibe/direct-last-error-1705ac from upstream/main, pushed to my fork only
  • No game/GDK files, no dumps/logs in the repo
  • No AI attribution in the diff
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:5540048506 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/609). Originally posted by [AnastasisKon](https://github.com/AnastasisKon) on 2026-11-28T12:18:42Z. --- Refs #179 ## What Every host call pays a ~370 ns GS swap each way (`docs/native-host.md`): at 100,000 host calls per frame that is ~37 ms — a third of a 4K120 frame budget on its own. Four functions the title calls constantly touch only the TEB at the caller's GS base, so they do not need the swap at all. Eight functions the title calls constantly are now small functions of their own in `teb.rs` (`_victory_*`), bound with the existing `declare_direct` mechanism so the binder hands the title the raw address and a call costs the call itself: `GetLastError`/`SetLastError` (`gs:[0x68]`), `TlsGetValue`/`TlsSetValue` (`gs:[0x1480 + 8*index]`), `GetCurrentThreadId`/`GetCurrentProcessId` (`gs:[0x48]`/`gs:[0x40]` — the ids the TEB was made with), and `GetCurrentProcess`/`GetCurrentThread` (the constant pseudo-handles -1/-2). `lookup` intercepts all eight before the module tables (so `system::lookup` can't win for the pseudo-handles) and only for kernel-ish DLLs (kernel32/kernelbase/ntdll/vcruntime140/api-ms-win-core-*); the host-side `extern "system"` functions are unchanged. `TlsAlloc`/`TlsFree` stay bridged — they take the loader's global TLS-module lock. Semantics mirror the bridged implementations exactly: `TlsGetValue`/`TlsSetValue` with `index >= 64` set LastError to 87 (`ERROR_INVALID_PARAMETER`) and fail; success clears LastError, as the bridged versions do. The id functions return exactly what the TEB was populated with (`victory_platform::thread::current_id()` / `std::process::id()`). ## Correctness - An execution test under Rosetta points GS at a fake TEB via the same `thread_fast_set_cthread_self` machdep call the bridge uses (drop-guard restores the real host GS base via `host_tsd_base`), then checks: GetLastError returns the value at `fake+0x68`; SetLastError writes it; TlsGetValue(3) reads slot 3; TlsSetValue(4, v) writes slot 4 and returns 1; TlsGetValue(64) returns null and sets LastError 87; GetCurrentThreadId/GetCurrentProcessId read `fake+0x48`/`fake+0x40`; GetCurrentProcess/GetCurrentThread return -1/-2. - A registry test checks `lookup` returns the direct addresses for all eight names across kernel32/kernelbase/ntdll/api-ms-win-* and that `stub_for` gives them no stub. ## Testing - [x] `cargo test --target x86_64-apple-darwin -p victory-loader -p victory-hle` (Rosetta) — all pass incl. both new tests (one unrelated test flaked once in a full run, not reproducible in 3 reruns; the Rosetta flake pattern is known on clean main) - [x] `cargo test -p victory-hle -p victory -p victory-gpu -p xtask` (host arm64) — all pass - [x] `cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings` — clean - [x] `cargo xtask policy` — 404 files ok ## Checklist - [x] One change, one PR - [x] Branch `vibe/direct-last-error-1705ac` from `upstream/main`, pushed to my fork only - [x] No game/GDK files, no dumps/logs in the repo - [x] No AI attribution in the diff
AnastasisKon commented 2026-11-28 12:30:13 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by AnastasisKon on 2026-11-28T12:30:13Z.


CI green (check + macos-scripts SUCCESS) on the final head (all eight direct functions), marked ready.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7239271806 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/609#issuecomment-7239271806). Originally posted by [AnastasisKon](https://github.com/AnastasisKon) on 2026-11-28T12:30:13Z. --- CI green (check + macos-scripts SUCCESS) on the final head (all eight direct functions), marked ready.
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/609:upstream-pr/609'
git switch 'upstream-pr/609'

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/609'
git switch 'upstream-pr/609'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/609'
git switch 'upstream-pr/609'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/609'
git switch 'main'
git merge --squash 'upstream-pr/609'
git switch 'main'
git merge --ff-only 'upstream-pr/609'
git switch 'main'
git merge 'upstream-pr/609'
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
LakshanSivakumar/Native-Victory-macOS!609
No description provided.