Skip to content

feat(libSceJxr): implement bit I/O through the pinned jxrlib - #14916

Merged
oneandonlydean merged 7 commits into
boykopovar:mainfrom
Noxy-HNR:claude/jxr-bitio
Nov 5, 2026
Merged

oneandonlydean merged 7 commits into
boykopovar:mainfrom
Noxy-HNR:claude/jxr-bitio

Conversation

@Noxy-HNR

@Noxy-HNR Noxy-HNR commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

What

Sixteen libSceJxr bit reader/writer exports currently throw: peekBit16, flushBit16, getBit16, getBool16, getBit16s, getBit32, flushToByte, putBit16z, putBit16, putBit32, fillToByte, getBit16_S, getSizeRead, getSizeWrite, getPosRead and dquantBits.

They now call the non-ARM bit I/O in strcodec.c, built from 3rdparty/jxrlib, through a C bridge (src/NativeBitIo.c) on the existing guest Jxr::BitIo / BitIOInfo layout (48 bytes). No jxrlib logic is copied into core/.

The C++ adapters reject bit counts over 16 (15 for getBit16s, 32 for getBit32/putBit32), a putBit16z value wider than its count, an odd read wrap mask, a write position of 16 bits or more, a write pointer not aligned for a 16-bit store, and a null state or current pointer. getBit16_S is getBit16 followed by the existing readIS, as jxrlib's readIS_L1 is readIS.

Review fix 369b743519 defines the accepted boundaries that jxrlib leaves undefined in C. A zero-bit peek returns 0; zero-bit getBit16/getBit32 use the defined flushBit16(0) path and return 0. An empty store or byte fill at a word boundary leaves the state and buffer unchanged. Masked writes use unsigned masks and the dependency's putBit16z, in 16-bit parts for a 32-bit write. The unmodified strcodec.c and segdec.c are built with jxrlib's existing aligned-load branch through a per-file _M_IA64 definition. The implementation remains in the pinned submodule. Follow-up 99a3aaaf67 rejects odd byte write positions before changing state or memory; these otherwise reach an undefined U16 store in the dependency.

That the console's module uses these prototypes is inferred from the export names; nothing is checked on a console. The debt file records these inferred contracts and the defined boundary behavior that still needs console verification. No title unblock is claimed.

Tested

Latest independent verification at 99a3aaaf67: Windows 11 x86-64, WinLibs GCC 15.2.0, Release. Rebuilt guest_jxr_bitio_tests, guest_jxr_stream_tests and guest_jxr_tests; 3/3 passed.

Linux source-level sanitizer probe: Ubuntu 26.04, GCC 15.2.0, -fsanitize=undefined -fno-sanitize-recover=all, UBSAN_OPTIONS=halt_on_error=1, using the exact adapter, C bridge and pinned native bit functions. Zero-bit reads, word-boundary stores, unsigned masking, 20/32-bit writes and 2-byte-aligned read positions pass without a sanitizer diagnostic. The same probe using the pre-fix adapter/bridge from f3a9091e51 stops on the native shift by 32. The expanded probe also detects an undefined odd-position store at 369b743519; 99a3aaaf67 rejects that input without a sanitizer report. The Windows regression covers all four write/fill entry points at bit positions 0, 1, 8 and 15 and checks unchanged state and buffer after rejection. This is a focused source probe, not a full Linux guest-runtime run.

Earlier reported validation:

New guest_jxr_bitio test:

No console or retail-title test.

Checklist

Sixteen bit reader/writer exports (peekBit16, flushBit16, getBit16,
getBool16, getBit16s, getBit32, flushToByte, putBit16z, putBit16,
putBit32, fillToByte, getBit16_S, getSizeRead, getSizeWrite,
getPosRead, dquantBits) now call strcodec.c from 3rdparty/jxrlib
through a C bridge. The guest adapters only reject the arguments
jxrlib asserts on; getBit16_S adds the existing readIS. A new test
drives 256k random reads and writes on a ring buffer against jxrlib
directly and covers the rejected arguments.
@github-actions

github-actions Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Progress report (e9247a9 → e2a2929)

📈 System libraries: 27.33% (+0.04%, +16 implemented functions)

✅ 16 implemented
Library Name
libSceJxr dquantBits_nid_postfix
libSceJxr fillToByte_nid_postfix
libSceJxr flushBit16_nid_postfix
libSceJxr flushToByte_nid_postfix
libSceJxr getBit16_S_nid_postfix
libSceJxr getBit16_nid_postfix
libSceJxr getBit16s_nid_postfix
libSceJxr getBit32_nid_postfix
libSceJxr getBool16_nid_postfix
libSceJxr getPosRead_nid_postfix
libSceJxr getSizeRead_nid_postfix
libSceJxr getSizeWrite_nid_postfix
libSceJxr peekBit16_nid_postfix
libSceJxr putBit16_nid_postfix
libSceJxr putBit16z_nid_postfix
libSceJxr putBit32_nid_postfix

Changed libraries (implemented exports)

Library Before After Change
libSceJxr 23/136 (16.91%) 39/136 (28.68%) +11.76%

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Hi Noxy-HNR, thanks for the bit I/O bridge; keeping the implementation in the pinned 3rdparty/jxrlib submodule is the right placement. One problem: the new bridge still reaches undefined arithmetic on accepted boundary calls. At this head, peekBit16 accepts a zero bit count and forwards it to the pinned strcodec.c implementation, whose PEEKBIT16 shifts a 32-bit accumulator by 32. An allocated, aligned state with the exact new export and C bridge exits with a UBSan shift error for that call; the one-bit control passes. The debt entry already describes this problem, but recording it does not make the accepted path defined. Byte-aligned stores have a related boundary noted in that entry.

