acphal: power-of-two alignment in ApuIsVirtualAddressValid; ApuAlloc(flags=2) gets the lazy default heap (RT-ACPHAL-ALIGNMENT-AND-LAZY-HEAP) #2535

Merged
leonida/mandy
Tyrannys merged 1 commit from rt-acphal-alignment-lazy-heap into main 2026-11-28 23:22:46 +00:00

Pull request successfully merged and closed

Tyrannys commented 2026-11-28 01:13:28 +00:00

Claimed in the coordination issue as: RT-ACPHAL-ALIGNMENT-AND-LAZY-HEAP (https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7231475417)

What and why

Item 4 of @kevincommits' batch 18 in #1608 (https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7231277215). Thanks for the repro.

runtime/shims/acphal.cpp, the audio hardware layer's APU heaps:

  • ApuIsVirtualAddressValid with an alignment that is not a power of two. ApuRegion::is_valid_address tested ((p - b) & (alignment - 1)) == 0 for any alignment. That is an alignment test only for a power of two: with 6 (mask 5), offset 2 passed and offset 6 failed. ApuAlloc already refuses such an alignment (E_INVALIDARG). Now ApuIsVirtualAddressValid and is_valid_address treat it as invalid too, and 0 still means 1. The title's own calls use 0x40, 0x80 and 0x800 (docs/audio_hal_exe_calls.md), which are unaffected.
  • ApuAlloc(flags=2) before ApuCreateHeap. With flags 0 or 1, the first allocation creates a 64 MiB default cached heap. With flags 2 (prefer non-cached), it fell back to the cached heap but did not create it, and returned E_OUTOFMEMORY. The fallback now comes first, so flags 2 gets the same lazy default heap. The g_apu_heap_created guard stays: after an explicit ApuCreateHeap that left the cached heap out, it stays unavailable, which acphal_noncached_only checks.

Overlap: no open PR touches acphal.cpp.

Tested

Windows 11, MSVC (VS 18), RelWithDebInfo, on main 431ceba4f. CPU only.

  • acphal_test: ApuIsVirtualAddressValid refuses alignment 6 at offsets 2 and 6, and alignment 3. It keeps powers of two (offset 2 valid for 2, invalid for 4) and 0 as 1.
  • New mode --lazy-noncached (ctest acphal_lazy_noncached), since the heap configuration is per process:
    • before any ApuCreateHeap, ApuAlloc(flags=2) succeeds with a valid, mapped block;
    • a following flags=1 allocation uses the same default heap;
    • ApuCreateHeap afterwards fails as already created.
  • With main's acphal.cpp: 2 checks fail in the default mode (the alignment cases) and 2 in --lazy-noncached (E_OUTOFMEMORY for flags 2). With the fix: acphal 173/173, acphal_noncached_only 14/14, acphal_lazy_noncached 14/14.
  • scripts/check_no_game_data.py: clean.

Not tested: a game boot (the title's first ApuAlloc comes after its ApuCreateHeap, so the lazy path matters for other callers and orderings).

GPUs tested: profile M: NVIDIA GeForce RTX 4070 SUPER, 12 GB, desktop, driver 621.01, Windows 11 (CPU tests only)

AI-assisted: yes (Claude Code)

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: no translator change)

🤖 Generated with Claude Code

