fix(libScePngDec): stop callback input at the PNG boundary #18361

Merged
Ondra97 merged 1 commit from upstream-pr/18361 into main 2026-11-12 11:48:35 +00:00
Ondra97 commented 2026-11-11 12:13:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Ondra97 on 2026-11-11T12:13:06Z.


What

scePngDecDecodeWithInputControl keeps requesting input after a complete PNG image. When the next callback reports an error, a valid image fails to decode. Bound each read by the declared PNG chunk length and stop after the complete IEND chunk, including its CRC. The existing direct decode path still validates chunks and image data.

The framing follows PNG section 11.2.4. Exact native callback counts and read-ahead remain unmeasured and are recorded in the library's debt entry.

Regression coverage uses literal expected RGBA pixels, callback chunks of 1/3/7/12/1024 bytes, a 2 KiB post-IDAT ancillary chunk containing a false IEND trailer, trailing bytes offered in the same callback, repeated decoding without another callback, and incomplete-IEND EOF/error cases.

Tested

Windows 11 Pro 10.0.26300; AMD Ryzen 9 7900X; 32 GB RAM; NVIDIA RTX 4070 Ti SUPER (codec tests do not use the GPU). WinLibs MinGW-w64 GCC 15.2.0 r7, CMake 4.3.1, Ninja, Release.

Base reproduction: 1b485ff74570a0508ef5cd30409d00659a9bada1. Final commit: 2464b95fe12373ab2a4b46024b6b29390d615935, based on a57e435a537f7a71ef8b3c780f65033d4b53f617. The intervening main change only extends release-workflow timeouts; codec and test source files are unchanged.

Metric Before (base) After (fix)
PNG input-control regression Fails the complete-image success assertion Passes
Existing direct PNG decoder test Passes Passes

A fresh full-project configuration used BUILD_TESTING=ON, ANYPS5_RELINKER_ONLY=OFF and exact current submodules. Built decoder_png_tests, guest_png_dec_tests and guest_png_dec_input_control_tests with --parallel 6.

cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=ON -DANYPS5_RELINKER_ONLY=OFF -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++
cmake --build build --target decoder_png_tests guest_png_dec_tests guest_png_dec_input_control_tests --parallel 6
ctest --test-dir build -R '^(public_headers|decoder_png|guest_png_dec(_input_control)?)$' -V
python tools/check_conventions.py --base origin/main

All four selected tests passed (three runtime tests plus the public-header audit). Conventions and git diff --check passed. A separate read-only review found no blocking defect. Linux, macOS, console, titles and the full CTest suite were not tested.

Hosted Windows job 130515344372 passed for head 2464b95fe12373ab2a4b46024b6b29390d615935; its changed-test selection ran guest_png_dec_input_control successfully (1/1). Conventions, library portability, intake and eligibility also passed. The Linux and other unrelated jobs were skipped.

Pre-opening python tools/pr_overlap.py --repo boykopovar/AnyPS5 --branch codex/fix-png-input-end-boundary scanned 1,973 open PR heads. It exited 2 because #14212 conflicts with main and its shared debt file could not be compared automatically. I manually compared head 6b4941bfe250142672c12b48b7b6c706b9691452: that file only gains three blank lines. There is no implementation overlap or ordering dependency.

