runtime: validate ApuAlloc outputs and synchronize AcpHal heap range queries #1895

Merged
Asimmimx merged 3 commits from upstream-pr/1902 into main 2026-11-26 04:25:35 +00:00
Asimmimx commented 2026-11-24 20:41:27 +00:00 (Migrated from github.com)

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:

  1. ApuAlloc crashed on non-writable outputs and could leak or truncate:
    • Unlike AcpHalCreate and AcpHalAllocateShapeContexts, ApuAlloc did not probe irtual_address or optional pu_address with x2w_local::writable() before allocating. Passing a read-only pointer allocated a live block and then faulted with
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6590997001 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1902). Originally posted by [Asimmimx](https://github.com/Asimmimx) on 2026-11-24T20:41:27Z. --- ### Summary Fixes pointer-validation, concurrency, and APU-range bugs in src/startup_stubs/acphal.cpp: 1. **ApuAlloc crashed on non-writable outputs and could leak or truncate:** - Unlike AcpHalCreate and AcpHalAllocateShapeContexts, ApuAlloc did not probe irtual_address or optional pu_address with x2w_local::writable() before allocating. Passing a read-only pointer allocated a live block and then faulted with
tedin7 commented 2026-11-24 21:27:29 +00:00 (Migrated from github.com)

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 only docs/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=0
  • tests/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):

  1. in_apu_range: address < base_addr + heap_start changed to address < base_addr (guard range accepted). startup_acphal_apu_free FAILED, so the new guard-range assertions do bite.
  2. ApuAlloc: if (!virtual_ok || !apu_ok) changed to if (!virtual_ok). startup_acphal_apu_free crashed with SEGFAULT (a write to the read-only apu_address), so the read-only APU-output tests bite.

What I checked by reading:

  • ApuAlloc probes both outputs before allocating, so no block is leaked on an unwritable output. A refusal after a successful heap_take (offset above 32 bits) calls heap_give, so the heap is left as it was.
  • heap_snapshot takes the shared lock and releases it before callers use the values. It is never called while heap_lock is held, so there is no recursive SRW acquire.
  • ApuMapApuAddress uses offset >= heap_start and offset < bytes. The offset is already 32-bit, so the 4 GiB cap in in_apu_range agrees with it. Boundaries match the doc text ([0x10000, min(heap_bytes, 4 GiB))).
  • AcpHalCreate: the static const auto table = []{...}() form is initialised once by the C++11 magic static, which removes the old unsynchronised ready flag.

P3 (optional, not blocking):

  • src/startup_stubs/acphal.cpp (the 4 GiB cap in in_apu_range, and the offset > 0xFFFFFFFF branches in ApuAlloc and AcpHalAllocateShapeContexts): 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 (concurrent AcpHalCreate block): it checks that all callers get the same object, but that holds with or without the old ready race, 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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6047344244 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1902#pullrequestreview-6047344244). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T21:01:52Z. **Review:** COMMENTED --- Review of exact head `e88d2c8122bc6cde7dd0b5469be1989e08815071` (title and body are neutral; the diff touches only `docs/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=0 - `tests/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): 1. `in_apu_range`: `address < base_addr + heap_start` changed to `address < base_addr` (guard range accepted). `startup_acphal_apu_free` FAILED, so the new guard-range assertions do bite. 2. `ApuAlloc`: `if (!virtual_ok || !apu_ok)` changed to `if (!virtual_ok)`. `startup_acphal_apu_free` crashed with SEGFAULT (a write to the read-only `apu_address`), so the read-only APU-output tests bite. What I checked by reading: - `ApuAlloc` probes both outputs before allocating, so no block is leaked on an unwritable output. A refusal after a successful `heap_take` (offset above 32 bits) calls `heap_give`, so the heap is left as it was. - `heap_snapshot` takes the shared lock and releases it before callers use the values. It is never called while `heap_lock` is held, so there is no recursive SRW acquire. - `ApuMapApuAddress` uses `offset >= heap_start` and `offset < bytes`. The offset is already 32-bit, so the 4 GiB cap in `in_apu_range` agrees with it. Boundaries match the doc text (`[0x10000, min(heap_bytes, 4 GiB))`). - `AcpHalCreate`: the `static const auto table = []{...}()` form is initialised once by the C++11 magic static, which removes the old unsynchronised `ready` flag. P3 (optional, not blocking): - `src/startup_stubs/acphal.cpp` (the 4 GiB cap in `in_apu_range`, and the `offset > 0xFFFFFFFF` branches in `ApuAlloc` and `AcpHalAllocateShapeContexts`): 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` (concurrent `AcpHalCreate` block): it checks that all callers get the same object, but that holds with or without the old `ready` race, 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.
