Skip to content

feat: support byte streams in host and guest calls - #2106

Open
andreiltd wants to merge 1 commit into
mainfrom
tandr/streams/byte-streams
Open

andreiltd wants to merge 1 commit into
mainfrom
tandr/streams/byte-streams

Conversation

@andreiltd

@andreiltd andreiltd commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Overview

  • A stream is a one-way byte pipe with one writer and one reader.
  • Within one side (both reader and writer sits in the host or guest), a stream is a bounded queue in memory.
  • A reader can move to the peer as a function call argument or a return value. The writer never moves.
  • Once a reader moves, each side's Mux stands in for the missing end. It turns pipe activity into small records and sends them over the virtqueues.
  • Credits cap the bytes in flight to one window (16 KiB by default) per stream.
  • The guest runs only when the host enters it. The host enters it for calls and for progress turns. block_on runs progress turns.
  • Guest async code runs as tasks in a tiny executor. Each turn polls them.
   host                                                       guest

   StreamWriter --> pipe --> Mux ======== DATA, END =========> Mux --> pipe --> StreamReader
                             Mux <=== CREDIT, CLOSE_READER === Mux

Local Pipe

                      +--------------------- Pipe ---------------------+
   StreamWriter ----> |  queue:  [Bytes "hel"] [Bytes "lo wor"] [...]  | ----> StreamReader
   (writer end)       |  len: 9 bytes         capacity: 16384          |       (reader end)
                      |  writer: Open, Local  reader: Open, Local      |
                      +------------------------------------------------+
  • stream::new() returns (reader, writer). They share one Arc<Pipe> with a capacity of DEFAULT_WINDOW (16384 bytes).
  • The queue is a VecDeque<Bytes>. A chunk keeps its storage, so moving a chunk never copies it.
  • Each end has a state: Open, Closed or Aborted.
  • Each end has an owner:
    • Local: a handle on this side holds the end. The pipe stores its waker.
    • Remote: the Mux holds the end for a handle on the peer.

Moving a reader to the peer

The side that sends a reader away is its origin. The origin names the stream with a StreamId. After the move, each side has its own pipe. The Mux on each side holds the end whose handle is on the other side.

          ORIGIN (host)                                  PEER (guest)

           StreamWriter                                  StreamReader
                |                                              ^
                v                                              |
   +---------------------------+                  +---------------------------+
   | origin pipe               |                  | peer pipe                 |
   | writer: Local             |                  | writer: Remote (Mux)      |
   | reader: Remote (Mux)      |                  | reader: Local             |
   +---------------------------+                  +---------------------------+
                |                                              ^
   Mux.send[H1] = SendStream                     Mux.recv[H1] = RecvStream
                |                                              |
                +-------------- DATA, END ------------------->-+
                +<------------- CREDIT, CLOSE_READER ----------+

On the wire, a reader inside a call is WireValue::StreamReader(Transfer)

Peer can return its reader (returning reader) in which case it will be reattached to the pipe and pipe will become local.

Records

A record is a postcard enum. The control part holds the records. The payload bytes follow in record order with no framing.

Record Sent by Meaning
CREDIT{sid, bytes} reader side I read these bytes. Send that many more.
CLOSE_READER{sid} reader side My reader is gone. Stop writing.
DATA{sid, len} origin The next len payload bytes belong to sid.
END{sid, aborted} origin The writer closed or aborted after all DATA.
STATUS{flags} guest only I have work, or I hold state.

Mux::prepare_records fills a batch up to the buffer size:

  1. Control records first: CREDIT and CLOSE_READER.
  2. Then DATA and END, round-robin over streams from a cursor. One busy stream cannot starve the others.
  3. A DATA payload is a Bytes clone of the origin pipe (peek). The bytes are copied once, into the queue buffer.

Example:

The guest wrote "hello" to S1 (sid) and closed the writer. One flush made this message:

   offset  bytes          field
   ------  -------------  -------------------------------
      0    03             kind = Stream
      1    00 00 00       reserved
      4    00 00 00 00    cid = 0, a Stream message belongs to no call
      8    0b 00 00 00    payload_len = 11
     12    06 00 00 00    control_len = 6
     16    02 03 05       DATA{S1, 5}         <- control
     19    03 03 00       END{S1, false}      <- control
     22    68 65 6c 6c 6f "hello"             <- DATA payload

