Skip to content

leakpocalypse in internal resource table API - #2076

Open
syntactically wants to merge 1 commit into
mainfrom
lm/lent-guard
Open

syntactically wants to merge 1 commit into
mainfrom
lm/lent-guard

Conversation

@syntactically

Copy link
Copy Markdown
Member

Using the reference produced by ResourceEntry::borrow soundly requires the guarantee that destructors of LentResourceGuards are properly run, which is famously not provided in Rust. The relevant APIs are only used internally by the component bindgen macros, and are always used in the correct fashion, but the functions are pub in hyperlight_common to allow them to be used from generated code.

This commit just makes the minimal change of marking ResourceEntry::lend() as unsafe and making it the caller's responsibility to ensure that drop is called properly.

A more satisfying solution would be to switch the lend API to only lend the resource entry within the scope of a passed-in closure. Unfortunately, (a) this would require restructuring the component bindgen code significantly and (b) it is not entirely clear that it would be expressive enough to support upcoming async guest calls.

Copilot AI balanced review requested due to automatic review settings November 19, 2026 01:09
@syntactically syntactically added the C-bug For PRs that fix bugs label Nov 19, 2026

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.

🟡 Changes recommended

The resource safety documentation contains an unresolvable Lend intra-doc link and a borrowg typo that need correction.

2 open issues
What changed in this PR

This PR makes the resource-lending safety requirement explicit in Hyperlight’s shared resource API and component bindings.

Changes:

  • Marks ResourceEntry::lend as unsafe and requires callers to run the guard’s destructor before the borrowed lifetime expires.
  • Wraps the generated call in an unsafe block with a cleanup justification.
File Description
src/​hyperlight_component_util/​src/​hl.rs Handles the unsafe lending call in generated bindings.
src/​hyperlight_common/​src/​resource.rs Defines the unsafe lending contract and updates its safety documentation.

Review effort: Balanced

Comment thread src/hyperlight_common/src/resource.rs Resolved
Comment thread src/hyperlight_common/src/resource.rs Outdated
/// (a) there are no outstanding [`BorrowedResourceGuard`]s alive
/// (since they would be holding the read side of the [`RwLock`] if
/// they were), and that (b) the shared flag has been set to false, so
/// [`ResourceEntry::borrow`] will never create another borrowg
Using the reference produced by `ResourceEntry::borrow` soundly
requires the guarantee that destructors of `LentResourceGuard`s are
properly run, which is famously not provided in Rust.  The relevant
APIs are only used internally by the component bindgen macros, and are
always used in the correct fashion, but the functions are `pub` in
`hyperlight_common` to allow them to be used from generated code.

This commit just makes the minimal change of marking
`ResourceEntry::lend()` as unsafe and making it the caller's
responsibility to ensure that `drop` is called properly.

A more satisfying solution would be to switch the `lend` API to only
lend the resource entry within the scope of a passed-in
closure. Unfortunately, (a) this would require restructuring the
component bindgen code significantly and (b) it is not entirely clear
that it would be expressive enough to support upcoming async guest
calls.

Signed-off-by: Lucy Menon <168595099+syntactically@users.noreply.github.com>
@hyperlight-gh-bot

Copy link
Copy Markdown

Benchmark Results

Measured commit: 5ec974b166de
Baseline commit: b9dfef3f7cc7

