Skip to content

fix(videodec2): validate ClearOutput destinations upfront before mutating output - #21081

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
Kikoarl:fix/libSceVideodec2-decode-copyin
Nov 28, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
Kikoarl:fix/libSceVideodec2-decode-copyin

Conversation

@Kikoarl

@Kikoarl Kikoarl commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

What

Validates cleared guest destinations upfront in ClearOutput (core/libs/prx/libSceVideodec2/Export.cpp), closing the remaining transactional gap after #20614 landed checked CopyInGuest/CopyOutGuest/auBytes:

  • Checks optional frame (sizeof(FrameBuffer)) before output (min(thisSize, sizeof(OutputInfo))) under GuestArena::HostWrite paired with GuestWritable_nid_no_patch while preserving caller errno.
  • Throws Videodec2: invalid argument pointer without mutating either struct and without opening output in HostWrite when frame is read-only (keeping write-watched output pages clean on Windows as well as Linux). Applies uniformly to both sceVideodec2Decode_nid_postfix and sceVideodec2Flush_nid_postfix.
  • Extends core/libs/tests/GuestVideodec2HevcAliases.cpp with unreadable/unwritable pointer, errno preservation, armed GuestArena (std::array<void*, 64> drain loop for 32 pages in kSpan = 0x20000), sceVideodec2Decode/sceVideodec2Flush read-only frame non-mutation/non-dirtying, and page-straddling unwritable output checks, plus debt in docs/dev/debt/changes/fix-libSceVideodec2-decode-copyin.md.

Tested

  • Linux x86_64 Release RAM-disk build (BUILD_TESTING=ON), rebased onto origin/main (115a7036e2, PR head 7b2b577729):
    • ctest --test-dir /tmp/anyps5_verify_build --output-on-failure -R "^(guest_videodec2|guest_videodec2_hevc_aliases|guest_hevc_metadata|guest_videodec2_unimplemented)$": 4/4 passed (0.24 s), including guest_videodec2_hevc_aliases (0.01 s).
  • python3 tools/debt_fold.py --check and python3 tools/check_conventions.py --base origin/main: passed cleanly.

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 the licence notice of code adapted from upstream (see AGENTS.md)
  • 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: none
  • AI-assisted: yes

@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 e1878384d6:

  • Conventions: tools/check_conventions.py failed; git diff --check clean.
    (pull request): error: [pr-template] Fill in the pull request template: fill Depends on with #N or none
    1 findings, 1 errors; GitHub shows at most 10 error annotations, the job summary lists all
    
  • Build: guest_videodec2_hevc_aliases_tests built cleanly.
  • CTest (guest_videodec2_hevc_aliases):
    1/1 Test #4872: guest_videodec2_hevc_aliases .....   Passed    0.02 sec
    100% tests passed, 0 tests failed out of 1
    

Requesting changes for the points above.

@Kikoarl

Kikoarl commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @PrivacyOnion! Added - [x] Depends on: none to the checklist template.

@maskym

