Skip to content

Commit c94f373

Browse files
authored
Clear trap pointer on non-trapping instantiation (#13035)
* Clear trap pointer on non-trapping instantiation * Fix C API instance test formatting
1 parent 0db81cc commit c94f373

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

crates/c-api/src/instance.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::{
44
wasmtime_module_t,
55
};
66
use std::mem::MaybeUninit;
7+
use std::ptr;
78
use wasmtime::{Instance, InstancePre, Trap};
89

910
#[derive(Clone)]
@@ -94,6 +95,7 @@ pub(crate) fn handle_instantiate(
9495
instance_ptr: &mut Instance,
9596
trap_ptr: &mut *mut wasm_trap_t,
9697
) -> Option<Box<wasmtime_error_t>> {
98+
*trap_ptr = ptr::null_mut();
9799
match instance {
98100
Ok(i) => {
99101
*instance_ptr = i;

crates/c-api/tests/instance.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,26 @@ TEST(Instance, Smoke) {
4545
auto [name, func] = *i.get(store, 0);
4646
EXPECT_EQ(name, "f");
4747
}
48+
49+
TEST(Instance, NewClearsTrapPointer) {
50+
Engine engine;
51+
Store store(engine);
52+
auto context = store.context();
53+
54+
auto ok_module = Module::compile(engine, "(module)").unwrap();
55+
wasmtime_instance_t instance;
56+
wasm_trap_t *trap = reinterpret_cast<wasm_trap_t *>(1);
57+
auto *error = wasmtime_instance_new(context.capi(), ok_module.capi(), nullptr,
58+
0, &instance, &trap);
59+
EXPECT_EQ(error, nullptr);
60+
EXPECT_EQ(trap, nullptr);
61+
62+
auto import_module =
63+
Module::compile(engine, "(module (import \"\" \"\" (func)))").unwrap();
64+
trap = reinterpret_cast<wasm_trap_t *>(1);
65+
error = wasmtime_instance_new(context.capi(), import_module.capi(), nullptr,
66+
0, &instance, &trap);
67+
EXPECT_NE(error, nullptr);
68+
EXPECT_EQ(trap, nullptr);
69+
wasmtime_error_delete(error);
70+
}

0 commit comments

Comments
 (0)