API

You can check this demo app showcasing streams: https://github.com/andreiltd/hyperlight-stream-demo

Signed-off-by: Tomasz Andrzejak <andreiltd@gmail.com>
@andreiltd
andreiltd added this pull request to stack #2110 November 25, 2026 17:58
@andreiltd andreiltd changed the title tandr/streams/byte streams feat: support byte streams in host and guest calls Nov 25, 2026
@andreiltd andreiltd added the C-enhancement For PRs adding features, improving functionality, docs, tests, etc. label Nov 25, 2026
@andreiltd
andreiltd marked this pull request as ready for review November 25, 2026 18:12
Copilot AI balanced review requested due to automatic review settings November 25, 2026 18:12

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

The cross-boundary stream protocol couples credit accounting, backpressure, guest scheduling, cancellation, and snapshot lifetimes, requiring human review and runtime validation on supported hypervisors to confirm security and teardown invariants.

No open issues 🎉

What changed in this PR

This PR adds optional byte streams to Hyperlight’s host and guest calls. Readers can cross the sandbox boundary while writers remain local, with bounded buffering and credit-based flow control over virtqueues.

Changes:

  • Adds stream endpoints, wire records, reader transfers, and publication tracking.
  • Integrates stream traffic with guest tasks, progress turns, cancellation, and snapshot constraints.
  • Adds protocol tests, fuzzing support, CI coverage, and API documentation.
