fix(libSceAjm.native): guard LibAtrac9 BitReverse32/SignExtend32 against undefined shifts in the ATRAC9 decoder #9548
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9548"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-22T01:39:30Z.
What
Building the full (non-relinker) tree with UndefinedBehaviorSanitizer and running any
ATRAC9 path aborts in the bundled LibAtrac9. Every ATRAC9 decoder init hits a
shift exponent 32 is too large for 32-bit typeinBitReverse32, reached throughGenerateShuffleTable(0)during MDCT-table setup. Both shifts are undefined behaviourthat is benign on a normal x86-64 build (the result happens to be correct), but UBSan
aborts the process when
-fno-sanitize-recover=undefinedis set.This PR guards the two undefined shifts in
3rdparty/LibAtrac9/C/src/utility.cthroughcore/libs/prx/libSceAjm.native/PatchLibAtrac9.cmake, in the samefile(READ)/string(FIND)guard /string(REPLACE)/file(WRITE)style already used there forunpack.c,bit_reader.canddecinit.c.utility.cis now built from the patchedcopy (
CMakeLists.txt), and added toCMAKE_CONFIGURE_DEPENDSso the patch re-runs whenupstream changes. The
string(FIND)guards fail the configure with a clear message ifthe upstream code drifts.
The two bugs
1.
BitReverse32— shift by 32 (utility.c:14)decinit.cbuilds the shuffle tables for sizes1 << 0 .. 1 << 8, soGenerateShuffleTable(0)callsBitReverse32(0, 0), which evaluatesvalue >> 32on a32-bit
unsigned int(undefined). On x86-64 the shift count is masked to32 & 31 == 0,so the result is the correct bit-reverse of
0, i.e.0; no functional impact on anormal build. This fires on every ATRAC9 decoder initialization.
Fix (
BitReverse32of any value withbitCount == 0is0):2.
SignExtend32— signed left-shift overflow (utility.c:20)Left-shifting a signed
intinto/over the sign bit is undefined. This was previouslymasked: the process always aborted earlier at
BitReverse32during init, before anyframe was decoded, so
SignExtend32was never reached under UBSan. OnceBitReverse32is guarded, the decoders that actually decode audio (
guest_ajm,ngs2_atrac9) reachthis shift and UBSan reports
left shift of N places cannot be represented in type 'int'.Fix (perform the shift on the unsigned value — defined wraparound — then reinterpret; the
subsequent arithmetic right shift of a signed value is implementation-defined, not UB, so
UBSan is satisfied and the sign-extension result is unchanged on normal builds):
Sanitizer output (before, from
guest_audiodec)Tested
Red → green
Environment: Ubuntu 26.04, GCC 15.2.0,
-DCMAKE_BUILD_TYPE=RelWithDebInfo, full build(not
ANYPS5_RELINKER_ONLY),-DANYPS5_ENABLE_ASAN=ONplus-fsanitize=undefined -fno-sanitize-recover=undefined -fno-sanitize=vptr(C++) and-fsanitize=undefined -fno-sanitize-recover=undefined(C).Before the fix, the four ATRAC9 tests all abort at
utility.c:14:After the fix, all four pass with no UBSan error:
Guarding only
BitReverse32makesguest_ajmandngs2_atrac9then abort atutility.c:20(theSignExtend32UB described above); guarding both makes the wholeajm/audiodec/ngs2/atrac9set green under UBSan (19/19). The normal build(no sanitizers) still compiles and those four tests pass.
Minimal reproduction (bug 1)
AI-assisted (Claude Code); the builds and tests above were run on my machine.
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)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T01:47:46Z.
Review: CHANGES_REQUESTED
The sanitizer diagnosis and the guarded patch approach are clearly described, and the two replacements match the exact upstream snippets they intend to change. However, the current hosted Conventions check fails with three required-template errors: sections
What,Tested, andChecklistare missing or empty. Please move the existing summary and test evidence under the repository template headings and fill out the checklist, including theAI-assisted: yesitem, then rerun conventions. The other current policy checks pass; product and relinker jobs were skipped by path policy. I have not run a local build or sanitizer suite; the 19/19 UBSan results are author-reported.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T01:53:50Z.
Review: CHANGES_REQUESTED
The current Conventions run still fails with three required-template errors: sections
What,TestedandChecklistare missing or empty. Please move the existing summary and sanitizer evidence into the required### Whatand### Testedsections, restore the full checklist from the repository template, and complete every item includingDepends on,Unblocks,GPUs testedandAI-assisted. The current body contains useful UBSan evidence, but the project checker cannot recognize it in the present free-form layout.Hosted eligibility, intake, labels and report checks pass. The required Conventions check fails; product/relinker jobs are skipped, and I did not run local tests. The reported UBSan results are author-reported.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T01:53:51Z.
The newest Conventions run still reports
What,TestedandChecklistas missing or empty. Please preserve your detailed sanitizer report but put it under the required template headings and fill every checklist line, including AI-assisted and applicable dependencies/unblocks/GPU fields. Eligibility, intake, labels and report pass; product/relinker jobs are skipped.Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-22T04:28:29Z.
Thanks, moved the sanitizer report under
What/Testedand filled every checklist line (Depends on, Fixes, Unblocks, GPUs tested, AI-assisted). Same for #9570.Imported from boykopovar/AnyPS5. Originally posted by fogydes on 2026-10-22T08:12:56Z.
The Conventions check fails on the head of this PR (job 119589122291):
Fill in the pull request template: section 'What' is missing or empty, and the same forTestedandChecklist.The relinker, relinker-asan, register-context and write-watch jobs all pass on the same commit, and
python3 tools/check_conventions.py --base origin/mainprints the same three template findings locally. Filling in every line of.github/pull_request_template.md(What, Tested, Depends on, Unblocks, GPUs tested, AI-assisted, and tick the boxes) is all that appears to block CI.Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-22T08:27:38Z.
Thanks. That job ran on the old body; the body now follows the template and Conventions passes on the same head: https://github.com/boykopovar/AnyPS5/actions/runs/39722568967/job/119639159960
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T11:30:35Z.
The template update is now reflected by a passing hosted conventions check. I ran the PR CMake patch script against its pinned LibAtrac9 submodule and compiled the generated
utility.cwith UBSan; a local boundary harness passedBitReverse32at bit counts 0–32 andSignExtend32across boundary patterns 1–32. The 19/19 full sanitizer results remain author-reported. I checked the #6044 overlap manually: itslibSceAjm.native/CMakeLists.txtchanges are in the test-registration block, while this PR changes configure dependencies and theatrac9source list; the combined branch merge is text-clean and no ordering dependency appears necessary. Current main is4777caca34; this head is 1 ahead/116 behind (merge base94fbb8cea). Diff-check and conventions pass, and the merge with main is clean. Please update to current main and rerun the four ATRAC9 UBSan tests before merge. Product builds are skipped; I did not run the full runtime build locally.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T11:30:37Z.
Review: DISMISSED
The formatting issue from my prior review is resolved. The source patch and updated CMake wiring look coherent; locally I ran the PR patch script against the pinned LibAtrac9 submodule and UBSan-tested the generated
utility.cboundary cases successfully. The reported full sanitizer suite is author-reported. The #6044 CMake overlap is in a separate test-registration block and merges text-cleanly, so I found no sequencing requirement. I am retaining changes requested for the repository freshness requirement: this branch is 1 ahead/116 behind current upstream/main4777caca34(merge base94fbb8cea). Please update and rerun the four ATRAC9 tests under UBSan on the resulting head. Diff-check and conventions pass on the current head; product builds are skipped.Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-22T12:53:07Z.
Rebased onto current
main(8b3a6ba88d, which is past4777caca34). New head:fcf6dece99(1 commit, diff unchanged).Rerun on the new head: Ubuntu 26.04, GCC 15.2.0, fresh full build (not
ANYPS5_RELINKER_ONLY):The wider
ajm|audiodec|ngs2|atrac9set is also green under UBSan (18/18, excludingatrac9_gradient).Side note, not caused by this PR: with these sanitizer flags
tests/atrac9_gradient_testsfails to link (undefined reference to symbol 'pow@@GLIBC_2.29', DSO missing). The test includesunpack.c, which pullsband_extension.c.ofromlibatrac9.avia theBex*tables, and that target does not linkm(unlikeatrac9_config_data_tests). This PR does not touch that target,unpack.corband_extension.c; the plain Release build links it fine.Written with AI assistance (Claude Code).
I ran the builds and tests above on my machine.