Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down
40 changes: 24 additions & 16 deletions src/hyperlight_component_csharp/src/Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -354,16 +354,23 @@ private ReadOnlySpan<byte> Take(int count)

/// <summary>
/// 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.
/// </summary>
public sealed class ResourceTable<T> where T : class
{
private readonly List<Entry> _entries = new();
private readonly Dictionary<uint, (T Value, bool Lent)> _entries = new();
private uint _next;

private sealed class Entry
internal int Count
{
public T? Value;
public bool Lent;
get
{
lock (_entries)
{
return _entries.Count;
}
}
}

/// <summary>
Expand Down Expand Up @@ -391,7 +398,7 @@ private void Clear(uint handle)
{
lock (_entries)
{
_entries[(int)handle].Value = null;
_entries.Remove(handle);
}
}

Expand All @@ -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;
}
}

Expand All @@ -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;
}
}

Expand All @@ -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}");
}

Expand Down
9 changes: 9 additions & 0 deletions src/tests/csharp_host/RuntimeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<WireFormatException>(() => table.Take(lent), "lent value cannot be taken");
loan.End();
Check.True(table.Count == 0, "ended loans are removed");
Check.Throws<WireFormatException>(() => table.Borrow(lent), "ended loan is invalid");
Check.True(table.Give("next") > lent, "handles are not reused");
}
}
Loading