maskym commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Windows result for head e1878384d6, merged alone into main 56c87adbdc (the PR's Tested section names no Windows run).

Host: Windows 11 26300, Intel i7-12700K, WinLibs GCC 15.2.0 (x86_64-ucrt-posix-seh, r6), Release, BUILD_TESTING=ON, Ninja. main itself fails to compile one test on Windows at this commit (UploadCacheReadOnlyFlush.cpp:177, setenv; #19899 fixes it), so the tree was built with ninja -k 0. Microsoft Defender on this machine quarantines 34 AGC test executables as Trojan:Win64/ShellCode.MKA!MTB when they are linked (the case docs/dev/BUILD.md describes), so their objcopy step fails on both trees and those tests do not run here. This PR adds no other build failure.

guest_videodec2_hevc_aliases, the test this PR adds or extends, passes 3 of 3 runs. Not run on a PS5.

AI-assisted: yes (Claude Code), the runs and the numbers above are from this machine.

@Kikoarl

Kikoarl commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

@PrivacyOnion Updated PR description to match the verbatim 12-item pull request template checklist, resolving the conventions failure.

@kiwe147

kiwe147 commented Nov 21, 2026

Copy link
Copy Markdown

Tested head e1878384d6, applied with git apply on main fd4aeed483 together with the other 29 PRs of one batch (#18156, #21208, #22013, #22795, #23226, #23385, #24324, #24533, #24582, #24690, #24693, #17553, #19619, #20085, #19682, #22123, #22529, #22572, #22662, #23479, #23787, #24568, #15330, #21249, #21630, #21713, #22040, #23490, #24394). All applied without conflicts and the whole tree built.

Setup: Windows 11 Pro 10.0.26200, Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON. GPUs: NVIDIA GeForce RTX 3080 (driver 621.24) and the AMD Radeon iGPU of the CPU (gfx1036, driver 32.0.21036.18). No validation layer. iGPU run: ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 ANYPS5_GPU="AMD Radeon", agc_/video tests only.

tests RTX 3080 iGPU
guest_videodec2_hevc_aliases (1 test, matched by name to the test file this PR changes) all pass all pass

Whole batch: the full ctest -j 12 (6540 tests) on the RTX 3080 has 5 failing tests; the 4 that also fail on main fd4aeed483 are agc_driver_stencil_plane_memory, agc_driver_msaa_dcc_unwatch, agc_storage_write_back_host_retile, and pr_overlap_names, which needs a newer Python than my 3.10. Intermittent in this run (pass when rerun alone, or known flaky): agc_driver_store_over_untracked. Traced to other PRs of the batch, each by reverting it and testing again: #17553 (guest_memory); none is in this PR's area. The 2143 agc_/video tests on the iGPU have 2 failing: agc_driver_stencil_plane_memory, agc_driver_store_over_untracked (agc_driver_stencil_plane_memory also fails on main).

This is one combined tree, not a run of this PR alone.

AI-assisted: yes (Claude Code)

@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 points below are optional.

What I ran

Reviewed head e1878384d6cb76c5fd3ac317be2018d213e3ac69 using repo_read.py to inspect the prepared diff, the changed files, and relevant helper and caller code. No build or tests were run; the commands in the description are author-reported.

Results

Check Result Note
Changed code review passed All three changed files and their added lines were read.
Build and tests not run Only author-reported results are available.

Findings

Blocking

None.

Non-blocking

None.

Not verified

  • Build, tests, and convention checks were not run in this review.
  • files.json was unavailable in the review folder, so excluded paths and any repository-wide review coverage beyond the manifest could not be confirmed.
  • Console behavior for an unmapped input pointer is unmeasured, as the change’s debt note states.

To get this to a good state

Nothing is required.

@Helldiverr

Copy link
Copy Markdown
Contributor

Windows run of e187838 merged onto main 403fbe6: guest_hevc_metadata, guest_videodec2, guest_videodec2_hevc_aliases and guest_videodec2_unimplemented pass 3 of 3, and the PR's test on main stops with a SegFault. Two of three mutations fail the test; the third changes nothing once the copy has succeeded. A probe with the input struct across a page boundary gets the argument error each time. A second probe with an unreadable output stops with a SegFault.

Merge state

Measured (Windows)

  • Main 403fbe6: guest_hevc_metadata, guest_videodec2, guest_videodec2_hevc_aliases and guest_videodec2_unimplemented pass.
  • Negative control: the PR's core/libs/tests/GuestVideodec2HevcAliases.cpp on main. guest_videodec2_hevc_aliases stops with Exception: SegFault; the other three pass.
  • Merge 345e1a724c: all four pass 3 of 3.
  • Probe: a temporary block in core/libs/tests/GuestVideodec2HevcAliases.cpp after test line 53, not part of the PR. On Windows it places an InputData so that only 8, 16 or 40 of its bytes are readable before a PAGE_NOACCESS page. It ran 3 times on the merge with the same output each time, and 0 failures.
    • All three placements throw Videodec2: invalid argument pointer.
    • With an invalid handle (0x1234) and the input at 0x10, the call throws the same argument error, not invalid decoder handle. The copy at line 590 runs before the handle lookup at line 591.
  • Second probe: a separate temporary block in the same place. It calls Decode with the empty InputData from test line 50 and output set to 0x10. It ran once on the merge. The test stops with Exception: SegFault right after the probe prints that it is calling. The copy-in covers input only; the empty-input path reaches ClearOutput (line 596), which writes through output without a check.
  • Mutations: each was applied to core/libs/prx/libSceVideodec2/Export.cpp on the merge, together with the probe. The tests were rebuilt and run once. Lines are at head.
Mutation (head line) guest_videodec2_hevc_aliases probe
localInput = *input; instead of the checked copy (line 590) stops with Exception: SegFault not reached
copy result ignored (line 590) fails with invalid argument did not throw (test line 29): the 0x10 call at test line 53 returns not needed
input->auData / input->auSize read again at line 595 passes no change
  • The third row reads the same bytes again after the copy has succeeded, so no test can tell it apart. It is listed only to show that line 595 reads the local copy.
  • After the mutations, the restored merge passes.
  • tools/check_conventions.py --base 403fbe6fc1 and tools/debt_fold.py --check pass on the merge.

Inferred / suggestions

  • Other guest pointers. Only the InputData struct is copied in. The access-unit bytes are still read through localInput.auData (ToAnnexB at line 635). output is written directly by ClearOutput (lines 451-456, called at line 596), as the second probe shows, and frame is used the same way. The debt fragment (docs/dev/debt/changes/fix-libSceVideodec2-decode-copyin.md:5) names only the input data pointer. A line there on the pointers left unchecked would record the remaining gap.
  • Throw or error code. On an unreadable pointer, sceVideodec2QueryComputeMemoryInfo_nid_postfix returns Videodec2ErrorArgumentPointer (0x811D0102, line 504). The new check in Decode throws, as the null check at line 588 already did. The PS5 result for an unmapped pointer is not measured; the fragment says so.
  • errno. CopyInGuest (lines 43-48) saves and restores errno around GuestCopyIn_nid_no_patch, as the PR body says. This run did not measure errno.

Existing runs

  • maskym ran Windows at e187838 (pass), with no control.
  • kiwe147 ran Windows at e187838 in a batch of 30 PRs (pass), with no control.
  • PrivacyOnion ran Linux at e187838 (pass), with no control, and raised the empty Depends on line; the PR body now fills it.
  • MarcZX10 read the change and did not run it.
  • This run adds a Windows negative control, three mutations and the two probes.

Host

  • CPU: AMD Ryzen 9 9950X3D
  • OS: Windows 11 Pro 26300
  • Toolchain: WinLibs GCC 15.2, Release
  • Commits: head e187838, main 403fbe6, merge 345e1a724c

AI-assisted: yes

@Kikoarl
Kikoarl force-pushed the fix/libSceVideodec2-decode-copyin branch from e187838 to 929db08 Compare November 24, 2026 18:00
@Kikoarl

Kikoarl commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

@Helldiverr @PrivacyOnion Rebased onto origin/main as 929db08c7b and addressed the ClearOutput finding: ClearOutput (Export.cpp:462) now clears output fields (isValid, isErrorFrame, pictureCount, codecType) and frame->isAccepted through CopyOutGuest (GuestCopyOut_nid_no_patch), throwing Videodec2: invalid argument pointer instead of crashing on unwritable pointers (including empty InputData calls). Added regression tests for unwritable output and frame in GuestVideodec2HevcAliases.cpp, documented remaining unchecked inner auData reads and non-empty EmitDecoded writes in docs/dev/debt/changes/fix-libSceVideodec2-decode-copyin.md, and re-verified guest_videodec2_hevc_aliases (0.01 s) plus tools/check_conventions.py --base origin/main.

@PrivacyOnion Ready for re-review (GitHub API blocks non-collaborator PR authors from invoking RequestReviews).

@Kikoarl
Kikoarl force-pushed the fix/libSceVideodec2-decode-copyin branch from 929db08 to 3afcb24 Compare November 24, 2026 18:56
@Kikoarl

Kikoarl commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Updated in 3afcb24d6c (rebased on origin/main):

  1. ClearOutput now validates all cleared output fields and optional frame->isAccepted upfront under GuestArena::HostWrite + GuestWritable_nid_no_patch before calling CopyOutGuest, so an unwritable frame throws Videodec2: invalid argument pointer without mutating output.
  2. Trace() logging in sceVideodec2Decode_nid_postfix now copies frame and output via CopyInGuest instead of dereferencing guest pointers directly.
  3. Extended GuestVideodec2HevcAliases.cpp with output non-mutation assertions (verified failing on prior head 929db08c7b) and armed GuestArena write-watched page coverage.

@Kikoarl
Kikoarl requested a review from PrivacyOnion November 24, 2026 19:54
@Helldiverr

Copy link
Copy Markdown
Contributor

I ran 3afcb24 on Windows, merged onto main 46610c5 as 54c6d0fa22. The new checks work: an unwritable frame throws without changing output, and the mutations of the upfront check fail a test. The PR's own test still fails on Windows, 3 of 3 runs, at core/libs/tests/GuestVideodec2HevcAliases.cpp:114 ("videodec2 watch clear"). The cause is the 16-entry pages array, which is too small for the 32 pages of the span. With a 64-entry array the test gets further and fails at line 145 ("read-only frame dirtied watched output page"), because the upfront HostWrite marks the output page as written. The test stops at line 114, so the HEVC alias decode and hash checks after it (lines 154-176) no longer run on Windows. They pass on main.

Merge state

  • The merge onto main 46610c5 is clean (54c6d0fa22). It touches core/libs/prx/libSceVideodec2/Export.cpp, core/libs/tests/GuestVideodec2HevcAliases.cpp and the fragment docs/dev/debt/changes/fix-libSceVideodec2-decode-copyin.md.

Measured (Windows)

Run guest_hevc_metadata guest_videodec2 guest_videodec2_hevc_aliases guest_videodec2_unimplemented
main 46610c5 pass pass pass pass
PR test on main (negative control) pass pass Exception: SegFault pass
merge 54c6d0fa22, run 1 pass pass fails: videodec2 watch clear pass
merge 54c6d0fa22, run 2 pass pass fails: videodec2 watch clear pass
merge 54c6d0fa22, run 3 pass pass fails: videodec2 watch clear pass
restored merge after the mutations pass pass fails: videodec2 watch clear pass
  • Negative control: the PR's test built against main sources. main reads the input at 0x10 (test line 65) directly, so the test stops with a SegFault.
  • The test lines before 78 pass on the merge. These are the input, output and frame pointer checks and the errno checks at lines 64-77. The failure is in the arena block (lines 78-152).

Why line 114 fails

  • collectDirty (test lines 98-111) collects into std::array<void*, 16> pages{} (line 101). The span is kSpan = 0x20000 (line 80), which is 32 pages of 4 KiB.
  • GuestArenaCollectWrites_nid_postfix stops at the capacity. A diagnostic build that printed the count got 16 for the first flush (line 113) and 16 again for the "clear" collect (line 114). The second 16 pages were still dirty, so !collectDirty() is false.
  • With the array raised to 64 and the count and pages printed:
    • the first flush returned 32;
    • the clear collect returned 0;
    • after the empty decode it returned 2 (+0x0 output page, +0x10000 frame page);
    • after the HostWrite that prepares the read-only check it returned 32, then 0.
  • The test then fails at line 145. After the read-only frame is rejected, the collect returns 1 page (+0x0, the output page). The output bytes are unchanged, since lines 142-143 pass.
  • Likely cause: WritableGuest (Export.cpp:58-64) opens a GuestArena::HostWrite for each output field. On Windows that is WindowsMappings::BeginHostWrite and EndHostWrite with the default store = true (core/libs/prx/libc/include/WindowsMappings.hpp:371 and :400). Both call invalidate for the range (:387 and :409), so the page is recorded as written even though nothing was stored.
    • The import variant (store = false, core/libs/prx/libc/src/GuestArena.cpp:1019-1026) does not record the write.
    • It is not a drop-in replacement, though. It only opens an armed page when the page is resident (WindowsMappings.hpp:392).
  • Possible fixes:
    • Size pages to kSpan / 0x1000, or repeat the collect until the count is below the capacity.
    • Then decide whether a rejected call may mark the output page as written on Windows. Line 145 could allow that on Windows, or the upfront check could avoid a store-mode HostWrite.
  • The windows CI job for 3afcb24 was still pending when I wrote this.

Probe

  • Setup: a temporary block after test line 77, not part of the PR. It ran 3 times on the merge, with identical output each time and 0 failures. It ran before line 114, so the line-114 failure did not hide it.
  • errno: errno was set to EDOM (33) before a call with output at 0x10. The call throws Videodec2: invalid argument pointer, and errno is still 33.
  • Unwritable frame: frame at 0x10 with a writable, pre-filled output throws the argument error, and output is unchanged.
  • Flush: sceVideodec2Flush_nid_postfix was called twice.
    • With frame at 0x10, it throws the argument error and output is unchanged.
    • With output at 0x10, it throws, and frame.isAccepted stays 1.
  • Page boundary, output: output was placed so that only 8, 9, 11, 12 or 15 of its bytes were writable before a PAGE_NOACCESS page. Each placement throws the argument error, with 0 bytes changed. Round 2 cleared some fields in these placements before throwing. Now nothing is cleared.
  • Page boundary, frame: with frame placed so that 8 or 24 of its bytes are writable, the call throws. frame and output are both unchanged.
  • Invalid handle: handle 0x1234 with output at 0x10 throws Videodec2: invalid decoder handle.

Mutations

Each mutation was applied to core/libs/prx/libSceVideodec2/Export.cpp on the merge, together with the probe. The tests were rebuilt and run once. Lines are at head.

Mutation (head line) Result
localInput = *input; instead of the checked copy (line 616) guest_videodec2_hevc_aliases stops with Exception: SegFault
ClearOutput restored to main's direct writes (lines 470-483) guest_videodec2_hevc_aliases stops with Exception: SegFault
all five WritableGuest clauses removed (lines 471-475) fails unwritable frame pointer mutated output (test line 77)
frame clause removed (line 475) fails unwritable frame pointer mutated output
WritableGuest returns only write.Open() (line 61) fails unwritable frame pointer mutated output: Open() succeeds for 0x10, which is outside every mapping
throw replaced by return (line 481) fails invalid argument did not throw
both codecType clauses removed (lines 474 and 479) guest_videodec2 fails unexpected picture geometry; the probe's 11-, 12- and 15-byte output placements change bytes or no longer throw
WritableGuest without the HostWrite, GuestWritable only (line 60) with the 16-entry array, only the same line-114 failure as the unmodified merge, probe 0 failures. With a 64-entry array, the empty decode on the watched output (test line 122) throws Videodec2: invalid argument pointer: GuestWritable rejects the armed read-only page unless the HostWrite opens it
errno = savedErrno; removed (line 62) not caught on Windows: only the line-114 failure, and the probe still sees errno 33

tools/check_conventions.py --base 46610c5119 and tools/debt_fold.py --check pass on the merge.

Debt fragment

  • Now covered: the fragment covers the upfront check and the no-mutation throw, and the probe confirms both. The partial clearing from round 2 is gone.
  • Not named:
    • Frame reads in EmitDecoded. EmitDecoded still reads frame->frameBuffer and frame->frameBufferSize directly (lines 488 and 502). The fragment names only the EmitDecoded writes (lines 505 and 513-521).
    • sceVideodec2Flush. Flush (lines 714-726) reaches ClearOutput through EmitDecoded (line 724). The probe shows it now throws without changing output or frame, but neither the fragment nor the test mentions it.
  • Overlap with the existing entry. docs/dev/debt/libSceVideodec2.md:25 at main 46610c5 still ends with "sceVideodec2Decode still reads and writes its input, frame and output structs with plain host accesses." After this PR that is only partly true. The fragment adds a new ## add Functional entry next to it. A ## change of line 25 would keep the two entries from contradicting each other.

Inferred / not measured

  • Throw vs. error code. sceVideodec2QueryComputeMemoryInfo_nid_postfix returns Videodec2ErrorArgumentPointer (0x811D0102, line 76) for a bad pointer (lines 530 and 534). Decode throws instead (lines 614, 616 and 481). The PS5 result is not measured, and the fragment says so.
  • Linux. The line-114 and line-145 results are Windows-only. collectDirty takes the GuestWriteWatch path on Linux (lines 105-109), which has no capacity. I did not run Linux.

Existing runs

  • Kikoarl:
    • re-ran the tests at 929db08 and at 3afcb24;
    • showed that the new output no-mutation asserts fail on 929db08.
  • At 929db08, the same 4 tests passed 3 of 3 on Windows here (merged onto f5d7f0d). The line-114 failure comes from the arena block added in 3afcb24.
  • At e187838:
    • maskym ran Windows (pass), with no control.
    • kiwe147 ran Windows in a batch of 30 PRs (pass), with no control.
    • PrivacyOnion ran Linux, with no control, and requested changes on 2026-11-15.
    • MarcZX10 read the change and approved it on 2026-11-23.
    • My earlier Windows run was e187838 merged onto 403fbe6.
  • This run adds, at 3afcb24:
    • a Windows negative control;
    • nine mutations;
    • the probe;
    • the collect-capacity diagnosis.

Host

  • CPU: AMD Ryzen 9 9950X3D
  • OS: Windows 11 Pro 26300
  • Toolchain: WinLibs GCC 15.2, Release
  • Commits: head 3afcb24, merged onto main 46610c5 as 54c6d0fa22

main has since moved 15 commits to bd3a694f2b, none of them in the files this PR touches, and the head still merges cleanly.

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 — no blocking findings

The reviewed change at 3afcb24d6ccfce02bf203c8a0e6bf4dfe2c81d6b has no verified blocking defect; checks reported by the author were not run in this review.

What I ran

Read the prepared description, commit summary, manifest, and diff, then inspected the changed code and relevant guest-memory helper definitions through repo_read.py. No commands or tests were run.

Results

Check Result Note
Diff and changed code passed Three changed files inspected
Author-reported tests not run Reported as passing in the PR description
Negative controls not run Results are author-reported

Negative control

Not run during this review. The author reports two negative controls in the PR description; those results were not independently verified.

Findings

Blocking

None.

Non-blocking

None.

Sources

Not verified

No tests or negative controls were run here. Console behavior is not verified. The PR description and debt entry identify unchecked access-unit data reads and EmitDecoded writes as remaining work; those behaviors were not audited in depth. Read: core/libs/prx/libSceVideodec2/Export.cpp (relevant ranges), core/libs/tests/GuestVideodec2HevcAliases.cpp (full file), and docs/dev/debt/changes/fix-libSceVideodec2-decode-copyin.md (full file). No changed files were excluded by the manifest.

To get this to a good state

Nothing is required.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

This passed review on 3afcb24, but it now conflicts with main in Export.cpp. Please merge main into the branch and push; it goes back to the merge queue on the new head.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Nov 26, 2026
@Kikoarl
Kikoarl force-pushed the fix/libSceVideodec2-decode-copyin branch from 3afcb24 to 7b2b577 Compare November 26, 2026 11:16
@Kikoarl Kikoarl changed the title fix(videodec2): safely copy in input structure in sceVideodec2Decode fix(videodec2): validate ClearOutput destinations upfront before mutating output Nov 26, 2026
@Kikoarl

Kikoarl commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

@oneandonlydean @Helldiverr Rebased onto origin/main (115a7036e2) as 7b2b577729 after #20614 landed the checked guest struct and auData copies, and resolved the remaining issues:

  1. Upfront ClearOutput validation (core/libs/prx/libSceVideodec2/Export.cpp): Validates optional frame (sizeof(FrameBuffer)) and output (min(thisSize, sizeof(OutputInfo))) upfront via GuestArena::HostWrite + GuestWritable_nid_no_patch (preserving caller errno) before mutating either buffer, protecting both sceVideodec2Decode and sceVideodec2Flush. Checking frame before output ensures a read-only frame rejects without opening output in HostWrite, avoiding spurious dirty marks on write-watched output pages on Windows.
  2. Windows watch-drain fix and extra checks (core/libs/tests/GuestVideodec2HevcAliases.cpp): Drains GuestArenaCollectWrites_nid_postfix in a 64-entry loop until count < pages.size() so the 32 pages of kSpan = 0x20000 flush cleanly on Windows, and adds read-only frame non-mutation checks for sceVideodec2Flush as well as page-straddling unwritable output checks.
  3. Clean debt fragment (docs/dev/debt/changes/fix-libSceVideodec2-decode-copyin.md): Records ClearOutput upfront validation cleanly without colliding with fix(libSceVideodec2): safely copy guest decode and flush buffers #20614's folded debt update.

Verified guest_hevc_metadata, guest_videodec2, guest_videodec2_hevc_aliases, and guest_videodec2_unimplemented (4/4 passed, 0.24 s), tools/debt_fold.py --check, and tools/check_conventions.py --base origin/main.

@Helldiverr

Copy link
Copy Markdown
Contributor

I ran 7b2b577 on Windows. Both points from my last run are fixed: the collect loop now repeats until every dirty page is collected, and a read-only frame no longer marks the output page as written. The four tests pass 3 of 3, and the PR's test fails without the fix. Two of the new checks change no test result: the upfront output check and the GuestWritable_nid_no_patch part of writableGuest. In each case WriteOutputGuest already rejects the bad pointer without writing.

Base used

  • The head is based on main 115a703, which needs the 3rdparty/libyuv submodule. I could not fetch that submodule here.
  • So the base is main 90c248e with these files taken from 115a703 (e7fe056e70):
  • libSceVideodec2 does not use libyuv.
  • The PR commit cherry-picks cleanly onto that base (78cfc309fa).

Measured (Windows)

Run guest_hevc_metadata guest_videodec2 guest_videodec2_hevc_aliases guest_videodec2_unimplemented
base e7fe056e70 pass pass pass pass
PR test on the base (negative control) pass pass fails: read-only frame mutated output before rejecting pass
78cfc309fa, runs 1 to 3 pass pass pass pass
restored after the mutations pass pass pass pass
  • videodec2 watch clear (test line 120) now passes: the loop at lines 103-109 repeats the collect until fewer than 64 pages come back.
  • read-only frame dirtied watched output page (line 151) and read-only flush frame dirtied watched output page (line 159) pass.

Mutations

Each mutation was applied to core/libs/prx/libSceVideodec2/Export.cpp on 78cfc309fa. The four tests were rebuilt and run once. Lines are on 78cfc309fa.

Mutation Result
frame check removed (lines 540-542) fails read-only frame mutated output before rejecting
output check moved before the frame check fails read-only frame dirtied watched output page
writableGuest without the HostWrite, GuestWritable_nid_no_patch only (line 534) the watched empty decode (test line 128) throws Videodec2: invalid argument pointer
outputBytes set to offsetof(OutputInfo, codecType) (line 546) fails straddling unwritable output dirtied watched prefix page
upfront output check removed (lines 546-549) all pass
writableGuest returns only write.Open() (line 535) all pass
errno = savedErrno; removed (line 537) all pass
outputBytes set to sizeof(OutputInfo) (line 546) all pass
  • Ordering. The first two rows show that the frame check and its place before the output check are both needed.
  • The two checks with no effect.
    • With the output check removed, the output pointer at 0x10 (test line 69) and the straddling output (line 174) still throw. Nothing is changed and no page is marked as written.
    • The reason is that WriteOutputGuest (lines 88-92) copies through GuestCopyOut_nid_no_patch, which rejects the whole range before writing.
    • With writableGuest returning only write.Open(), the tests still pass, so WriteOutputGuest rejects these pointers on its own.
    • Either drop these checks, or add a test that fails without them. I did not find one that does on Windows.
  • errno and size. In these paths, the errno restore and the thisSize bound are not reached by any test. HostWrite and GuestWritable_nid_no_patch did not change errno here. No test uses an OutputInfo whose thisSize is below sizeof(OutputInfo) and whose tail lies on an unwritable page.

Debt

  • The fragment docs/dev/debt/changes/fix-libSceVideodec2-decode-copyin.md describes the upfront check and the order.
  • The overlap with docs/dev/debt/libSceVideodec2.md:25 that I raised last time is resolved: fix(libSceVideodec2): safely copy guest decode and flush buffers #20614's fragment fix-videodec2-decode-guest-copy.md changes that line.
  • tools/check_conventions.py --base e7fe056e70 and tools/debt_fold.py --check pass.

Not measured

  • I did not run Linux. There collectDirty uses GuestWriteWatch (test lines 111-116).
  • What the PS5 returns for a bad pointer here (a throw or an error code) is not measured. The fragment says so.

Host

  • CPU: AMD Ryzen 9 9950X3D
  • OS: Windows 11 Pro 26300
  • Toolchain: WinLibs GCC 15.2, Release
  • Commits:

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

The change validates the caller’s frame and output destinations before clearing output state; the reviewed code has no verified blocking defects.

What I ran

  • Reviewed head 7b2b5777292016e57d0fd8063690452c706c5618.
  • Read the prepared description, commit summary, manifest and diff, plus all three changed files and targeted caller/helper definitions through repo_read.py.
  • No commands or tests were run. The checks listed in the PR description are author-reported.

Results

Check Result Note
Code review passed All changed files read
Automated checks not run PR description reports four tests and two checks passed
Console behavior not run No hardware measurement supplied

Findings

Blocking

None.

Non-blocking

None.

Not verified

  • Linux build and test results, debt fold check, and conventions check were not run by this review.
  • Console behavior for invalid pointers and EmitDecoded metadata writes remain unmeasured, as the change’s debt note states.
  • No files were excluded: the manifest lists all three changed files as included, and each was read.

To get this to a good state

Nothing is required.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Nov 26, 2026

@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 7b2b577729:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_videodec2_hevc_aliases_tests built cleanly.
  • CTest (guest_videodec2_hevc_aliases):
    1/1 Test #6162: guest_videodec2_hevc_aliases .....   Passed    0.04 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libSceVideodec2/Export.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #6162: guest_videodec2_hevc_aliases .....***Failed    0.03 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified Export.cpp ClearOutput checks optional frame (sizeof(FrameBuffer)) writability before output validation, throwing Videodec2: invalid argument pointer without opening output or mutating state when frame is unwritable
    • Verified OutputInfo destination pointer null check and localOutput.thisSize >= offsetof(OutputInfo, frameFormat) bounds check before output writability check
    • Verified HostWrite + GuestWritable_nid_no_patch pairing under saved and restored errno preservation in writableGuest helper
    • Verified test coverage in GuestVideodec2HevcAliases.cpp exercising unreadable input, unwritable/read-only frame, unwritable/straddling output, errno preservation, and dirty page write-watch tracking under GuestArena on Linux and Windows
    • Verified debt fragment docs/dev/debt/changes/fix-libSceVideodec2-decode-copyin.md adheres to AGENTS.md conventions and records unmeasured hardware behavior

Looks Good To Me

@oneandonlydean
oneandonlydean merged commit 00b8924 into boykopovar:main Nov 28, 2026
14 of 16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants