feat(libc): implement qsort_s #6707
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6707"
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 karimibrahim07 on 2026-10-16T05:41:38Z.
What
qsort_s(C11 Annex K,G7yOZJObV+4) was missing. The Quake homebrew'slibOSMesa.so.8imports it (#6698), and with #6697 and #6702 it is that title's last unresolved import.qsort_s(base, n, es, cmp, context)follows FreeBSD 13'sqsort_s. FreeBSD 11, which the rest of libc follows, has none.qsort(FreeBSD's Bentley–McIlroy quicksort) and passescontextas the comparator's third argument (CMP(t, x, y)iscmp(x, y, t), L76-L77). The sort helpers now take any comparator, so both functions share the code;qsortis unchanged.EINVALafter calling the constraint handler with FreeBSD's message whennoresexceedsRSIZE_MAX, or whennis not 0 andbaseorcmpis null (L111-L127). Otherwise it returns 0.The handler is the one
SonyCrt.hppkeeps per source file. A handler set withset_constraint_handler_s(inWideConversion.cpp) is therefore not the one called here, as for the other_sfunctions outside that file; the debt entry says so.Tested
Windows 11 Pro (26300), WinLibs GCC 15.2.0 r7, Ninja Release,
BUILD_TESTING=ON, onmain5d9dcc42d:guest_qsortpasses. The new cases check thatqsort_s:qsortin the insertion-sort and median-of-three casesn == 0with a null base and a null comparatorOn
mainthe test does not link (qsort_s_nid_postfixis undefined).The full suite passes, 1300 tests. One
conventions_function_bodiesshard timed out while the machine was busy and passed alone.python tools/check_conventions.py --base origin/mainpasses.Merges cleanly with the open PRs that touch
FileAndHeap.cpp,docs/dev/debt/libc.mdorGuestQsort.cpp. The debt entry sits afterset_constraint_handler_sbecause #6144 and #6322 edit the lines on both sides of theqsortentry.The Quake homebrew (#6698), with #6697, #6702 and this PR, relinked with
--windows --to-intel --trap-sdk-syscall-stubs --registry: nounresolved ELF importis left, and the title runs into its own code. It then stops withapplication heap: process metadata is missing; relink the executable, because a payload ELF has noPT_SCE_PROCPARAMsegment and so no allocator table. That is a separate change.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 github-actions[bot] on 2026-10-16T05:42:18Z.
Progress report (
5d9dcc4→ceec3da)➖ System libraries: 88.83% (+0.0%, +1 implemented functions)
✅ 1 implemented
qsort_s_nid_postfixImported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-16T05:58:43Z.
One contract issue to consider:
SonyCrt.hppputsSonyConstraintHandlerSlot()in an unnamed namespace, soFileAndHeap.cppandWideConversion.cppeach get a separate handler slot. That means a handler installed throughset_constraint_handler_sis not called forqsort_sruntime-constraint violations; this PR invokes the default handler instance fromFileAndHeap.cppinstead. Annex K specifies that these violations call the currently installed constraint handler (C11 draft K.3.6.3.2). Could the handler state be shared across libc translation units, and could a regression test set an ignoring/counting handler then trigger aqsort_sviolation? The debt note documents this current limitation, but for this new function it means callers cannot use the handler they registered.Imported from boykopovar/AnyPS5. Originally posted by UmillD on 2026-10-16T05:58:48Z.
Independent focused Windows validation on exact head
edf4efe360e530f7f3d023889851a2b7124fe798, WinLibs GCC 15.2.0 r7, Ninja Release:guest_qsortCTest passed 10 consecutive runs.APS5_VABIpassed 1,728 combinations of element size/count, unaligned starting offsets, input distributions and ascending/descending context. It checked complete-record preservation, surrounding sentinels, callback addresses/context, and comparison counts/traces.qsortmatched the main-production-libc control;qsort_smatched the candidate'sqsortfor every input and trace. The control build includes test-only fixture changes, with no changes incore/libs/prx/libc. Zero-count/null arguments, zero element size and the one-element/no-callback case also passed.I also reproduced the already documented constraint-handler limitation. With a returning handler installed through
set_constraint_handler_s,wcscpy_s(nullptr, 1, nullptr)returned 22 and invoked it once. The four invalidqsort_scases (count/size above RSIZE_MAX, null base or null comparator with nonzero count) instead reached SIGABRT. The harness intercepted the signal in child processes. This confirms the limitation noted in the PR; it is not a new finding, and I am not claiming those EINVAL-return paths pass.No additional regression was found within this scope. I did not run Quake, Linux/macOS or the full suite, and this does not verify the console's qsort_s contract. Standalone harness sources and logs are retained locally and can be shared if useful.
AI-assisted: yes, investigated with Codex.
Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-16T06:44:36Z.
Review: COMMENTED
Native Linux run on exact head
edf4efe360e530f7f3d023889851a2b7124fe798, applied onmain5d9dcc42d.Machine: Intel i7-8700 (6C/12T), Ubuntu 26.04, GCC 15.2, Ninja Release,
BUILD_TESTING=ON, full build.guest_qsortpasses, and 10/10 with--repeat until-fail:10.ctest -E "^agc|gpu|vulkan|video_out"): 648/650, same asmain. The two failures (file_writer_binary,file_writer_text) also fail onmainon this machine.main,guest_qsort_testsdoes not link (undefined reference to qsort_s_nid_postfix), as the description says.python3 tools/check_conventions.py --base main: passed.I checked the semantics against the sources:
(x, y, context), no sort and a nonzero return on a violation, and the same four constraints (nmemb/size>RSIZE_MAX, nullbaseorcomparwhennmemb != 0). They match.EINVALandRSIZE_MAX = SIZE_MAX >> 1match FreeBSD 13.0'sqsort_s.src/util/u_qsort.hcallsqsort_s(base, nmemb, size, compar, arg)with the context last outside_WIN32("C11 added qsort_s"). It uses an adapter only for the MSVC/MinGW order, where the context comes first. A non-Windows build that importsqsort_s, and so found no GNU or BSDqsort_r, takes that C11 branch. It passescomparunchanged, so the comparator is called ascmp(x, y, context), which is what this PR does. I have not looked at the binary itself.I have nothing to add on the constraint-handler point. It is already raised above and recorded in the debt entry.
Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-16T06:49:08Z.
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.
main)core/libs/prx/libc/include/SonyCrt.hppL23-25Some 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.
docs/dev/debt/libc.mddocs/dev/debt/libc.mddocs/dev/debt/libc.mdcore/libs/tests/GuestQsort.cppdocs/dev/debt/libc.mddocs/dev/debt/libc.mddocs/dev/debt/libc.mddocs/dev/debt/libc.mddocs/dev/debt/libc.mddocs/dev/debt/libc.mdcore/libs/prx/libc/include/SonyCrt.hppcore/libs/prx/libc/src/FileAndHeap.cppdocs/dev/debt/libc.mdcore/libs/prx/libc/src/FileAndHeap.cppcore/libs/prx/libc/src/FileAndHeap.cppdocs/dev/debt/libc.mddocs/dev/debt/libc.mddocs/dev/debt/libc.mddocs/dev/debt/libc.mdImported from boykopovar/AnyPS5. Originally posted by karimibrahim07 on 2026-10-16T06:52:08Z.
@MarcZX100 Fixed in
ceec3da0a.SonyConstraintHandlerSlot()and its mutex are no longer in the unnamed namespace, so libc has one handler for all its source files. The handler type is nowAPS5_VABI: the handler a title installs, and the default one it gets back fromset_constraint_handler_s, are guest functions. They were called with the host calling convention before. Each module still keeps its own state, as theset_constraint_handler_sdebt entry says; that sentence now also says every_sfunction in a module calls the installed handler.guest_qsortnow installs a counting handler throughset_constraint_handler_s. It checks that the fourqsort_sviolations return 22 and call it with FreeBSD's messages, and that the handler comes back when the previous one is restored. WithSonyCrt.hppfrom the previous head the test aborts, as @UmillD saw. The full suite passes, 1300 tests (oneconventions_function_bodiesshard timed out on the busy machine and passed alone). Thanks to both of you for the review and the testing.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-16T08:48:23Z.
Thanks, that addresses both points I raised: libc source files now share one handler slot, and the handler uses the guest ABI. The new test exercises registration, restoration, and the
qsort_sviolations. I have no further request on this review; the latest CI checks pass.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-19T09:48:47Z.
Checked
qsort_sagainst FreeBSD 13 (qsort.cL97-L127): shared sort helpers, context passed as the comparator's third argument,EINVALplus constraint handler on the four violations. The handler slot is now shared across libc source files with the guest ABI, andguest_qsortcovers order, context,n == 0,es == 0and each violation through an installed handler (it does not link onmain). Independent Windows and Linux runs in the thread agree. Merged.