feat(libSceLibreSsl): implement sk_num, sk_value, sk_pop and sk_pop_free #10373

Merged
alvaroelpob merged 6 commits from upstream-pr/10373 into main 2026-10-26 18:16:11 +00:00
alvaroelpob commented 2026-10-23 19:56:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by alvaroelpob on 2026-10-23T19:56:44Z.


What

Implements sk_num, sk_value, sk_pop and sk_pop_free in libSceLibreSsl, which threw and were declared without parameters.

The module is LibreSSL 2.5.4 (firmware 13.40 measurements in #8981). Signatures, the _STACK layout and the results follow that release's stack.h and stack.c:

Export Behaviour
int sk_num(const _STACK*) -1 for a null stack, otherwise num (unchecked, so a negative count is returned as is)
void* sk_value(const _STACK*, int) null for a null stack or an index outside [0, num), otherwise data[i]
void* sk_pop(_STACK*) null for a null or empty stack; otherwise returns the last element and decrements num, leaving data and num_alloc as they are
void sk_pop_free(_STACK*, void (*)(void*)) nothing for a null stack; otherwise calls the function on every non-null element in order, then frees data and the stack

The layout is 32 bytes (num at 0, data at 8, sorted at 16, num_alloc at 20, comp at 24), checked with static_asserts. sk_pop_free frees through the guest free, as the module would through libc. The free function is called with the guest ABI.

LibreSSL faults in a few cases: a null data array it would read through, and a null free function given a non-null element. Those throw. No export of this module creates a stack yet, so for now only stacks built by the caller reach these functions.

The debt entry records the source and what is unverified, and the pending-export count drops by 4 (342 to 338 on current main).

Tested

Ubuntu (WSL2, kernel 6.18), GCC 15.2.0, Release, full build with BUILD_TESTING=ON on main 3c44638fc. There is one commit per function, in the order sk_num, sk_value, sk_pop, sk_pop_free. Each commit adds that function with its test block and debt update. Each was built and its guest_libressl_stack test and check_conventions.py run on its own.

  • New guest_libressl_stack test: null stacks for all four exports; sk_num on a stack holding a null element and on a negative count; sk_value for in-range, negative and out-of-range indices, including an element past num that must not be returned; sk_pop down to empty, checking that num drops while num_alloc and data stay as they were, and that a negative count stays unchanged; sk_pop_free calls the function only for non-null elements, in order, and frees the stack; the null free function and null element array cases throw. On main the first call throws.
  • ctest --test-dir build -R guest_libressl: 6/6 pass.
  • Full ctest -j16 at the last commit: 1992/1993 pass. agc_driver_multisample_retirement fails the same way on main on this machine.
  • Not tested: Windows, a title.
  • Rebased on main 1e740a23a3 (after #9033), head 0bc03c028f: the conflicts were only the add_library source list, the end of the test block, the pending-export count and the last debt entry; both sides are kept and the count is 357 minus this PR's exports. Full build and ctest -j16: 2043/2044 pass; agc_driver_multisample_retirement fails the same way on that main. check_conventions.py --base upstream/main passes.
  • Rebased on main e7e3e9c198, head 64df489045, to resolve the conflict from #9529 on the shared CMakeLists.txt and debt-file lines; both sides are kept and the count is 353 minus this PR's exports. 64df489045 (refactor(libSceLibreSsl): declare the stack exports in the public header) puts the real signatures and their structs in libSceLibreSsl/include/Api.hpp and drops the test's redeclarations, which public_headers requires since #10706. Full build and ctest -j16: 2202/2203 pass; agc_driver_multisample_retirement fails the same way on that main. check_conventions.py --base upstream/main passes.
  • Rebased on main ae7d59258e (after #9949), head 3602624126, to resolve the conflicts with main on the shared CMakeLists.txt and debt-file lines; both sides are kept and the count is 342 minus this PR's exports. Full build and ctest -j16: 2275/2276 pass; agc_driver_multisample_retirement fails the same way on that main. check_conventions.py --base upstream/main passes.
  • Review fix 00ae472afa registers a recording heap and checks that sk_pop_free frees the element array and then the stack, and starts the pop test with sorted set and checks it stays set; dropping the array free, swapping the frees or clearing sorted in sk_pop fails guest_libressl_stack. Full build and ctest -j16 on main ae7d59258e: 2275/2276 pass; agc_driver_multisample_retirement fails the same way on that main. check_conventions.py --base upstream/main passes.
  • Rebased on main 2dbe9b99f5 (after #9745, #9870 and #11111), head 7ed657680a, to resolve the conflicts with main on the shared CMakeLists.txt and debt-file lines; both sides are kept and the count is 340 minus this PR's exports. Full build and ctest -j16: 2370/2371 pass; agc_driver_multisample_retirement fails the same way on that main. check_conventions.py --base upstream/main passes.

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: #9448, #9571, #10078
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4988494160 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10373). Originally posted by [alvaroelpob](https://github.com/alvaroelpob) on 2026-10-23T19:56:44Z. --- ### What Implements `sk_num`, `sk_value`, `sk_pop` and `sk_pop_free` in libSceLibreSsl, which threw and were declared without parameters. The module is LibreSSL 2.5.4 ([firmware 13.40 measurements in #8981](https://github.com/boykopovar/AnyPS5/discussions/8981#discussioncomment-19111214)). Signatures, the `_STACK` layout and the results follow that release's [`stack.h` and `stack.c`](https://github.com/libressl/openbsd/tree/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/stack): | Export | Behaviour | |---|---| | `int sk_num(const _STACK*)` | -1 for a null stack, otherwise `num` (unchecked, so a negative count is returned as is) | | `void* sk_value(const _STACK*, int)` | null for a null stack or an index outside `[0, num)`, otherwise `data[i]` | | `void* sk_pop(_STACK*)` | null for a null or empty stack; otherwise returns the last element and decrements `num`, leaving `data` and `num_alloc` as they are | | `void sk_pop_free(_STACK*, void (*)(void*))` | nothing for a null stack; otherwise calls the function on every non-null element in order, then frees `data` and the stack | The layout is 32 bytes (`num` at 0, `data` at 8, `sorted` at 16, `num_alloc` at 20, `comp` at 24), checked with `static_assert`s. `sk_pop_free` frees through the guest `free`, as the module would through libc. The free function is called with the guest ABI. LibreSSL faults in a few cases: a null `data` array it would read through, and a null free function given a non-null element. Those throw. No export of this module creates a stack yet, so for now only stacks built by the caller reach these functions. The debt entry records the source and what is unverified, and the pending-export count drops by 4 (342 to 338 on current `main`). ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Ubuntu (WSL2, kernel 6.18), GCC 15.2.0, Release, full build with `BUILD_TESTING=ON` on `main` 3c44638fc. There is one commit per function, in the order `sk_num`, `sk_value`, `sk_pop`, `sk_pop_free`. Each commit adds that function with its test block and debt update. Each was built and its `guest_libressl_stack` test and `check_conventions.py` run on its own. - New `guest_libressl_stack` test: null stacks for all four exports; `sk_num` on a stack holding a null element and on a negative count; `sk_value` for in-range, negative and out-of-range indices, including an element past `num` that must not be returned; `sk_pop` down to empty, checking that `num` drops while `num_alloc` and `data` stay as they were, and that a negative count stays unchanged; `sk_pop_free` calls the function only for non-null elements, in order, and frees the stack; the null free function and null element array cases throw. On `main` the first call throws. - `ctest --test-dir build -R guest_libressl`: 6/6 pass. - Full `ctest -j16` at the last commit: 1992/1993 pass. `agc_driver_multisample_retirement` fails the same way on `main` on this machine. - Not tested: Windows, a title. - Rebased on `main` 1e740a23a3 (after #9033), head 0bc03c028f: the conflicts were only the `add_library` source list, the end of the test block, the pending-export count and the last debt entry; both sides are kept and the count is 357 minus this PR's exports. Full build and `ctest -j16`: 2043/2044 pass; `agc_driver_multisample_retirement` fails the same way on that `main`. `check_conventions.py --base upstream/main` passes. - Rebased on `main` e7e3e9c198, head 64df489045, to resolve the conflict from #9529 on the shared `CMakeLists.txt` and debt-file lines; both sides are kept and the count is 353 minus this PR's exports. 64df489045 (`refactor(libSceLibreSsl): declare the stack exports in the public header`) puts the real signatures and their structs in `libSceLibreSsl/include/Api.hpp` and drops the test's redeclarations, which `public_headers` requires since #10706. Full build and `ctest -j16`: 2202/2203 pass; `agc_driver_multisample_retirement` fails the same way on that `main`. `check_conventions.py --base upstream/main` passes. - Rebased on `main` ae7d59258e (after #9949), head 3602624126, to resolve the conflicts with `main` on the shared `CMakeLists.txt` and debt-file lines; both sides are kept and the count is 342 minus this PR's exports. Full build and `ctest -j16`: 2275/2276 pass; `agc_driver_multisample_retirement` fails the same way on that `main`. `check_conventions.py --base upstream/main` passes. - Review fix 00ae472afa registers a recording heap and checks that `sk_pop_free` frees the element array and then the stack, and starts the pop test with `sorted` set and checks it stays set; dropping the array free, swapping the frees or clearing `sorted` in `sk_pop` fails `guest_libressl_stack`. Full build and `ctest -j16` on `main` ae7d59258e: 2275/2276 pass; `agc_driver_multisample_retirement` fails the same way on that `main`. `check_conventions.py --base upstream/main` passes. - Rebased on `main` 2dbe9b99f5 (after #9745, #9870 and #11111), head 7ed657680a, to resolve the conflicts with `main` on the shared `CMakeLists.txt` and debt-file lines; both sides are kept and the count is 340 minus this PR's exports. Full build and `ctest -j16`: 2370/2371 pass; `agc_driver_multisample_retirement` fails the same way on that `main`. `check_conventions.py --base upstream/main` passes. ### 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: #9448, #9571, #10078 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
copilot-pull-request-reviewer[bot] commented 2026-10-23 19:56:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by copilot-pull-request-reviewer[bot] on 2026-10-23T19:56:51Z.

Review: COMMENTED


Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5633742088 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10373#pullrequestreview-5633742088). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-10-23T19:56:51Z. **Review:** COMMENTED --- Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.
MarcZX10 commented 2026-10-23 19:58:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T19:58:35Z.

Review: APPROVED


Reviewed the exact head dce8b38c2503824819f2e010a02ef5095aef8399. The ABI/layout and edge-case results for sk_num, sk_value, sk_pop and sk_pop_free match the pinned LibreSSL 2.5.4 source, including null element handling and freeing the element array and stack through the guest allocator. The added tests cover range/count and ownership cases, and the debt entry clearly records the unverified console behavior. Hosted conventions, eligibility, intake, labels and report checks pass; product and relinker jobs were skipped by path policy. I reviewed the reported local test results but did not run the test locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5633758468 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10373#pullrequestreview-5633758468). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T19:58:35Z. **Review:** APPROVED --- Reviewed the exact head dce8b38c2503824819f2e010a02ef5095aef8399. The ABI/layout and edge-case results for sk_num, sk_value, sk_pop and sk_pop_free match the pinned LibreSSL 2.5.4 source, including null element handling and freeing the element array and stack through the guest allocator. The added tests cover range/count and ownership cases, and the debt entry clearly records the unverified console behavior. Hosted conventions, eligibility, intake, labels and report checks pass; product and relinker jobs were skipped by path policy. I reviewed the reported local test results but did not run the test locally.
MarcZX10 commented 2026-10-23 20:00:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T20:00:12Z.

Review: DISMISSED


Follow-up to my approval: a pairwise merge check with current open #9529 (git merge-tree --write-tree upstream/pr/9529 upstream/pr/10373) reports content conflicts in core/libs/prx/libSceLibreSsl/CMakeLists.txt and docs/dev/debt/libSceLibreSsl.md. This PR currently says Depends on: none, so the integration order is not recorded. CONTRIBUTING requires coordination for overlapping shared-file changes and a declared dependency. Please coordinate with #9529’s author, update the checklist to reflect the agreed order (the older #9529 first is a straightforward option), and confirm the combined CMake/test registration and debt count will be retained. The four function implementations still look consistent with the pinned LibreSSL source; this request is for the reproducible merge conflict and missing ordering, not those semantics.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5633773627 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10373#pullrequestreview-5633773627). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T20:00:12Z. **Review:** DISMISSED --- Follow-up to my approval: a pairwise merge check with current open #9529 (`git merge-tree --write-tree upstream/pr/9529 upstream/pr/10373`) reports content conflicts in `core/libs/prx/libSceLibreSsl/CMakeLists.txt` and `docs/dev/debt/libSceLibreSsl.md`. This PR currently says `Depends on: none`, so the integration order is not recorded. CONTRIBUTING requires coordination for overlapping shared-file changes and a declared dependency. Please coordinate with #9529’s author, update the checklist to reflect the agreed order (the older #9529 first is a straightforward option), and confirm the combined CMake/test registration and debt count will be retained. The four function implementations still look consistent with the pinned LibreSSL source; this request is for the reproducible merge conflict and missing ordering, not those semantics.
ahmadmuj commented 2026-10-26 05:59:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ahmadmuj on 2026-10-26T05:59:52Z.

Review: APPROVED


Reviewed head 3602624 (base ae7d592), merged locally onto current main 14c99b5 (clean). WSL2 Ubuntu 24.04, GCC 13.3, Release.

What I ran:

  • ctest -R '^guest_libressl' 12/12, guest_libressl_stack 3 runs all pass, ctest -R '^public_headers' 65/65.
  • tools/check_conventions.py passes on the head (base ae7d592) and on the merge (base main).
  • Layout: a small C probe built against the real 2.5.4 include/openssl/stack.h prints sizeof=32 num=0 data=8 sorted=16 num_alloc=20 comp=24, same as your static_asserts.
  • Read Stack.cpp against 2.5.4 crypto/stack/stack.c (sk_num, sk_value, sk_pop through sk_delete, sk_pop_free through sk_free). Same results on every input that doesn't fault there. The throws only fire where 2.5.4 would dereference or call NULL.
  • Mutations: sk_num(NULL) returning 0, index > num in sk_value, sk_pop not lowering num, sk_pop returning data[0], and releasing NULL elements all fail the test.

The merge-order points from the earlier change request look done on this head: #9529 merged first, line 1 of CMakeLists.txt keeps main's Error.cpp and Ocsp.cpp and only appends Stack.cpp, the guest_libressl_ocsp block is kept, and the 338 count matches the 338 NotImplemented_nid_no_patch stubs left in Export.cpp.

Two optional test notes, not blocking:

  • The frees aren't checked. Dropping free_nid_postfix(stack->data); or swapping the two frees still passes. A counting heap like the one in libSceLibreSsl/tests/Asn1.cpp (Allocator()) could assert both pointers get freed, array first.
  • sorted isn't pinned after a pop. Adding stack->sorted = 0; to sk_pop still passes, since TestPop starts at 0 and never reads it. Starting the stack with sorted = 1 and checking it after the pops would catch that.

Limits: no console or module here. BunnyFl's 13.42 differential above covers the module side.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5647955575 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10373#pullrequestreview-5647955575). Originally posted by [ahmadmuj](https://github.com/ahmadmuj) on 2026-10-26T05:59:52Z. **Review:** APPROVED --- Reviewed head 3602624 (base ae7d592), merged locally onto current main 14c99b5 (clean). WSL2 Ubuntu 24.04, GCC 13.3, Release. What I ran: - `ctest -R '^guest_libressl'` 12/12, `guest_libressl_stack` 3 runs all pass, `ctest -R '^public_headers'` 65/65. - `tools/check_conventions.py` passes on the head (base ae7d592) and on the merge (base main). - Layout: a small C probe built against the real 2.5.4 `include/openssl/stack.h` prints `sizeof=32 num=0 data=8 sorted=16 num_alloc=20 comp=24`, same as your `static_assert`s. - Read `Stack.cpp` against 2.5.4 `crypto/stack/stack.c` (`sk_num`, `sk_value`, `sk_pop` through `sk_delete`, `sk_pop_free` through `sk_free`). Same results on every input that doesn't fault there. The throws only fire where 2.5.4 would dereference or call NULL. - Mutations: `sk_num(NULL)` returning 0, `index > num` in `sk_value`, `sk_pop` not lowering `num`, `sk_pop` returning `data[0]`, and releasing NULL elements all fail the test. The merge-order points from the earlier change request look done on this head: #9529 merged first, line 1 of `CMakeLists.txt` keeps main's `Error.cpp` and `Ocsp.cpp` and only appends `Stack.cpp`, the `guest_libressl_ocsp` block is kept, and the 338 count matches the 338 `NotImplemented_nid_no_patch` stubs left in `Export.cpp`. Two optional test notes, not blocking: - The frees aren't checked. Dropping `free_nid_postfix(stack->data);` or swapping the two frees still passes. A counting heap like the one in `libSceLibreSsl/tests/Asn1.cpp` (`Allocator()`) could assert both pointers get freed, array first. - `sorted` isn't pinned after a pop. Adding `stack->sorted = 0;` to `sk_pop` still passes, since `TestPop` starts at 0 and never reads it. Starting the stack with `sorted = 1` and checking it after the pops would catch that. Limits: no console or module here. BunnyFl's 13.42 differential above covers the module side.
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!10373
No description provided.