[PR #1869] tools/shader_carve_accel: release non-readonly/empty buffer pins and guard native fallback #1881

Open
opened 2026-11-26 21:19:16 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

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

Base: main ← Head: fix/shader-carve-accel-safety


📝 Commits (1)

  • 7e38f9b Harden shader_carve_accel buffer release, native lookup, and fallback checks

📊 Changes

2 files changed (+183 additions, -4 deletions)

View changed files

➕ tests/test_shader_carve_accel.py (+162 -0)
📝 tools/shader_carve_accel.py (+21 -4)

📄 Description

Claimed in the coordination issue as: SHADER-CARVE-ACCEL-SAFETY-AND-UNIT-TESTS (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7164153026)

What and why

  • Check path.is_file() before calling ctypes.CDLL in _load_native() (tools/shader_carve_accel.py) so a missing optional native binary returns None cleanly.
  • Treat false, off, and no (case-insensitive, stripped) identically to 0 for LEONIDA_SHADER_CARVE_NATIVE.
  • Immediately release the acquired Py_buffer pin in entry_search() when not view.readonly, view.len <= 0, or not view.buf so writable (bytearray / writable mmap) and empty buffers do not stay pinned on fallback, and guard finally cleanup against double-release.
  • Validate isinstance(pos, int) and not isinstance(pos, bool) and 0 <= pos < view.len before invoking native scan, and also catch TypeError/OverflowError when falling back cleanly to reference(pos).
  • Add dedicated mocked unit tests in tests/test_shader_carve_accel.py.

Tested

  • Patched: python -I tests/test_shader_carve_accel.py -v -> Ran 4 tests in 0.015s (OK); python -I tests/test_shader_carve.py -v -> Ran 31 tests in 1.434s (OK); python -I tests/test_shader_carve_native.py -v -> Ran 3 tests in 0.016s (OK (skipped=1), NativeParityTest skipped because the optional compiled native library is not present on this host).
  • Baseline check (upstream/main 5e95fa2f with tests/test_shader_carve_accel.py copied into detached worktree): Ran 4 tests in 0.012s (FAILED (failures=6, errors=2)).
  • Repo gates: python -I tests/test_ci_registration.py (Ran 16 tests in 5.145s, OK), python -I tests/test_text_write_line_endings.py (Ran 3 tests in 12.917s, OK), python -I scripts/check_no_game_data.py (exit 0), python -I -m compileall -q (exit 0), git diff --check (exit 0).

GPUs tested: n/a (Py_buffer lifetime, fallback, and native-scanner safety unit tests)

AI-assisted: yes

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.

🔄 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/1869 **Author:** [@kirigayakazima](https://github.com/kirigayakazima) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/shader-carve-accel-safety` --- ### 📝 Commits (1) - [`7e38f9b`](https://github.com/ubiquitarianism/leonida/commit/7e38f9b243a96597f1ed1d5420a15f18f0400aea) Harden shader_carve_accel buffer release, native lookup, and fallback checks ### 📊 Changes **2 files changed** (+183 additions, -4 deletions) <details> <summary>View changed files</summary> ➕ `tests/test_shader_carve_accel.py` (+162 -0) 📝 `tools/shader_carve_accel.py` (+21 -4) </details> ### 📄 Description Claimed in the coordination issue as: SHADER-CARVE-ACCEL-SAFETY-AND-UNIT-TESTS (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7164153026) ### What and why - Check `path.is_file()` before calling `ctypes.CDLL` in `_load_native()` (`tools/shader_carve_accel.py`) so a missing optional native binary returns `None` cleanly. - Treat `false`, `off`, and `no` (case-insensitive, stripped) identically to `0` for `LEONIDA_SHADER_CARVE_NATIVE`. - Immediately release the acquired `Py_buffer` pin in `entry_search()` when `not view.readonly`, `view.len <= 0`, or `not view.buf` so writable (`bytearray` / writable `mmap`) and empty buffers do not stay pinned on fallback, and guard `finally` cleanup against double-release. - Validate `isinstance(pos, int) and not isinstance(pos, bool) and 0 <= pos < view.len` before invoking native `scan`, and also catch `TypeError`/`OverflowError` when falling back cleanly to `reference(pos)`. - Add dedicated mocked unit tests in `tests/test_shader_carve_accel.py`. ### Tested - Patched: `python -I tests/test_shader_carve_accel.py -v` -> `Ran 4 tests in 0.015s` (`OK`); `python -I tests/test_shader_carve.py -v` -> `Ran 31 tests in 1.434s` (`OK`); `python -I tests/test_shader_carve_native.py -v` -> `Ran 3 tests in 0.016s` (`OK (skipped=1)`, `NativeParityTest` skipped because the optional compiled native library is not present on this host). - Baseline check (`upstream/main` `5e95fa2f` with `tests/test_shader_carve_accel.py` copied into detached worktree): `Ran 4 tests in 0.012s` (`FAILED (failures=6, errors=2)`). - Repo gates: `python -I tests/test_ci_registration.py` (`Ran 16 tests in 5.145s`, `OK`), `python -I tests/test_text_write_line_endings.py` (`Ran 3 tests in 12.917s`, `OK`), `python -I scripts/check_no_game_data.py` (exit 0), `python -I -m compileall -q` (exit 0), `git diff --check` (exit 0). GPUs tested: n/a (Py_buffer lifetime, fallback, and native-scanner safety unit tests) AI-assisted: yes ### 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. --- <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#1881
No description provided.