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

Open
opened 2026-11-26 20:22:11 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1698
Author: @IlyassRS3
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: fix/image-memory-pool-guards


📝 Commits (1)

  • 68fd4df image_memory: guard zero requests, make allocate strong-exception-safe, and free outside mutex

📊 Changes

2 files changed (+189 additions, -19 deletions)

View changed files

📝 runtime/src/image_memory.h (+59 -19)
📝 runtime/test/image_memory_test.cpp (+130 -0)

📄 Description

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)


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1698 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/image-memory-pool-guards` --- ### 📝 Commits (1) - [`68fd4df`](https://github.com/ubiquitarianism/leonida/commit/68fd4dfe284194ed86a42cbe08f74dad8220ee21) image_memory: guard zero requests, make allocate strong-exception-safe, and free outside mutex ### 📊 Changes **2 files changed** (+189 additions, -19 deletions) <details> <summary>View changed files</summary> 📝 `runtime/src/image_memory.h` (+59 -19) 📝 `runtime/test/image_memory_test.cpp` (+130 -0) </details> ### 📄 Description 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) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#1712
No description provided.