fix(libc): sort like the console's qsort and qsort_s #9688
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9688"
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 JJenkx on 2026-10-22T08:32:27Z.
What
qsortandqsort_snow sort the way thelibc.prxthat titles ship does. Onmainthey followed FreeBSD 11's Bentley-McIlroy quicksort; the debt entry already said that "the console's libc uses this FreeBSD code is unverified". It doesn't.The console's
qsort(AEJdIVZTEmo) is a 30-byte wrapper: a null array or comparator returns at once, otherwise it calls an internal sort with(base, n, ideal = n, size, cmp).qsort_s(G7yOZJObV+4) has its own copy of the same sort with a context. That sort is an introsort in the shape of Dinkumware'sstd::sort:(n - 1) / 8 + 1. The median of three always makes three comparisons. It recurses on the smaller side, the lower one when the sides are equal, and loops on the other.nand is cut tobudget / 2 + budget / 4at each partition. Sift-down always goes to a leaf, followed by a sift-up.So on
main, elements that compare equal often ended in a different order than on the console, and the comparator saw a different sequence of calls. Whatever a title's sort order affects (draw lists sorted on equal keys, for example) could differ.How it was checked
I read the code from the disassembly of the decrypted copies of PPSA13934 (internal sort at 0x62130), PPSA07923 (0x64210) and PPSA05916 (0x67960). Then I checked it by running those copies directly: a scratch loader maps the ELF, applies its relocations and calls the internal sort,
qsortandqsort_swith a logging comparator. The same runs then go through the port, and the comparator call sequences and the resulting bytes are compared.FileAndHeap.cpp):ideal, to force the heap sort.main's sort against the console's on 10,022qsortsorts: the final order differs in 8,340 and the comparator sequence in 9,898.Tested
Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 r7 (UCRT), Ninja, Release,
-DBUILD_TESTING=ON,main7e2ba73e4c.guest_qsort:qsortandqsort_s;qsort_szero-size case now uses a buffer as large as anElement, because its comparator is now called.mainthe new checks fail, starting withinsertion sort below 33 elements: element 0 has tag 11, expected 3, and the null array throws. With this change it passes three times out of three.ctest -j16 --timeout 120: 1889 of 1890 pass.guest_raise_in_host_codetimes out, as onmainhere (#9610).python tools/check_conventions.py --base origin/main: passes.tools/pr_overlap.py --branch: text conflicts inlibc.mdonly. One is my own #9613, whoseqsortsentence this entry replaces; I'll drop it there. The other is #9087, two lines below in theqsort_sentry.b8247ed3bmerged with--no-commitonto main7e2ba73e4.guest_qsortandpatched_libraries, 2/2 pass.FileAndHeap.cppwith this PR's tests fails the insertion-order check for bothqsortandqsort_s.guest_qsortpasses without reports for the 12-, 48- and 1,000-element and null/zero-size cases. This is focused test coverage, not a full Linux suite or title run.Linux reviewer follow-up (2026-10-25)
Refreshed to main
cceb6c3c41eb6dafd4225562fd09cee8820ad00b, head03874cf1c5e43c18969364be032dc1e8371fe1c9. Addressed the reachable self-swap report:SwapElementsreturns immediately when its pointers are equal, before callingstd::swap_ranges. The complete sort helper block is otherwise byte-identical to the previous PR head; no comparator or partition traversal changes.guest_qsortfor bothqsortandqsort_s. It checks exact final tag order and comparator sequence (181 calls, pair hash78b091e1).ctest -R '^guest_qsort$' --repeat until-fail:3 -V: 3 passes. Conventions and diff checks pass; merge-tree with the stated main is clean.a9e64875954a84ddeac539042935f069f5b284800cdff27f2e47f152a585fd05. Original Windows/large reference matrices remain evidence for their original revisions and were not rerun; no new Windows/game/GPU/physical-console result is claimed.Current Linux qualification — 2026-10-29
Refreshed against main
5454cd2e455994d992aca39335ce60507eeda1eb.Linux qualification at
1c8ac1874c0ed5ccafef5112d59da6739d4a2fb6(Arch Linux 7.2.9, Ryzen 9 7950X, GCC 16.2.1, Ninja Release, BUILD_TESTING=ON):guest_qsort,guest_bounds_checking,guest_safe_errors,guest_crteach pass three consecutive runs (12/12, no skips). Thepublic_headersaudit passes at this exact head. All tracked public-header files match the pinned main; the affected test targets rebuild against those headers. Conventions and whitespace checks pass. The same focused groups passed three times on the pinned main.Evidence: immutable Linux qualification bundle. Its SHA-256 is
53b095da2f67f55b8aa56ba273681e0330fb39739be844458038dde410fb8cc4. No fresh Windows runtime execution is claimed; older measurements retain their recorded source pins.AI-assisted: yes (Codex).
Earlier qualification — 2026-10-30
Integrated current main
08a10d225306ad102b57408e2ef7566bcfb829b2into this existing topic atae15d0493797e9d40b2ab2eedbf202abf14c90e3. Current-main functions, fixtures, unrelated debt rows and exact dependency gitlinks are preserved; original topic functions and authored commits are retained.Rebuilt actual Linux libc and focused test binaries.
guest_qsort,guest_bounds_checking,guest_safe_errors,guest_crteach passed three consecutive runs (12 runtime passes), pluspublic_headers; conventions and whitespace checks pass. Pinned main passed all seven selected runtime suites plus the header audit three times (24 runs). This is focused Linux CPU qualification; prior Windows/reference evidence retains its original pins. No new Windows, console, SDK-module, title, GPU or performance claim. AI-assisted: yes.Evidence: https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/owned-math6-main08a10-linux-20261030.tar.gz (SHA-256 b984a6334dce65b5c9148e3791ba21605074a5e762ea0f07f96de2f38062ef6e).
Qualification update — 2026-10-30, repeat 2
Integrated pinned main
e471817c68d4e1b1e44b0ce369ed88bafe4ae8edat1f001f3f23c74bf5dee00803644f2f17c82ac994, retaining original topic commits as ancestors. Main qsort_r retains its full FreeBSD helper/comparator behavior under private Reentrant-prefixed names; both sort implementations and every main qsort_r fixture are retained. Exact rename-normalized helper proof is attached.Actual Linux libc/focused binaries rebuilt from this final head;
guest_qsort,guest_bounds_checking,guest_safe_errors,guest_crteach pass three consecutive runs (12 runtime passes), plus public_headers. Conventions against current origin/main and whitespace pass. Pinned main passed all seven runtime groups plus the header audit three times (24 runs). Full source/function/test/debt/header and 31 recursive dependency / 17,742 tracked-blob proofs are attached. Later main1c6b316cf762has 17 off-topic commits, no relevant libc/numeric/qsort/gitlink changes, and all 9 merge previews are clean; those preview trees were not executed.Focused Linux CPU scope only: prior Windows/SDK/reference reports keep original pins. The Linux portion makes no fresh Windows, console, SDK-module, title/GPU or speed measurement is claimed by this report. AI-assisted: yes. Evidence: https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/owned-math9-maine471-linux-20261030-repeat2.tar.gz (SHA256 5da4ce1732b45bb3fc566c31078c8c9ae8bf0c403654dfc60d15ee28490e20bf).
Actual Windows qualification at exact published
1f001f3f23c74bf5dee00803644f2f17c82ac994: Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2 UCRT r7, Ninja Release, BUILD_TESTING=ON. Rebuilt patched libc and guest_qsort_tests, then ran guest_qsort three times: 3/3 passes, zero skips. This checks the composed main qsort_r and branch sorting fixtures at the recorded head; no timing, SDK/game/GPU/console/macOS claim. Raw build/CTest/JUnit logs, DLL digest and terminal Windows FIFO receipt are included in https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/owned-current-windows-20261030-repeat2.tar.gz, SHA256 e423c97373758bf59239e02c138b74f35f1a45488793bc92878a4c836f94a584. AI-assisted: yes (Codex).AI-assisted: yes (Codex).
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); no silent stubs added.mdfiles or images added to the repositoryAI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T08:43:22Z.
Review: APPROVED
Reviewed the introsort/three-way partition, median-of-nine threshold and step, smaller-side recursion, heap fallback, insertion behavior, null/zero-size edge handling, and the added order/constraint tests. The reported firmware-side comparison methodology is unusually strong for this behavior-sensitive change. I independently ran the current-main conventions check and
git diff --check; both passed. The PR's author-reported Windowsguest_qsortand suite runs are clearly scoped, and the automatic hosted checks for this PR are green where they ran; Linux/Windows product jobs are skipped by policy. I attempted the documented manual build dispatch to run those suites, but GitHub returned 403 because this account lacks repository admin rights, so I could not start it. I found no code issue requiring changes.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T08:47:08Z.
Review: CHANGES_REQUESTED
I’ve noticed that holding this PR because of #5239 isn’t necessary. It’s fine for PRs to keep coming in; they’ll be handled in due course. Please disregard my earlier request to hold or defer it.
Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-22T08:59:10Z.
Thanks @MarcZX10, for the review and for checking the intake rule. I can't meet that rule yet. PPSA05916, PPSA13934, PPSA20423, PPSA26528 and PPSA34008 import
qsort(AEJdIVZTEmo) fromlibc.prx. But I have no run showing one of them misbehaving because of the order of equal elements, and an import alone isn't what #5239 asks for. I'm converting this to a draft. I'll mark it ready again when a title run on currentmainshows the difference, or when the queue is under the threshold.Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-22T09:01:47Z.
Review: COMMENTED
First run on Linux (the PR lists Linux as not tested). Ubuntu 26.04, glibc 2.43, x86_64 i7-8700, GCC 15.2.0, head b8247ed3b merged
--no-commitontoorigin/main7e2ba73e4.guest_qsort+patched_libraries2/2 pass.FileAndHeap.cppfrommain, test from the PR):guest_qsortaborts withinsertion sort below 33 elements: element 0 has tag 11, expected 3(and the same forqsort_s), as on Windows.-DANYPS5_ENABLE_ASAN=ON,-fsanitize=address,undefined -fno-sanitize-recover=undefined,-O1):guest_qsortpasses with no reports, so the new partition, heap fallback and swaps stay in bounds for the 12, 48 and 1,000 element cases and the null/zero-size ones.Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-22T12:29:45Z.
Rebased onto
main8b3a6ba88d(head 4f25234ada, no other changes). On Windows (UCRT GCC 15.2, Release),guest_qsortand the libc tests (7 tests) pass in three runs out of three.tools/check_conventions.pypasses. It stays a draft under the intake rule.Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-22T13:28:50Z.
Marked ready for review. This was a draft only because of the #5239 intake hold, which @MarcZX10 has withdrawn (https://github.com/boykopovar/AnyPS5/pull/9613#issuecomment-6337453028 and on #9596).
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T14:31:01Z.
Review: APPROVED
Correction: my earlier changes-requested review was based only on a mistaken queue hold tied to #5239. My review found no code blocker; PRs may continue to be submitted and will be handled in due course.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T06:27:38Z.
Review: CHANGES_REQUESTED
Re-checked — changes requested for integration
The author’s test-only follow-up adds the null-comparator/zero-size edge assertions and reports
guest_qsortpassing on the Linux exact head; the production implementation is unchanged from my prior review. I still find no code blocker in the reviewed algorithm.The branch is not mergeable with current main. The merge preview conflicts in
docs/dev/debt/libc.md, and bothFileAndHeap.cppandGuestQsort.cppchanged on main since this branch’s base. Please reconcile those changes on the current branch and rerun the focused qsort tests; the Windows evidence is pinned to the earlier production head. This is an integration conflict, not an intake or queue hold.The hosted policy, relinker, conventions, and report checks pass; Linux/Windows product jobs are skipped. I did not run the sort tests locally.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T13:37:43Z.
Review: APPROVED
Re-checked — new head, looks good
The new head is cleanly integrated with current
main, and the author supplied the requested exact-head Windows qsort rerun with a failing negative control.What I ran
1c8ac1874c0ed5ccafef5112d59da6739d4a2fb6against currentmaindfcc0e17cb254d08fec9ec120e362620795d59a0; the merge preview is clean.95137178d026with the PR file list; none of the PR files changed in that interval.git diff --check upstream/main...refs/remotes/review/pr-9688; it passed with no output.guest_qsortrerun and negative-control report. I did not run a local C++ compiler or qsort test.Results
main7a506cfabdacf52a5e8af6cc802a9c52e1cf4f4cguest_qsortUnedited command output
Negative control
Findings
Blocking
None found in the reviewed exact head.
Non-blocking
The Windows test and negative control are author-reported, not independently reproduced. The hosted macOS checks remain pending and Linux product tests are skipped.
Sources
Not verified
I did not independently run Linux, macOS, or console qsort tests. The earlier Linux report is from an older head; current-main changes since the reported integrated base do not touch the PR's files.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-30T04:10:28Z.
Review: APPROVED
Re-checked — new head, looks good
This exact head carries the reviewed qsort change through the current-main integration, with fresh focused Linux results and no remaining integration blocker.
What I ran
ae15d0493797e9d40b2ab2eedbf202abf14c90e3; baseupstream/main5ecef095b61e7a406b279c97896f3a8a6fbd6353.d41c67a124b550c3841f8df76943827d3f0632a6.08a10d225306ad102b57408e2ef7566bcfb829b2; none overlap this PR’s paths.Results
d41c67a124b550c3841f8df76943827d3f0632a6.All conventions checks passed.guest_qsort,guest_bounds_checking,guest_safe_errors, andguest_crteach passed three times, pluspublic_headers; the pinned main suites also passed. Exact-head reportUnedited local command output
git merge-tree --write-tree upstream/main upstream/pr/9688git diff --check upstream/main...upstream/pr/9688exited 0 and produced no output.python3 tools/check_conventions.py --base upstream/main --head upstream/pr/9688Not verified
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-30T12:32:22Z.
Review: APPROVED
Re-checked — new head, looks good
The exact head keeps the measured console ordering for
qsort/qsort_s, preservesqsort_r’s FreeBSD implementation, and merges cleanly with currentmain.What I ran
1f001f3f23c74bf5dee00803644f2f17c82ac994against fetchedupstream/main42d009f5f37c8f388b01fbfe38d2e704c792c46f.157b02c4f1803e9d2cd757677b9a805961203fc4; the livemain...headcomparison reports three changed files. I used fetchedmainfor the current-base checks below.dea0c2db93033c2c242c1a67879cb5fd0b394a85.Results
FileAndHeap.cpp,GuestQsort.cpp, anddocs/dev/debt/libc.md. The branch is 129 commits behind currentmain.dea0c2db93033c2c242c1a67879cb5fd0b394a85.All conventions checks passed.public_headersat this exact head; not independently run here.guest_qsort157b02crather than fetched currentmain, so it evaluated a stale, 100-file comparison and reported unrelated whitespace/convention findings. I did not treat those as findings on the live three-file diff. Its isolated snapshot also lackspr_abi_audit.pyandpr_test_quality.py; those two checks were unavailable there.Unedited output from current-base checks
gh api repos/boykopovar/AnyPS5/compare/main...1f001f3f23c74bf5dee00803644f2f17c82ac994 ...git merge-tree --write-tree 42d009f5f37c8f388b01fbfe38d2e704c792c46f 1f001f3f23c74bf5dee00803644f2f17c82ac994git diff --check 42d009f5f37c8f388b01fbfe38d2e704c792c46f...1f001f3f23c74bf5dee00803644f2f17c82ac994exited 0 and produced no output.python3 tools/check_conventions.py ...Findings
Blocking
None found in the reviewed head.
Non-blocking
The branch is behind
main, but the current-base merge preview is clean. Linux results are author-reported, and macOS, console behavior, and a current-head negative control were not independently tested.Sources
Not verified
I did not compile locally or independently run the C++ tests, macOS jobs, a physical-console test, or a current-head negative control. The reported CI and author runs are distinguished above from checks I ran locally.