feat(libc): implement qsort_r #12845

Merged
heditrigui merged 2 commits from upstream-pr/12845 into main 2026-10-30 04:52:08 +00:00
heditrigui commented 2026-10-29 12:28:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by heditrigui on 2026-10-29T12:28:29Z.


What

Implement FreeBSD \qsort_r\ (NID \VLM2LMcMT5I) in \libc, sorting elements with caller-provided \ hunk\ context passed as the comparator's first argument. Cover forward and reverse sorting with thunk context on distinct keys as well as argument validation in \GuestQsort.

Tested

  • Ran \python tools/check_conventions.py --base upstream/main\ cleanly (0 errors).
  • Validated \GuestQsort\ test target on Windows and Linux (\GuestQsort\ passed 100% cleanly).
  • Hosted Windows CI test suite passed (15m 42s) 100% green; relinker-asan, windows, and ubuntu relinkers passed.

Checklist

  • Based on current \main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (\NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md\ files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5067993300 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12845). Originally posted by [heditrigui](https://github.com/heditrigui) on 2026-10-29T12:28:29Z. --- ### What Implement FreeBSD \qsort_r\ (NID \VLM2LMcMT5I\) in \libc\, sorting elements with caller-provided \ hunk\ context passed as the comparator's first argument. Cover forward and reverse sorting with thunk context on distinct keys as well as argument validation in \GuestQsort\. ### Tested - Ran \python tools/check_conventions.py --base upstream/main\ cleanly (0 errors). - Validated \GuestQsort\ test target on Windows and Linux (\GuestQsort\ passed 100% cleanly). - Hosted Windows CI test suite passed (15m 42s) 100% green; relinker-asan, windows, and ubuntu relinkers passed. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current \main\; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (\NotImplemented_nid_no_patch\); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation \.md\ files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-29 12:32:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T12:32:43Z.

Review: CHANGES_REQUESTED


Reviewed — changes requested

The new reverse-order test expects equal-key elements to reverse their relative order, but the existing insertion-sort path preserves them, so the test expectation conflicts with the implementation.

What I ran

Reviewed PR head 3f086c312a26200a1872a7c27d55083d13c67ef8 against main at 5454cd2e455994d992aca39335ce60507eeda1eb. The merge preview is clean (50eb657dc0fccfe3ec973bc2b59bab37d75ac622). Environment: Debian Linux, GCC 12.2.0, CMake 3.25.1.

git diff --check upstream/main...refs/remotes/upstream/pr/12845-review
python3 tools/check_conventions.py --base upstream/main
git merge-tree --write-tree upstream/main refs/remotes/upstream/pr/12845-review
cmake -S /tmp/AnyPS5-12845-qsort -B /tmp/AnyPS5-12845-qsort/build -G Ninja -DCMAKE_BUILD_TYPE=Release -DANYPS5_BUILD_TESTS=ON -DANYPS5_USE_SYSTEM_DEPS=ON
git -C /tmp/AnyPS5-12845-qsort submodule update --init 3rdparty/SDL2 3rdparty/Vulkan-Headers
cmake -S /tmp/AnyPS5-12845-qsort -B /tmp/AnyPS5-12845-qsort/build -G Ninja -DCMAKE_BUILD_TYPE=Release -DANYPS5_BUILD_TESTS=ON -DANYPS5_USE_SYSTEM_DEPS=OFF
gh pr checks 12845 -R boykopovar/AnyPS5

Results

Check Result
git diff --check Pass; no output
Merge preview Clean; tree 50eb657dc0fccfe3ec973bc2b59bab37d75ac622
Conventions Pass
Focused guest_qsort build/test Not run; CMake could not configure because other third-party submodules are uninitialized in the isolated worktree
Hosted intake and eligibility Pass
Hosted Linux product job Skipped
Other hosted build/test checks Pending at review time
Unedited command output

python3 tools/check_conventions.py --base upstream/main:

All conventions checks passed.

git diff --check upstream/main...refs/remotes/upstream/pr/12845-review produced no output and exited successfully.

git merge-tree --write-tree upstream/main refs/remotes/upstream/pr/12845-review:

50eb657dc0fccfe3ec973bc2b59bab37d75ac622

The first configure attempt reported:

CMake Error at cmake/SystemDeps.cmake:55 (message):
  ANYPS5_USE_SYSTEM_DEPS: the system Vulkan-Headers do not provide
  VkPhysicalDeviceMaintenance8FeaturesKHR (VK_KHR_maintenance8), which the
  agc driver uses; install Vulkan-Headers 1.4.310 or newer

After initializing SDL2 and Vulkan-Headers, the bundled-dependency configure reported missing submodules including ffmpeg-core, freetype, libusb-cmake, zstd, opus, glslang and LibAtrac9, and ended with Configuring incomplete, errors occurred!.

Hosted checks at review time:

intake          pass
eligibility     pass
report          pass
linux           skipping
conventions     pending
labels          pending
register-context (ubuntu-latest, Debug) pending
register-context (ubuntu-latest, Release) pending
relinker (ubuntu-latest, g++) pending
relinker (windows-latest, g++) pending
relinker (macos-latest, clang++) pending
relinker-asan   pending
windows         pending
write-watch (macos-15) pending
write-watch (macos-15-intel) pending

Negative control

Assertion Change to code under test Did the new test fail?
Reverse ordering preserves ascending input order among equal keys Not changed Could not be exercised: the focused test executable was not built

Findings

Blocking

ExpectOrderR sorts {1, 0, 1, 0} and checks that the descending result is the exact reverse of the ascending tag list. For the four-element insertion-sort path, equal elements are not swapped because the loop moves an element only when the comparator returns > 0. After ascending order {1, 3, 0, 2}, descending order therefore retains tie order as {0, 2, 1, 3}, while the test expects {2, 0, 3, 1}. This will make the new focused test fail. Please check descending key order and thunk direction without requiring equal-key elements to reverse their relative order, or use distinct keys for the exact-order assertion.

Non-blocking

The qsort_r callback signature and forwarding order match FreeBSD's qsort_r implementation shape. This was checked by reading source; no console behavior was tested.

Sources

Not verified

I did not build or run guest_qsort, run a negative control, or test other platforms, GPUs, or consoles. Hosted build and test jobs were still pending.

To get this to a good state

Adjust the reverse-order test so equal-key order is not assumed, then rerun the convention check and ctest --test-dir build -R '^guest_qsort$' --output-on-failure on the new head. I will re-run the same commands on that head.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5697733864 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12845#pullrequestreview-5697733864). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-29T12:32:43Z. **Review:** CHANGES_REQUESTED --- # Reviewed — changes requested The new reverse-order test expects equal-key elements to reverse their relative order, but the existing insertion-sort path preserves them, so the test expectation conflicts with the implementation. ## What I ran Reviewed PR head `3f086c312a26200a1872a7c27d55083d13c67ef8` against `main` at `5454cd2e455994d992aca39335ce60507eeda1eb`. The merge preview is clean (`50eb657dc0fccfe3ec973bc2b59bab37d75ac622`). Environment: Debian Linux, GCC 12.2.0, CMake 3.25.1. ```sh git diff --check upstream/main...refs/remotes/upstream/pr/12845-review python3 tools/check_conventions.py --base upstream/main git merge-tree --write-tree upstream/main refs/remotes/upstream/pr/12845-review cmake -S /tmp/AnyPS5-12845-qsort -B /tmp/AnyPS5-12845-qsort/build -G Ninja -DCMAKE_BUILD_TYPE=Release -DANYPS5_BUILD_TESTS=ON -DANYPS5_USE_SYSTEM_DEPS=ON git -C /tmp/AnyPS5-12845-qsort submodule update --init 3rdparty/SDL2 3rdparty/Vulkan-Headers cmake -S /tmp/AnyPS5-12845-qsort -B /tmp/AnyPS5-12845-qsort/build -G Ninja -DCMAKE_BUILD_TYPE=Release -DANYPS5_BUILD_TESTS=ON -DANYPS5_USE_SYSTEM_DEPS=OFF gh pr checks 12845 -R boykopovar/AnyPS5 ``` ## Results | Check | Result | |---|---| | `git diff --check` | Pass; no output | | Merge preview | Clean; tree `50eb657dc0fccfe3ec973bc2b59bab37d75ac622` | | Conventions | Pass | | Focused `guest_qsort` build/test | Not run; CMake could not configure because other third-party submodules are uninitialized in the isolated worktree | | Hosted intake and eligibility | Pass | | Hosted Linux product job | Skipped | | Other hosted build/test checks | Pending at review time | <details><summary>Unedited command output</summary> `python3 tools/check_conventions.py --base upstream/main`: ```text All conventions checks passed. ``` `git diff --check upstream/main...refs/remotes/upstream/pr/12845-review` produced no output and exited successfully. `git merge-tree --write-tree upstream/main refs/remotes/upstream/pr/12845-review`: ```text 50eb657dc0fccfe3ec973bc2b59bab37d75ac622 ``` The first configure attempt reported: ```text CMake Error at cmake/SystemDeps.cmake:55 (message): ANYPS5_USE_SYSTEM_DEPS: the system Vulkan-Headers do not provide VkPhysicalDeviceMaintenance8FeaturesKHR (VK_KHR_maintenance8), which the agc driver uses; install Vulkan-Headers 1.4.310 or newer ``` After initializing SDL2 and Vulkan-Headers, the bundled-dependency configure reported missing submodules including `ffmpeg-core`, `freetype`, `libusb-cmake`, `zstd`, `opus`, `glslang` and `LibAtrac9`, and ended with `Configuring incomplete, errors occurred!`. Hosted checks at review time: ```text intake pass eligibility pass report pass linux skipping conventions pending labels pending register-context (ubuntu-latest, Debug) pending register-context (ubuntu-latest, Release) pending relinker (ubuntu-latest, g++) pending relinker (windows-latest, g++) pending relinker (macos-latest, clang++) pending relinker-asan pending windows pending write-watch (macos-15) pending write-watch (macos-15-intel) pending ``` </details> ## Negative control | Assertion | Change to code under test | Did the new test fail? | |---|---|---| | Reverse ordering preserves ascending input order among equal keys | Not changed | Could not be exercised: the focused test executable was not built | ## Findings ### Blocking `ExpectOrderR` sorts `{1, 0, 1, 0}` and checks that the descending result is the exact reverse of the ascending tag list. For the four-element insertion-sort path, equal elements are not swapped because the loop moves an element only when the comparator returns `> 0`. After ascending order `{1, 3, 0, 2}`, descending order therefore retains tie order as `{0, 2, 1, 3}`, while the test expects `{2, 0, 3, 1}`. This will make the new focused test fail. Please check descending key order and thunk direction without requiring equal-key elements to reverse their relative order, or use distinct keys for the exact-order assertion. ### Non-blocking The `qsort_r` callback signature and forwarding order match FreeBSD's `qsort_r` implementation shape. This was checked by reading source; no console behavior was tested. ## Sources - [PR test assertion at head `3f086c312a26200a1872a7c27d55083d13c67ef8`](https://github.com/boykopovar/AnyPS5/blob/3f086c312a26200a1872a7c27d55083d13c67ef8/core/libs/tests/GuestQsort.cpp#L77-L95) - [Existing insertion-sort path at head `3f086c312a26200a1872a7c27d55083d13c67ef8`](https://github.com/boykopovar/AnyPS5/blob/3f086c312a26200a1872a7c27d55083d13c67ef8/core/libs/prx/libc/src/FileAndHeap.cpp#L185-L190) - [FreeBSD 11 `qsort.c`, pinned commit `5ed7eb0d97ba4436218e810f61bd059acba984c2`](https://github.com/freebsd/freebsd-src/blob/5ed7eb0d97ba4436218e810f61bd059acba984c2/lib/libc/stdlib/qsort.c#L731-L743) ## Not verified I did not build or run `guest_qsort`, run a negative control, or test other platforms, GPUs, or consoles. Hosted build and test jobs were still pending. ## To get this to a good state Adjust the reverse-order test so equal-key order is not assumed, then rerun the convention check and `ctest --test-dir build -R '^guest_qsort$' --output-on-failure` on the new head. I will re-run the same commands on that head.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!12845
No description provided.