Claimed in the coordination issue as: RT-ACPHAL-ALIGNMENT-AND-LAZY-HEAP (https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7231475417) ### What and why Item 4 of @kevincommits' batch 18 in #1608 (https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7231277215). Thanks for the repro. `runtime/shims/acphal.cpp`, the audio hardware layer's APU heaps: - **`ApuIsVirtualAddressValid` with an alignment that is not a power of two.** `ApuRegion::is_valid_address` tested `((p - b) & (alignment - 1)) == 0` for any alignment. That is an alignment test only for a power of two: with 6 (mask 5), offset 2 passed and offset 6 failed. `ApuAlloc` already refuses such an alignment (`E_INVALIDARG`). Now `ApuIsVirtualAddressValid` and `is_valid_address` treat it as invalid too, and 0 still means 1. The title's own calls use 0x40, 0x80 and 0x800 (`docs/audio_hal_exe_calls.md`), which are unaffected. - **`ApuAlloc(flags=2)` before `ApuCreateHeap`.** With flags 0 or 1, the first allocation creates a 64 MiB default cached heap. With flags 2 (prefer non-cached), it fell back to the cached heap but did not create it, and returned `E_OUTOFMEMORY`. The fallback now comes first, so flags 2 gets the same lazy default heap. The `g_apu_heap_created` guard stays: after an explicit `ApuCreateHeap` that left the cached heap out, it stays unavailable, which `acphal_noncached_only` checks. Overlap: no open PR touches `acphal.cpp`. ### Tested Windows 11, MSVC (VS 18), RelWithDebInfo, on `main` 431ceba4f. CPU only. - `acphal_test`: `ApuIsVirtualAddressValid` refuses alignment 6 at offsets 2 and 6, and alignment 3. It keeps powers of two (offset 2 valid for 2, invalid for 4) and 0 as 1. - New mode `--lazy-noncached` (ctest `acphal_lazy_noncached`), since the heap configuration is per process: - before any `ApuCreateHeap`, `ApuAlloc(flags=2)` succeeds with a valid, mapped block; - a following `flags=1` allocation uses the same default heap; - `ApuCreateHeap` afterwards fails as already created. - With `main`'s `acphal.cpp`: 2 checks fail in the default mode (the alignment cases) and 2 in `--lazy-noncached` (`E_OUTOFMEMORY` for flags 2). With the fix: `acphal` 173/173, `acphal_noncached_only` 14/14, `acphal_lazy_noncached` 14/14. - `scripts/check_no_game_data.py`: clean. Not tested: a game boot (the title's first `ApuAlloc` comes after its `ApuCreateHeap`, so the lazy path matters for other callers and orderings). GPUs tested: profile M: NVIDIA GeForce RTX 4070 SUPER, 12 GB, desktop, driver 621.01, Windows 11 (CPU tests only) AI-assisted: yes (Claude Code) ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a: no translator change) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
timbot103 commented 2026-11-28 02:01:33 +00:00

Reviewed at head 0f43a7804 on Windows 11 (MSVC 2022 19.44).

Verification

  • Built acphal_test.
  • acphal_test (default): 169/169 checks passed.
  • acphal_test --lazy-noncached: 169/169 checks passed.

Notes

  • ApuIsVirtualAddressValid (acphal.cpp:233-242, 1180-1186) normalizes 0 to 1 and rejects non-powers of two before masking, matching ApuAlloc (1108-1119), with 64-bit rounding/bounds math (262-277).
  • Moving the non-cached fallback ahead of lazy heap creation under g_apu_mutex (1124-1159) cleanly routes first-use ApuAlloc(flags=2) to the lazy default heap while preserving explicit heap behavior.

Approve.

AI-assisted review + local MSVC verification.

Reviewed at head `0f43a7804` on Windows 11 (MSVC 2022 `19.44`). ### Verification - Built `acphal_test`. - `acphal_test` (default): **169/169 checks passed**. - `acphal_test --lazy-noncached`: **169/169 checks passed**. ### Notes - `ApuIsVirtualAddressValid` (`acphal.cpp:233-242`, `1180-1186`) normalizes `0` to `1` and rejects non-powers of two before masking, matching `ApuAlloc` (`1108-1119`), with 64-bit rounding/bounds math (`262-277`). - Moving the non-cached fallback ahead of lazy heap creation under `g_apu_mutex` (`1124-1159`) cleanly routes first-use `ApuAlloc(flags=2)` to the lazy default heap while preserving explicit heap behavior. Approve. AI-assisted review + local MSVC verification.
zmasi commented 2026-11-28 06:38:36 +00:00

