feat(libSceLibreSsl): implement the per-thread error queue #9529

Merged
2Swagger merged 12 commits from upstream-pr/9529 into main 2026-10-25 02:17:22 +00:00
2Swagger commented 2026-10-22 00:18:39 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by 2Swagger on 2026-10-22T00:18:39Z.


What

On main, ERR_get_error, ERR_peek_error, ERR_clear_error and ERR_remove_thread_state are no-parameter stubs that throw NotImplemented_nid_no_patch. This implements them as LibreSSL's err.c and err.h do, at the same commit #9025 cites.

No export of libSceLibreSsl adds an error yet, so guest code always sees an empty queue. The queue is still LibreSSL's general behaviour, not a constant 0. Later LibreSsl implementations (BN, EVP, SSL) can report failures into it through LibreSslPutError_nid_no_patch (include/Error.hpp). That is the same packing as ERR_put_error, which is not in the PS5 export table.

  • Each thread has its own queue of 16 slots, which holds 15 codes, as ERR_NUM_ERRORS and ERR_put_error do. A 16th push drops the oldest code.
  • ERR_get_error returns the earliest code and removes it. ERR_peek_error returns it and keeps it. Both return 0 on an empty queue.
  • ERR_clear_error empties the calling thread's queue.
  • ERR_remove_thread_state(NULL) drops the calling thread's queue.
  • Codes are packed as ERR_PACK: library & 0xff in bits 24-31, function & 0xfff in bits 12-23, reason & 0xfff in bits 0-11. They are returned as std::uint64_t, since LibreSSL returns unsigned long, which is 64 bits on the PS5 and 32 bits on Windows.
  • The per-thread state is a trivially destructible thread_local struct of integers only, enforced by a static_assert.

The PS5 module's LibreSSL version is unknown, so the case that differs between versions throws. ERR_remove_thread_state with a non-null CRYPTO_THREADID asserts in the cited version, but removes that thread's state in older ones. The queue keeps no file, line or data, as no export reads them.

Left out on purpose:

  • ERR_error_string and ERR_error_string_n: their text depends on whether the error strings are loaded. The cited version loads them through OPENSSL_init_crypto and prints names such as unknown library even for code 0. Older versions print lib(0):func(0):reason(0) until ERR_load_crypto_strings or SSL_load_error_strings is called. With the version unknown, every code would have to throw.
  • ERR_peek_last_error: it is exported only by libSceLibreSsl3, a separate module with no ERR_get_error and no export that adds errors. Whether it shares a queue with libSceLibreSsl is unknown.

The debt entry in docs/dev/debt/libSceLibreSsl.md records the above, and the pending count drops from 399 to 395.

