runtime: validate ApuAlloc outputs and synchronize AcpHal heap range queries #1895
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1902"
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 R0Y00/Xbox2Windows. Originally posted by Asimmimx on 2026-11-24T20:41:27Z.
Summary
Fixes pointer-validation, concurrency, and APU-range bugs in src/startup_stubs/acphal.cpp:
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T21:01:52Z.
Review: COMMENTED
Review of exact head
e88d2c8122bc6cde7dd0b5469be1989e08815071(title and body are neutral; the diff touches onlydocs/startup-runtime.md,src/startup_stubs/acphal.cpp,tests/startup_acphal_apu_free.cpp).Verdict: no P1, no P2 found. Two optional P3 notes below.
Commands run locally (against
origin/main...HEAD):git diff --check origin/main...HEAD: clean, rc=0tests/docs_links_check.py: "Self-test passed. Checked 18 Markdown files and 376 links."tests/bidi_controls_check.py: "Checked 1335 tracked source, script and build/CI files: no bidirectional controls."x2w_pipe.py win2025 1902 --targets d3d12_xs_shim,startup_acphal_apu_free,startup_stub_acphal,startup_stub_chad,startup_stub_consumer,startup_stub_gameinput,startup_stub_xapu,startup_stub_xcrdapi,startup_stub_xg_xs,xmem_shim --regex '^(startup_acphal_apu_free|startup_stubs)$'on Windows Server 2025 (MSVC 14.44, Debug and Release /O2): 2/2 passed in both (startup_acphal_apu_free,startup_stubs).Mutations (applied as patches on top of the exact head, then discarded; the checkout stayed clean):
in_apu_range:address < base_addr + heap_startchanged toaddress < base_addr(guard range accepted).startup_acphal_apu_freeFAILED, so the new guard-range assertions do bite.ApuAlloc:if (!virtual_ok || !apu_ok)changed toif (!virtual_ok).startup_acphal_apu_freecrashed with SEGFAULT (a write to the read-onlyapu_address), so the read-only APU-output tests bite.What I checked by reading:
ApuAllocprobes both outputs before allocating, so no block is leaked on an unwritable output. A refusal after a successfulheap_take(offset above 32 bits) callsheap_give, so the heap is left as it was.heap_snapshottakes the shared lock and releases it before callers use the values. It is never called whileheap_lockis held, so there is no recursive SRW acquire.ApuMapApuAddressusesoffset >= heap_startandoffset < bytes. The offset is already 32-bit, so the 4 GiB cap inin_apu_rangeagrees with it. Boundaries match the doc text ([0x10000, min(heap_bytes, 4 GiB))).AcpHalCreate: thestatic const auto table = []{...}()form is initialised once by the C++11 magic static, which removes the old unsynchronisedreadyflag.P3 (optional, not blocking):
src/startup_stubs/acphal.cpp(the 4 GiB cap inin_apu_range, and theoffset > 0xFFFFFFFFbranches inApuAllocandAcpHalAllocateShapeContexts): the test heap is 2 MiB, so these branches are never exercised. They read correctly, but nothing fails if they are removed.tests/startup_acphal_apu_free.cpp(concurrentAcpHalCreateblock): it checks that all callers get the same object, but that holds with or without the oldreadyrace, so the test would not have failed before the fix. It is a fine smoke test; only the claim that it proves the race fix is not backed.AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T21:17:40Z.
Review: CHANGES_REQUESTED
The synchronized queries now define an APU-addressable interval ending at 4 GiB, but both allocation paths only check the starting offset. ApuCreateHeap can accept two 32-bit sizes whose sum exceeds 4 GiB, and heap_take can return an extent with a representable start and unrepresentable tail. ApuMapVirtualAddress then refuses addresses inside a block that was returned successfully. Please constrain the full extent under the allocator lock, including AcpHalAllocateShapeContexts, or reject unsupported heap sizes up front with a documented failure. A synthetic boundary check can exercise an exact fit and a one-byte crossing without committing a large Windows heap. Keep the invalid-output and concurrent publishing controls.
Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-24T21:13:55Z.
Built it, ran it, and broke all three ApuAlloc behaviours the description claims
At the PR head, Windows 11 build 26300.9974, MSVC 19.44:
Three mutations to
src/startup_stubs/acphal.cpp, each rebuilt intoAcpHal.dll(LoadLibraryAin the fixture) with thetimestamp checked, then reverted:
virtual_ok = virtual_address && writable(...)→virtual_address != nullptrapu_ok = !apu_address || writable(...)→trueif (virtual_ok) *virtual_address = memory;→ unconditional storeThe third is the one I care about most: it is not a "did it return the right code" check but the crash-vs-leak distinction the
description opens with — a read-only output that still gets written. The fixture notices, which means it exercises the
PAGE_READONLYpath and not just a null pointer.Measurement note again: my first attempt rebuilt
startup_acphal_apu_free.exeand reported "not rebuilt" for all three;the fixture loads
AcpHal.dll, so the exe never changes. Rebuilding the DLL is what makes the mutants visible.The part I did not verify
The description also lists "Synchronize AcpHal heap range queries" and the APU-range bug (a
ApuAllocwhose offset exceeds32 bits). I mutated only the three output-pointer paths. The range/synchronization change would need a mutation inside
in_apu_range/heap_giveand a concurrent case; I did not attempt it, so my table says nothing about that half.One observation on the shape
if (virtual_ok) *virtual_address = memory;at line 311 means a nullvirtual_addressstill allocates and returnsE_POINTER— the block is then leaked. The fixture's negative control covers the read-only pointer; if a null-pointer case isalso in there, the leak path is pinned as intended rather than tolerated. Worth a sentence in the description either way, since
"refused but allocated" is the kind of thing a later reader will ask about.
Verdict
Reviewed. All three pointer-validation behaviours are pinned by mutations, and the concurrency/range half is left as reported
rather than verified by me.
Notes
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T03:51:55Z.
Review: COMMENTED
Addressed the requested change (full extent within 4 GiB) by taking the "reject unsupported heap sizes up front" option, pushed as
6864e3352.src/startup_stubs/acphal.cpp(ApuCreateHeap): a 64 KiB-rounded heap size above 4 GiB returnsE_INVALIDARGunder the heap lock, beforeVirtualAlloc. Every extent (including the tail fromheap_take, and soAcpHalAllocateShapeContexts) therefore ends at or below 4 GiB.tests/startup_acphal_apu_free.cpp(first_creation): a heap crossing 4 GiB by one byte and two maximal 32-bit sizes are refused, and a refused create leaves no heap. No large heap is committed. The invalid-output and concurrent-publishing controls are unchanged.docs/startup-runtime.md: documents the refusal.Verification (Win2025, WARP host, dev and release):
x2w_pipe.py win2025 1902 --patch <diff> --regex acphalgave 2/2 tests passed in both presets. With only the test change applied,startup_acphal_apu_freefails (3 new checks), so the test fails before and passes after.docs_links_check.py,bidi_controls_check.pyandgit diff --checkare clean. Win2025 ran on the previous head plus this patch; the pushed head also merges current main (no conflicts), which was not re-run on Win2025.@R0Y00 could you re-review? @Asimmimx thanks, feel free to adjust.
AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-26T04:25:28Z.
Review: APPROVED
The 4 GiB bound now applies to the rounded heap extent before it is published. Both size inputs are widened before addition, and the range readers pair the base with its size under the lock. Windows Release: cmake --build --preset release --target startup_acphal_apu_free, then ctest --preset release --no-tests=error -j 1 -R "^startup_acphal_apu_free$" -V passed 1/1. The fixture exercises the one-byte crossing, two maximal inputs, invalid output pointers, concurrent publication, and allocation/free. This resolves my prior extent concern.