kvm / amd (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 285.04 ns (➖ 1.09x slower)
vec_bytes 81.03 ns (➖ 1.22x faster)
372.08 µs (➖ 1.00x faster)

payload_allocation

slot_pool_segmented
65536 141.45 ns (➖ 1.02x faster)
262144 532.31 ns (➖ 1.01x faster)

sandboxes

create_initialized_and_drop
medium 13.30 ms (➖ 1.02x faster)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
7.69 ns (➖ 1.01x faster) 7.68 ns (➖ 1.00x slower) 7.67 ns (➖ 1.01x faster)

snapshot_files

load_snapshot_unverified
small 99.81 µs (➖ 1.01x slower)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 1.92 µs (➖ 1.09x faster) 2.03 µs (➖ 1.06x slower)
262144 7.17 µs (➖ 1.01x slower) 7.97 µs (➖ 1.12x slower)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 6.16 µs (➖ 1.02x faster) 6.19 µs (➖ 1.05x faster)
262144 28.27 µs (➖ 1.03x slower)
8192 1.03 µs (➖ 1.02x slower) 1.04 µs (➖ 1.05x faster)
kvm / intel (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 250.51 ns (➖ 1.01x slower)
vec_bytes 82.08 ns (➖ 1.01x faster)
662.28 µs (➖ 1.00x faster)

payload_allocation

slot_pool_segmented
65536 172.73 ns (➖ 1.01x slower)
262144 491.32 ns (➖ 1.00x slower)

sandboxes

create_initialized_and_drop
medium 15.73 ms (➖ 1.05x slower)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
6.84 ns (➖ 1.01x slower) 6.76 ns (➖ 1.00x faster) 6.76 ns (➖ 1.01x faster)

snapshot_files

load_snapshot_unverified
small 48.77 µs (➖ 1.02x slower)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.10 µs (➖ 1.00x faster) 2.10 µs (➖ 1.01x faster)
262144 7.48 µs (➖ 1.00x faster) 7.44 µs (➖ 1.00x faster)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 6.92 µs (➖ 1.02x faster) 7.01 µs (➖ 1.11x faster)
262144 30.08 µs (➖ 1.04x slower)
8192 701.06 ns (➖ 1.00x slower) 723.15 ns (➖ 1.01x slower)
mshv3 / amd (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 377.89 ns (➖ 1.04x slower)
vec_bytes 98.00 ns (➖ 1.00x slower)
333.61 µs (➖ 1.04x slower)

payload_allocation

slot_pool_segmented
65536 249.39 ns (➖ 1.32x slower)
262144 717.33 ns (➖ 1.01x faster)

sandboxes

create_initialized_and_drop
medium 67.28 ms (➖ 1.05x slower)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
9.71 ns (➖ 1.00x faster) 9.77 ns (➖ 1.01x slower) 9.80 ns (➖ 1.00x faster)

snapshot_files

load_snapshot_unverified
small 110.96 µs (➖ 1.08x slower)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.26 µs (➖ 1.01x slower) 2.39 µs (➖ 1.03x faster)
262144 9.94 µs (➖ 1.02x slower) 10.83 µs (➖ 1.06x faster)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 8.23 µs (➖ 1.21x faster) 8.20 µs (➖ 1.18x faster)
262144 35.66 µs (➖ 1.05x faster)
8192 1.30 µs (➖ 1.07x slower) 1.26 µs (➖ 1.01x faster)
mshv3 / intel (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 357.14 ns (➖ 1.02x faster)
vec_bytes 85.82 ns (➖ 1.00x slower)
689.29 µs (➖ 1.05x slower)

payload_allocation

slot_pool_segmented
65536 217.51 ns (➖ 1.09x faster)
262144 622.41 ns (➖ 1.07x faster)

sandboxes

create_initialized_and_drop
medium 70.32 ms (➖ 1.12x faster)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
9.76 ns (➖ 1.07x slower) 8.99 ns (➖ 1.03x faster) 9.06 ns (➖ 1.01x faster)

snapshot_files

load_snapshot_unverified
small 53.96 µs (➖ 1.00x slower)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.26 µs (➖ 1.00x faster) 2.29 µs (➖ 1.01x slower)
262144 7.98 µs (➖ 1.01x slower) 7.89 µs (➖ 1.00x faster)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 7.38 µs (➖ 1.00x faster) 7.43 µs (➖ 1.00x slower)
262144 36.53 µs (➖ 1.16x faster)
8192 891.84 ns (➖ 1.02x faster) 878.60 ns (➖ 1.03x faster)
hyperv-ws2025 / amd (Windows) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 551.10 ns (➖ 1.00x faster)
vec_bytes 164.87 ns (➖ 1.05x slower)
2.03 ms (➖ 1.03x slower)

payload_allocation

slot_pool_segmented
65536 231.94 ns (➖ 1.01x faster)
262144 783.56 ns (➖ 1.03x faster)

sandboxes

create_initialized_and_drop
medium 12.36 ms (➖ 1.04x faster)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
9.98 ns (➖ 1.04x faster) 9.97 ns (➖ 1.02x faster) 9.98 ns (➖ 1.05x faster)

snapshot_files

load_snapshot_unverified
small 765.47 µs (➖ 1.05x faster)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.30 µs (➖ 1.02x faster) 2.36 µs (➖ 1.01x slower)
262144 9.14 µs (➖ 1.01x faster) 9.37 µs (➖ 1.02x slower)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 8.91 µs (➖ 1.01x faster) 8.77 µs (➖ 1.00x slower)
262144 39.08 µs (➖ 1.01x faster)
8192 1.25 µs (➖ 1.08x faster) 1.29 µs (➖ 1.02x faster)
hyperv-ws2025 / intel (Windows) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 595.02 ns (➖ 1.02x slower)
vec_bytes 171.59 ns (➖ 1.02x slower)
3.12 ms (➖ 1.07x slower)

payload_allocation

slot_pool_segmented
65536 220.78 ns (➖ 1.05x slower)
262144 746.28 ns (➖ 1.00x faster)

sandboxes

create_initialized_and_drop
medium 14.42 ms (➖ 1.01x faster)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
9.83 ns (➖ 1.03x faster) 9.64 ns (➖ 1.05x faster) 9.78 ns (➖ 1.00x slower)

snapshot_files

load_snapshot_unverified
small 672.78 µs (➖ 1.00x faster)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.33 µs (➖ 1.01x slower) 2.27 µs (➖ 1.00x slower)
262144 7.87 µs (➖ 1.02x slower) 7.67 µs (➖ 1.00x slower)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 8.40 µs (➖ 1.18x faster) 8.49 µs (➖ 1.16x faster)
262144 40.38 µs (➖ 1.31x faster)
8192 1.07 µs (➖ 1.11x faster) 1.16 µs (➖ 1.06x slower)

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

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-bug For PRs that fix bugs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants