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

Open
github-bot wants to merge 3 commits from github-bot/gh-609 into main AGit
Owner

@⁠AnastasisKon on GitHub · 2026-11-28 12:18 UTC · original
AnastasisKon:vibe/direct-last-error-1705ac → main · +258 −16 · 3 files · 3 commits · open

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
**@⁠AnastasisKon** on GitHub · 2026-11-28 12:18 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/609) `AnastasisKon:vibe/direct-last-error-1705ac` → `main` · +258 −16 · 3 files · 3 commits · open 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
Every host call pays a ~370 ns GS swap each way (docs/native-host.md);
at 100k calls a frame that is ~37 ms, a third of a 4K120 frame budget on
its own. GetLastError and SetLastError touch only the TEB at the
caller's GS base, so they do not need the swap: they are now two
instructions of their own (mov eax, gs:[0x68] / mov gs:[0x68], ecx),
bound with declare_direct, so the title calls them with its GS base
already in place and a call costs the call itself.

The host-side extern "system" functions are unchanged (host code still
calls those through the bridge). A test points GS at a fake TEB with
the same machdep call the bridge uses and checks both functions read
and write gs:[0x68] of it; another checks lookup hands out the direct
addresses for kernel32/kernelbase/ntdll/api-ms-win-* and that stub_for
gives them no stub.

Docs updated in native-host.md.

Refs #179

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <[email protected]>
Same TEB-only path as Get/SetLastError: the 64 TLS slots live at
gs:[0x1480 + 8*index] of the caller's TEB and the out-of-range failure
sets LastError (gs:[0x68]) to 87, so _victory_TlsGetValue/_victory_TlsSetValue
mirror the bridged implementation exactly (success also clears LastError)
and are bound with declare_direct. TlsAlloc/TlsFree stay bridged: they
take the loader's global TLS-module lock.

The Rosetta execution test now also checks a slot read, a slot write,
and that index >= 64 returns null with LastError 87; the registry test
covers all four direct functions across kernel32/kernelbase/ntdll/
api-ms-win-*.

Refs #179

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-28 12:24:02 +00:00
github-bot changed title from loader: bind GetLastError/SetLastError/TlsGetValue/TlsSetValue with no bridge swap to loader: bind 8 TEB-only/constant kernel functions with no bridge swap 2026-11-28 12:33:23 +00:00
GetCurrentThreadId and GetCurrentProcessId read ids the TEB was made
with (gs:[0x48] / gs:[0x40]); GetCurrentProcess and GetCurrentThread
return the constant pseudo-handles -1 / -2. All four are bound direct
like the last-error and TLS functions. lookup now intercepts the eight
direct names before the module tables (system::lookup would otherwise
win for the pseudo-handles) and only for the kernel-ish DLLs.

The Rosetta execution test checks the two TEB id reads and both
pseudo-handles against a fake TEB; the registry test covers all eight
names across kernel32/kernelbase/ntdll/api-ms-win-*.

Refs #179

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <[email protected]>
Author
Owner

@⁠AnastasisKon on GitHub · 2026-11-28 12:30 UTC · original

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

**@⁠AnastasisKon** on GitHub · 2026-11-28 12:30 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/609#issuecomment-7239271806) CI green (check + macos-scripts SUCCESS) on the final head (all eight direct functions), marked ready.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/613/head:github-bot/gh-609'
git switch 'github-bot/gh-609'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-609'
git switch 'github-bot/gh-609'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-609'
git switch 'github-bot/gh-609'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-609'
git switch 'main'
git merge --squash 'github-bot/gh-609'
git switch 'main'
git merge --ff-only 'github-bot/gh-609'
git switch 'main'
git merge 'github-bot/gh-609'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.