Loader: never pass NULL address hint to VirtualAlloc in tls::near #3064
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3066"
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 Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T06:02:25Z.
Summary
Prevents
tls::nearfrom passing aNULL(0) address hint toVirtualAllocand uses checked arithmetic for upper candidate addresses so allocated TLS thunk pages are guaranteed to stay within ±2 GiBrel32reach of the image.Why
In
crates/victory-loader/src/tls.rs,nearsearched downward withbase.checked_sub((k + 1) * GRANULE). Wheneverbaseis a multiple ofGRANULE(0x1_0000) and<= 1 GiB,(k + 1) * GRANULE == baseyieldsSome(0). Passing0(NULL) aslpAddresstellsVirtualAllocto ignore the hint and allocate anywhere in the 64-bit address space (e.g.,0x1d068de0000forbase = 0x10000), whichpatchthen silently truncates viarel as i32into an invalidcall rel32target.Filtering out
0(.filter(|&h| h != 0)) and usingchecked_add/checked_next_multiple_offor the upper hints avoids both theNULL-hint escape and high-address overflow.Verification
tls::tests::near_never_uses_null_hint_outside_rel32_reach:63bcca4(allocated at 0x1d068de0000, outside rel32 reach of 0x10000).cargo test -p victory-loader -- --test-threads=1: 40 passed, 0 failed (36 unit + 4 integration).cargo clippy -p victory-loader --all-targets -- -D warnings: clean.rustfmt --edition 2024 --check crates/victory-loader/src/tls.rs: clean.cargo xtask policy: 191 files ok.Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T06:20:39Z.
Review: APPROVED
Verified at
d2a67c31on Windows 11 x64 (Rust 1.98.1 MSVC):tls::near: filteringh != 0on the downward probe preventsVirtualAlloc(NULL, ...)whenbaseis an exact multiple ofGRANULEand(k + 1) * GRANULE == base(such as0x10000), and switchingabove+above + k * GRANULEto checked arithmetic prevents wraparound near the top of the address space. (Non-blocking note: filtering!= 0on the loophintitself instead of only the downward arm would also guard the degeneratebase == 0, size == 0input, though real mapped images are never at0.)cargo test -p victory-loader --locked: 40 passed, 0 failed (includingtls::tests::near_never_uses_null_hint_outside_rel32_reach).cargo clippy -p victory-loader --locked --all-targets -- -D warnings: clean.Looks good.
Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-26T06:31:57Z.
Review: APPROVED
Verified: base.checked_sub could yield Some(0) for granule-aligned bases -> VirtualAlloc(NULL) picks any 64-bit address outside rel32 reach; the filter plugs it and the checked_add path covers end/above overflow. The test's rel32 assertion exercises the real constraint.
Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-26T06:18:15Z.
Automated triage for the maintainers.
Proposed close: guard for a case that does not occur. In
crates/victory-loader/src/tls.rs(near), the search alternates between a hint below the image and one above it, and returns at the first free page. For the downward hint to reach 0, the title image would need to sit at or below 1 GiB, with every 64 KiB granule below it taken, and the same number of granules just above its end taken too. The only caller istls::redirect, with the image the OS loaded. The new test only reaches the NULL hint by passingbase = 0x10000, where the very first downward hint is 0, which is not an address an image is loaded at. Thechecked_addandchecked_next_multiple_ofon the upward hint likewise guard against overflowing 64-bit user space, which cannot happen.This PR will be closed in 48 hours unless the author pushes a change, or a maintainer removes the label. A report of the title's image actually landing that low would change this.
CI: no checks ran on this head.
Generated by Claude Code
Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T06:45:36Z.
Fair point from triage — a real 64-bit PE image will not sit at or below 1 GiB with the entire +1 GiB range above it occupied. Closing to keep the queue clean. AI-assisted (Gemini 4 Neon).
Pull request closed