fix(libSceVideodec2): reject out-of-order HEVC NAL units #27925

Merged
gurtgg merged 1 commit from upstream-pr/27925 into main 2026-12-04 07:04:06 +00:00
gurtgg commented 2026-11-27 20:32:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gurtgg on 2026-11-27T20:32:10Z.


What

  • Validate ITU-T H.265 7.4.2.4.4 access-unit NAL unit ordering in Videodec2Hevc::Parse_nid_no_patch (core/libs/prx/libSceVideodec2/HevcMetadata.cpp) before mutating persistent parser state: reject AUD (type 35) when not first, reject VPS/SPS/PPS (types 32-34) after any slice NAL unit, and reject EOS/EOB (types 36-37) when appearing without a slice, before lastSliceIndex, duplicated, in invalid EOB-before-EOS order, or followed by unexpected NAL units.
  • Return immediately on ordering violations so invalid units cannot mutate state.vps, state.sps, state.pps, or state.afterEnd. Because Loop 1 rejects pictureless EOS/EOB units before Loop 2 runs, if (!selectedSps) retains if (flags.endSequencePresent || flags.endStreamPresent) state.afterEnd = true; so slice-containing access units with unavailable SPS/PPS still close the sequence/bitstream at EOS/EOB.

Tested

  • InvalidNalOrder (core/libs/tests/GuestHevcMetadata.cpp): negative-control tests verifying out-of-order VPS/SPS/PPS after slices, duplicate or non-leading AUDs, EOS/EOB before the last slice or in invalid sequence/bitstream order, and slice + unavailable SPS + EOS/EOB followed by a non-IRAP slice are rejected (failing on main and passing with this fix), alongside positive controls for valid EOS/EOB ordering.
  • MalformedParameterHistory (core/libs/tests/GuestHevcMetadata.cpp) and all existing fixture tests: placed the truncated PPS after the leading AUD so state.unsupported persists across Reset under valid NAL ordering, verified via CheckProjectionWrites(decoder, true).

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 and upstream licence notices (tools/check_conventions.py passes)
  • 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
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Negative-control regression test (plus valid/boundary cases) fails against unmodified main and passes after fix
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of malformed HEVC video streams by rejecting invalid NAL unit ordering before parsing.
    • Invalid access-unit delimiters, parameter sets, and end markers are now handled consistently, reducing the risk of corrupted metadata.
    • Valid end markers following slices continue to be supported.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5532733790 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/27925). Originally posted by [gurtgg](https://github.com/gurtgg) on 2026-11-27T20:32:10Z. --- ### What - Validate ITU-T H.265 7.4.2.4.4 access-unit NAL unit ordering in `Videodec2Hevc::Parse_nid_no_patch` (`core/libs/prx/libSceVideodec2/HevcMetadata.cpp`) before mutating persistent parser state: reject AUD (type 35) when not first, reject VPS/SPS/PPS (types 32-34) after any slice NAL unit, and reject EOS/EOB (types 36-37) when appearing without a slice, before `lastSliceIndex`, duplicated, in invalid EOB-before-EOS order, or followed by unexpected NAL units. - Return immediately on ordering violations so invalid units cannot mutate `state.vps`, `state.sps`, `state.pps`, or `state.afterEnd`. Because Loop 1 rejects pictureless EOS/EOB units before Loop 2 runs, `if (!selectedSps)` retains `if (flags.endSequencePresent || flags.endStreamPresent) state.afterEnd = true;` so slice-containing access units with unavailable SPS/PPS still close the sequence/bitstream at EOS/EOB. ### Tested - `InvalidNalOrder` (`core/libs/tests/GuestHevcMetadata.cpp`): negative-control tests verifying out-of-order VPS/SPS/PPS after slices, duplicate or non-leading AUDs, EOS/EOB before the last slice or in invalid sequence/bitstream order, and slice + unavailable SPS + EOS/EOB followed by a non-IRAP slice are rejected (failing on `main` and passing with this fix), alongside positive controls for valid EOS/EOB ordering. - `MalformedParameterHistory` (`core/libs/tests/GuestHevcMetadata.cpp`) and all existing fixture tests: placed the truncated PPS after the leading AUD so `state.unsupported` persists across `Reset` under valid NAL ordering, verified via `CheckProjectionWrites(decoder, true)`. ### Checklist - [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 and upstream licence notices (`tools/check_conventions.py` passes) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's technical debt file - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Negative-control regression test (plus valid/boundary cases) fails against unmodified `main` and passes after fix - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Bug Fixes** * Improved handling of malformed HEVC video streams by rejecting invalid NAL unit ordering before parsing. * Invalid access-unit delimiters, parameter sets, and end markers are now handled consistently, reducing the risk of corrupted metadata. * Valid end markers following slices continue to be supported. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
PrivacyOnion commented 2026-11-27 20:36:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-27T20:36:20Z.

Review: APPROVED


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

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_hevc_metadata_tests built cleanly.
  • CTest (guest_hevc_metadata):
    1/1 Test #6231: guest_hevc_metadata ..............   Passed    0.14 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libSceVideodec2/HevcMetadata.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #6231: guest_hevc_metadata ..............Subprocess aborted***Exception:   0.38 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • core/libs/prx/libSceVideodec2/HevcMetadata.cpp: verified NAL unit ordering checks in Parse_nid_no_patch against ITU-T H.265 Section 7.4.2.4.4 (leading AUD constraint, parameter sets VPS/SPS/PPS restricted before slices, EOS/EOB ordering and trailing restrictions).
    • core/libs/prx/libSceVideodec2/HevcMetadata.cpp: verified state preservation semantics ensuring metadata.unsupported is populated and persistent parser state (vps, sps, pps, afterEnd) remains uncorrupted when returning early on ordering violations.
    • core/libs/prx/libSceVideodec2/HevcMetadata.cpp: verified handling of EOS/EOB in access units with missing/unavailable SPS/PPS so afterEnd is properly set while pictureless EOS/EOB sequences are cleanly rejected before loop 2.
    • core/libs/tests/GuestHevcMetadata.cpp: verified comprehensive regression coverage in InvalidNalOrder for out-of-order parameter sets, non-leading/duplicate AUD, malformed EOS/EOB sequences, and state persistence assertions.
    • core/libs/tests/GuestHevcMetadata.cpp: verified MalformedParameterHistory fixture updates correctly accommodate AUD requirements without masking parser regression checks.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6093127479 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/27925#pullrequestreview-6093127479). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-27T20:36:20Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `5e16c5ef31`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_hevc_metadata_tests` built cleanly. - **CTest** (`guest_hevc_metadata`): ``` 1/1 Test #6231: guest_hevc_metadata .............. Passed 0.14 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libSceVideodec2/HevcMetadata.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #6231: guest_hevc_metadata ..............Subprocess aborted***Exception: 0.38 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - core/libs/prx/libSceVideodec2/HevcMetadata.cpp: verified NAL unit ordering checks in Parse_nid_no_patch against ITU-T H.265 Section 7.4.2.4.4 (leading AUD constraint, parameter sets VPS/SPS/PPS restricted before slices, EOS/EOB ordering and trailing restrictions). - core/libs/prx/libSceVideodec2/HevcMetadata.cpp: verified state preservation semantics ensuring metadata.unsupported is populated and persistent parser state (vps, sps, pps, afterEnd) remains uncorrupted when returning early on ordering violations. - core/libs/prx/libSceVideodec2/HevcMetadata.cpp: verified handling of EOS/EOB in access units with missing/unavailable SPS/PPS so afterEnd is properly set while pictureless EOS/EOB sequences are cleanly rejected before loop 2. - core/libs/tests/GuestHevcMetadata.cpp: verified comprehensive regression coverage in InvalidNalOrder for out-of-order parameter sets, non-leading/duplicate AUD, malformed EOS/EOB sequences, and state persistence assertions. - core/libs/tests/GuestHevcMetadata.cpp: verified MalformedParameterHistory fixture updates correctly accommodate AUD requirements without masking parser regression checks. Looks Good To Me
MarcZX10 commented 2026-11-27 20:53:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-27T20:53:48Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the inspected code supports the stated NAL ordering checks and preserves end-state handling when slice metadata cannot be resolved.

What I ran

  • Inspected head 5e16c5ef312d72d97853979c43e8eba99e878ba6 using repo_read.py to read the prepared diff and both changed files.
  • No test or build commands were run.

Results

Check Result Note
Diff and changed code review passed Both changed files inspected
Tests and build not run No execution performed

Findings

Blocking

None.

Non-blocking

None.

Not verified

  • Runtime behavior and test results, including the negative controls, were not verified by execution.
  • The changed files were core/libs/prx/libSceVideodec2/HevcMetadata.cpp and core/libs/tests/GuestHevcMetadata.cpp; both were read. No files were excluded from the prepared manifest.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6093255819 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/27925#pullrequestreview-6093255819). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-27T20:53:48Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the inspected code supports the stated NAL ordering checks and preserves end-state handling when slice metadata cannot be resolved. ## What I ran - Inspected head `5e16c5ef312d72d97853979c43e8eba99e878ba6` using `repo_read.py` to read the prepared diff and both changed files. - No test or build commands were run. ## Results | Check | Result | Note | |---|---|---| | Diff and changed code review | passed | Both changed files inspected | | Tests and build | not run | No execution performed | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified - Runtime behavior and test results, including the negative controls, were not verified by execution. - The changed files were `core/libs/prx/libSceVideodec2/HevcMetadata.cpp` and `core/libs/tests/GuestHevcMetadata.cpp`; both were read. No files were excluded from the prepared manifest. ## To get this to a good state Nothing is required.
Plyxen commented 2026-11-28 05:06:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Plyxen on 2026-11-28T05:06:05Z.

Review: APPROVED


Checked head 5e16c5ef31. All on Windows 11 with WinLibs GCC 15.2 (Release), applied to main a163d4b886 together with #27930, #27929, #27926, #27924, #27923, #27922, #27901, #27828, #27771, #27755, #27748, #27682, #27765. tools/check_conventions.py --head and tools/debt_fold.py --check passed and the full build succeeded. guest_hevc_metadata passes. In the full CPU suite (5,120 tests) the only failures are unrelated:

  • windows_read_trap_unknown fails on main too (fixed in #28028).
  • frame_capture_minimize, jpeg_external_toolchain and guest_pthread_mutex_contention are timeouts under load; they pass when run on their own.
  • guest_audio_in_capture passes 20/20 on main and with #27922.

One non-blocking note: the new rule reports any VPS, SPS or PPS after a slice as unsupported. H.265 7.4.2.4.4 only forbids parameter sets after the last VCL NAL unit of an access unit, so a PPS between two slices of one picture is valid. AvcMetadata.cpp has the same stricter rule for H.264, so this is consistent and safe, because the stream is reported as unsupported rather than accepted. It may be worth a line in docs/dev/debt/libSceVideodec2.md.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6095938189 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/27925#pullrequestreview-6095938189). Originally posted by [Plyxen](https://github.com/Plyxen) on 2026-11-28T05:06:05Z. **Review:** APPROVED --- Checked head `5e16c5ef31`. All on Windows 11 with WinLibs GCC 15.2 (Release), applied to `main` `a163d4b886` together with #27930, #27929, #27926, #27924, #27923, #27922, #27901, #27828, #27771, #27755, #27748, #27682, #27765. `tools/check_conventions.py --head` and `tools/debt_fold.py --check` passed and the full build succeeded. `guest_hevc_metadata` passes. In the full CPU suite (5,120 tests) the only failures are unrelated: - `windows_read_trap_unknown` fails on `main` too (fixed in #28028). - `frame_capture_minimize`, `jpeg_external_toolchain` and `guest_pthread_mutex_contention` are timeouts under load; they pass when run on their own. - `guest_audio_in_capture` passes 20/20 on `main` and with #27922. One non-blocking note: the new rule reports any VPS, SPS or PPS after a slice as unsupported. H.265 7.4.2.4.4 only forbids parameter sets after the *last* VCL NAL unit of an access unit, so a PPS between two slices of one picture is valid. `AvcMetadata.cpp` has the same stricter rule for H.264, so this is consistent and safe, because the stream is reported as unsupported rather than accepted. It may be worth a line in `docs/dev/debt/libSceVideodec2.md`. AI-assisted: yes (Claude Code)
ligmaSec commented 2026-12-03 09:18:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T09:18:02Z.

Review: COMMENTED


The Linux guest_hevc_metadata CTest passed with the exact parser change. The new cases check ordering of parameter sets and access-unit metadata, plus the decoder's ability to continue after rejecting an out-of-order unit.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6166799656 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/27925#pullrequestreview-6166799656). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T09:18:02Z. **Review:** COMMENTED --- The Linux `guest_hevc_metadata` CTest passed with the exact parser change. The new cases check ordering of parameter sets and access-unit metadata, plus the decoder's ability to continue after rejecting an out-of-order unit. AI-assisted: yes.
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!27925
No description provided.