core/libs/prx/libSceLibreSsl/CMakeLists.txt is new here, as it is in the other open LibreSsl PRs (#8956, #9025, #9033, #9041, #9043, #9047, #9446, #9448). Whichever lands first, I will rebase onto it and keep both source files and tests.

Tested

Windows 11 Pro 10.0.26200, WinLibs GCC 15.2.0, Release, BUILD_TESTING=ON, on head d21dd0637 (this branch merged with main).

Before: on main, all four exports are stubs that call NotImplemented_nid_no_patch (core/libs/prx/libSceLibreSsl/Export.cpp lines 515, 535, 540 and 545), and no LibreSsl error test exists. The new test was not run against main.

After: guest_libressl_error passes. Each case it checks passed:

  • Empty queue: ERR_get_error and ERR_peek_error return 0.
  • Order: after three pushes, peek returns the first code twice without removing it. Get returns the three codes in push order, peek shows the next one each time, and get and peek return 0 once the queue is drained.
  • Overflow: after 16 pushes, get returns codes 2 to 16 and then 0. The oldest code is dropped and 15 are kept.
  • Masking: pushing library 0x1ff, function 0x1fff and reason 0x1fff returns exactly 0xFFFFFFFF. This checks that each field is masked as ERR_PACK does. It does not check the 64-bit return width: no code uses the upper 32 bits, so a 32-bit return type would pass too.
  • Clear: ERR_clear_error empties a queue holding two codes, and a later push and get work.
  • Per thread: a second std::thread starts with an empty queue and reads only its own code. The main thread keeps its code, and the other thread's code never appears on it.
  • ERR_remove_thread_state(nullptr) empties the queue.

Also:

  • guest_libressl_error, guest_libressl_digest, guest_libressl_des, guest_libressl_x509_errors, guest_libressl_runtime, guest_libressl3_digest, guest_ssl, guest_ssl_load, guest_ssl_ps5_readers, guest_ssl_transport_kernel and guest_ssl_transport_net pass (11/11, 0.86 s).
  • The test targets and the libraries they load build with no warnings or errors in this PR's files.
  • python tools/check_conventions.py --base upstream/main: all checks passed.
  • python tools/check_test_registration.py build: passed, every test executable is registered with ctest.
  • git merge-tree --write-tree upstream/main HEAD: clean.

Linux: Ubuntu 24.04 under WSL2, GCC 13.3.0, Release, BUILD_TESTING=ON: guest_libressl_error passes on d21dd0637.

A reviewer reported a passing Linux run (Ubuntu 26.04, GCC 15.2.0), including a clean run under ASan and UBSan, in the comments; that run was on the original commit ca32e706e, before the refreshes. Not tested on macOS. Not tested with a title.

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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4960132796 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9529). Originally posted by [2Swagger](https://github.com/2Swagger) on 2026-10-22T00:18:39Z. --- ### What On `main`, `ERR_get_error`, `ERR_peek_error`, `ERR_clear_error` and `ERR_remove_thread_state` are no-parameter stubs that throw `NotImplemented_nid_no_patch`. This implements them as LibreSSL's [err.c](https://github.com/libressl/openbsd/blob/bc9483725c5815efb64b6d9acc52fc5fba139212/src/lib/libcrypto/err/err.c) and [err.h](https://github.com/libressl/openbsd/blob/bc9483725c5815efb64b6d9acc52fc5fba139212/src/lib/libcrypto/err/err.h) do, at the same commit #9025 cites. No export of libSceLibreSsl adds an error yet, so guest code always sees an empty queue. The queue is still LibreSSL's general behaviour, not a constant 0. Later LibreSsl implementations (BN, EVP, SSL) can report failures into it through `LibreSslPutError_nid_no_patch` (`include/Error.hpp`). That is the same packing as `ERR_put_error`, which is not in the PS5 export table. - Each thread has its own queue of 16 slots, which holds 15 codes, as `ERR_NUM_ERRORS` and `ERR_put_error` do. A 16th push drops the oldest code. - `ERR_get_error` returns the earliest code and removes it. `ERR_peek_error` returns it and keeps it. Both return 0 on an empty queue. - `ERR_clear_error` empties the calling thread's queue. - `ERR_remove_thread_state(NULL)` drops the calling thread's queue. - Codes are packed as `ERR_PACK`: library `& 0xff` in bits 24-31, function `& 0xfff` in bits 12-23, reason `& 0xfff` in bits 0-11. They are returned as `std::uint64_t`, since LibreSSL returns `unsigned long`, which is 64 bits on the PS5 and 32 bits on Windows. - The per-thread state is a trivially destructible `thread_local` struct of integers only, enforced by a `static_assert`. The PS5 module's LibreSSL version is unknown, so the case that differs between versions throws. `ERR_remove_thread_state` with a non-null `CRYPTO_THREADID` asserts in the cited version, but removes that thread's state in older ones. The queue keeps no file, line or data, as no export reads them. Left out on purpose: - `ERR_error_string` and `ERR_error_string_n`: their text depends on whether the error strings are loaded. The cited version loads them through `OPENSSL_init_crypto` and prints names such as `unknown library` even for code 0. Older versions print `lib(0):func(0):reason(0)` until `ERR_load_crypto_strings` or `SSL_load_error_strings` is called. With the version unknown, every code would have to throw. - `ERR_peek_last_error`: it is exported only by libSceLibreSsl3, a separate module with no `ERR_get_error` and no export that adds errors. Whether it shares a queue with libSceLibreSsl is unknown. The debt entry in `docs/dev/debt/libSceLibreSsl.md` records the above, and the pending count drops from 399 to 395. `core/libs/prx/libSceLibreSsl/CMakeLists.txt` is new here, as it is in the other open LibreSsl PRs (#8956, #9025, #9033, #9041, #9043, #9047, #9446, #9448). Whichever lands first, I will rebase onto it and keep both source files and tests. ### Tested Windows 11 Pro 10.0.26200, WinLibs GCC 15.2.0, Release, `BUILD_TESTING=ON`, on head d21dd0637 (this branch merged with `main`). Before: on `main`, all four exports are stubs that call `NotImplemented_nid_no_patch` (`core/libs/prx/libSceLibreSsl/Export.cpp` lines 515, 535, 540 and 545), and no LibreSsl error test exists. The new test was not run against `main`. After: `guest_libressl_error` passes. Each case it checks passed: - Empty queue: `ERR_get_error` and `ERR_peek_error` return 0. - Order: after three pushes, peek returns the first code twice without removing it. Get returns the three codes in push order, peek shows the next one each time, and get and peek return 0 once the queue is drained. - Overflow: after 16 pushes, get returns codes 2 to 16 and then 0. The oldest code is dropped and 15 are kept. - Masking: pushing library `0x1ff`, function `0x1fff` and reason `0x1fff` returns exactly `0xFFFFFFFF`. This checks that each field is masked as `ERR_PACK` does. It does not check the 64-bit return width: no code uses the upper 32 bits, so a 32-bit return type would pass too. - Clear: `ERR_clear_error` empties a queue holding two codes, and a later push and get work. - Per thread: a second `std::thread` starts with an empty queue and reads only its own code. The main thread keeps its code, and the other thread's code never appears on it. - `ERR_remove_thread_state(nullptr)` empties the queue. Also: - `guest_libressl_error`, `guest_libressl_digest`, `guest_libressl_des`, `guest_libressl_x509_errors`, `guest_libressl_runtime`, `guest_libressl3_digest`, `guest_ssl`, `guest_ssl_load`, `guest_ssl_ps5_readers`, `guest_ssl_transport_kernel` and `guest_ssl_transport_net` pass (11/11, 0.86 s). - The test targets and the libraries they load build with no warnings or errors in this PR's files. - `python tools/check_conventions.py --base upstream/main`: all checks passed. - `python tools/check_test_registration.py build`: passed, every test executable is registered with ctest. - `git merge-tree --write-tree upstream/main HEAD`: clean. Linux: Ubuntu 24.04 under WSL2, GCC 13.3.0, Release, `BUILD_TESTING=ON`: `guest_libressl_error` passes on d21dd0637. A reviewer reported a passing Linux run (Ubuntu 26.04, GCC 15.2.0), including a clean run under ASan and UBSan, in the comments; that run was on the original commit ca32e706e, before the refreshes. Not tested on macOS. Not tested with a title. ### 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 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-22 00:23:58 +00:00 (Migrated from github.com)

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

Review: CHANGES_REQUESTED


The four queue functions and their tests look internally consistent with the cited LibreSSL implementation: the 16-slot ring retains 15 codes, preserves FIFO get/peek semantics, masks ERR_PACK fields, and keeps state thread-local. The focused test and full-build outcome are author-reported; I did not run a local build. Current hosted conventions, intake, and labels checks pass, while no product build/test job is present.

There is one merge-order blocker under CONTRIBUTING.md. The current overlap scan reports shared new core/libs/prx/libSceLibreSsl/CMakeLists.txt and docs/dev/debt/libSceLibreSsl.md work with #9025, #9033, #9041, #9043, #9047, #9446, and #9448. This PR says it will rebase whichever lands first, but the checklist says Depends on: none, and no ordering agreement is recorded. Please coordinate with the affected authors on which shared-file change lands first, then declare the agreed prerequisite in Depends on and rebase/resolve the combined CMake and debt changes. I’m requesting changes for that repository-required overlap coordination, not for the queue implementation.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5607829351 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9529#pullrequestreview-5607829351). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T00:23:58Z. **Review:** CHANGES_REQUESTED --- The four queue functions and their tests look internally consistent with the cited LibreSSL implementation: the 16-slot ring retains 15 codes, preserves FIFO get/peek semantics, masks ERR_PACK fields, and keeps state thread-local. The focused test and full-build outcome are author-reported; I did not run a local build. Current hosted conventions, intake, and labels checks pass, while no product build/test job is present. There is one merge-order blocker under CONTRIBUTING.md. The current overlap scan reports shared new `core/libs/prx/libSceLibreSsl/CMakeLists.txt` and `docs/dev/debt/libSceLibreSsl.md` work with #9025, #9033, #9041, #9043, #9047, #9446, and #9448. This PR says it will rebase whichever lands first, but the checklist says `Depends on: none`, and no ordering agreement is recorded. Please coordinate with the affected authors on which shared-file change lands first, then declare the agreed prerequisite in `Depends on` and rebase/resolve the combined CMake and debt changes. I’m requesting changes for that repository-required overlap coordination, not for the queue implementation.
2Swagger commented 2026-10-22 00:35:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by 2Swagger on 2026-10-22T00:35:55Z.


Thanks for the review, @MarcZX10. No overall order has been agreed among the overlapping LibreSsl PRs, so I'm proposing that this PR goes last and have declared Depends on: #9025, #9033, #9041, #9043, #9047, #9446, #9448. Nobody else needs to rebase because of it; I'll rebase onto each one as it lands (one source and one test in the shared CMakeLists.txt, plus the debt file's pending count).

@HQasper @VakzOs @AlxcBTW, could you confirm you're fine with this PR merging after yours? If you'd prefer a different order, say so here and I'll update Depends on.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6329770410 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9529#issuecomment-6329770410). Originally posted by [2Swagger](https://github.com/2Swagger) on 2026-10-22T00:35:55Z. --- Thanks for the review, @MarcZX10. No overall order has been agreed among the overlapping LibreSsl PRs, so I'm proposing that this PR goes last and have declared Depends on: #9025, #9033, #9041, #9043, #9047, #9446, #9448. Nobody else needs to rebase because of it; I'll rebase onto each one as it lands (one source and one test in the shared CMakeLists.txt, plus the debt file's pending count). @HQasper @VakzOs @AlxcBTW, could you confirm you're fine with this PR merging after yours? If you'd prefer a different order, say so here and I'll update Depends on.
VakzOs commented 2026-10-22 07:30:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by VakzOs on 2026-10-22T07:30:21Z.


Fine with me, thanks for coordinating. #9047 doesn’t depend on any of the other LibreSsl PRs: if one of #9025, #9033, #9041, #9043, #9446 or #9448 lands before it, I’ll rebase #9047 onto it (one source/test entry in the shared CMakeLists.txt plus the debt file’s pending count), so nobody has to wait for it.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6335291243 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9529#issuecomment-6335291243). Originally posted by [VakzOs](https://github.com/VakzOs) on 2026-10-22T07:30:21Z. --- Fine with me, thanks for coordinating. #9047 doesn’t depend on any of the other LibreSsl PRs: if one of #9025, #9033, #9041, #9043, #9446 or #9448 lands before it, I’ll rebase #9047 onto it (one source/test entry in the shared CMakeLists.txt plus the debt file’s pending count), so nobody has to wait for it.
jotitadev commented 2026-10-22 07:42:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-22T07:42:54Z.

Review: COMMENTED


Linux run, since the description says it was not tested on Linux: it passes, and the per-thread queue runs clean under ASan and UBSan.

Machine: Linux, Ubuntu 26.04, GCC 15.2.0, x86-64. Head ca32e706e merged with git merge --no-commit onto current main d6eafa54d (clean).

  • Release (Ninja, BUILD_TESTING=ON, ANYPS5_RELINKER_ONLY=OFF): guest_libressl_error and patched_libraries pass (2/2).
  • ASan + UBSan (-fsanitize=address,undefined -fno-sanitize-recover=undefined -O1 -g, ANYPS5_ENABLE_ASAN=ON, leak detection off): guest_libressl_error_tests prints guest_libressl_error: ok and exits 0 with no reports.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5610784614 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9529#pullrequestreview-5610784614). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-22T07:42:54Z. **Review:** COMMENTED --- Linux run, since the description says it was not tested on Linux: it passes, and the per-thread queue runs clean under ASan and UBSan. **Machine:** Linux, Ubuntu 26.04, GCC 15.2.0, x86-64. Head `ca32e706e` merged with `git merge --no-commit` onto current `main` `d6eafa54d` (clean). - **Release** (Ninja, `BUILD_TESTING=ON`, `ANYPS5_RELINKER_ONLY=OFF`): `guest_libressl_error` and `patched_libraries` pass (2/2). - **ASan + UBSan** (`-fsanitize=address,undefined -fno-sanitize-recover=undefined -O1 -g`, `ANYPS5_ENABLE_ASAN=ON`, leak detection off): `guest_libressl_error_tests` prints `guest_libressl_error: ok` and exits 0 with no reports. Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
MarcZX10 commented 2026-10-22 11:38:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T11:38:14Z.


I rechecked current head ca32e706e8: current conventions and diff-check pass, and it merges cleanly with today’s main. The focused Windows and independent Linux queue tests pass per the reports in this PR. The branch is 1 ahead / 119 behind current main (merge base a6dcea927); please refresh and rerun guest_libressl_error, conventions, test-registration and the updated overlap scan. The Depends on list now declares the shared-file landing order, and #9047’s author has acknowledged it; please keep the remaining author coordination up to date before merge.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6339806918 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9529#issuecomment-6339806918). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T11:38:14Z. --- I rechecked current head `ca32e706e8`: current conventions and diff-check pass, and it merges cleanly with today’s `main`. The focused Windows and independent Linux queue tests pass per the reports in this PR. The branch is 1 ahead / 119 behind current `main` (merge base `a6dcea927`); please refresh and rerun `guest_libressl_error`, conventions, test-registration and the updated overlap scan. The `Depends on` list now declares the shared-file landing order, and #9047’s author has acknowledged it; please keep the remaining author coordination up to date before merge.
MarcZX10 commented 2026-10-23 20:01:05 +00:00 (Migrated from github.com)

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

Review: APPROVED


Reviewed current head 43315af3954f372e49f2e7047f187b7883aa2c7c (exactly 43315af395): the per-thread 16-slot/15-error ring, FIFO get/peek behavior, masking, clear, and null thread-state removal match the cited LibreSSL implementation; unsupported non-null thread IDs still throw and are documented. The focused test covers ordering, overflow, masking, thread isolation, and clear/removal. The author reports 11/11 focused and related tests on this exact head, and git merge-tree with current main is clean; current hosted conventions and labels checks pass. The branch explicitly depends on older shared-file PRs #9033, #9446, and #9448; newer LibreSSL changes should stack on this PR where required. I did not run the tests locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5633782631 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9529#pullrequestreview-5633782631). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T20:01:05Z. **Review:** APPROVED --- Reviewed current head 43315af3954f372e49f2e7047f187b7883aa2c7c (exactly 43315af3954f372e49f2c7047f187b7883aa2c7c): the per-thread 16-slot/15-error ring, FIFO get/peek behavior, masking, clear, and null thread-state removal match the cited LibreSSL implementation; unsupported non-null thread IDs still throw and are documented. The focused test covers ordering, overflow, masking, thread isolation, and clear/removal. The author reports 11/11 focused and related tests on this exact head, and `git merge-tree` with current main is clean; current hosted conventions and labels checks pass. The branch explicitly depends on older shared-file PRs #9033, #9446, and #9448; newer LibreSSL changes should stack on this PR where required. I did not run the tests locally.
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!9529
No description provided.