Repository navigation
Hold the Noise handshake's ids, prologue and scratch off the heap - #228
Merged
Merged
Conversation
7 tasks done
kahrendt
force-pushed
the
alloc-4-handshake-state
branch
from
November 3, 2026 14:38
cd4d6ce to
930d898
Compare
The handshake copied the server_id from server/init into the driver and its result, and the psk_id from message 1 into the resolved PSK, its counterparty id and the connection, each a std::string past the inline capacity, and server/hello copied the whole ServerInformationObject to set its name. IdText holds a peer_id or psk_id inline (both are 43 characters); a longer psk_id is a lookup miss, so the Sentinel fallback still answers it. server/hello now moves only the name in. Accept to admission: 24 -> 17 allocations under libc++, 27 -> 19 under libstdc++.
The Noise prologue was client/init's text, server/init's text and their concatenation, three std::strings and three operator new allocations per handshake. NoiseHandshake now serializes client/init into a PlatformBuffer sized for it and a server/init of up to MAX_SERVER_INIT_BYTES behind it, and copies server/init in when it arrives, so the prologue is the buffer itself. The buffer comes from platform_malloc: if it cannot be had, the connection closes instead of the program aborting. A server/init longer than the cap fails the handshake. Accept to admission: 17 -> 14 allocations under libc++, 19 -> 16 under libstdc++.
NoiseSession::read_msg1() decrypts message 1 in place into a payload buffer the caller sizes (a PlatformBuffer capped at MAX_HANDSHAKE_MESSAGE_BYTES), and write_msg2_and_split() writes message 2 into a fixed 50-byte array, so the handshake no longer copies msg1 or builds msg2 and its payload in vectors. A payload over the cap still aborts the handshake; the driver table now pins both sides of that boundary. Accept to admission: 14 -> 11 allocations under libc++, 16 -> 13 under libstdc++.
kahrendt
force-pushed
the
alloc-4-handshake-state
branch
from
November 3, 2026 15:15
930d898 to
80a12d5
Compare
kahrendt
marked this pull request as ready for review
November 3, 2026 15:18
Contributor
There was a problem hiding this comment.
🔵 Needs a closer look
Inlining IDs and msg2 enlarges nested handshake stack frames, requiring ESP -Os/-Og stack-usage measurements to confirm sufficient margin within the existing 7,168-byte task stack.
No open issues 🎉
What changed in this PR
This PR reduces allocation overhead during Noise connection admission by storing identifiers inline and replacing handshake strings and vectors with bounded buffers.
Changes:
- Introduces inline
IdTextstorage and a fixed 50-byte Noise message 2 buffer. - Uses platform buffers for the prologue and decrypted payload, with explicit size limits and allocation-failure handling.
- Adds boundary and Sentinel-fallback tests, and lowers expected admission allocations to 11 under libc++ and 13 under libstdc++.
| File | Description |
|---|---|
| tests/test_noise_transport.cpp | Adds oversized-ID fallback and handshake size-boundary tests. |
| tests/test_noise_rehandshake.cpp | Adapts tests to mutable message 1 and fixed-size message 2 buffers. |
| tests/test_allocations.cpp | Updates admission allocation expectations. |
| tests/record_test_helpers.h | Adds readable IdText assertion output. |
| src/record_store.h | Uses inline resolved identifiers and string-view lookup parameters. |
| src/record_store.cpp | Adapts identifier storage and lookups. |
| src/noise_session.h | Defines fixed message 2 storage and caller-owned payload APIs. |
| src/noise_session.cpp | Decrypts mutable input and writes into supplied buffers. |
| src/noise_handshake.h | Declares bounded prologue storage and inline handshake identifiers. |
| src/noise_handshake.cpp | Implements buffer allocation, size limits, and oversized-ID fallback. |
| src/crypto/keys.h | Introduces the inline IdText type. |
| src/crypto/constants.h | Clarifies the message 1 payload limit. |
| src/connection.h | Stores the PSK ID inline and adds a name-only server-information setter. |
| src/connection.cpp | Handles prologue allocation failure and adapts handshake integration. |
| src/connection_manager.h | Updates the PSK-based disconnect interface to IdText. |
| src/connection_manager.cpp | Adapts PSK enumeration, disconnects, and playback tracking. |
| src/connection_manager_pairing.cpp | Uses inline PSK identifiers during unpairing. |
| src/client_dispatch.cpp | Moves the server name and passes mutable re-handshake input. |
Review effort: Balanced
Contributor
Author
|
The next stack PR re-derives all of the stack usages: #229 |
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.
Layer 4 of 7 (base:
alloc-3-messages). Accept to admission: 24 -> 11 allocations under libc++, 27 -> 13 under libstdc++.Summary
The Noise handshake kept its peer and PSK ids, its prologue and its message scratch in heap strings and vectors. They now live inline or in platform buffers.
Changes
IdText(crypto/keys.h) holds a peer_id or psk_id inline; both are 43 characters. The handshake's server_id, the resolved PSK's psk_id and counterparty id, and the connection's psk_id use it; the connection's server_id stays in the publicServerInformationObject. A psk_id longer than 43 characters is a lookup miss, so the Sentinel fallback still answers it.server/hellonow moves only the name into the server information instead of copying the whole object.PlatformBuffersized forclient/initplus aserver/initof up toMAX_SERVER_INIT_BYTES(512). If the buffer cannot be had, the connection closes. A longerserver/initfails the handshake; that cap is a trade against unknown future fields.NoiseSession::read_msg1()decrypts message 1 in place into a payload buffer the caller sizes: a per-handshakePlatformBufferof at mostMAX_HANDSHAKE_MESSAGE_BYTES, whose allocation failure aborts the handshake.write_msg2_and_split()writes message 2 into a fixed 50-byteNoiseMsg2.Testing
server/initexactly at the cap and one byte over it; a message 1 payload exactly at its cap and one byte over it. A mutant on either cap fails a test.Not covered: the close when the prologue buffer cannot be allocated.