Skip to content

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

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
gurtgg:fix-hevc-metadata-nal-order
Dec 4, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
gurtgg:fix-hevc-metadata-nal-order

Conversation

@gurtgg

@gurtgg gurtgg commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

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.

@coderabbitai

coderabbitai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Changes: HEVC NAL Ordering

Layer / File(s) Summary
Pre-parse NAL ordering checks
core/libs/prx/libSceVideodec2/HevcMetadata.cpp
Parse_nid_no_patch rejects specified ordering errors before parsing. It preserves an existing metadata.unsupported value.
Ordering and state regression tests
core/libs/tests/GuestHevcMetadata.cpp
Tests cover invalid and valid NAL sequences, malformed parameter history, unsupported projection results, and parser state. CheckProjectionWrites can now assert that projection is unsupported.

Suggested reviewers: azlisme, louisdibiasi

Change: Bug fix

Merge Risk: Minimal · up to 5e16c

This change makes the HEVC metadata parser reject out-of-order NAL units before it modifies parser state, and it adds regression tests for the rejected and valid sequences. I found no concrete merge-blocking problem. The one remaining uncertainty is how the FFmpeg backend treats parameter sets in a packet the parser rejected. It affects only streams that already break NAL ordering.

Security architecture risk: Low · up to 5e16c

The new check protects metadata state from out-of-order input. Video decoding still receives the rejected input, so later picture information may disagree with the decoded picture if the decoder accepts a change that metadata ignored. The identified risk is limited to picture information for the affected decoder; broader impact was not established.

Retained concerns

  • Low · reliability · inferred: Ordering rejection now leaves the metadata parser on its previous parameter sets and sequence state while still submitting the rejected unit to FFmpeg. If FFmpeg applies a parameter-set update or end marker from that unit, later pictures can be decoded against state that metadata did not adopt. The per-picture unsupported check does not protect those later pictures from stale metadata.
Security review details

Security Blast Radius

  • inferred — The changed path uses the existing guest decode entrypoint and the decoder selected by its handle. The full comparison shows no expansion of FFmpeg input reachability; the new behavior changes metadata state and picture-info availability for that decoder.

Trust Boundaries and Controls

  • observed — The new check guards metadata state, not packet submission to FFmpeg. Guest picture-info writes remain guarded by decoder ownership checks and a writable guest-memory check, with the unsupported-result check before the metadata copy.
Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 4b95015b-834a-4980-8db6-c2bc2a9aceb3
Commits

Reviewing files that changed from the base of the PR and between fdf873a and 5e16c5e.

Files selected for processing: 2

  • core/libs/prx/libSceVideodec2/HevcMetadata.cpp
  • core/libs/tests/GuestHevcMetadata.cpp

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 4 remain after this review.

@PrivacyOnion PrivacyOnion left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@JJenkx

JJenkx commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Windows at this head (5e16c5ef312d): guest_hevc_metadata passes, 3/3 runs. This is the CTest of the executable that compiles the changed GuestHevcMetadata.cpp.

Setup:

  • Windows 11 22621, Ryzen 9 5950X, MinGW-w64 GCC 15.2 UCRT r7, Release.
  • The exact PR head was built (not merged into main).
  • 3 runs through the shared FIFO.

The added test lines have no platform guard.

AI-assisted: yes.

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Plyxen left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ligmaSec left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@oneandonlydean
oneandonlydean merged commit 33c4be2 into boykopovar:main Dec 4, 2026
15 of 17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants