Skip to content

Commit 8660f62

Browse files
authored
Correctly declare the return type of native trap functions returning a bool as 1-byte integer. (#170)
Otherwise, this caused .NET to incorrectly marshal the return value - for example, this lead to the TrapException.Type being incorrectly set to StackOverflow instead of Undefined for host traps.
1 parent acc50d5 commit 8660f62

3 files changed

Lines changed: 17 additions & 0 deletions

File tree

src/TrapException.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,11 @@ public void Dispose()
333333
public static extern void wasm_frame_vec_delete(in FrameArray vec);
334334

335335
[DllImport(Engine.LibraryName)]
336+
[return: MarshalAs(UnmanagedType.I1)]
336337
internal static extern bool wasmtime_trap_exit_status(IntPtr trap, out int exitStatus);
337338

338339
[DllImport(Engine.LibraryName)]
340+
[return: MarshalAs(UnmanagedType.I1)]
339341
internal static extern bool wasmtime_trap_code(IntPtr trap, out TrapCode exitCode);
340342
}
341343
}

tests/Modules/Trap.wat

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
(module
2+
(import "" "host_trap" (func $host_trap))
23
(export "ok" (func $ok))
34
(export "ok_value" (func $ok_value))
45
(export "run" (func $run))
56
(export "run_div_zero" (func $run_div_zero))
67
(export "run_div_zero_with_result" (func $run_div_zero_with_result))
8+
(export "host_trap" (func $host_trap))
79

810
(func $run
911
(call $first)

tests/TrapTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public TrapTests(TrapFixture fixture)
5252
Fixture = fixture;
5353
Store = new Store(Fixture.Engine);
5454
Linker = new Linker(Fixture.Engine);
55+
56+
Linker.Define("", "host_trap", Function.FromCallback(Store, () => throw new Exception()));
5557
}
5658

5759
[Fact]
@@ -144,6 +146,17 @@ public void ItReturnsATrapCodeAndBacktraceFunctionFromResult()
144146
result.Trap.Frames[0].FunctionName.Should().Be("run_div_zero_with_result");
145147
}
146148

149+
[Fact]
150+
public void ItReturnsCorrectTrapCodeForHostTrap()
151+
{
152+
var instance = Linker.Instantiate(Store, Fixture.Module);
153+
var hostTrap = instance.GetFunction<ActionResult>("host_trap");
154+
var result = hostTrap();
155+
156+
result.Type.Should().Be(ResultType.Trap);
157+
result.Trap.Type.Should().Be(TrapCode.Undefined);
158+
}
159+
147160
[Fact]
148161
public void ItThrowsWhenAccessingValueResultFromTrapResult()
149162
{

0 commit comments

Comments
 (0)