Repository navigation
crypto: OpenSSL memory option (OpenSSL allocations through a std::pmr resource) - #3
Merged
Merged
Conversation
added 30 commits
November 17, 2026 11:18
…apter, the install, and make's guard
…er test checks the size bookkeeping
…ter test pass clang-tidy: #ifdef for a lone defined(), the max guard's parentheses and the empty catch named in NOLINT lines, a designated initializer and [[nodiscard]] in the test
…y allocation goes to it
…'s counts, and clean on Linux's clang-tidy The counting resource, its constructor, destructor and arm() have Doc Comments, and drain() says what it returns and sets. The table's hash shift is derived from its size, which a static_assert keeps a power of two. The buffer is 16 MiB, from everything_test's counts on OpenSSL 3.5.9 (5,042 blocks, 388,040 bytes, at most 4,452 blocks and 349,350 bytes live). main_returned() is noexcept, everything_test returns its error count through exit_status, the Linux interposer's declarations of glibc's __libc_* functions carry the NOLINT lines clang-tidy asks for, and the OpenSSL header's empty catch has a NOLINT line within 100 columns.
… with exceptions on and off
…itionally, and fail_init pins OpenSSL 3.5.9's dead queue and unfreed blocks
…mory option's two lines merged into OpenSSL's
… sentence, as the reference's check asks
… 1.3 handshake neighbours links Boost.Asio's TLS (with the function that makes a context, so that Asio's openssl_init runs before main) and libdatachannel beside the option; the install, main's first statement, succeeds. handshake runs a TLS 1.3 handshake through libssl over a pair of memory BIOs, a record each way, with no call of the C library's allocator from the key to the last free, no foreign free and OpenSSL's cleanup giving every block back after main.
…hannel's static initialisers it links The test's comment states what the linker's map shows on macOS and Linux: rtcInitLogger brings in every object of libdatachannel.a whose static initialiser names OpenSSL, so R15's fallback is not needed. The handshake's Jamfile comment matches its counted range, from the key to the last free, and its NOLINT reason is a full sentence.
doc/memory.adoc gains "OpenSSL's allocations in your own memory resource": what goes wrong without the option, the three steps (the define and /webcpp/crypto//openssl in the project's requirements, the resource defined once and never destroyed, the install first in main), and its limits: the whole process (neighbours, handshake), CRYPTO_set_mem_functions, one module, a failed first initialisation and the blocks it leaves, the C library's FILE and the secure heap, native only. example/openssl_memory.cpp is its program; doc/jamfile.adoc shows the project form from the superproject's memory_option fixture. crypto.json describes the operation memory_option, a use case, a row of the root namespace and the fixture; generate.py reads test/openssl_memory's tests and an operation's own reason for the targets it misses. config.hpp leaves the define on under __MRDOCS__, so the reference lists the option's declarations. The changelog's OpenSSL line says native only and names the option's functions and errors.
…tags and use-case Jamfile Task 16d's review Minors. The changelog's OpenSSL line says what the option does and that it is refused on WebAssembly, WASI included; the guide carries spec 7.3's condition that a later replacement is caught only when it has finished before the make; the example's resource tag ends right after its brace so the rendered program shows one blank line; the use case's Jamfile cell puts the define and OpenSSL in the project requirements; generate.py refuses a reason elsewhere on a backend other than OpenSSL, where it was read and never shown.
…rce, and generate_test's docstring keeps to 100 columns
…nodiscard]], as the lint's nodiscard-results rule requires
…printing it, with no fluent chain
…which the static libcrypto on Windows needs to give its blocks back, and the page says so
…thout threads, and fail_init_threads pins the boundary on one built with threads On an OpenSSL built with threads (OPENSSL_THREADS: MSVC's only build, b2's threading=multi elsewhere) a resource that fails during OpenSSL's base initialisation makes OpenSSL re-enter its own run-once: CRYPTO_THREAD_lock_new allocates with a file and a line, so CRYPTO_malloc's failure path raises an error, whose state needs the initialisation that is running. The process aborts on macOS, deadlocks on Linux and spins on Windows. threads_none.c allocates its lock with neither, so without threads the failure returns. fail_init keeps its pins where OpenSSL has no threads; with threads it makes the first make with a resource that does not fail, checks that it took blocks of the resource, prints the count, and ends through the exit check. fail_init_threads builds the same program with threading=multi so that both branches run on macOS and Linux.
… failing in OpenSSL's initialisation is unrecoverable on an OpenSSL built with threads The promise that make gives backend_failure from then on holds only on an OpenSSL built without threads; with threads, MSVC's only build, the call that runs OpenSSL's initialisation never returns. The resource must serve it.
…SSL_thread_stop, and the page says what a Windows thread without it costs On Windows the static libcrypto webcpp builds is never told that a thread ends: every thread that used OpenSSL leaves its error queue and handler list in the resource, not even freed at exit, unless it calls OPENSSL_thread_stop(). A fixed-size resource under thread churn runs out. The page shows the call from the example, and openssl_memory_resource's Doc Comment says it.
…s in the program's resource under WEBCPP_CRYPTO_OPENSSL_MEMORY_RESOURCE
…s.txt reads well outside it
…ctions costs: the resource's blocks freed through the other party's free
…es its symbols has its own adapter, and one with default visibility usually shares the executable's
added 3 commits
November 18, 2026 05:06
…during OpenSSL's base initialisation, not any of its initialisation, is unrecoverable on a threaded OpenSSL
…on every path, a failed make included
This was referenced Nov 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What the option does
webcpp::crypto's OpenSSL memory option routes every allocation OpenSSL makes in the process through astd::pmrmemory resource that the program chooses, by installing three adapter functions withCRYPTO_set_mem_functions(install_memory_functions(), called first inmain). It is native only: it is refused on all WebAssembly targets (Emscripten, WASIp2, WASIp3) with a diagnostic naming the target.This PR carries the library side: the adapter, the
install_memory_functions()andopenssl_memory_resource()API, the memory tests and example, and the guide pagedoc/memory.adoc.Verification
From the stream's verify and final-fix reports:
-a: native 180, gcc-15 168, wasip2 141, wasip3 134, emscripten 92, all green. After the final fix wave, native-a(29 memory tests plus docs) and gcc-15 were rerun; the last commits touch native-only docs, Doc Comments and the example.Ruling on a failing resource
A resource that fails during OpenSSL's base initialisation is unrecoverable on a threaded OpenSSL (MSVC always,
threading=multielsewhere): OpenSSL re-enters its own initialisation and the call never returns (abort on macOS, deadlock on Linux, spin on Windows). The resource must serve OpenSSL's start-up. On a single-threaded OpenSSL the failure is permanent and everymakereturnsbackend_failure.fail_initpins the single-threaded behaviour;fail_init_threadspins the threaded boundary. The guide, the Doc Comments, the guidelines and the proposals say the same.Related PRs
libs/cryptogitlink to this branch head, cc2b801)🤖 Generated with Claude Code