File Description
src/​tests/​rust_guests/​simpleguest/​src/​main.rs Adds guest functions for stream lifecycle tests.
src/​tests/​rust_guests/​simpleguest/​Cargo.toml Enables guest stream support.
src/​hyperlight_host/​tests/​sandbox_host_tests.rs Tests recovery after oversized guest results.
src/​hyperlight_host/​src/​sandbox/​snapshot/​tripwires.rs Pins stream protocol constants.
src/​hyperlight_host/​src/​sandbox/​snapshot/​file/​config.rs Supports stream reader types in snapshot metadata.
src/​hyperlight_host/​src/​sandbox/​outb.rs Handles stream messages and transactional host replies.
src/​hyperlight_host/​src/​mem/​virtq/​mod.rs Exposes one-way message handling.
src/​hyperlight_host/​src/​mem/​virtq/​codec.rs Decodes reader transfers and validates one-way messages.
src/​hyperlight_host/​src/​mem/​mgr.rs Manages stream sessions and queue traffic.
src/​hyperlight_host/​src/​lib.rs Exposes host stream endpoints.
src/​hyperlight_host/​src/​hypervisor/​mod.rs Adds cancellation wakeups for stream drives.
src/​hyperlight_host/​src/​hypervisor/​hyperlight_vm/​x86_64.rs Preserves pending TLB flushes after execution errors.
src/​hyperlight_host/​src/​hypervisor/​hyperlight_vm/​mod.rs Distinguishes cancellation before guest entry.
src/​hyperlight_host/​src/​hypervisor/​hyperlight_vm/​aarch64.rs Preserves pending TLB flushes after execution errors.
src/​hyperlight_host/​src/​error.rs Defines the active-stream snapshot error.
src/​hyperlight_host/​Cargo.toml Adds the optional streams feature.
src/​hyperlight_host/​benches/​benchmarks.rs Adapts codec benchmarks to owned pending messages.
src/​hyperlight_guest/​src/​transport/​mod.rs Propagates initialization validation errors.
src/​hyperlight_guest/​src/​transport/​context.rs Handles stream dispatch, status, and response capacity.
src/​hyperlight_guest/​src/​transport/​codec.rs Decodes readers and copies incoming stream payloads.
src/​hyperlight_guest/​src/​task.rs Adds a bounded, wake-driven guest executor.
src/​hyperlight_guest/​src/​task_noop.rs Provides task stubs without streams.
src/​hyperlight_guest/​src/​stream.rs Exposes task-backed stream production.
src/​hyperlight_guest/​src/​lib.rs Exposes stream and task modules.
src/​hyperlight_guest/​Cargo.toml Adds the guest streams feature.
src/​hyperlight_guest_capi/​src/​types/​return_value.rs Handles the unsupported stream return variant.
src/​hyperlight_guest_capi/​src/​types/​parameter.rs Rejects stream parameters in the C API.
src/​hyperlight_guest_bin/​src/​lib.rs Checks transport initialization completion.
src/​hyperlight_guest_bin/​src/​host_comm.rs Constructs parameters without cloning values.
src/​hyperlight_guest_bin/​src/​guest_function/​call.rs Polls tasks and completes progress turns.
src/​hyperlight_guest_bin/​Cargo.toml Forwards the streams feature.
src/​hyperlight_common/​src/​virtq/​producer.rs Adds descriptor and slot reservation support.
src/​hyperlight_common/​src/​virtq/​pool/​tests.rs Tests largest-free-slot reporting.
src/​hyperlight_common/​src/​virtq/​pool/​slot.rs Reports the largest available slot size.
src/​hyperlight_common/​src/​transport.rs Adds stream framing and pending publication ownership.
src/​hyperlight_common/​src/​stream/​sync.rs Provides pipe locking and session notifications.
src/​hyperlight_common/​src/​stream/​record.rs Defines and validates stream wire records.
src/​hyperlight_common/​src/​stream/​producer.rs Implements producer completion and cancellation.
src/​hyperlight_common/​src/​stream/​noop.rs Rejects stream operations when disabled.
src/​hyperlight_common/​src/​stream/​mod.rs Defines shared stream exports and limits.
src/​hyperlight_common/​src/​stream/​id.rs Defines validated stream identifiers.
src/​hyperlight_common/​src/​stream/​error.rs Defines stream and protocol errors.
src/​hyperlight_common/​src/​stream/​endpoint.rs Implements asynchronous reader and writer APIs.
src/​hyperlight_common/​src/​lib.rs Exposes shared stream functionality.
src/​hyperlight_common/​src/​func/​ret_type.rs Supports non-cloneable stream return values.
src/​hyperlight_common/​src/​func/​param_type.rs Supports non-cloneable stream parameters.
src/​hyperlight_common/​src/​flatbuffer_wrappers/​wire.rs Encodes reader transfers and caps control data.
src/​hyperlight_common/​src/​flatbuffer_wrappers/​function_types.rs Adds stream value types and decoding.
src/​hyperlight_common/​src/​flatbuffer_wrappers/​function_call.rs Tracks reader ownership through call encoding.
src/​hyperlight_common/​src/​flatbuffer_wrappers/​codec.rs Adds payload limits and transfer lifecycle tests.
src/​hyperlight_common/​src/​flatbuffer_wrappers/​abi_tests.rs Pins stream value and return-type tags.
src/​hyperlight_common/​Cargo.toml Declares the shared streams feature.
Justfile Adds stream test commands to CI-equivalent recipes.
fuzz/​fuzz_targets/​control_decode.rs Exercises decoding with stream session state.
fuzz/​Cargo.toml Enables streams for fuzzing.
docs/​virtio-host-guest-communication.md Documents stream transport and snapshot behavior.
docs/​snapshot-versioning.md Documents stream protocol compatibility checks.
CHANGELOG.md Records stream APIs and breaking changes.
.github/​workflows/​dep_build_test.yml Runs stream tests in CI.

Review effort: Balanced

@hyperlight-gh-bot

Copy link
Copy Markdown

Benchmark Results

Measured commit: e89d90903530
Baseline commit: c46735cd076c

