image_memory: guard zero requests, make allocate strong-exception-safe, and free outside mutex #1698

Closed
IlyassRS3 wants to merge 1 commit from upstream-pr/1698 into main
IlyassRS3 commented 2026-11-25 08:40:44 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by IlyassRS3 on 2026-11-25T08:40:44Z.


Item ID: IMAGE-MEMORY-POOL-GUARDS-AND-EXCEPTION-SAFETY
Claim: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7157274006

Summary

Hardens BlockRanges and ImageMemoryPool in runtime/src/image_memory.h and adds regression coverage in runtime/test/image_memory_test.cpp:

  • Reject zero size or zero alignment upfront in ImageMemoryPool::allocate: previously, size == 0 or alignment == 0 walked existing blocks, decremented the OOM skip counter, called allocate_(type, block_bytes) to request a new driver allocation, failed block->ranges.allocate(...), and freed it while holding mutex_.
  • Guard zero-size BlockRanges::release: ignore size == 0 early instead of mutating free-range maps.
  • Strong exception safety on std::bad_alloc in BlockRanges::allocate and BlockRanges::insert: extract the chosen free-piece nodes from by_size_ and by_offset_ and roll back via no-throw node re-insertion if inserting the lead or tail remainder throws; clean up by_offset_ if by_size_.insert throws.
  • Strong exception safety & lock-free cleanup in ImageMemoryPool::allocate: pre-allocate the Range wrapper and reserve pool.blocks before mutating ranges or calling allocate_(), attach/commit only via noexcept operations, and release memory via a scope guard declared outside lock so free_ is always invoked after mutex_ is unlocked (matching release()) on any failure or std::bad_alloc without leaking driver memory or corrupting stats().ranges.

