feat(libc): implement heapsort #11629

Closed
B-Howell wants to merge 4 commits from upstream-pr/11629 into main
B-Howell commented 2026-10-26 23:49:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by B-Howell on 2026-10-26T23:49:36Z.


What

Draft — overlapping implementation. Open PR #11400 by another contributor already implements the same heapsort_nid_postfix export and has been approved. The earlier "no other open PR implements the same function" claim on this PR was wrong; the overlap check correctly flagged heapsort as a shared export. This PR is being reclassified: see the Consolidation plan below.

Original scope (retained for review history): implement the BSD heapsort(3) export for the libc PRX, following FreeBSD 11 lib/libc/stdlib/heapsort.c: in-place binary max-heap built from the array, then repeated root/last swaps and a sift-down of the new root over the shrinking heap, using a one-element malloc scratch. Guard ordering follows the reference: count <= 1 returns 0 before any other check; above that, size == 0 sets errno to EINVAL and returns -1; a scratch allocation failure returns -1 with the allocator's errno. Null base/compare throw std::invalid_argument and count * size past SIZE_MAX throws std::overflow_error before any element access or comparator call. Documented in docs/dev/debt/libc.md. NID 3z6y7yolHpA registry-confirmed via tools/nid_names.py.

Consolidation plan

  • #11400 is the implementation PR to merge; this PR will not land a competing second implementation.
  • A tests-only follow-up branch (codex/libc-heapsort-tests-on-11400, local) is prepared on top of #11400's head b999ea3c adding the useful differential coverage from this PR: 11-bucket histogram validation across counts {0,1,2,3,7,16,33,1000}, byte-array (size=1) sort, non-power-of-2 three-byte-stride sort, a comparator-ABI probe that verifies the comparator sees in-array pointers (now using std::uintptr_t range/stride checks with non-wrapping bounds, addressing the review note), and extra null+count edge cases. It will be opened as a new PR after #11400 merges, not as a competitor.
  • MarcZX10's test-portability finding on RecordingCompareKeys (cross-object pointer relational comparison and subtraction) is addressed in commit a0c82ac07c on this branch using std::uintptr_t conversions before any range/stride arithmetic; guest_qsort passes locally. That commit will migrate to the follow-up branch rather than be published here.

Tested

Windows 11 Pro, MinGW UCRT64 (gcc 16.2.0): built guest_qsort_tests and ran ctest -R "^guest_qsort$" → 1/1 Passed. Rebuilt and reran after the uintptr_t fix (commit a0c82ac07c) → still 1/1 Passed. Also built and ran the same focused test on the tests-only follow-up branch rebased onto #11400 (b999ea3c) → 1/1 Passed. Not run on a console.

