loader: bind 8 TEB-only/constant kernel functions with no bridge swap #609
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/609"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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 existingdeclare_directmechanism 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), andGetCurrentProcess/GetCurrentThread(the constant pseudo-handles -1/-2).lookupintercepts all eight before the module tables (sosystem::lookupcan't win for the pseudo-handles) and only for kernel-ish DLLs (kernel32/kernelbase/ntdll/vcruntime140/api-ms-win-core-*); the host-sideextern "system"functions are unchanged.TlsAlloc/TlsFreestay bridged — they take the loader's global TLS-module lock.Semantics mirror the bridged implementations exactly:
TlsGetValue/TlsSetValuewithindex >= 64set 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
thread_fast_set_cthread_selfmachdep call the bridge uses (drop-guard restores the real host GS base viahost_tsd_base), then checks: GetLastError returns the value atfake+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 readfake+0x48/fake+0x40; GetCurrentProcess/GetCurrentThread return -1/-2.lookupreturns the direct addresses for all eight names across kernel32/kernelbase/ntdll/api-ms-win-* and thatstub_forgives 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 passcargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings— cleancargo xtask policy— 404 files okChecklist
vibe/direct-last-error-1705acfromupstream/main, pushed to my fork onlyImported 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.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.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.