Additional integration validation: current main 0effa485f96a4fcf8fef433a09cdd862c65b7767 plus #18361 (2464b95fe12373ab2a4b46024b6b29390d615935), #18370 (c8388f97ab26a188bd07751a2f5ba2afce058bc6) and #18387 (887c2c6e51e6b926a773abaa43e9f15b71af1efc), composed locally as 7b8bc0d80db09dee5e39bcdf5159d8b8791d9db9. A fresh full-project build passed all 10 focused PNG/JPEG runtime CTests and public_headers (11/11). A separate temporary PNG probe passed in fresh processes through both raw and production-NID-resolved exports with an isolated patched libc/kernel/PNG closure. It checks level-0 stored RGB/RGBA streams, literal decoded pixels, 1/7/full-image callback chunks with an error only after IEND, repeated decode without another callback and row padding. This is synthetic Windows integration coverage; no title or console run is claimed. Probe source and reproduction commands are included in #18387.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5259229208 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18361). Originally posted by [Ondra97](https://github.com/Ondra97) on 2026-11-11T12:13:06Z. --- ### What `scePngDecDecodeWithInputControl` keeps requesting input after a complete PNG image. When the next callback reports an error, a valid image fails to decode. Bound each read by the declared PNG chunk length and stop after the complete IEND chunk, including its CRC. The existing direct decode path still validates chunks and image data. The framing follows [PNG section 11.2.4](https://www.w3.org/TR/png/#11IEND). Exact native callback counts and read-ahead remain unmeasured and are recorded in the library's debt entry. Regression coverage uses literal expected RGBA pixels, callback chunks of 1/3/7/12/1024 bytes, a 2 KiB post-IDAT ancillary chunk containing a false IEND trailer, trailing bytes offered in the same callback, repeated decoding without another callback, and incomplete-IEND EOF/error cases. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows 11 Pro 10.0.26300; AMD Ryzen 9 7900X; 32 GB RAM; NVIDIA RTX 4070 Ti SUPER (codec tests do not use the GPU). WinLibs MinGW-w64 GCC 15.2.0 r7, CMake 4.3.1, Ninja, Release. Base reproduction: `1b485ff74570a0508ef5cd30409d00659a9bada1`. Final commit: `2464b95fe12373ab2a4b46024b6b29390d615935`, based on `a57e435a537f7a71ef8b3c780f65033d4b53f617`. The intervening main change only extends release-workflow timeouts; codec and test source files are unchanged. | Metric | Before (base) | After (fix) | | --- | --- | --- | | PNG input-control regression | Fails the complete-image success assertion | Passes | | Existing direct PNG decoder test | Passes | Passes | A fresh full-project configuration used `BUILD_TESTING=ON`, `ANYPS5_RELINKER_ONLY=OFF` and exact current submodules. Built `decoder_png_tests`, `guest_png_dec_tests` and `guest_png_dec_input_control_tests` with `--parallel 6`. ```sh cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=ON -DANYPS5_RELINKER_ONLY=OFF -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ cmake --build build --target decoder_png_tests guest_png_dec_tests guest_png_dec_input_control_tests --parallel 6 ctest --test-dir build -R '^(public_headers|decoder_png|guest_png_dec(_input_control)?)$' -V python tools/check_conventions.py --base origin/main ``` All four selected tests passed (three runtime tests plus the public-header audit). Conventions and `git diff --check` passed. A separate read-only review found no blocking defect. Linux, macOS, console, titles and the full CTest suite were not tested. Hosted [Windows job 130515344372](https://github.com/boykopovar/AnyPS5/actions/runs/43018440363/job/130515344372) passed for head `2464b95fe12373ab2a4b46024b6b29390d615935`; its changed-test selection ran `guest_png_dec_input_control` successfully (1/1). Conventions, library portability, intake and eligibility also passed. The Linux and other unrelated jobs were skipped. Pre-opening `python tools/pr_overlap.py --repo boykopovar/AnyPS5 --branch codex/fix-png-input-end-boundary` scanned 1,973 open PR heads. It exited 2 because #14212 conflicts with main and its shared debt file could not be compared automatically. I manually compared head `6b4941bfe250142672c12b48b7b6c706b9691452`: that file only gains three blank lines. There is no implementation overlap or ordering dependency. Additional integration validation: current main `0effa485f96a4fcf8fef433a09cdd862c65b7767` plus #18361 (`2464b95fe12373ab2a4b46024b6b29390d615935`), #18370 (`c8388f97ab26a188bd07751a2f5ba2afce058bc6`) and #18387 (`887c2c6e51e6b926a773abaa43e9f15b71af1efc`), composed locally as `7b8bc0d80db09dee5e39bcdf5159d8b8791d9db9`. A fresh full-project build passed all 10 focused PNG/JPEG runtime CTests and `public_headers` (11/11). A separate temporary PNG probe passed in fresh processes through both raw and production-NID-resolved exports with an isolated patched libc/kernel/PNG closure. It checks level-0 stored RGB/RGBA streams, literal decoded pixels, 1/7/full-image callback chunks with an error only after IEND, repeated decode without another callback and row padding. This is synthetic Windows integration coverage; no title or console run is claimed. Probe source and reproduction commands are included in #18387. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no -->
PrivacyOnion commented 2026-11-11 16:25:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-11T16:25:07Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 2464b95fe1:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_png_dec_input_control_tests built cleanly.
  • CTest (guest_png_dec_input_control):
    1/1 Test #3910: guest_png_dec_input_control ......   Passed    0.04 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libScePngDec/Export.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #3910: guest_png_dec_input_control ......Subprocess aborted***Exception:   0.34 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • core/libs/prx/libScePngDec/Export.cpp:pullImage bounds checking and chunk framing correctly calculate headerEnd (offset + CHUNK_HEADER_SIZE) and chunkEnd (headerEnd + length + CHUNK_CRC_SIZE), verifying length <= MAX_CHUNK_LENGTH (0x7FFFFFFF) without integer overflow on 64-bit platforms.
    • core/libs/prx/libScePngDec/Export.cpp:pullImage terminates callback consumption immediately upon reading the full IEND chunk and its 4-byte CRC (isEnd = std::memcmp(...) == 0), preventing spurious reads and trailing callback errors on subsequent operations.
    • core/libs/prx/libScePngDec/Export.cpp:scePngDecDecodeWithInputControl preserves negative status codes ((*use).status < 0) from input callbacks and delegates incomplete chunk and corrupted payload validation to scePngDecDecode, which returns SCE_PNG_DEC_ERROR_DECODE_ERROR (0x80690012).
    • core/libs/prx/libScePngDec/Export.cpp: StreamUse RAII lock and busy flag exchange prevent concurrent handle use without deadlocks, while streamMutex protects the streams map lifetime across create, decode, and delete calls.
    • core/libs/tests/GuestPngDecInputControl.cpp exercises multiple refill chunk sizes (1, 3, 7, 12, 1024 bytes), post-IDAT ancillary chunks containing false IEND payloads, trailing callback bytes, repeated decode calls without new callbacks, and incomplete IEND EOF/error paths.
    • docs/dev/debt/libScePngDec.md and AGENTS.md rules are fully satisfied: zero code comments added, accurate technical debt notes updated for unmeasured native refill boundaries and read-ahead, and Conventional Commits title formatting observed.
  • Note: the AI-assisted checkbox can stay.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5861946276 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18361#pullrequestreview-5861946276). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-11T16:25:07Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `2464b95fe1`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_png_dec_input_control_tests` built cleanly. - **CTest** (`guest_png_dec_input_control`): ``` 1/1 Test #3910: guest_png_dec_input_control ...... Passed 0.04 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libScePngDec/Export.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #3910: guest_png_dec_input_control ......Subprocess aborted***Exception: 0.34 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - core/libs/prx/libScePngDec/Export.cpp:pullImage bounds checking and chunk framing correctly calculate headerEnd (offset + CHUNK_HEADER_SIZE) and chunkEnd (headerEnd + length + CHUNK_CRC_SIZE), verifying length <= MAX_CHUNK_LENGTH (0x7FFFFFFF) without integer overflow on 64-bit platforms. - core/libs/prx/libScePngDec/Export.cpp:pullImage terminates callback consumption immediately upon reading the full IEND chunk and its 4-byte CRC (isEnd = std::memcmp(...) == 0), preventing spurious reads and trailing callback errors on subsequent operations. - core/libs/prx/libScePngDec/Export.cpp:scePngDecDecodeWithInputControl preserves negative status codes ((*use).status < 0) from input callbacks and delegates incomplete chunk and corrupted payload validation to scePngDecDecode, which returns SCE_PNG_DEC_ERROR_DECODE_ERROR (0x80690012). - core/libs/prx/libScePngDec/Export.cpp: StreamUse RAII lock and busy flag exchange prevent concurrent handle use without deadlocks, while streamMutex protects the streams map lifetime across create, decode, and delete calls. - core/libs/tests/GuestPngDecInputControl.cpp exercises multiple refill chunk sizes (1, 3, 7, 12, 1024 bytes), post-IDAT ancillary chunks containing false IEND payloads, trailing callback bytes, repeated decode calls without new callbacks, and incomplete IEND EOF/error paths. - docs/dev/debt/libScePngDec.md and AGENTS.md rules are fully satisfied: zero code comments added, accurate technical debt notes updated for unmeasured native refill boundaries and read-ahead, and Conventional Commits title formatting observed. - Note: the AI-assisted checkbox can stay. Looks Good To Me
Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
boykopovar/AnyPS5!18361
No description provided.