Skip to content

crypto: OpenSSL memory option (OpenSSL allocations through a std::pmr resource) - #3

Merged
pinhopro merged 33 commits into
mainfrom
crypto/openssl-memory
Nov 18, 2026
Merged

pinhopro merged 33 commits into
mainfrom
crypto/openssl-memory

Conversation

@pinhopro

@pinhopro pinhopro commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

What the option does

webcpp::crypto's OpenSSL memory option routes every allocation OpenSSL makes in the process through a std::pmr memory resource that the program chooses, by installing three adapter functions with CRYPTO_set_mem_functions (install_memory_functions(), called first in main). 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() and openssl_memory_resource() API, the memory tests and example, and the guide page doc/memory.adoc.

Verification

From the stream's verify and final-fix reports:

  • Full lint, clean after the final fixes.
  • Five lanes, -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.
  • C suites (x4), docs and site builds, and the tools tests: green.
  • Linux gcc-15 container: 27/27 (29 after the final fixes).
  • Local MSVC lane (Wine): 14.3 and 14.5 green except the 8 known Wine failures. The memory link-fail cases fail on LNK2019/LNK1120 and LNK2038 as required.

Ruling on a failing resource

A resource that fails during OpenSSL's base initialisation is unrecoverable on a threaded OpenSSL (MSVC always, threading=multi elsewhere): 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 every make returns backend_failure. fail_init pins the single-threaded behaviour; fail_init_threads pins the threaded boundary. The guide, the Doc Comments, the guidelines and the proposals say the same.

Related PRs

🤖 Generated with Claude Code

R added 30 commits November 17, 2026 11:18
…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
…'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.
…itionally, and fail_init pins OpenSSL 3.5.9's dead queue and unfreed blocks
…mory option's two lines merged into OpenSSL's
… 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
…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
…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
R added 3 commits November 18, 2026 05:06
…during OpenSSL's base initialisation, not any of its initialisation, is unrecoverable on a threaded OpenSSL
@pinhopro
pinhopro merged commit 175e20b into main Nov 18, 2026
29 of 30 checks passed
@pinhopro
pinhopro deleted the crypto/openssl-memory branch November 18, 2026 17:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant