diff --git a/CHANGELOG.md b/CHANGELOG.md index 901e71350..e6aa9effe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). * HVF sandbox initialization accepts seeds that match Apple hypercall identifiers. * Host-written snapshot memory has instruction-cache coherence before guest execution. * Fix slow scratch zeroing on restore under KVM in default builds. +* Fix C# component bindings keeping an entry for every resource handle passed to or from the guest. ### Removed * **Breaking:** `SandboxBuilder::init_data`, `GuestEnvironment`, `GuestBlob`, diff --git a/src/hyperlight_component_csharp/src/Runtime.cs b/src/hyperlight_component_csharp/src/Runtime.cs index 7ab7c96c7..ffd98a0de 100644 --- a/src/hyperlight_component_csharp/src/Runtime.cs +++ b/src/hyperlight_component_csharp/src/Runtime.cs @@ -354,16 +354,23 @@ private ReadOnlySpan Take(int count) /// /// Host objects of one resource type that the guest refers to by - /// handle. A handle is an index into the table. + /// handle. Handles are never reused, so a stale handle cannot reach a + /// later object. /// public sealed class ResourceTable where T : class { - private readonly List _entries = new(); + private readonly Dictionary _entries = new(); + private uint _next; - private sealed class Entry + internal int Count { - public T? Value; - public bool Lent; + get + { + lock (_entries) + { + return _entries.Count; + } + } } /// @@ -391,7 +398,7 @@ private void Clear(uint handle) { lock (_entries) { - _entries[(int)handle].Value = null; + _entries.Remove(handle); } } @@ -401,13 +408,12 @@ public T Take(uint handle) lock (_entries) { var entry = Get(handle); - if (entry.Lent || entry.Value is null) + if (entry.Lent) { throw new WireFormatException($"resource handle {handle} is not owned"); } - var value = entry.Value; - entry.Value = null; - return value; + _entries.Remove(handle); + return entry.Value; } } @@ -416,7 +422,7 @@ public T Borrow(uint handle) { lock (_entries) { - return Get(handle).Value ?? throw new WireFormatException($"resource handle {handle} is not valid"); + return Get(handle).Value; } } @@ -425,14 +431,16 @@ private uint Add(T value, bool lent) ArgumentNullException.ThrowIfNull(value); lock (_entries) { - _entries.Add(new Entry { Value = value, Lent = lent }); - return checked((uint)(_entries.Count - 1)); + var handle = _next; + _next = checked(_next + 1); + _entries.Add(handle, (value, lent)); + return handle; } } - private Entry Get(uint handle) => - handle < (uint)_entries.Count - ? _entries[(int)handle] + private (T Value, bool Lent) Get(uint handle) => + _entries.TryGetValue(handle, out var entry) + ? entry : throw new WireFormatException($"invalid resource handle {handle}"); } diff --git a/src/tests/csharp_host/RuntimeTests.cs b/src/tests/csharp_host/RuntimeTests.cs index b38cfb418..998638749 100644 --- a/src/tests/csharp_host/RuntimeTests.cs +++ b/src/tests/csharp_host/RuntimeTests.cs @@ -22,5 +22,14 @@ public static void Run() started.Commit(); started.End(); Check.True(table.Take(kept) == "kept", "gift kept after the call starts"); + Check.True(table.Count == 0, "taken and undone entries are removed"); + + var loan = new CallScope(); + var lent = table.Lend("lent", loan); + Check.Throws(() => table.Take(lent), "lent value cannot be taken"); + loan.End(); + Check.True(table.Count == 0, "ended loans are removed"); + Check.Throws(() => table.Borrow(lent), "ended loan is invalid"); + Check.True(table.Give("next") > lent, "handles are not reused"); } }