Skip to content

feat(libSceLibreSsl): implement the SSL and SSL_CTX ex_data index registry as LibreSSL 2.5.4 does - #11340

Merged
adriadam10 merged 5 commits into
boykopovar:mainfrom
harisibrar1:feat/libressl-ex-data-indices
Oct 27, 2026
Merged

adriadam10 merged 5 commits into
boykopovar:mainfrom
harisibrar1:feat/libressl-ex-data-indices

Conversation

@harisibrar1

@harisibrar1 harisibrar1 commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

What

SSL_get_ex_new_index, SSL_CTX_get_ex_new_index, SSL_get_ex_data_X509_STORE_CTX_idx and CRYPTO_cleanup_all_ex_data are parameterless placeholders that throw. This implements them as LibreSSL 2.5.4 does. Discussion #8981 places 2.5.4 on firmware 13.40.

In 2.5.4:

  • SSL_get_ex_new_index and SSL_CTX_get_ex_new_index (ssl_lib.c:2828-2854) call CRYPTO_get_ex_new_index with class 1 (CRYPTO_EX_INDEX_SSL) or 2 (CRYPTO_EX_INDEX_SSL_CTX).
  • That (ex_data.c:310-406, 553-570) stores argl, argp and the three callbacks for the class and returns the class's next index, from 0. An allocation failure queues CRYPTOerror(ERR_R_MALLOC_FAILURE) (library 15, reason 65) and returns -1.
  • SSL_get_ex_data_X509_STORE_CTX_idx (ssl_cert.c:133-160) allocates one index in class 5 (X509_STORE_CTX) with argp "SSL for verify callback" the first time and caches it in a static.
  • CRYPTO_cleanup_all_ex_data frees every class's table, so numbering restarts at 0. The cached verify-callback index isn't reset.

The callbacks are declared as LibreSslExNew, LibreSslExDup and LibreSslExFree, the guest-ABI forms of CRYPTO_EX_new, CRYPTO_EX_dup and CRYPTO_EX_free (ossl_typ.h:163-168), so callers can pass real callbacks. src/ExData.cpp keeps one list per class under a mutex and follows those rules. std::bad_alloc from the list is caught and reported as the 2.5.4 malloc failure, so no host exception reaches the guest. No other export of this library allocates in class 5, so the verify-callback index is always 0. CRYPTO_cleanup_all_ex_data returns void as in 2.5.4.

The stored argl, argp and callbacks are never called: no export creates an SSL or SSL_CTX, and SSL_get_ex_data/SSL_set_ex_data still throw. The new debt entry says so. #10248 leaves CRYPTO_cleanup_all_ex_data throwing "because no ex_data is implemented yet", and #10878 lists it as still throwing; this adds that piece.

Tested

Windows 11, WinLibs GCC 15.2.0 r7, full Release build of d91daf8 (current main merged in).

  • New guest_libressl_ex_data (tests/ExData.cpp):
    • SSL indices 0, 1, 2 and SSL_CTX indices 0, 1, counted apart.
    • The verify-callback index is 0, stays 0, and doesn't take an SSL index.
    • After cleanup, SSL and SSL_CTX restart at 0 while the verify-callback index stays 0; repeated cleanup is harmless.
    • Non-null callbacks with the LibreSSL signatures are accepted by both functions and never called (they abort if called).
    • 8 threads taking 64 SSL indices get 64 distinct ones.
  • All 13 guest_libressl* tests pass, 3 runs; the full build had no failed steps.
  • Negative control: a mutant where every class shares one counter fails guest_libressl_ex_data at "SSL_CTX indices are counted apart from SSL ones" (2 runs). Main's placeholders take no arguments, so the test can't be built against them.
  • tools/check_conventions.py --base origin/main passes.
  • Not tested: the allocation-failure path (it needs a failing host allocator), Linux and macOS, or a console. The behaviour follows the 2.5.4 sources.

Overlap

pr_overlap finds no PR implementing these exports. The conflicts are text-only:

Whichever lands second keeps both.

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

🤖 Generated with Claude Code

…istry as LibreSSL 2.5.4 does

SSL_get_ex_new_index and SSL_CTX_get_ex_new_index number their indices
per class from 0, SSL_get_ex_data_X509_STORE_CTX_idx allocates its
class-5 index once, and CRYPTO_cleanup_all_ex_data releases every class.
An allocation failure returns -1 with ERR_R_MALLOC_FAILURE queued.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Progress report (74acfad → 63ef393)

📈 System libraries: 51.86% (+0.03%, +4 implemented functions)