kvm / amd (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 262.42 ns (➖ 1.15x faster)
vec_bytes 73.31 ns (➖ 1.08x faster)
372.90 µs (➖ 1.00x slower)

payload_allocation

slot_pool_segmented
65536 142.09 ns (➖ 1.01x slower)
262144 523.92 ns (➖ 1.01x faster)

sandboxes

create_initialized_and_drop
medium 13.77 ms (➖ 1.03x slower)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
8.94 ns (➖ 1.16x slower) 7.68 ns (➖ 1.00x faster) 7.68 ns (➖ 1.00x faster)

snapshot_files

load_snapshot_unverified
small 104.54 µs (➖ 1.03x faster)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 1.97 µs (➖ 1.06x faster) 2.03 µs (➖ 1.07x slower)
262144 6.97 µs (➖ 1.05x faster) 7.24 µs (➖ 1.03x slower)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 6.30 µs (➖ 1.01x faster) 6.18 µs (➖ 1.08x faster)
262144 26.88 µs (➖ 1.03x faster)
8192 1.04 µs (➖ 1.02x slower) 1.04 µs (➖ 1.01x faster)
kvm / intel (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 291.17 ns (➖ 1.04x faster)
vec_bytes 83.03 ns (➖ 1.14x faster)
724.51 µs (➖ 1.04x faster)

payload_allocation

slot_pool_segmented
65536 152.42 ns (➖ 1.05x faster)
262144 564.26 ns (➖ 1.05x faster)

sandboxes

create_initialized_and_drop
medium 15.36 ms (➖ 1.02x faster)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
7.82 ns (➖ 1.05x faster) 7.78 ns (➖ 1.05x faster) 7.78 ns (➖ 1.05x faster)

snapshot_files

load_snapshot_unverified
small 61.96 µs (➖ 1.03x slower)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.39 µs (➖ 1.05x faster) 2.39 µs (➖ 1.07x faster)
262144 8.49 µs (➖ 1.04x faster) 8.44 µs (➖ 1.04x faster)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 7.96 µs (➖ 1.05x faster) 7.87 µs (➖ 1.06x faster)
262144 34.59 µs (➖ 1.04x faster)
8192 797.59 ns (➖ 1.04x faster) 816.83 ns (➖ 1.04x faster)
mshv3 / amd (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 368.66 ns (➖ 1.06x faster)
vec_bytes 101.83 ns (➖ 1.05x slower)
268.46 µs (➖ 1.01x slower)

payload_allocation

slot_pool_segmented
65536 193.17 ns (➖ 1.31x faster)
262144 739.61 ns (➖ 1.02x slower)

sandboxes

create_initialized_and_drop
medium 64.36 ms (➖ 1.01x slower)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
9.87 ns (➖ 1.00x faster) 9.93 ns (➖ 1.00x slower) 9.86 ns (➖ 1.00x slower)

snapshot_files

load_snapshot_unverified
small 96.99 µs (➖ 1.01x faster)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.22 µs (➖ 1.01x slower) 2.37 µs (➖ 1.03x faster)
262144 9.80 µs (➖ 1.04x slower) 9.87 µs (➖ 1.04x faster)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 8.40 µs (➖ 1.17x faster) 8.32 µs (➖ 1.23x faster)
262144 36.31 µs (➖ 1.05x faster)
8192 1.24 µs (➖ 1.03x slower) 1.25 µs (➖ 1.00x slower)
mshv3 / intel (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 376.29 ns (➖ 1.05x slower)
vec_bytes 84.88 ns (➖ 1.03x faster)
692.70 µs (➖ 1.22x faster)

payload_allocation

slot_pool_segmented
65536 168.11 ns (➖ 1.03x faster)
262144 632.09 ns (➖ 1.00x faster)

sandboxes

create_initialized_and_drop
medium 77.92 ms (➖ 1.03x faster)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
10.00 ns (➖ 1.15x slower) 9.07 ns (➖ 1.00x faster) 8.71 ns (➖ 1.06x faster)

snapshot_files

load_snapshot_unverified
small 54.18 µs (➖ 1.04x slower)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.26 µs (➖ 1.02x faster) 2.27 µs (➖ 1.00x faster)
262144 8.15 µs (➖ 1.02x slower) 8.01 µs (➖ 1.01x slower)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 7.52 µs (➖ 1.02x slower) 7.45 µs (➖ 1.01x faster)
262144 37.79 µs (➖ 1.08x faster)
8192 906.21 ns (➖ 1.02x faster) 922.04 ns (➖ 1.00x slower)
hyperv-ws2025 / amd (Windows) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 568.52 ns (➖ 1.05x slower)
vec_bytes 146.24 ns (➖ 1.08x faster)
2.19 ms (➖ 1.01x faster)

payload_allocation

slot_pool_segmented
65536 227.96 ns (➖ 1.02x faster)
262144 792.68 ns (➖ 1.01x faster)

sandboxes

create_initialized_and_drop
medium 13.71 ms (➖ 1.03x slower)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
10.36 ns (➖ 1.10x faster) 10.38 ns (➖ 1.03x slower) 10.38 ns (➖ 1.00x slower)

snapshot_files

load_snapshot_unverified
small 829.19 µs (➖ 1.03x faster)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.30 µs (➖ 1.02x faster) 2.42 µs (➖ 1.03x slower)
262144 9.30 µs (➖ 1.03x slower) 9.07 µs (➖ 1.01x slower)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 8.90 µs (➖ 1.02x slower) 9.07 µs (➖ 1.02x slower)
262144 38.65 µs (➖ 1.22x faster)
8192 1.28 µs (➖ 1.01x slower) 1.28 µs (➖ 1.00x slower)
hyperv-ws2025 / intel (Windows) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 548.99 ns (➖ 1.06x faster)
vec_bytes 151.12 ns (➖ 1.07x faster)
3.04 ms (➖ 1.02x slower)

payload_allocation

slot_pool_segmented
65536 211.11 ns (➖ 1.00x slower)
262144 756.95 ns (➖ 1.02x faster)

sandboxes

create_initialized_and_drop
medium 17.63 ms (➖ 1.25x slower)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
10.04 ns (➖ 1.01x faster) 9.81 ns (➖ 1.00x slower) 9.88 ns (➖ 1.02x faster)

snapshot_files

load_snapshot_unverified
small 624.25 µs (➖ 1.04x slower)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.31 µs (➖ 1.01x faster) 2.26 µs (➖ 1.02x faster)
262144 7.71 µs (➖ 1.01x faster) 7.61 µs (➖ 1.02x faster)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 8.25 µs (➖ 1.04x faster) 8.35 µs (➖ 1.00x faster)
262144 41.35 µs (➖ 1.17x faster)
8192 1.03 µs (➖ 1.11x faster) 1.11 µs (➖ 1.05x slower)

Reported by cargo ci bench-report --candidate run:45670283273 --config-file bench_report.toml.

@ludfjig ludfjig 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.

this looks good to me. https://github.com/andreiltd/hyperlight-stream-demo is very clean. Btw I assume an awaitable host-side driver is planned for later? Would this remove Sandbox::block_on? Or keep it? I am a bit fuzzy on the async details :D

@andreiltd

Copy link
Copy Markdown
Member Author

Btw I assume an awaitable host-side driver is planned for later? Would this remove Sandbox::block_on? Or keep it?

Hey, yes in the HIP I proposed async run_until() function. block_on stays for convenience.

run_until() expose async driving that a host task can await on an executor like Tokio

The question whether run_until should be blocking or internally spawn a thread for running the guest remains open.

https://github.com/hyperlight-dev/hyperlight/blob/main/proposals/0009-hip-streams/README.md?plain=1#L262

The HIP also proposed spawn_local on the guest, which after discussion with @yoshuawuyts I replaced with structured concurrency counterparts, like stream::produce and future::produce.

@jprendes jprendes 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.

This looks relly good.
My main concern is with the blocking nature of drive.
That beind said, I would be more than happy for us to merge this as is and we can iterate on the exact mechanics of drive later.

$macro!(bool, Bool);
$macro!(Vec<u8>, VecBytes);
$macro!(Vec<$crate::func::Bytes>, ByteChunks);
$macro!($crate::stream::StreamReader<u8>, StreamReader);

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.

I was a bit surprised that the StreamReader can cross the host-guest boundary and not the writer.
@andreiltd pointed out that this is how CM does it (for good reasons), which is fair enough.
We might want to consider allowing the StreamWriter to cross the boundary in some future.

/// Poll `work` to completion, running progress turns while it waits.
#[cfg(feature = "streams")]
#[instrument(err(Debug), skip_all, parent = Span::current())]
fn drive<F: Future>(&mut self, work: F) -> Result<F::Output> {

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.

I was hoping this could be async fn drive(&mut self) -> () that drives the sandbox.
We would have to think when the sandbox wakes up.

In the past I've linked that with having some activity in the host functions, but maybe we need a different model here.
Maybe a better approach here is a shared state between the streams and the sandbox, where we can put a Notify or something similar.

This branch has not been deployed

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

Labels

C-enhancement For PRs adding features, improving functionality, docs, tests, etc.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants