Skip to content

Hold the Noise handshake's ids, prologue and scratch off the heap - #228

Merged
kahrendt merged 3 commits into
alloc-3-messagesfrom
alloc-4-handshake-state
Nov 3, 2026
Merged

kahrendt merged 3 commits into
alloc-3-messagesfrom
alloc-4-handshake-state

Conversation

@kahrendt

@kahrendt kahrendt commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

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 public ServerInformationObject. A psk_id longer than 43 characters is a lookup miss, so the Sentinel fallback still answers it. server/hello now moves only the name into the server information instead of copying the whole object.
  • The prologue is one PlatformBuffer sized for client/init plus a server/init of up to MAX_SERVER_INIT_BYTES (512). If the buffer cannot be had, the connection closes. A longer server/init fails 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-handshake PlatformBuffer of at most MAX_HANDSHAKE_MESSAGE_BYTES, whose allocation failure aborts the handshake. write_msg2_and_split() writes message 2 into a fixed 50-byte NoiseMsg2.

Testing

  • New checks: a psk_id longer than a peer_id falls back to the Sentinel PSK; a server/init exactly 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.

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
kahrendt force-pushed the alloc-4-handshake-state branch from 930d898 to 80a12d5 Compare November 3, 2026 15:15
@kahrendt
kahrendt marked this pull request as ready for review November 3, 2026 15:18
@kahrendt
kahrendt requested a balanced review from Copilot November 3, 2026 15:19

Copilot AI 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.

🔵 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 IdText storage 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

@kahrendt

kahrendt commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

The next stack PR re-derives all of the stack usages: #229

@kahrendt
kahrendt merged commit a7b4086 into main Nov 3, 2026
17 of 33 checks passed
@kahrendt
kahrendt deleted the alloc-4-handshake-state branch November 3, 2026 16:01
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.

2 participants