Checklist

  • Based on current main; no other open PR implements the same function — false: PR #11400 implements the same heapsort export
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw; silent stubs listed under Silent stubs in the library's technical-debt file
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: #11400 (reclassified from "none")
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Opus 4.7 + Codex)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5026383689 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11629). Originally posted by [B-Howell](https://github.com/B-Howell) on 2026-10-26T23:49:36Z. --- ### What **Draft — overlapping implementation.** Open PR #11400 by another contributor already implements the same `heapsort_nid_postfix` export and has been approved. The earlier "no other open PR implements the same function" claim on this PR was wrong; the overlap check correctly flagged `heapsort` as a shared export. This PR is being reclassified: see the Consolidation plan below. Original scope (retained for review history): implement the BSD `heapsort(3)` export for the libc PRX, following FreeBSD 11 `lib/libc/stdlib/heapsort.c`: in-place binary max-heap built from the array, then repeated root/last swaps and a sift-down of the new root over the shrinking heap, using a one-element `malloc` scratch. Guard ordering follows the reference: `count <= 1` returns `0` before any other check; above that, `size == 0` sets `errno` to `EINVAL` and returns `-1`; a scratch allocation failure returns `-1` with the allocator's `errno`. Null `base`/`compare` throw `std::invalid_argument` and `count * size` past `SIZE_MAX` throws `std::overflow_error` before any element access or comparator call. Documented in `docs/dev/debt/libc.md`. NID `3z6y7yolHpA` registry-confirmed via `tools/nid_names.py`. ### Consolidation plan - #11400 is the implementation PR to merge; this PR will not land a competing second implementation. - A tests-only follow-up branch (`codex/libc-heapsort-tests-on-11400`, local) is prepared on top of #11400's head `b999ea3c` adding the useful differential coverage from this PR: 11-bucket histogram validation across counts {0,1,2,3,7,16,33,1000}, byte-array (size=1) sort, non-power-of-2 three-byte-stride sort, a comparator-ABI probe that verifies the comparator sees in-array pointers (now using `std::uintptr_t` range/stride checks with non-wrapping bounds, addressing the review note), and extra null+count edge cases. It will be opened as a new PR after #11400 merges, not as a competitor. - MarcZX10's test-portability finding on `RecordingCompareKeys` (cross-object pointer relational comparison and subtraction) is addressed in commit `a0c82ac07c` on this branch using `std::uintptr_t` conversions before any range/stride arithmetic; `guest_qsort` passes locally. That commit will migrate to the follow-up branch rather than be published here. ### Tested Windows 11 Pro, MinGW UCRT64 (gcc 16.2.0): built `guest_qsort_tests` and ran `ctest -R "^guest_qsort$"` → `1/1 Passed`. Rebuilt and reran after the `uintptr_t` fix (commit `a0c82ac07c`) → still `1/1 Passed`. Also built and ran the same focused test on the tests-only follow-up branch rebased onto #11400 (`b999ea3c`) → `1/1 Passed`. Not run on a console. ### Checklist - [ ] Based on current `main`; no other open PR implements the same function — **false**: PR #11400 implements the same `heapsort` export - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw; silent stubs listed under Silent stubs in the library's technical-debt file - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: #11400 (reclassified from "none") - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Opus 4.7 + Codex)
MarcZX10 commented 2026-10-27 00:00:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T00:00:35Z.

Review: COMMENTED


Focused review of 268fa0bdb2fdff34cd8624bec67a635a23915304: the heapsort implementation follows the stated BSD control-flow and error cases, and the focused Linux Release target builds. ctest -R '^guest_qsort$' passes (1/1); conventions and git diff --check pass. Hosted product jobs are skipped by path policy; some relinker/register-context checks remain pending, so this is not a full product build result.

One test portability issue: RecordingCompareKeys compares each comparator pointer against the array bounds with built-in >=/< at core/libs/tests/GuestQsort.cpp:198, then may subtract unrelated pointers at line 199. For a scratch-buffer argument, those pointers do not belong to the same array, so the relational result is unspecified; if it happens to satisfy the range test, subtraction is undefined. Please compare std::uintptr_t address values (or otherwise avoid cross-object relational comparison/subtraction) so the comparator-ABI regression is deterministic across platforms. This is a test-only finding; I found no implementation blocker in the focused review.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5660682223 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11629#pullrequestreview-5660682223). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-27T00:00:35Z. **Review:** COMMENTED --- Focused review of `268fa0bdb2fdff34cd8624bec67a635a23915304`: the heapsort implementation follows the stated BSD control-flow and error cases, and the focused Linux Release target builds. `ctest -R '^guest_qsort$'` passes (1/1); conventions and `git diff --check` pass. Hosted product jobs are skipped by path policy; some relinker/register-context checks remain pending, so this is not a full product build result. One test portability issue: `RecordingCompareKeys` compares each comparator pointer against the array bounds with built-in `>=`/`<` at `core/libs/tests/GuestQsort.cpp:198`, then may subtract unrelated pointers at line 199. For a scratch-buffer argument, those pointers do not belong to the same array, so the relational result is unspecified; if it happens to satisfy the range test, subtraction is undefined. Please compare `std::uintptr_t` address values (or otherwise avoid cross-object relational comparison/subtraction) so the comparator-ABI regression is deterministic across platforms. This is a test-only finding; I found no implementation blocker in the focused review.
MarcZX10 commented 2026-10-27 00:25:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T00:25:02Z.

Review: CHANGES_REQUESTED


One test portability issue remains in RecordingCompareKeys. When the heapsort passes its scratch allocation, inArray compares that pointer relationally against heapsortBase/heapsortLimit and subtracts heapsortBase from it. Those operations are only defined for pointers into the same array object; the scratch allocation is a separate object. A passing Linux run therefore does not make this check portable across the project's supported compilers.

Please classify the pointer addresses as std::uintptr_t values before doing range/stride checks (with the bounds constructed so the integer arithmetic cannot wrap), or use another defined way to identify in-array arguments. Then rerun guest_qsort and the supported compiler checks. I have not rerun the test locally in this turn.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5660836887 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11629#pullrequestreview-5660836887). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-27T00:25:02Z. **Review:** CHANGES_REQUESTED --- One test portability issue remains in `RecordingCompareKeys`. When the heapsort passes its scratch allocation, `inArray` compares that pointer relationally against `heapsortBase`/`heapsortLimit` and subtracts `heapsortBase` from it. Those operations are only defined for pointers into the same array object; the scratch allocation is a separate object. A passing Linux run therefore does not make this check portable across the project's supported compilers. Please classify the pointer addresses as `std::uintptr_t` values before doing range/stride checks (with the bounds constructed so the integer arithmetic cannot wrap), or use another defined way to identify in-array arguments. Then rerun `guest_qsort` and the supported compiler checks. I have not rerun the test locally in this turn.
MarcZX10 commented 2026-10-27 02:01:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T02:01:29Z.

Review: CHANGES_REQUESTED


There is an unresolved duplicate implementation: open PR #11400 by another contributor exports and implements the same heapsort_nid_postfix NID, and the overlap check identifies the same heapsort export. #11400 remains open and is not marked intake-paused; its current head already has an approval and passing focused guest_qsort evidence. This PR also changes the same API declaration, implementation and test files, so merging both would duplicate the export and conflict in those files. The added differential/error-path coverage here may still be useful, but please coordinate with #11400 and the maintainers on one implementation/merge order, then record any dependency or consolidation in the checklist. I am not asking either contributor to close work solely because the queue is large. Separately, my prior portability request remains: the current RecordingCompareKeys still performs relational comparison and subtraction between its array pointers and a separately allocated scratch pointer; the exact current head still contains that code.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5661397364 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11629#pullrequestreview-5661397364). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-27T02:01:29Z. **Review:** CHANGES_REQUESTED --- There is an unresolved duplicate implementation: open PR #11400 by another contributor exports and implements the same `heapsort_nid_postfix` NID, and the overlap check identifies the same `heapsort` export. #11400 remains open and is not marked intake-paused; its current head already has an approval and passing focused `guest_qsort` evidence. This PR also changes the same API declaration, implementation and test files, so merging both would duplicate the export and conflict in those files. The added differential/error-path coverage here may still be useful, but please coordinate with #11400 and the maintainers on one implementation/merge order, then record any dependency or consolidation in the checklist. I am not asking either contributor to close work solely because the queue is large. Separately, my prior portability request remains: the current `RecordingCompareKeys` still performs relational comparison and subtraction between its array pointers and a separately allocated scratch pointer; the exact current head still contains that code.

Pull request closed

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!11629
No description provided.