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

Open
github-bot wants to merge 1 commit from github-bot/gh-1698 into main AGit
Owner

@⁠IlyassRS3 on GitHub · 2026-11-25 08:40 UTC · original
IlyassRS3:fix/image-memory-pool-guards → main · +189 −19 · 2 files · 1 commits · open

Item ID: IMAGE-MEMORY-POOL-GUARDS-AND-EXCEPTION-SAFETY
Claim: #1608

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)

**@⁠IlyassRS3** on GitHub · 2026-11-25 08:40 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1698) `IlyassRS3:fix/image-memory-pool-guards` → `main` · +189 −19 · 2 files · 1 commits · open Item ID: `IMAGE-MEMORY-POOL-GUARDS-AND-EXCEPTION-SAFETY` Claim: https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608 ## 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)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:01:19 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 08:40 UTC · original · reviewed

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.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-25 08:40 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1698#pullrequestreview-6054289308) · **reviewed** 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.
Author
Owner

@⁠lixonik on GitHub · 2026-11-25 10:34 UTC · original · reviewed

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.

**@⁠lixonik** on GitHub · 2026-11-25 10:34 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1698#pullrequestreview-6055948820) · **reviewed** 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.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1698/head:github-bot/gh-1698'
git switch 'github-bot/gh-1698'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1698'
git switch 'github-bot/gh-1698'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1698'
git switch 'github-bot/gh-1698'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1698'
git switch 'main'
git merge --squash 'github-bot/gh-1698'
git switch 'main'
git merge --ff-only 'github-bot/gh-1698'
git switch 'main'
git merge 'github-bot/gh-1698'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.