Repository navigation
feat(libSceJxr): implement the PKFactory and PKCodecFactory constructors - #15232
Conversation
Progress report (e4f4f56 → 21928d3)📈 System libraries: 31.15% (+0.02%, +7 implemented functions) ✅ 7 implemented
Changed libraries (implemented exports)
|
BunnyFl
left a comment
There was a problem hiding this comment.
@simnJS thanks for this. The stream and format converter layouts match the console module, but the codec factory doesn't, and a guest calling its Release would read past the allocation.
PKCodecFactory is 40 bytes on the console, with Release at +0x20. In firmware 13.42 system/common/lib/libSceJxr.sprx (sha256 73c51baf72937450751e8437e2744fcbe23a58844625f28375f7c423fbe50362), PKCreateCodecFactory (we4rPX+-zNg, 0x378d0) is, read from disassembly and not run:
378e0: mov $0x1,%edi
378e5: mov $0x28,%esi
378ea: call *(%rax) ; allocator at 0x60138, (1, 0x28)
378f4: lea -0x23b(%rip),%rcx # 0x376c0 PKCodecFactory_CreateCodec
378fb: lea -0x212(%rip),%rdx # 0x376f0 PKCodecFactory_CreateDecoderFromFile
37902: lea -0x109(%rip),%rsi # 0x37800 PKCodecFactory_CreateFormatConverter
37909: mov %rcx,(%rax)
3790c: mov %rdx,0x8(%rax)
37910: lea -0x77(%rip),%rdx # 0x378a0 PKCreateCodecFactory_Release
37917: mov %rsi,0x10(%rax)
3791b: mov %rdx,0x20(%rax)
Nothing is stored at +0x18 (378f4–3791b above). The allocator gets (1, size), presumably jxrlib's calloc, so that slot is presumably zero. I named the four slot targets by matching their addresses against the module's export table (G+lqj7diiFM, nSR7deGcfPw, -sUOc+vW9iw, xTVG4ZQMwVk).
In this PR Jxr::CodecFactory is 32 bytes (Factory.hpp, static_assert(sizeof(CodecFactory) == 32)), and release sits at +0x18. A title or SDK module built against Sony's header calls pCodecFactory->Release(&pCodecFactory) through +0x20. Here that reads the 8 bytes right after the 32-byte calloc block and calls them. On the console it's PKCreateCodecFactory_Release.
Suggested fix: add an 8-byte field at +0x18 before release and make the struct 40 bytes, with static_assert(offsetof(CodecFactory, release) == 0x20). Then have the test check that +0x18 stays zero. I don't know what Sony's fourth slot is, and the constructor above doesn't set it. A void* the code leaves null would match what I read.
What matches (same module, same reading):
PKCreateFactory(0x37650) allocates0x20and storesPKCreateFactory_CreateStream(0x375e0) at+0, an unexported function at+8,CreateWS_Memory(0x34f80) at+0x10andPKCreateFactory_Release(0x37620) at+0x18. That is yourFactory. The+8target is presumablyCreateWS_File; I didn't read it, and your throwingCreateStreamFromFilenamethere is fine.PKCreateFactory_CreateStream(0x375e0) allocates0x18, which matches your 24-bytePlainStream. It returns-101(0xffffffffffffff9b) when the allocation fails, as all four constructors do.PKCodecFactory_CreateFormatConverter(0x37800) allocates0x60and fills the slots in your order:Initialize,InitializeConvert,GetPixelFormat,GetSourcePixelFormat,GetSize,GetResolution,Copy,Convert(0x4b740), and thenRelease(0x4b750) at+0x40.
What I ran: this head merged onto main (merge d19ea27c68), Windows 11, WinLibs GCC, Release. guest_jxr_factory and public_headers pass (2 of 2). The test reads release through the PR's own struct (tests/JxrFactory.cpp:121 and :150), which can't show the console's offset.
Small note: my #15626 gives PKFormatConverter_Convert_nid_postfix its jxrlib signature. Your Slot<decltype(value->convert)>(&PKFormatConverter_Convert_nid_postfix) is a reinterpret_cast (Export.cpp, Slot), and I expect it to build against either signature. Whichever of us lands second should rebuild to confirm.
XairRrR
left a comment
There was a problem hiding this comment.
Reviewed — comments
The seven factory exports follow jxrlib's JXRGlue.c exactly, and the test passes and fails when allocation-failure handling is broken; the open question is the console layout of PKCodecFactory that BunnyFl raised, which I could not verify.
What I ran
Head 0d783107d35b4240a248eff9db145726f4b272d0 merged into main 1c8c0efc8c41314402e6966da5bef6ea3d639d10 (merged tree 0156b3beef9b31462b656c7cbb8731b60793d37d), Debian 13 VM, GCC 14.2.0, Release, Ninja.
git diff --check 1c8c0efc8c41314402e6966da5bef6ea3d639d10...0d783107d35b4240a248eff9db145726f4b272d0
python3 tools/check_conventions.py --base 1c8c0efc8c41314402e6966da5bef6ea3d639d10
ctest --test-dir build --output-on-failure -R "^guest_jxr"
# negative control 1: Export.cpp and include/Api.hpp from main, Factory.hpp kept
ninja -C build -j32 -l32 guest_jxr_factory_tests
# negative control 2: merged files, AllocateZeroed made to always return WmpErrSuccess
ninja -C build -j32 -l32 guest_jxr_factory_tests
ctest --test-dir build --output-on-failure -R "^guest_jxr_factory$"
# restore merged files
ninja -C build -j32 -l32 guest_jxr_factory_tests libSceJxr
ctest --test-dir build -R "^guest_jxr_factory$"
git status --porcelain
Results
| Check | Result |
|---|---|
git diff --check |
clean |
check_conventions.py |
All conventions checks passed |
| Build (script) | configure, build and libs OK |
| Script test run | 822 ran, no new failures or timeouts; unwrap_self_UnwrapTest_test_new_output_inherits_the_destination_directory_group also fails on main (known) |
guest_jxr* (10 tests incl. guest_jxr_factory) |
10/10 pass, guest_jxr_factory 0.01 s |
| Negative control | fails in both forms (see below) |
| Worktree after restore | git status --porcelain empty |
| Validation layer | not applicable (no AGC/shader/VideoOut change) |
Negative control
-
With only
Export.cppandinclude/Api.hpptaken frommain, the test doesn't compile.maindeclares these exports as parameterlessintplaceholders:excerpt
JxrFactory.cpp:91:40: error: too many arguments to function 'int PKCreateFactory_nid_postfix()' JxrFactory.cpp:105:73: error: too many arguments to function 'int PKCreateFactory_CreateStream_nid_postfix()' JxrFactory.cpp:117:45: error: too many arguments to function 'int PKCreateCodecFactory_nid_postfix()' -
That only shows the signatures changed, so I also ran a behavioural mutation. In the merged
Export.cpp, I changedAllocateZeroedto returnWmpErrSuccesseven whencallocfails.guest_jxr_factorythen failed (SEGFAULT on the allocation-failure path). -
After restoring the merged files and rebuilding, the test passes again and the worktree is clean.
Findings
Blocking
None of my own. BunnyFl's change request (PKCodecFactory is 40 bytes on the console, with Release at +0x20) has no reply yet; see Non-blocking for what I could add.
Non-blocking
- BunnyFl's
PKCodecFactorylayout finding: more evidence, still open.core/libs/prx/libSceJxr/Factory.hpp:29-34and:51match the pinned jxrlib (3rdparty/jxrlibatf7521879862b9085318e814c6157490dd9dbbdb4) exactly. In itsJXRGlue.h(lines 351-361),tagPKCodecFactoryhas four function pointers:CreateCodec,CreateDecoderFromFile,CreateFormatConverter,Release. That is 32 bytes.PKFactory,PKStream(24 bytes) andPKFormatConverter(96 bytes,pDecoderat 72) also match the header.- So the PR is faithful to the reference. If the firmware disassembly in that review is right, Sony's header has a fifth slot at
+0x18, and the reference cannot show it. - The test reads
releasethrough the PR's own struct (tests/JxrFactory.cpp:121,:150), so it cannot tell the two layouts apart. - I have no firmware module here, so I can neither confirm nor refute the disassembly. As described, a guest built against Sony's header would call 8 bytes past the 32-byte block. The author should answer that review, or adopt the 40-byte layout, before merge.
- So the PR is faithful to the reference. If the firmware disassembly in that review is right, Sony's header has a fifth slot at
- Wrong line range in the debt entry link.
docs/dev/debt/libSceJxr.md:17linksJXRGlue.c#L334-L480. At that commit,PKCreateFactory_CreateStreamstarts at line 333 andPKCreateCodecFactoryends at line 482. Suggest#L333-L482. - The known-IID paths are only checked by compiling.
core/libs/prx/libSceJxr/Export.cpp:151-156callPKImageEncode_Create_WMP_nid_postfix/PKImageDecode_Create_WMP_nid_postfixthrough a cast toint64_t(*)(void**). Both are still parameterlessintplaceholders that throw, so IIDs 101 and 201 currently end inNotImplemented.- The test only covers unknown IIDs. It's acceptable while those exports throw.
- When they get their real signatures, drop the casts so the compiler checks the call. Also add a test that IID 101/201 dispatches.
- Two
if(BUILD_TESTING)blocks.core/libs/prx/libSceJxr/CMakeLists.txt:73-84opens a secondif(BUILD_TESTING)block right after the existing one. The registration could go inside the existing block. Cosmetic.
Not verified
- The console layouts and slot order: no firmware module or PS5 was available. BunnyFl's disassembly is unconfirmed by me.
- Windows/MinGW build of the new test executable; only Linux GCC was built here.
- I reran the
guest_jxrandpublic_headerstests in the background; all 75 passed. That run overlapped the negative-control edits, so I don't count it. I rely on the script's full run of the merged tree, which reported no new failures. - No title was run.
To get this to a good state
- Answer BunnyFl's layout review. If you accept the console disassembly, make
Jxr::CodecFactory40 bytes withreleaseat0x20and assert it withoffsetof. Have the test check that the+0x18slot is zero. Update the debt entry to say which layout follows the console and which follows jxrlib. - Fix the line range of the
JXRGlue.clink in the debt entry.
✅ AMD GPU test: passedPR head
Unstable on main (2, these also failed in recent main runs on this machine; not counted against this PR)Failing on main too (3, not counted against this PR)Validation IDs already present on main (8 IDs in 467 tests)Skipped (7)ReproduceAutomated build and test on a contributor's AMD machine; it does not replace upstream CI. A new comment is posted for each new push. Full logs. |
… notice Apply review request from 2026-11-05, with a merge of main. CodecFactory now has the empty slot before Release, so Release sits at offset 0x20 in a 40-byte block as the firmware allocates it. initializeConvert takes the source pixel format GUID by value, as JXRGlue.h declares. Export.cpp and Factory.hpp carry the Microsoft notice from jxrlib JXRGlue.c, lines 1 to 27 at the pinned revision.
|
Hi Thanks for the JXR factories. Three things changed on your branch so it can land, on top of |
# Conflicts: # core/libs/prx/libSceJxr/CMakeLists.txt # core/libs/prx/libSceJxr/Export.cpp # core/libs/prx/libSceJxr/include/Api.hpp # docs/dev/debt/libSceJxr.md
# Conflicts: # docs/dev/debt/libSceJxr.md
BunnyFl
left a comment
There was a problem hiding this comment.
@simnJS re-checked at bfc31e1c4e, and my change request is resolved (cc @oneandonlydean, thanks for pushing the fix in 25f5019918).
The codec factory now matches what PKCreateCodecFactory allocates in firmware 13.42. Jxr::CodecFactory has an empty void* reserved at +0x18, and release sits at +0x20 in a 40-byte struct. core/libs/prx/libSceJxr/Factory.hpp:79 asserts both (sizeof == 40, offsetof(release) == 0x20). The test now reads the raw slots, so it no longer depends on the PR's own struct: tests/JxrFactory.cpp:122 requires slot 3 to be null and slot 4 to be Release. That is the layout from my disassembly at 0d783107d3.
I didn't check the other change, initializeConvert taking the GUID by value, against JXRGlue.h myself.
What I ran
Windows 11 Pro 10.0.26300, WinLibs GCC 15.2.0, Release, this head merged onto current main (merge 22edf6e602):
guest_jxr_factoryandpublic_headers: 2 of 2 pass.- Mutation, on the same kind of merge (
43cadd451b):PKCreateCodecFactoryalso writesReleaseinto the reserved slot.guest_jxr_factorythen aborts (0xc0000409), 0 of 1 pass. So the new check catches a factory that fills+0x18. tools/check_conventions.py --base upstream/main --headon this head: all checks pass.tools/pr_overlap.py --branch: the confirmed line conflicts arelibSceJxr/CMakeLists.txtL121 (#15148, #15479, #15689), the debt entry list at L38-39 ofdocs/dev/debt/libSceJxr.md, andinclude/Api.hppL68-74 with my #15626. That last one is thePKFormatConverter_Convertsignature I mentioned at0d783107d3; whichever of us lands second rebuilds. Its "same exports" rows (Allocate,Align,Callocand so on) are the test's local heap callbacks, not exports.
Still open from XairRrR's earlier review, non-blocking. At bfc31e1c4e, the debt entry's JXRGlue.c link still reads #L334-L480 (docs/dev/debt/libSceJxr.md:39).
AI-assisted: yes (Claude Code)
|
Hi @simnJS, thanks for merging main. We merged the latest main on top (merge commit c4ad4c7) to clear the conflict in |
|
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
|
|
Hi @simnJS, main had moved again, so we merged the latest main on top of your head c4ad4c7 (merge commit b42c6a7, no force-push). The CMake source and test blocks from both sides are kept, your Factories entry and main's new Predict entry in |
|
Hi @simnJS, we merged main into your branch once more (21928d3), because #15148 landed a JPEG XR bit-reader change that conflicted with this PR. In |
What
Implements seven libSceJxr factory exports on the pinned jxrlib:
PKCreateFactory,PKCreateFactory_CreateStream,PKCreateFactory_Release,PKCreateCodecFactory,PKCreateCodecFactory_Release,PKCodecFactory_CreateCodec(an unknown IID returnsWMP_errUnsupportedFormat) andPKCodecFactory_CreateFormatConverter. The function slots hold guest-ABI pointers.PKCreateFactory_CreateStreamallocatessizeof(PKStream), 24 bytes, asjxrgluelib/JXRGlue.cdoes:PKStream(JXRGlue.h) is a separate structure from the 88-byteWMPStreamthatCreateWS_FileandCreateWS_Memorycreate.CreateStreamFromFilename,PKCodecFactory_CreateDecoderFromFileand thePKFormatConverter_*andPKImageDecode_*functions reached through the slots stayNotImplemented_nid_no_patch; the debt file records it.Tested
Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, Release:
guest_jxr_factorypasses (creation and release, slots called, unknown IID -106, allocation failure -101, null pointers). Only this test was run, not the full suite. Not run on Linux or on a title.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)