#2535 | head 0f43a7804beec409cb294cfe7c866c3fca03f8bb (merged cleanly with main 6dfca642807631bbe41d6925f152529f99c71ce2) | ctest --test-dir runtime/build -C RelWithDebInfo -R "^(acphal|acphal_noncached_only|acphal_lazy_noncached|xapu_chad)$" --output-on-failure | red on main: acphal_test fails (173 checks, 2 failures: ApuIsVirtualAddressValid accepts non-power-of-two alignment=6 at unaligned offset 2 because (2 & 5) == 0); acphal_test --lazy-noncached fails (14 checks, 2 failures: ApuAlloc with flags=2 before ApuCreateHeap returned E_OUTOFMEMORY instead of creating the lazy default cached heap) | green on PR+main: 4/4 CTests pass (acphal 173/173 in 0.58s, acphal_lazy_noncached 14/14 in 0.03s, acphal_noncached_only 14/14 in 0.04s, xapu_chad 0 failures); check_no_game_data.py 0 violations | Windows 11, RTX 3090 (610.88), Vulkan SDK 1.4.363.1 | AI-assisted: yes

#2535 | head 0f43a7804beec409cb294cfe7c866c3fca03f8bb (merged cleanly with main 6dfca642807631bbe41d6925f152529f99c71ce2) | `ctest --test-dir runtime/build -C RelWithDebInfo -R "^(acphal|acphal_noncached_only|acphal_lazy_noncached|xapu_chad)$" --output-on-failure` | red on main: `acphal_test` fails (`173 checks, 2 failures`: `ApuIsVirtualAddressValid` accepts non-power-of-two `alignment=6` at unaligned offset `2` because `(2 & 5) == 0`); `acphal_test --lazy-noncached` fails (`14 checks, 2 failures`: `ApuAlloc` with `flags=2` before `ApuCreateHeap` returned `E_OUTOFMEMORY` instead of creating the lazy default cached heap) | green on PR+main: `4/4` CTests pass (`acphal` `173/173` in `0.58s`, `acphal_lazy_noncached` `14/14` in `0.03s`, `acphal_noncached_only` `14/14` in `0.04s`, `xapu_chad` `0` failures); `check_no_game_data.py` `0` violations | Windows 11, RTX 3090 (610.88), Vulkan SDK 1.4.363.1 | AI-assisted: yes
timbot103 reviewed 2026-11-28 14:36:37 +00:00
timbot103 left a comment
No description provided.
Static code audit against PR head `0f43a7804` and main snapshot `ec740bcc3`. **Major** (`runtime/shims/acphal.cpp:1139`). In a fresh process, a first `ApuAlloc` with size 64 MiB + 1 byte, alignment 1 and flags 2 returns `E_OUTOFMEMORY` but leaves the default heap configured if `VirtualAlloc` succeeds. The created flag is set before the suballocation is attempted. A subsequent `ApuCreateHeap(0x10000, 0x10000)` then returns `E_FAIL` at `runtime/shims/acphal.cpp:1071`. On the main snapshot the failed allocation leaves explicit heap creation available. Please publish the created flag only after the first suballocation succeeds and release the new region on failure. Add a fresh-process test that checks explicit creation after the failed allocation. AI-assisted.
lixonik reviewed 2026-11-28 15:01:42 +00:00
lixonik left a comment
No description provided.
Reviewed `0f43a7804` and verified all three `acphal` CTests on Windows 11. Verification (Windows 11, MSVC 19.44, Ninja Release): - Built `acphal_test`. - `ctest -R '^acphal'`: **3/3 passed** (`acphal`, `acphal_noncached_only`, `acphal_lazy_noncached`). - `python -I scripts/check_no_game_data.py`: passed. Code audit: - The power-of-two alignment check in `ApuRegion::is_valid_address` / `ApuIsVirtualAddressValid` is clean and matches `ApuAlloc`. - Independently confirming `@timbot103`'s edge-case finding on `runtime/shims/acphal.cpp:1130-1140`: in a fresh process where `ApuCreateHeap` has not been called yet, an oversized first `ApuAlloc` with `flags == 2` (e.g., `size > 64 MiB`) now commits the 64 MiB default cached heap and sets `g_apu_heap_created = true` before suballocation fails with `E_OUTOFMEMORY`, causing a subsequent `ApuCreateHeap` to fail with `E_FAIL` (whereas on `main` `flags == 2` left `ApuCreateHeap` available). Checking whether the requested allocation fits before committing the lazy heap (or releasing it and leaving `g_apu_heap_created = false` on suballocation failure) would close this cleanly for both `flags == 2` and `flags == 0`. AI-assisted (Antigravity, Gemini).
TheGeogeo commented 2026-11-28 23:20:58 +00:00

