fix(libSceJpegDec): stop callback refills after EOI #18370

Merged
Ondra97 merged 1 commit from upstream-pr/18370 into main 2026-11-12 11:56:12 +00:00
Ondra97 commented 2026-11-11 12:36:51 +00:00 (Migrated from github.com)

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


What

sceJpegDecDecodeWithInputControl can reject a complete valid JPEG while filling a 2 KiB input block: it requests another callback after copying EOI, then propagates that callback's error. Check the existing incremental end detector after each copied callback chunk and stop refilling when it has found EOI. This fixes both the parse and decode paths.

The existing detector still handles marker lengths, entropy byte stuffing and restart markers. This follows the bundled libjpeg-turbo input contract. Exact native refill counts and read-ahead remain unmeasured; bytes offered in the same callback still follow the existing buffering policy.

Regressions compare pixels with the direct decode path for short grayscale and large RGB images, with 1/7/65536-byte callback chunks. They cover an error after EOI, an error before its final byte with unchanged output, repeated decoding without another callback, restart markers, false EOI bytes inside APP1, and trailing bytes in the same callback. The existing missing-frame EOI fixture still returns decode error; its callback count changes from three to one because refilling now stops at EOI.

Tested

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

Base reproduction: aece3f1a226c61c1e4d493132bbc3ec82cdbd48e. Final tested commit: c8388f97ab26a188bd07751a2f5ba2afce058bc6, based on 1e43d62f13896d5e6ec91d1c2c409a684b73ea8d.

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

Fresh full-project configuration with BUILD_TESTING=ON, ANYPS5_RELINKER_ONLY=OFF and exact current submodules. Focused build and runtime commands:

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_jpeg_tests guest_jpeg_dec_tests guest_jpeg_dec_input_control_tests guest_jpeg_input_extent_tests --parallel 8
ctest --test-dir build -R '^(public_headers|decoder_jpeg|guest_jpeg_dec(_input_control)?|guest_jpeg_input_extent)$' -V
python tools/check_conventions.py --base origin/main

All five selected tests passed: four runtime tests and the public-header audit. Conventions and git diff --check passed. Independent read-only review found no introduced defect in the incremental marker handling. Linux, macOS, console, titles, progressive/multi-scan runtime fixtures and the full CTest suite were not tested.

Hosted Windows job 130526826046 passed for head c8388f97ab26a188bd07751a2f5ba2afce058bc6; its changed-test selection ran guest_jpeg_dec_input_control successfully (1/1). Conventions, library portability, intake and eligibility also passed. Linux and unrelated jobs were skipped.