✅ 4 implemented
Library Name
libSceLibreSsl CRYPTO_cleanup_all_ex_data_nid_postfix
libSceLibreSsl SSL_CTX_get_ex_new_index_nid_postfix
libSceLibreSsl SSL_get_ex_data_X509_STORE_CTX_idx_nid_postfix
libSceLibreSsl SSL_get_ex_new_index_nid_postfix

Changed libraries (implemented exports)

Library Before After Change
libSceLibreSsl 77/399 (19.30%) 81/399 (20.30%) +1.00 pp

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The registered class indices and cleanup behavior match the cited LibreSSL 2.5.4 implementation, and the new test covers separate counters, cached store-context indexing, cleanup and concurrent registrations. I independently compiled src/ExData.cpp with tests/ExData.cpp using GCC 12 (-std=c++20 -Wall -Wextra -Werror -pthread); those test cases passed with a no-op stub for LibreSslPutError_nid_no_patch. This was a focused source-level check, not a full product build or allocation-failure test.

The branch is 32 commits behind current main (merge preview is clean). CONTRIBUTING.md requires branches to be based on and kept up to date with main; please rebase and rerun the focused guest_libressl_ex_data test on the resulting head before approval. The hosted product build jobs are path-skipped.

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocking API issue: both new declarations use void* for newFunction, dupFunction and freeFunction. LibreSSL 2.5.4 declares these as callback function pointers (CRYPTO_EX_new*, CRYPTO_EX_dup*, CRYPTO_EX_free*) in its SSL API. C++ does not implicitly convert function pointers to object pointers, so a client with valid typed callbacks cannot call either declaration.

I confirmed this with a minimal source including this PR's Api.hpp and passing callbacks with the LibreSSL signatures: g++ -std=c++20 -Wall -Wextra -Werror fails on all three callback arguments to SSL_get_ex_new_index_nid_postfix. Please declare callback-compatible function pointer types for the target ABI, use them in both public declarations and implementations, and add a compile/regression case that passes non-null callbacks (they can remain stored but uninvoked while SSL/SSL_CTX accessors are unimplemented). Then rerun guest_libressl_ex_data on the updated head.

The previously reported focused test passed, but it passes only null callbacks and therefore does not exercise this API contract. The branch is also 32 commits behind current main; please rebase it while addressing this and rerun the focused test there.

@github-actions

github-actions Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 10395 docs/dev/debt/libSceLibreSsl.md L17
PR 10483 core/libs/prx/libSceLibreSsl/Export.cpp L997-1006
core/libs/prx/libSceLibreSsl/include/Api.hpp L338-339

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 9446 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
docs/dev/debt/libSceLibreSsl.md
PR 9448 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
docs/dev/debt/libSceLibreSsl.md
PR 9535 core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
docs/dev/debt/libSceLibreSsl.md
PR 9536 core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
docs/dev/debt/libSceLibreSsl.md
PR 9571 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
docs/dev/debt/libSceLibreSsl.md
PR 10475 core/libs/prx/libSceLibreSsl/Export.cpp
docs/dev/debt/libSceLibreSsl.md
PR 10508 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
docs/dev/debt/libSceLibreSsl.md
PR 10834 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
docs/dev/debt/libSceLibreSsl.md
PR 10878 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
docs/dev/debt/libSceLibreSsl.md
PR 10915 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
docs/dev/debt/libSceLibreSsl.md
PR 11089 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
docs/dev/debt/libSceLibreSsl.md
PR 11100 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
docs/dev/debt/libSceLibreSsl.md
PR 11105 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
docs/dev/debt/libSceLibreSsl.md
PR 11128 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
docs/dev/debt/libSceLibreSsl.md
PR 11129 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
docs/dev/debt/libSceLibreSsl.md
PR 11138 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
docs/dev/debt/libSceLibreSsl.md
PR 11143 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
docs/dev/debt/libSceLibreSsl.md
PR 11146 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
docs/dev/debt/libSceLibreSsl.md
PR 11151 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
docs/dev/debt/libSceLibreSsl.md
PR 11174 core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
docs/dev/debt/libSceLibreSsl.md
PR 11175 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
docs/dev/debt/libSceLibreSsl.md
PR 11176 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
docs/dev/debt/libSceLibreSsl.md
PR 11189 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
docs/dev/debt/libSceLibreSsl.md
PR 11466 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
docs/dev/debt/libSceLibreSsl.md

harisibrar1 and others added 3 commits October 26, 2026 09:50
…tion pointer types

SSL_get_ex_new_index and SSL_CTX_get_ex_new_index took the new, dup and
free callbacks as void*, which a caller passing real callbacks can't
convert to. They now take LibreSslExNew, LibreSslExDup and
LibreSslExFree, the guest-ABI forms of CRYPTO_EX_new, CRYPTO_EX_dup and
CRYPTO_EX_free from LibreSSL 2.5.4's ossl_typ.h. Reported by @MarcZX10.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 26, 2026
@harisibrar1

