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

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

@⁠kirigayakazima on GitHub · 2026-11-25 13:43 UTC · original
kirigayakazima:fix/shader-carve-accel-safety → main · +183 −4 · 2 files · 1 commits · open

Claimed in the coordination issue as: SHADER-CARVE-ACCEL-SAFETY-AND-UNIT-TESTS (#1608)

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.
**@⁠kirigayakazima** on GitHub · 2026-11-25 13:43 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1869) `kirigayakazima:fix/shader-carve-accel-safety` → `main` · +183 −4 · 2 files · 1 commits · open Claimed in the coordination issue as: SHADER-CARVE-ACCEL-SAFETY-AND-UNIT-TESTS (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608) ### 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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:09:23 +00:00
Author
Owner

@⁠GeorgiyDemo on GitHub · 2026-11-25 14:08 UTC · original

Tested 7e38f9b24 on Apple M5 Pro (16-core GPU), macOS 26.7.3 (25G342), against main 71a769d7d (Python shader tool; no GPU):

  • pytest 9.2.0 on tests/test_shader_carve_accel.py: 4 passed, 14 subtests passed at this head.
  • The same file on main: 8 failed, 1 passed — the is_file()/native-library load guard, the false/off/no env handling and the native-fallback cases are new tests.
  • Not run: a real native acceleration library; only the missing/declined path is exercised.

AI-assisted; synthetic fixtures only, no game data.

**@⁠GeorgiyDemo** on GitHub · 2026-11-25 14:08 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1869#issuecomment-7164828241) Tested `7e38f9b24` on Apple M5 Pro (16-core GPU), macOS 26.7.3 (25G342), against `main` `71a769d7d` (Python shader tool; no GPU): - pytest 9.2.0 on `tests/test_shader_carve_accel.py`: **4 passed, 14 subtests passed** at this head. - The same file on `main`: **8 failed**, 1 passed — the `is_file()`/native-library load guard, the `false/off/no` env handling and the native-fallback cases are new tests. - Not run: a real native acceleration library; only the missing/declined path is exercised. AI-assisted; synthetic fixtures only, no game data.
Author
Owner

@⁠DeadlyChirp on GitHub · 2026-11-25 15:32 UTC · original

Tested on Windows 11, Python 3.12.10, with this PR merged onto current upstream main.

python -m pytest -q tests/test_shader_carve_accel.py:

  • PR: 4 passed, 14 subtests passed.
  • The new test file run against main's tools/shader_carve_accel.py: 8 failed, 1 passed. So the tests catch the problem, and the tool change fixes it on Windows.

(AI-assisted test run; output checked by hand.)

**@⁠DeadlyChirp** on GitHub · 2026-11-25 15:32 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1869#issuecomment-7166993656) Tested on Windows 11, Python 3.12.10, with this PR merged onto current upstream main. `python -m pytest -q tests/test_shader_carve_accel.py`: - PR: 4 passed, 14 subtests passed. - The new test file run against main's `tools/shader_carve_accel.py`: 8 failed, 1 passed. So the tests catch the problem, and the tool change fixes it on Windows. (AI-assisted test run; output checked by hand.)
Author
Owner

@⁠MoonEyes on GitHub · 2026-11-25 17:11 UTC · original · reviewed

Independent test report (Windows 11, Python 3.14, origin/main + PR head).

Verdict: LGTM (one optional nit)
New tests/test_shader_carve_accel.py: 4 tests, 14 subtests, all pass on the PR head (file absent on main). Mutation check: PR tests against the pre-PR tools/shader_carve_accel.py gives 8 failed (5 disable-env subtests, missing-DLL CDLL call, validation/fallback/release test, writable+empty release test), so the tests do catch the fixed behaviours.
No Windows-specific failure: the tests mock _load_native, so no native DLL is needed; the missing-file guard returns None before CDLL.
Nit: the released flag looks redundant. The early release (line 64) sets release = None and falls back to reference before the try/finally, and the finally block runs once per generator, so release is not None already prevents a double release.
AI-assisted: yes (Claude Code ran the tests; I read them).

**@⁠MoonEyes** on GitHub · 2026-11-25 17:11 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1869#pullrequestreview-6061832310) · **reviewed** Independent test report (Windows 11, Python 3.14, origin/main + PR head). Verdict: LGTM (one optional nit) New tests/test_shader_carve_accel.py: 4 tests, 14 subtests, all pass on the PR head (file absent on main). Mutation check: PR tests against the pre-PR tools/shader_carve_accel.py gives 8 failed (5 disable-env subtests, missing-DLL CDLL call, validation/fallback/release test, writable+empty release test), so the tests do catch the fixed behaviours. No Windows-specific failure: the tests mock `_load_native`, so no native DLL is needed; the missing-file guard returns None before CDLL. Nit: the `released` flag looks redundant. The early release (line 64) sets `release = None` and falls back to `reference` before the try/finally, and the finally block runs once per generator, so `release is not None` already prevents a double release. AI-assisted: yes (Claude Code ran the tests; I read them).
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/1869/head:github-bot/gh-1869'
git switch 'github-bot/gh-1869'

Merge

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