Please make the accepted zero-bit and alignment boundaries defined without copying the third-party algorithm into core, and add sanitizer coverage for those boundaries. Preserve the submodule policy when changing the dependency or bridge. Comparing the guest call against another execution of the same undefined native operation is not a reliable oracle. No console behavior or full decoder test is claimed by this check.

A landing note: the description says this depends on #14905, but the two do not merge together (and #14933 conflicts with this one in libSceJxr/CMakeLists.txt, Export.cpp, Api.hpp and the debt file). Each merges cleanly with main on its own, so whichever lands second will need main merged in.

…ined

Return 0 from zero-bit reads, skip empty stores at a 16-bit word boundary, mask putBit16 and putBit32 values before calling putBit16z instead of jxrlib's negative left shift, reject write positions past the word, and build strcodec.c and segdec.c with jxrlib's 16-bit load path so stream loads stay aligned. The test checks these boundaries against explicit values and passes under UBSan.
@Noxy-HNR

Noxy-HNR commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, fixed in 369b743519. The submodule is untouched and no jxrlib algorithm moved into core/:

  • Zero-bit peekBit16 now returns 0 without calling jxrlib. Zero-bit getBit16/getBit32 return 0 after jxrlib's flushBit16(0), which is defined.
  • A zero-bit store or fillToByte at a 16-bit word boundary (the only case where putBit16z shifts by 32) is a no-op. Other empty stores still go through jxrlib.
  • Running the test under UBSan found two more problems in jxrlib itself. putBit16 masks with ~(-1 << cBits), a left shift of a negative value, so putBit16/putBit32 now mask in the adapter and call putBit16z in 16-bit parts. And load4BE/_load4 read a U32 from 2-byte-aligned stream positions, so cmake/Jxr.cmake now builds strcodec.c and segdec.c with jxrlib's existing aligned 16-bit load branch (_M_IA64/_ARM_) as a per-file definition. MinGW compiles both files with it and no new warnings.
  • A write position of 16 bits or more now throws.
  • The test no longer compares these boundaries against jxrlib. They are checked against explicit state and bytes, and the random comparison only calls jxrlib on defined paths.

guest_jxr_bitio passes on Linux built with -fsanitize=undefined and UBSAN_OPTIONS=halt_on_error=1. Before the change it stopped on the shift by 32 and on the misaligned load. ctest -R guest_jxr is 10/10 on Linux and on Windows (WinLibs GCC 15.2.0).

On landing: I've changed the description to Depends on: none and noted that #14905, #14916 and #14933 conflict with each other, so whichever lands later gets main merged in.

@Noxy-HNR

Noxy-HNR commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

@oneandonlydean I independently rechecked 369b743519 and updated the description to match the current boundary handling.

  • Windows 11, WinLibs GCC 15.2.0, Release: rebuilt the bit-I/O, stream and existing JXR test targets; 3/3 pass.
  • Ubuntu 26.04, GCC 15.2.0: a source-level probe links the exact adapter, C bridge and pinned native bit functions with -fsanitize=undefined -fno-sanitize-recover=all and UBSAN_OPTIONS=halt_on_error=1. Zero-bit reads, word-boundary stores, masked 20/32-bit writes and 2-byte-aligned read positions pass.
  • The same probe with the pre-fix adapter/bridge from f3a9091e51 stops on the native shift by 32, so the check detects the original defect. This is a focused sanitizer probe, not a full Linux guest-runtime run.

I also made the integration order explicit in the descriptions and checklists: #14905, then #14916, then #14933. These are merge-order prerequisites because the branches conflict in shared files; the features have no runtime dependency on one another. Each later branch will be rebased and retested after its prerequisites merge.

@Noxy-HNR

Noxy-HNR commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

@oneandonlydean follow-up 99a3aaaf67 fixes one further boundary found while expanding the sanitizer probe: a byte pointer at an odd address still reached jxrlib's U16 store. The exact 369b743519 adapter/bridge/native path reports a misaligned store at strcodec.c:988 for an allocated buffer with an odd write position.

All four write/fill entry points now reject a pointer that is not aligned for a 16-bit store before changing the state or buffer. The regression covers bit positions 0, 1, 8 and 15, including empty stores, masked/unmasked writes, 32-bit writes and byte fills. The debt entry records this divergence from undefined native behavior; console handling remains unverified. The submodule and native algorithm are unchanged.

Windows 11, WinLibs GCC 15.2.0, Release: the rebuilt bit-I/O, stream and existing JXR tests pass, 3/3. The expanded Ubuntu 26.04/GCC 15.2 UBSan probe passes, including the odd-position rejection and previous zero-bit/alignment/masking cases. Conventions and whitespace checks pass, and the new head merges cleanly with main at 096c45570f.

@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Nov 5, 2026
@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Nov 5, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

Hi @Noxy-HNR, I merged main (55064c81fb, merge e2a2929475) into this branch so it can land, after your glue PR #14905 landed in libSceJxr. In CMakeLists.txt your src/BitIo.cpp, src/NativeBitIo.c and bit I/O tests sit alongside main's glue and quantizer sources and tests; in include/Api.hpp both sets of declarations are kept, and the conflicting placeholders in Export.cpp are gone because each side implemented entries the other still stubbed. The debt doc keeps all entries, with the count set to 97 (the stubs left in the merged Export.cpp). libSceJxr and the guest_jxr tests build and pass locally. No other changes.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Nov 5, 2026
@oneandonlydean
oneandonlydean merged commit 333f36b into boykopovar:main Nov 5, 2026
15 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.

2 participants