R0Y00 commented 2026-11-24 21:27:29 +00:00 (Migrated from github.com)

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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6047561497 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1902#pullrequestreview-6047561497). Originally posted by [R0Y00](https://github.com/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.
ChazY11 commented 2026-11-24 21:27:40 +00:00 (Migrated from github.com)

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:

cmake --preset dev
cmake --build --preset dev --target startup_stub_acphal startup_acphal_apu_free
ctest --preset dev --no-tests=error -R 'acphal'
1/1 Test  #96: startup_acphal_apu_free .......   Passed
1/1 Test #753: cpu_native_acphal_hal ..........   Passed
100% tests passed, 0 tests failed out of 2

Three mutations to src/startup_stubs/acphal.cpp, each rebuilt into AcpHal.dll (LoadLibraryA in the fixture) with the
timestamp checked, then reverted:

mutation result
virtual_ok = virtual_address && writable(...) → virtual_address != nullptr caught
apu_ok = !apu_address || writable(...) → true caught
if (virtual_ok) *virtual_address = memory; → unconditional store caught

The 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_READONLY path and not just a null pointer.

Measurement note again: my first attempt rebuilt startup_acphal_apu_free.exe and 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 ApuAlloc whose offset exceeds
32 bits). I mutated only the three output-pointer paths. The range/synchronization change would need a mutation inside
in_apu_range / heap_give and 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 null virtual_address still allocates and returns
E_POINTER — the block is then leaked. The fixture's negative control covers the read-only pointer; if a null-pointer case is
also 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

  • AI-assisted review.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7144654046 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1902#issuecomment-7144654046). Originally posted by [ChazY11](https://github.com/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: ```powershell cmake --preset dev cmake --build --preset dev --target startup_stub_acphal startup_acphal_apu_free ctest --preset dev --no-tests=error -R 'acphal' 1/1 Test #96: startup_acphal_apu_free ....... Passed 1/1 Test #753: cpu_native_acphal_hal .......... Passed 100% tests passed, 0 tests failed out of 2 ``` Three mutations to `src/startup_stubs/acphal.cpp`, each rebuilt into **`AcpHal.dll`** (`LoadLibraryA` in the fixture) with the timestamp checked, then reverted: | mutation | result | |---|---| | `virtual_ok = virtual_address && writable(...)` → `virtual_address != nullptr` | **caught** | | `apu_ok = !apu_address \|\| writable(...)` → `true` | **caught** | | `if (virtual_ok) *virtual_address = memory;` → unconditional store | **caught** | The 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_READONLY` path and not just a null pointer. **Measurement note again**: my first attempt rebuilt `startup_acphal_apu_free.exe` and 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 `ApuAlloc` whose offset exceeds 32 bits). I mutated only the three output-pointer paths. The range/synchronization change would need a mutation inside `in_apu_range` / `heap_give` and 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 **null** `virtual_address` still allocates and returns `E_POINTER` — the block is then leaked. The fixture's negative control covers the read-only pointer; if a null-pointer case is also 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 - AI-assisted review.
tedin7 commented 2026-11-26 04:04:03 +00:00 (Migrated from github.com)

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 returns E_INVALIDARG under the heap lock, before VirtualAlloc. Every extent (including the tail from heap_take, and so AcpHalAllocateShapeContexts) 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 acphal gave 2/2 tests passed in both presets. With only the test change applied, startup_acphal_apu_free fails (3 new checks), so the test fails before and passes after. docs_links_check.py, bidi_controls_check.py and git diff --check are 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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6068312319 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1902#pullrequestreview-6068312319). Originally posted by [tedin7](https://github.com/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 returns `E_INVALIDARG` under the heap lock, before `VirtualAlloc`. Every extent (including the tail from `heap_take`, and so `AcpHalAllocateShapeContexts`) 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 acphal` gave 2/2 tests passed in both presets. With only the test change applied, `startup_acphal_apu_free` fails (3 new checks), so the test fails before and passes after. `docs_links_check.py`, `bidi_controls_check.py` and `git diff --check` are 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.
upstream-sync closed this pull request 2026-11-26 05:02:08 +00:00
R0Y00 commented 2026-11-26 05:02:08 +00:00 (Migrated from github.com)

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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6068538068 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1902#pullrequestreview-6068538068). Originally posted by [R0Y00](https://github.com/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.
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
R0Y00/Xbox2Windows!1895
No description provided.