Verification

  • clang++ -std=c++20 -O2 -Wall -Wextra -Werror -pedantic -fsanitize=undefined -pthread runtime/test/image_memory_test.cpp -> exit 0, all checks pass, 0 warnings / 0 UBSan issues.

  • Mutation check against unpatched main (git show main:runtime/src/image_memory.h with updated image_memory_test.cpp) -> exit 1 (30 FAIL lines reproducing zero-request driver allocation, skip-window corruption, BlockRanges corruption, and driver memory / range / free-space leaks).

  • python3 -I scripts/check_no_game_data.py runtime/src/image_memory.h runtime/test/image_memory_test.cpp -> OK.

  • python3 -m compileall -q make_game.py tools tests and python3 -I tests/test_text_write_line_endings.py -> OK (3/3).

  • AI-assisted: yes

  • GPUs tested: none (CPU-only portable image_memory_test with counted fake allocations; no game files)

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5487972445 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1698). Originally posted by [IlyassRS3](https://github.com/IlyassRS3) on 2026-11-25T08:40:44Z. --- Item ID: `IMAGE-MEMORY-POOL-GUARDS-AND-EXCEPTION-SAFETY` Claim: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7157274006 ## Summary Hardens `BlockRanges` and `ImageMemoryPool` in `runtime/src/image_memory.h` and adds regression coverage in `runtime/test/image_memory_test.cpp`: - **Reject zero `size` or zero `alignment` upfront in `ImageMemoryPool::allocate`**: previously, `size == 0` or `alignment == 0` walked existing blocks, decremented the OOM `skip` counter, called `allocate_(type, block_bytes)` to request a new driver allocation, failed `block->ranges.allocate(...)`, and freed it while holding `mutex_`. - **Guard zero-size `BlockRanges::release`**: ignore `size == 0` early instead of mutating free-range maps. - **Strong exception safety on `std::bad_alloc` in `BlockRanges::allocate` and `BlockRanges::insert`**: extract the chosen free-piece nodes from `by_size_` and `by_offset_` and roll back via no-throw node re-insertion if inserting the lead or tail remainder throws; clean up `by_offset_` if `by_size_.insert` throws. - **Strong exception safety & lock-free cleanup in `ImageMemoryPool::allocate`**: pre-allocate the `Range` wrapper and reserve `pool.blocks` before mutating ranges or calling `allocate_()`, attach/commit only via `noexcept` operations, and release `memory` via a scope guard declared outside `lock` so `free_` is always invoked after `mutex_` is unlocked (matching `release()`) on any failure or `std::bad_alloc` without leaking driver memory or corrupting `stats().ranges`. ## Verification - `clang++ -std=c++20 -O2 -Wall -Wextra -Werror -pedantic -fsanitize=undefined -pthread runtime/test/image_memory_test.cpp` -> exit 0, all checks pass, 0 warnings / 0 UBSan issues. - Mutation check against unpatched `main` (`git show main:runtime/src/image_memory.h` with updated `image_memory_test.cpp`) -> exit 1 (`30` FAIL lines reproducing zero-request driver allocation, skip-window corruption, `BlockRanges` corruption, and driver memory / range / free-space leaks). - `python3 -I scripts/check_no_game_data.py runtime/src/image_memory.h runtime/test/image_memory_test.cpp` -> OK. - `python3 -m compileall -q make_game.py tools tests` and `python3 -I tests/test_text_write_line_endings.py` -> OK (3/3). - AI-assisted: yes - GPUs tested: none (CPU-only portable `image_memory_test` with counted fake allocations; no game files)
copilot-pull-request-reviewer[bot] commented 2026-11-25 08:40:57 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-25T08:40:57Z.

Review: COMMENTED


Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6054289308 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1698#pullrequestreview-6054289308). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-25T08:40:57Z. **Review:** COMMENTED --- Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
lixonik commented 2026-11-25 10:34:38 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-25T10:34:38Z.

Review: COMMENTED


Independent CPU A/B under Ubuntu/WSL with GCC at 68fd4dfe against 237deb64. The stock image_memory_test passed. In a separate mock-driver probe, I forced the first host allocation after a successful driver callback to throw std::bad_alloc. The base issued zero frees for the acquired handle. The PR freed it exactly once, and the free callback reentered stats() successfully with zero live blocks or ranges. After disabling injection, a retry succeeded at offset zero on both trees.

AI-assisted. Host test with synthetic driver handles, no GPU.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6055948820 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1698#pullrequestreview-6055948820). Originally posted by [lixonik](https://github.com/lixonik) on 2026-11-25T10:34:38Z. **Review:** COMMENTED --- Independent CPU A/B under Ubuntu/WSL with GCC at `68fd4dfe` against `237deb64`. The stock `image_memory_test` passed. In a separate mock-driver probe, I forced the first host allocation after a successful driver callback to throw `std::bad_alloc`. The base issued zero frees for the acquired handle. The PR freed it exactly once, and the free callback reentered `stats()` successfully with zero live blocks or ranges. After disabling injection, a retry succeeded at offset zero on both trees. AI-assisted. Host test with synthetic driver handles, no GPU.
zmasi commented 2026-11-28 12:09:13 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T12:09:13Z.


Verified on Windows 11 (10.0.26200.9974) with MSVC 19.44.35228 (x64, /std:c++20 /EHsc /W4 /permissive- /utf-8 /O2) against main d48a957bb + 68fd4dfe28 (claim #2570#issuecomment-7238954420):

  • Line-by-line diff audit: Clean (runtime/src/image_memory.h, runtime/test/image_memory_test.cpp); rejects zero-size/zero-alignment requests without consuming the OOM retry skip window, makes BlockRanges::allocate / insert and ImageMemoryPool::allocate strong-exception-safe under std::bad_alloc via node extraction and pre-allocated Range + MemoryGuard, and releases driver memory outside mutex_. check_no_game_data.py clean.
  • Red (d48a957bb + PR test): image_memory_test.exe fails (rc=1, FAIL: failed pool.allocate leaks no driver memory, FAIL: pool teardown frees all driver memory after failure injection, FAIL: failed existing-block allocate leaks no bytes inside the block, FAIL: failed pool.allocate preserves block and range counts).
  • Green (d48a957bb + 68fd4dfe28 merge): Auto-merges cleanly (rc=0), compiles with 0 warnings under /W4 /permissive-, and passes (image memory: textures share blocks, and released ranges are used again, rc=0).

AI-assisted.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7239029731 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1698#issuecomment-7239029731). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-28T12:09:13Z. --- Verified on Windows 11 (`10.0.26200.9974`) with MSVC `19.44.35228` (`x64`, `/std:c++20 /EHsc /W4 /permissive- /utf-8 /O2`) against `main` `d48a957bb` + `68fd4dfe28` (claim `#2570#issuecomment-7238954420`): - **Line-by-line diff audit**: Clean (`runtime/src/image_memory.h`, `runtime/test/image_memory_test.cpp`); rejects zero-size/zero-alignment requests without consuming the OOM retry skip window, makes `BlockRanges::allocate` / `insert` and `ImageMemoryPool::allocate` strong-exception-safe under `std::bad_alloc` via node extraction and pre-allocated `Range` + `MemoryGuard`, and releases driver memory outside `mutex_`. `check_no_game_data.py` clean. - **Red (`d48a957bb` + PR test)**: `image_memory_test.exe` fails (`rc=1`, `FAIL: failed pool.allocate leaks no driver memory`, `FAIL: pool teardown frees all driver memory after failure injection`, `FAIL: failed existing-block allocate leaks no bytes inside the block`, `FAIL: failed pool.allocate preserves block and range counts`). - **Green (`d48a957bb` + `68fd4dfe28` merge)**: Auto-merges cleanly (`rc=0`), compiles with `0` warnings under `/W4 /permissive-`, and passes (`image memory: textures share blocks, and released ranges are used again`, `rc=0`). AI-assisted.
OreoFR commented 2026-11-28 21:53:07 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-28T21:53:07Z.


Verified by OreoFR

  • Base: main c1ecf04ed + PR head 68fd4dfe2; green run in a merge package of 15 PRs, no new ctest failure vs main
  • Test: ctest -R ^image_memory$
  • Red on main: 29 FAIL (null requests sent to the driver, OOM window, allocate not exception-safe)
  • Green with the PR: OK
  • Verdict: READY
  • Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11

AI-assisted (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7246343519 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1698#issuecomment-7246343519). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-28T21:53:07Z. --- **Verified by OreoFR** - Base: main `c1ecf04ed` + PR head `68fd4dfe2`; green run in a merge package of 15 PRs, no new ctest failure vs main - Test: `ctest -R ^image_memory$` - Red on main: 29 FAIL (null requests sent to the driver, OOM window, allocate not exception-safe) - Green with the PR: OK - Verdict: READY - Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11 AI-assisted (Claude Code, Claude Opus 5.5).
aslan2142 commented 2026-12-01 16:37:16 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T16:37:16Z.


Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (d3d12/) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, d3d12/, or general docs), please feel free to reopen it.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7317908161 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1698#issuecomment-7317908161). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T16:37:16Z. --- Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (`d3d12/`) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, `d3d12/`, or general docs), please feel free to reopen it.

Pull request closed

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
ProjectLeonida/mandy!1698
No description provided.