Pre-opening python tools/pr_overlap.py --repo boykopovar/AnyPS5 --branch codex/fix-jpeg-input-end-boundary --jobs 6 checked 1,977 open PR heads and reported no overlap. The existing Y8 and Delete PRs were also compared statically; neither changes this refill logic.

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:5259572822 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18370). Originally posted by [Ondra97](https://github.com/Ondra97) on 2026-11-11T12:36:51Z. --- ### What `sceJpegDecDecodeWithInputControl` can reject a complete valid JPEG while filling a 2 KiB input block: it requests another callback after copying EOI, then propagates that callback's error. Check the existing incremental end detector after each copied callback chunk and stop refilling when it has found EOI. This fixes both the parse and decode paths. The existing detector still handles marker lengths, entropy byte stuffing and restart markers. This follows the bundled [libjpeg-turbo input contract](https://github.com/libjpeg-turbo/libjpeg-turbo/blob/c85e6b905bf237038faa936dab160ebfc5da0344/doc/libjpeg.txt#L2199-L2207). Exact native refill counts and read-ahead remain unmeasured; bytes offered in the same callback still follow the existing buffering policy. Regressions compare pixels with the direct decode path for short grayscale and large RGB images, with 1/7/65536-byte callback chunks. They cover an error after EOI, an error before its final byte with unchanged output, repeated decoding without another callback, restart markers, false EOI bytes inside APP1, and trailing bytes in the same callback. The existing missing-frame EOI fixture still returns decode error; its callback count changes from three to one because refilling now stops at EOI. ### 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 (unused by these codec tests). WinLibs MinGW-w64 GCC 15.2.0 r7, CMake 4.3.1, Ninja, Release. Base reproduction: `aece3f1a226c61c1e4d493132bbc3ec82cdbd48e`. Final tested commit: `c8388f97ab26a188bd07751a2f5ba2afce058bc6`, based on `1e43d62f13896d5e6ec91d1c2c409a684b73ea8d`. | Metric | Before (base) | After (fix) | | --- | --- | --- | | JPEG input-control regression | Fails the complete-image parse assertion | Passes | | Existing direct JPEG decoder test | Passes | Passes | Fresh full-project configuration with `BUILD_TESTING=ON`, `ANYPS5_RELINKER_ONLY=OFF` and exact current submodules. Focused build and runtime commands: ```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_jpeg_tests guest_jpeg_dec_tests guest_jpeg_dec_input_control_tests guest_jpeg_input_extent_tests --parallel 8 ctest --test-dir build -R '^(public_headers|decoder_jpeg|guest_jpeg_dec(_input_control)?|guest_jpeg_input_extent)$' -V python tools/check_conventions.py --base origin/main ``` All five selected tests passed: four runtime tests and the public-header audit. Conventions and `git diff --check` passed. Independent read-only review found no introduced defect in the incremental marker handling. Linux, macOS, console, titles, progressive/multi-scan runtime fixtures and the full CTest suite were not tested. Hosted [Windows job 130526826046](https://github.com/boykopovar/AnyPS5/actions/runs/43021867169/job/130526826046) passed for head `c8388f97ab26a188bd07751a2f5ba2afce058bc6`; its changed-test selection ran `guest_jpeg_dec_input_control` successfully (1/1). Conventions, library portability, intake and eligibility also passed. Linux and unrelated jobs were skipped. Pre-opening `python tools/pr_overlap.py --repo boykopovar/AnyPS5 --branch codex/fix-jpeg-input-end-boundary --jobs 6` checked 1,977 open PR heads and reported no overlap. The existing Y8 and Delete PRs were also compared statically; neither changes this refill logic. 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:22:05 +00:00 (Migrated from github.com)

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

Review: APPROVED


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

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_jpeg_dec_input_control_tests built cleanly.
  • CTest (guest_jpeg_dec_input_control):
    1/1 Test #3228: guest_jpeg_dec_input_control .....   Passed    0.28 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libSceJpegDec/Export.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #3228: guest_jpeg_dec_input_control .....Subprocess aborted***Exception:   0.37 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified in core/libs/prx/libSceJpegDec/Export.cpp (lines 276-302) that endFound(stream) && stream.end halts the refill loop in pull immediately after the EOI chunk is copied, avoiding spurious subsequent guest callback invocations.
    • Audited incremental marker parsing in endFound in core/libs/prx/libSceJpegDec/Export.cpp (lines 304-343) across chunk boundaries to ensure marker lengths, APPn payloads, restart markers (0xFF 0xD0..0xD7), and entropy byte stuffing (0xFF 0x00) do not trigger false EOI detections or out-of-bounds accesses.
    • Verified in parseStream and decodeStream in core/libs/prx/libSceJpegDec/Export.cpp (lines 352-408) that early EOI detection allows complete single-block images to parse and decode without requesting extra callback data, while genuine stream errors continue to propagate properly.
    • Checked buffer extent calculation in decodeStream (line 406) where stream.end.value_or(stream.data.size()) cleanly excludes trailing bytes from the slice passed to writeImage.
    • Audited test additions in core/libs/tests/GuestJpegDecInputControl.cpp (lines 123-230) verifying chunk sizes of 1, 7, and 65536 bytes, trailing bytes, restart markers, pre-EOI failure handling, and idempotence across repeated decode calls on the same handle.
    • Confirmed strict AGENTS.md compliance: zero prohibited comments in code, Conventional Commit title, no stray files, and full C++20 / LP64 / LLP64 portability.
  • Note: the AI-assisted checkbox can stay.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5861906462 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18370#pullrequestreview-5861906462). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-11T16:22:05Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `c8388f97ab`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_jpeg_dec_input_control_tests` built cleanly. - **CTest** (`guest_jpeg_dec_input_control`): ``` 1/1 Test #3228: guest_jpeg_dec_input_control ..... Passed 0.28 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libSceJpegDec/Export.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #3228: guest_jpeg_dec_input_control .....Subprocess aborted***Exception: 0.37 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - Verified in core/libs/prx/libSceJpegDec/Export.cpp (lines 276-302) that endFound(stream) && stream.end halts the refill loop in pull immediately after the EOI chunk is copied, avoiding spurious subsequent guest callback invocations. - Audited incremental marker parsing in endFound in core/libs/prx/libSceJpegDec/Export.cpp (lines 304-343) across chunk boundaries to ensure marker lengths, APPn payloads, restart markers (0xFF 0xD0..0xD7), and entropy byte stuffing (0xFF 0x00) do not trigger false EOI detections or out-of-bounds accesses. - Verified in parseStream and decodeStream in core/libs/prx/libSceJpegDec/Export.cpp (lines 352-408) that early EOI detection allows complete single-block images to parse and decode without requesting extra callback data, while genuine stream errors continue to propagate properly. - Checked buffer extent calculation in decodeStream (line 406) where stream.end.value_or(stream.data.size()) cleanly excludes trailing bytes from the slice passed to writeImage. - Audited test additions in core/libs/tests/GuestJpegDecInputControl.cpp (lines 123-230) verifying chunk sizes of 1, 7, and 65536 bytes, trailing bytes, restart markers, pre-EOI failure handling, and idempotence across repeated decode calls on the same handle. - Confirmed strict AGENTS.md compliance: zero prohibited comments in code, Conventional Commit title, no stray files, and full C++20 / LP64 / LLP64 portability. - 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!18370
No description provided.