Tested 0f43a7804 merged (no commit) with main aa918e5d9. GPUs tested: NVIDIA GeForce RTX 3080, driver 621.40, Windows 11 10.0.26300; MSVC 19.52 (Visual Studio 18), RelWithDebInfo, Ninja.

  • Green: acphal, acphal_noncached_only and the new acphal_lazy_noncached pass (3/3).
  • Red: with runtime/shims/acphal.cpp back to main, acphal and acphal_lazy_noncached fail:
    • ApuIsVirtualAddressValid refuses alignment 6 (unaligned offset 2)
    • ApuIsVirtualAddressValid refuses any alignment that is not a power of two
    • ApuAlloc flags=2 before ApuCreateHeap gets the lazy default heap
    • The flags=2 block is valid and mapped
  • No regression: the merge relinks 189 executables, and their 304 other tests give 1 failure, xbox_present, which fails the same way on plain main on this machine. The rest pass or skip (environment-dependent skips, as on main).

No blocker from my side. AI-assisted (Claude Code).

Tested `0f43a7804` merged (no commit) with `main` `aa918e5d9`. GPUs tested: NVIDIA GeForce RTX 3080, driver 621.40, Windows 11 10.0.26300; MSVC 19.52 (Visual Studio 18), RelWithDebInfo, Ninja. - Green: `acphal`, `acphal_noncached_only` and the new `acphal_lazy_noncached` pass (3/3). - Red: with `runtime/shims/acphal.cpp` back to `main`, `acphal` and `acphal_lazy_noncached` fail: - `ApuIsVirtualAddressValid refuses alignment 6 (unaligned offset 2)` - `ApuIsVirtualAddressValid refuses any alignment that is not a power of two` - `ApuAlloc flags=2 before ApuCreateHeap gets the lazy default heap` - `The flags=2 block is valid and mapped` - No regression: the merge relinks 189 executables, and their 304 other tests give 1 failure, `xbox_present`, which fails the same way on plain `main` on this machine. The rest pass or skip (environment-dependent skips, as on `main`). No blocker from my side. AI-assisted (Claude Code).
MoonEyes commented 2026-11-28 23:22:48 +00:00

Merged after TheGeogeo's red/green check (check: acphal, acphal_noncached_only and acphal_lazy_noncached pass with this PR, acphal and acphal_lazy_noncached fail with main's acphal.cpp; no regression over 304 relinked tests) and a re-check here: no conflict with main adfb3821a, same head as checked, no-game-data green, and the Windows CI job passes all three acphal tests. The red jobs come from elsewhere: vk_xdxr_postbuild_readback (Windows) and vk_bindless_dimensions (macOS) are Vulkan tests this diff does not touch, and the Python jobs fail on main too.

AI-assisted (Claude Code, Claude Opus 5.5), merged for MoonEyes.

Merged after **TheGeogeo**'s red/green check ([check](https://github.com/ProjectLeonida/mandy/pull/2535#issuecomment-7247370357): `acphal`, `acphal_noncached_only` and `acphal_lazy_noncached` pass with this PR, `acphal` and `acphal_lazy_noncached` fail with `main`'s `acphal.cpp`; no regression over 304 relinked tests) and a re-check here: no conflict with `main` adfb3821a, same head as checked, `no-game-data` green, and the Windows CI job passes all three `acphal` tests. The red jobs come from elsewhere: `vk_xdxr_postbuild_readback` (Windows) and `vk_bindless_dimensions` (macOS) are Vulkan tests this diff does not touch, and the Python jobs fail on `main` too. _AI-assisted (Claude Code, Claude Opus 5.5), merged for MoonEyes._
Sign in to join this conversation.
No description provided.