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

Closed
leonida/mandy
quietfold wants to merge 0 commits from fix/image-memory-pool-guards into main

Pull request closed

This pull request cannot be reopened because the branch was deleted.

quietfold commented 2026-11-25 08:40:44 +00:00

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)

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] reviewed 2026-11-25 08:40:57 +00:00
copilot-pull-request-reviewer[bot] left a comment
No description provided.
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 reviewed 2026-11-25 10:34:38 +00:00
lixonik left a comment
No description provided.
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

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.

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

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).

**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

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.

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.
Sign in to join this conversation.
No description provided.