Copy link
Copy Markdown
Contributor Author

@MarcZX10 thanks, that was a real API bug and my null-only test hid it. Fixed in a0b14febf9; d91daf899f merges current main.

  • Api.hpp now declares LibreSslExNew, LibreSslExDup and LibreSslExFree, the guest-ABI (APS5_VABI) forms of LibreSSL 2.5.4's CRYPTO_EX_new, CRYPTO_EX_dup and CRYPTO_EX_free from ossl_typ.h:163-168, with LibreSslExData standing in for CRYPTO_EX_DATA. Both declarations and the stored entries use them.
  • tests/ExData.cpp now registers non-null callbacks with those exact signatures through both functions. The callbacks abort if called, so the test also pins that they're stored but never invoked.
  • The debt entry is shortened under the 1000-character check.

Windows 11, GCC 15.2.0, full build on the merged head: all 13 guest_libressl* tests pass in 3 runs, and the shared-counter mutant still fails guest_libressl_ex_data (2 runs). check_conventions passes. Could you take another look?

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

…-indices

# Conflicts:
#	core/libs/prx/libSceLibreSsl/include/Api.hpp
@harisibrar1

Copy link
Copy Markdown
Contributor Author

Merged current main in 63ef393ee9 to resolve the Api.hpp conflict with #10248, keeping its new CRYPTO_THREADID_set_callback/set_numeric signatures next to this PR's void CRYPTO_cleanup_all_ex_data.

Windows 11, GCC 15.2.0, full build of the merged head:

  • guest_libressl_ex_data and the other guest_libressl* tests pass in 3 runs.
  • guest_libressl_version, which came in from main, didn't start here (ctest "Not Run").
  • The shared-counter mutant still fails guest_libressl_ex_data (2 runs).

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 27, 2026

@jotitadev jotitadev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Linux run of the current head, since the description lists only Windows.

Machine: Linux x86_64, i7-8700 (6 cores / 12 threads, AVX2), Ubuntu 26.04, GCC 15.2, Release, BUILD_TESTING=ON. Head 63ef393ee merged onto main cb45ae2dd.

  • Build ok. All 19 guest_libressl* tests pass, including the new guest_libressl_ex_data (50 of 50 repeats), and so do patched_libraries and conventions_unknown_nids.
  • The four exports now live in src/ExData.cpp instead of Export.cpp. In the patched libSceLibreSsl.prx they appear under their NIDs (PGsWNnS7CE8, 2k6m8dF7sPc, E5opvIVafTc, FnLDy4R08cQ) at the same addresses as the _nid_postfix symbols in the unpatched one, so moving them out of Export.cpp does not lose the patching.
  • Replacing the four bodies with main's NotImplemented_nid_no_patch and keeping the test aborts with SSL_get_ex_new_index_nid_postfix not implemented. Registering every index in the SSL class instead of classes[classIndex] fails SSL_CTX indices are counted apart from SSL ones, so the test also checks that the classes are separate.

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

@adriadam10 adriadam10 self-assigned this Oct 27, 2026
@adriadam10
adriadam10 dismissed MarcZX10’s stale review October 27, 2026 08:56

Dismissing a stale review: the requested changes were addressed after it (new head) and the merge queue blocks on it. Please request changes again if anything is still open.

@Gonic0

Gonic0 commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

macOS run of head 63ef393ee9: Apple M5 Pro, macOS 27.2, Apple clang 21, x86-64 Release under Rosetta 2. Tree: main 801d5b9bc1 plus the open macOS build PRs (#7631, #11249, #11358, #10669, #10552, #7804, #11447, #11437, #11554), with this PR's diff on top (the docs/ changes left out).

All 23 guest_libressl* tests pass 3 of 3, including the new guest_libressl_ex_data. Without this PR the other 22 pass too.

AI-assisted: yes

@adriadam10
adriadam10 merged commit 771d8b5 into boykopovar:main Oct 27, 2026
10 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Checked the index numbering and cleanup against LibreSSL 2.5.4 ex_data.c (per-class counter from 0, cleanup frees the class tables so numbering restarts), ssl_lib.c and ssl_cert.c (verify-callback index allocated once and kept across cleanup). The callback types fixed after @MarcZX10's review are the guest-ABI forms of CRYPTO_EX_new/dup/free. The test pins the indices, the separate classes, cleanup and 8-thread distinctness, and these exports threw on main. Windows (author) and Linux (@jotitadev) runs are green; merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants