Skip to content

Commit

Permalink
Added handle.IsClosed checks on all NativeHandle properties. This…
Browse files Browse the repository at this point in the history
… protects against accessing various things after they have been disposed. (#289)
  • Loading branch information
martindevans authored Jan 2, 2024
1 parent e03d3e1 commit 6b926f8
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ internal Handle NativeHandle
{
get
{
if (handle.IsInvalid)
if (handle.IsInvalid || handle.IsClosed)
{
throw new ObjectDisposedException(typeof(Config).FullName);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ internal Handle NativeHandle
{
get
{
if (handle.IsInvalid)
if (handle.IsInvalid || handle.IsClosed)
{
throw new ObjectDisposedException(typeof(Engine).FullName);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ internal Handle NativeHandle
{
get
{
if (handle.IsInvalid)
if (handle.IsInvalid || handle.IsClosed)
{
throw new ObjectDisposedException(typeof(Module).FullName);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Store.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ internal Handle NativeHandle
{
get
{
if (handle.IsInvalid)
if (handle.IsInvalid || handle.IsClosed)
{
throw new ObjectDisposedException(typeof(Store).FullName);
}
Expand Down
12 changes: 12 additions & 0 deletions tests/ConfigTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,17 @@ public void ItSetsMultiValue()

act.Should().Throw<WasmtimeException>();
}

[Fact]
public void ItCannotBeAccessedOnceDisposed()
{
var config = new Config();
config.Dispose();

Assert.Throws<ObjectDisposedException>(() => config.NativeHandle);
Assert.Throws<ObjectDisposedException>(() => config.WithBulkMemory(true));
Assert.Throws<ObjectDisposedException>(() => config.WithCacheConfig(null));
Assert.Throws<ObjectDisposedException>(() => config.WithEpochInterruption(true));
}
}
}
18 changes: 18 additions & 0 deletions tests/EngineTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using Xunit;

namespace Wasmtime.Tests;

public class EngineTests
{
[Fact]
public void ItCannotBeAccessedOnceDisposed()
{
var engine = new Engine();

engine.Dispose();

Assert.Throws<ObjectDisposedException>(() => engine.NativeHandle);
Assert.Throws<ObjectDisposedException>(() => engine.IncrementEpoch());
}
}
15 changes: 15 additions & 0 deletions tests/ModuleLoadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,20 @@ public void ItLoadsModuleTextFromEmbeddedResource()
// `ObjectDisposedException`
stream.Read(new byte[0], 0, 0);
}

[Fact]
public void ItCannotBeAccessedOnceDisposed()
{
using var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("hello.wasm");
stream.Should().NotBeNull();

using var engine = new Engine();
var module = Module.FromStream(engine, "hello.wasm", stream);

module.Dispose();

Assert.Throws<ObjectDisposedException>(() => module.NativeHandle);
Assert.Throws<ObjectDisposedException>(() => module.Serialize());
}
}
}
17 changes: 16 additions & 1 deletion tests/StoreTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FluentAssertions;
using System;
using FluentAssertions;
using System.IO;
using Xunit;

Expand Down Expand Up @@ -91,5 +92,19 @@ public void ItLimitsMemories()
var act = () => { new Instance(Store, module); };
act.Should().Throw<WasmtimeException>();
}

[Fact]
public void ItCannotBeAccessedOnceDisposed()
{
var ctx = Store.Context;
Assert.Equal(Store, ctx.Store);

Store.Dispose();

Assert.Throws<ObjectDisposedException>(() => { var x = Store.Context; });
Assert.Throws<ObjectDisposedException>(() => Store.NativeHandle);
Assert.Throws<ObjectDisposedException>(() => Store.Fuel);
Assert.Throws<ObjectDisposedException>(() => Store.GC());
}
}
}

0 comments on commit 6b926f8

Please sign in to comment.