From 2d0eb88918bbdb4f8c7f1e437fd43f464b244df4 Mon Sep 17 00:00:00 2001 From: Edoardo Marangoni Date: Fri, 18 Oct 2024 14:59:07 +0200 Subject: [PATCH 1/3] fix(c-api): Change signature for `wasm_val_delete`, implement `Drop` for `wasm_val_t` --- Cargo.lock | 10 +++++----- lib/c-api/Cargo.toml | 2 +- lib/c-api/src/wasm_c_api/value.rs | 21 ++++++++++++++++----- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 721eaf6a9f3..2e716e90a9a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -597,20 +597,20 @@ checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" [[package]] name = "cbindgen" -version = "0.24.5" +version = "0.27.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4b922faaf31122819ec80c4047cc684c6979a087366c069611e33649bf98e18d" +checksum = "3fce8dd7fcfcbf3a0a87d8f515194b49d6135acab73e18bd380d1d93bb1a15eb" dependencies = [ "heck 0.4.1", - "indexmap 1.9.3", + "indexmap 2.6.0", "log 0.4.22", "proc-macro2", "quote", "serde", "serde_json", - "syn 1.0.109", + "syn 2.0.79", "tempfile", - "toml 0.5.11", + "toml 0.8.19", ] [[package]] diff --git a/lib/c-api/Cargo.toml b/lib/c-api/Cargo.toml index dad2b23e7b0..ade9ac88870 100644 --- a/lib/c-api/Cargo.toml +++ b/lib/c-api/Cargo.toml @@ -111,4 +111,4 @@ jit = ["compiler"] #emscripten = ["wasmer-emscripten"] [build-dependencies] -cbindgen = { version = "0.24", default-features = false } +cbindgen = { version = "0.27", default-features = false } diff --git a/lib/c-api/src/wasm_c_api/value.rs b/lib/c-api/src/wasm_c_api/value.rs index 3be2f2a340b..8c2e51d6515 100644 --- a/lib/c-api/src/wasm_c_api/value.rs +++ b/lib/c-api/src/wasm_c_api/value.rs @@ -156,14 +156,25 @@ pub unsafe extern "C" fn wasm_val_copy( }); otherwise ()); } -#[no_mangle] -pub unsafe extern "C" fn wasm_val_delete(val: Option>) { - if let Some(val) = val { - // TODO: figure out where wasm_val is allocated first... - let _ = val; +impl Drop for wasm_val_t { + fn drop(&mut self) { + let kind: Result = self.kind.try_into(); + match kind { + Ok(wasm_valkind_enum::WASM_EXTERNREF) | Ok(wasm_valkind_enum::WASM_FUNCREF) => unsafe { + if !self.of.wref.is_null() { + drop(Box::from_raw(self.of.wref)); + } + }, + _ => {} + } } } +#[no_mangle] +pub unsafe extern "C" fn wasm_val_delete(val: *mut wasm_val_t) { + std::ptr::drop_in_place(val); +} + impl TryFrom for wasm_valkind_enum { type Error = &'static str; From 79d5cded4c39193fceb49b6a73a512ad48df0490 Mon Sep 17 00:00:00 2001 From: Edoardo Marangoni Date: Fri, 18 Oct 2024 15:04:24 +0200 Subject: [PATCH 2/3] chore: Add `wamr` folder to .gitignore --- lib/api/third_party/.gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 lib/api/third_party/.gitignore diff --git a/lib/api/third_party/.gitignore b/lib/api/third_party/.gitignore new file mode 100644 index 00000000000..b2e7e790c55 --- /dev/null +++ b/lib/api/third_party/.gitignore @@ -0,0 +1 @@ +wamr From c9785748952a9336a95df3900237ff5adef5b6e7 Mon Sep 17 00:00:00 2001 From: Edoardo Marangoni Date: Fri, 18 Oct 2024 15:20:03 +0200 Subject: [PATCH 3/3] fix(c-api): Add null check --- lib/c-api/src/wasm_c_api/value.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/c-api/src/wasm_c_api/value.rs b/lib/c-api/src/wasm_c_api/value.rs index 8c2e51d6515..5a5a6e67561 100644 --- a/lib/c-api/src/wasm_c_api/value.rs +++ b/lib/c-api/src/wasm_c_api/value.rs @@ -172,7 +172,9 @@ impl Drop for wasm_val_t { #[no_mangle] pub unsafe extern "C" fn wasm_val_delete(val: *mut wasm_val_t) { - std::ptr::drop_in_place(val); + if !val.is_null() { + std::ptr::drop_in_place(val); + } } impl TryFrom for wasm_valkind_enum {