feat(libSceLibreSsl): implement the CRYPTO locking and thread id callbacks #10248

Merged
mantavisual merged 2 commits from upstream-pr/10248 into main 2026-10-26 16:46:18 +00:00
mantavisual commented 2026-10-23 15:31:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mantavisual on 2026-10-23T15:31:41Z.


What

Implements five libSceLibreSsl exports that threw: CRYPTO_num_locks, CRYPTO_set_locking_callback, CRYPTO_set_add_lock_callback, CRYPTO_THREADID_set_callback and CRYPTO_THREADID_set_numeric. They are the thread setup calls an OpenSSL 1.0-style application makes before anything else.

Signatures and results follow LibreSSL 2.5.4's cryptlib.c and crypto.h, the version identified in the firmware 13.40 measurements that X509_verify_cert_error_string already cites:

  • CRYPTO_num_locks returns CRYPTO_NUM_LOCKS, 41.

  • CRYPTO_set_locking_callback and CRYPTO_set_add_lock_callback store the callback (2.5.4's OPENSSL_init() call there is empty).

  • CRYPTO_THREADID_set_callback returns 0 when a callback is already set, otherwise stores it and returns 1, so a null callback while none is set returns 1 and leaves the slot empty. The first-set check is a compare-exchange.

  • CRYPTO_THREADID_set_numeric clears the 16-byte {void* ptr; unsigned long val} structure and stores the value. The value is a 64-bit integer here because unsigned long is 32 bits on Windows hosts. A null structure throws (APS5_INVALID_ARG_EX), since 2.5.4 writes through it unchecked.

The callbacks are stored and never called. In 2.5.4 they are called from CRYPTO_lock, CRYPTO_add_lock and CRYPTO_THREADID_current, around LibreSSL's own shared state. The PS5 3.20 export table lists none of those or any getter (its CRYPTO_ exports are these five plus CRYPTO_malloc, CRYPTO_free, CRYPTO_set_mem_functions and CRYPTO_cleanup_all_ex_data), so the stored callbacks are observable only through CRYPTO_THREADID_set_callback's result. This library's shared state uses host locking. The debt entry records this, along with the fact that nothing is checked on a console. CRYPTO_cleanup_all_ex_data is left throwing because no ex_data is implemented yet.

The five declarations in include/Api.hpp now carry these signatures, and the header holds the public CryptoThreadId layout. The pending-export count in docs/dev/debt/libSceLibreSsl.md goes from 340 to 335: Export.cpp has 340 unknown-signature stubs on main 5bdda3661e (including the five sceLibreSsl* exports) and 335 after this change.

Tested

Windows 11 Pro 23H2, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON, with main 5bdda3661e merged in (ad31835a6e). The merge only resolved the test registrations next to the new alert and version tests, took main's digest debt wording, and updated the pending count.

  • New guest_libressl_crypto_locks: CRYPTO_num_locks returns 41. The locking and add-lock setters accept a function and null. CRYPTO_THREADID_set_callback returns 1 twice for null, 1 for the first function, then 0 for another function, the same function and null. CRYPTO_THREADID_set_numeric clears a 0xaa-filled structure to {nullptr, 0x1234567890abcdef} and a 0x55-filled one to all zero bytes, and throws for a null structure. On main each of these calls throws through NotImplemented_nid_no_patch.

  • ctest -R "guest_libressl|patched_libraries|^public_headers$": 16 of 16 pass (public_headers, patched_libraries, guest_libressl_x509_errors, guest_libressl_digest, guest_libressl_asn1, guest_libressl_asn1_allocator, guest_libressl_des, guest_libressl_runtime, guest_libressl_aes, guest_libressl_error, guest_libressl_ocsp, guest_libressl_cipher, guest_libressl_alert, guest_libressl_version, guest_libressl_crypto_locks, guest_libressl3_digest).

  • The full build and the libs target complete. tools/check_test_registration.py build: 1397 test executables, none built and not run.

  • Firmware: BunnyFl ran firmware 13.42's libSceLibreSsl.sprx in an emulator and it matches all five: 41 locks, the 16-byte {ptr, val} write, first-set-wins for the thread id callback, and setters that only store.

  • python3 tools/check_conventions.py --base origin/main passes. Overlap: no other open PR changes or names these five exports (checked 2026-10-25). #9529, whose error queue does not use a thread id callback, has merged; this branch keeps its test registration.

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:4985204134 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10248). Originally posted by [mantavisual](https://github.com/mantavisual) on 2026-10-23T15:31:41Z. --- ### What Implements five `libSceLibreSsl` exports that threw: `CRYPTO_num_locks`, `CRYPTO_set_locking_callback`, `CRYPTO_set_add_lock_callback`, `CRYPTO_THREADID_set_callback` and `CRYPTO_THREADID_set_numeric`. They are the thread setup calls an OpenSSL 1.0-style application makes before anything else. Signatures and results follow [LibreSSL 2.5.4's `cryptlib.c`](https://github.com/libressl/openbsd/blob/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/cryptlib.c) and `crypto.h`, the version identified in the [firmware 13.40 measurements](https://github.com/boykopovar/AnyPS5/discussions/8981#discussioncomment-19111214) that `X509_verify_cert_error_string` already cites: - `CRYPTO_num_locks` returns `CRYPTO_NUM_LOCKS`, 41. - `CRYPTO_set_locking_callback` and `CRYPTO_set_add_lock_callback` store the callback (2.5.4's `OPENSSL_init()` call there is empty). - `CRYPTO_THREADID_set_callback` returns 0 when a callback is already set, otherwise stores it and returns 1, so a null callback while none is set returns 1 and leaves the slot empty. The first-set check is a compare-exchange. - `CRYPTO_THREADID_set_numeric` clears the 16-byte `{void* ptr; unsigned long val}` structure and stores the value. The value is a 64-bit integer here because `unsigned long` is 32 bits on Windows hosts. A null structure throws (`APS5_INVALID_ARG_EX`), since 2.5.4 writes through it unchecked. The callbacks are stored and never called. In 2.5.4 they are called from `CRYPTO_lock`, `CRYPTO_add_lock` and `CRYPTO_THREADID_current`, around LibreSSL's own shared state. The [PS5 3.20 export table](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceLibreSsl.c) lists none of those or any getter (its `CRYPTO_` exports are these five plus `CRYPTO_malloc`, `CRYPTO_free`, `CRYPTO_set_mem_functions` and `CRYPTO_cleanup_all_ex_data`), so the stored callbacks are observable only through `CRYPTO_THREADID_set_callback`'s result. This library's shared state uses host locking. The debt entry records this, along with the fact that nothing is checked on a console. `CRYPTO_cleanup_all_ex_data` is left throwing because no ex_data is implemented yet. The five declarations in `include/Api.hpp` now carry these signatures, and the header holds the public `CryptoThreadId` layout. The pending-export count in `docs/dev/debt/libSceLibreSsl.md` goes from 340 to 335: `Export.cpp` has 340 unknown-signature stubs on `main` `5bdda3661e` (including the five `sceLibreSsl*` exports) and 335 after this change. ### Tested Windows 11 Pro 23H2, WinLibs GCC 15.2.0 r7, Release, `BUILD_TESTING=ON`, with `main` `5bdda3661e` merged in (`ad31835a6e`). The merge only resolved the test registrations next to the new alert and version tests, took `main`'s digest debt wording, and updated the pending count. - New `guest_libressl_crypto_locks`: `CRYPTO_num_locks` returns 41. The locking and add-lock setters accept a function and null. `CRYPTO_THREADID_set_callback` returns 1 twice for null, 1 for the first function, then 0 for another function, the same function and null. `CRYPTO_THREADID_set_numeric` clears a `0xaa`-filled structure to `{nullptr, 0x1234567890abcdef}` and a `0x55`-filled one to all zero bytes, and throws for a null structure. On `main` each of these calls throws through `NotImplemented_nid_no_patch`. - `ctest -R "guest_libressl|patched_libraries|^public_headers$"`: 16 of 16 pass (`public_headers`, `patched_libraries`, `guest_libressl_x509_errors`, `guest_libressl_digest`, `guest_libressl_asn1`, `guest_libressl_asn1_allocator`, `guest_libressl_des`, `guest_libressl_runtime`, `guest_libressl_aes`, `guest_libressl_error`, `guest_libressl_ocsp`, `guest_libressl_cipher`, `guest_libressl_alert`, `guest_libressl_version`, `guest_libressl_crypto_locks`, `guest_libressl3_digest`). - The full build and the `libs` target complete. `tools/check_test_registration.py build`: 1397 test executables, none built and not run. - Firmware: [BunnyFl ran firmware 13.42's `libSceLibreSsl.sprx`](https://github.com/boykopovar/AnyPS5/pull/10248#issuecomment-6402989433) in an emulator and it matches all five: 41 locks, the 16-byte `{ptr, val}` write, first-set-wins for the thread id callback, and setters that only store. - `python3 tools/check_conventions.py --base origin/main` passes. Overlap: no other open PR changes or names these five exports (checked 2026-10-25). #9529, whose error queue does not use a thread id callback, has merged; this branch keeps its test registration. ### 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-23 20:02:48 +00:00 (Migrated from github.com)

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

Review: DISMISSED


The implementation changes five distinct LibreSSL exports and the code/test diff is internally consistent with the cited 2.5.4 source. There is a merge blocker: this PR says Depends on: none and GitHub currently reports DIRTY. A pairwise merge check against the open #9529 (git merge-tree --write-tree upstream/pr/9529 upstream/pr/10248) also has content conflicts in core/libs/prx/libSceLibreSsl/CMakeLists.txt and docs/dev/debt/libSceLibreSsl.md. Please coordinate the landing order with the overlapping PR authors, declare required prerequisites in the template, rebase/resolve against current main, and verify all test registrations and the pending-export count are retained. Current conventions, intake, labels and report checks pass; product/relinker jobs are skipped by path policy. I did not run the tests locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5633799755 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10248#pullrequestreview-5633799755). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T20:02:48Z. **Review:** DISMISSED --- The implementation changes five distinct LibreSSL exports and the code/test diff is internally consistent with the cited 2.5.4 source. There is a merge blocker: this PR says `Depends on: none` and GitHub currently reports `DIRTY`. A pairwise merge check against the open #9529 (`git merge-tree --write-tree upstream/pr/9529 upstream/pr/10248`) also has content conflicts in `core/libs/prx/libSceLibreSsl/CMakeLists.txt` and `docs/dev/debt/libSceLibreSsl.md`. Please coordinate the landing order with the overlapping PR authors, declare required prerequisites in the template, rebase/resolve against current main, and verify all test registrations and the pending-export count are retained. Current conventions, intake, labels and report checks pass; product/relinker jobs are skipped by path policy. I did not run the tests locally.
ahmadmuj commented 2026-10-25 18:57:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ahmadmuj on 2026-10-25T18:57:56Z.

Review: APPROVED


Reviewed head c15dce4, merged locally onto main a7a2eec (base when I started). Ubuntu 24.04 on WSL2, GCC 13.3, Release.

What I ran:

  • ctest -R "^(patched_libraries|guest_libressl)": 12/12 pass, including guest_libressl_crypto_locks.
  • Red control: put the five bodies back to NotImplemented_nid_no_patch (same signatures). The new test aborts on the first call, CRYPTO_num_locks_nid_postfix not implemented.
  • Differential against a real LibreSSL 2.5.4 libcrypto.a: the same call sequence through the PR's exports and through 2.5.4, with the output diffed. 38 lines each, no differences. It covered sizeof/offsetof of CRYPTO_THREADID (16, ptr 0, val 8), CRYPTO_num_locks (41), and CRYPTO_THREADID_set_callback with NULL, NULL, f1, f2, f1, NULL (1 1 1 0 0 0) plus two fresh-process orders. It also covered CRYPTO_THREADID_set_numeric on 0xaa/0x55-filled structs with 0x1234567890abcdef, 0 and ~0, with guard bytes on both sides, and none were touched. On the 2.5.4 side I read the getters back to confirm that the lock setters just store and that NULL overwrites.
  • Read 2.5.4 cryptlib.c/crypto.h. OPENSSL_init() in CRYPTO_set_locking_callback is empty in o_init.c, so skipping it is fine.
  • 3.20 export table: the only CRYPTO_ exports are the 9 listed in the description. There's no CRYPTO_lock, CRYPTO_add_lock, CRYPTO_THREADID_current or getter, so the "stored, never called" claim holds.
  • check_conventions.py --base origin/main passes, and the pending stub count went 345 -> 340 on that base.

Code looks right to me.

Two things:

  1. Needs another rebase. #9949 (OCSP strings) landed and main's debt entry now says 342. The PR again conflicts in docs/dev/debt/libSceLibreSsl.md only, and after merging Export.cpp has 337 NotImplemented_nid_no_patch bodies, so the number should be 337, not 340. Code files merge cleanly with current main (8a29e08).

  2. Small debt wording suggestion, not blocking. In 2.5.4 the thread id callback is also reachable from exports this library already has: ERR_get_error/ERR_peek_error/ERR_clear_error -> ERR_get_state -> CRYPTO_THREADID_current -> threadid_callback (err.c:1027, cryptlib.c:463). Here the queue is thread_local in src/Error.cpp. For example, a title whose callback returns one constant id for every thread would share one error queue on the console but get one per thread here. Nothing pushes errors yet, so it isn't observable today. Still, one clause in the entry saying the error queue is keyed by host thread, not by the guest's thread id callback, would save the next person a dig.

Not checked: anything on a console.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5644755657 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10248#pullrequestreview-5644755657). Originally posted by [ahmadmuj](https://github.com/ahmadmuj) on 2026-10-25T18:57:56Z. **Review:** APPROVED --- Reviewed head `c15dce4`, merged locally onto main `a7a2eec` (base when I started). Ubuntu 24.04 on WSL2, GCC 13.3, Release. What I ran: - `ctest -R "^(patched_libraries|guest_libressl)"`: 12/12 pass, including `guest_libressl_crypto_locks`. - Red control: put the five bodies back to `NotImplemented_nid_no_patch` (same signatures). The new test aborts on the first call, `CRYPTO_num_locks_nid_postfix not implemented`. - Differential against a real LibreSSL 2.5.4 `libcrypto.a`: the same call sequence through the PR's exports and through 2.5.4, with the output diffed. 38 lines each, no differences. It covered `sizeof`/`offsetof` of `CRYPTO_THREADID` (16, ptr 0, val 8), `CRYPTO_num_locks` (41), and `CRYPTO_THREADID_set_callback` with NULL, NULL, f1, f2, f1, NULL (1 1 1 0 0 0) plus two fresh-process orders. It also covered `CRYPTO_THREADID_set_numeric` on 0xaa/0x55-filled structs with 0x1234567890abcdef, 0 and ~0, with guard bytes on both sides, and none were touched. On the 2.5.4 side I read the getters back to confirm that the lock setters just store and that NULL overwrites. - Read 2.5.4 `cryptlib.c`/`crypto.h`. `OPENSSL_init()` in `CRYPTO_set_locking_callback` is empty in `o_init.c`, so skipping it is fine. - 3.20 export table: the only `CRYPTO_` exports are the 9 listed in the description. There's no `CRYPTO_lock`, `CRYPTO_add_lock`, `CRYPTO_THREADID_current` or getter, so the "stored, never called" claim holds. - `check_conventions.py --base origin/main` passes, and the pending stub count went 345 -> 340 on that base. Code looks right to me. Two things: 1. Needs another rebase. #9949 (OCSP strings) landed and main's debt entry now says 342. The PR again conflicts in `docs/dev/debt/libSceLibreSsl.md` only, and after merging `Export.cpp` has 337 `NotImplemented_nid_no_patch` bodies, so the number should be 337, not 340. Code files merge cleanly with current main (`8a29e08`). 2. Small debt wording suggestion, not blocking. In 2.5.4 the thread id callback is also reachable from exports this library already has: `ERR_get_error`/`ERR_peek_error`/`ERR_clear_error` -> `ERR_get_state` -> `CRYPTO_THREADID_current` -> `threadid_callback` (`err.c:1027`, `cryptlib.c:463`). Here the queue is `thread_local` in `src/Error.cpp`. For example, a title whose callback returns one constant id for every thread would share one error queue on the console but get one per thread here. Nothing pushes errors yet, so it isn't observable today. Still, one clause in the entry saying the error queue is keyed by host thread, not by the guest's thread id callback, would save the next person a dig. Not checked: anything on a console.
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!10248
No description provided.