diff --git a/CHANGELOG.md b/CHANGELOG.md index a932aace7b2..09ba0f76593 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,10 +4,13 @@ [Keep a Changelog]: http://keepachangelog.com/en/1.0.0/ +Looking for changes that affect our C API? See the [C API Changelog](lib/c-api/CHANGELOG.md). + ## **[Unreleased]** ### Added +- [#2208](https://github.com/wasmerio/wasmer/pull/2208) Add a new CHANGELOG.md specific to our C API to make it easier for users primarily consuming our C API to keep up to date with changes that affect them. - [#2103](https://github.com/wasmerio/wasmer/pull/2103) Add middleware (incl. metering) in the C API. - [#2003](https://github.com/wasmerio/wasmer/pull/2003) Wasmer works with musl, and is built, tested and packaged for musl. - [#2116](https://github.com/wasmerio/wasmer/pull/2116) Add a package for Windows that is not an installer, but all the `lib` and `include` files as for macOS and Linux. @@ -27,6 +30,7 @@ - [#2149](https://github.com/wasmerio/wasmer/pull/2144) `wasmer-engine-native` looks for clang-11 instead of clang-10. ### Fixed +- [#2208](https://github.com/wasmerio/wasmer/pull/2208) Fix ownership in Wasm C API of `wasm_extern_as_func`, `wasm_extern_as_memory`, `wasm_extern_as_table`, `wasm_extern_as_global`, `wasm_func_as_extern`, `wasm_memory_as_extern`, `wasm_table_as_extern`, and `wasm_global_as_extern`. These functions no longer allocate memory and thus their results should not be freed. This is a breaking change to align more closely with the Wasm C API's stated ownership. - [#2210](https://github.com/wasmerio/wasmer/pull/2210) Fix a memory leak in the Wasm C API in the strings used to identify imports and exports coming from user code. - [#2108](https://github.com/wasmerio/wasmer/pull/2108) The Object Native Engine generates code that now compiles correctly with C++. - [#2125](https://github.com/wasmerio/wasmer/pull/2125) Fix RUSTSEC-2021-0023. diff --git a/Cargo.lock b/Cargo.lock index 3814fe55e37..ed860bd9579 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -770,6 +770,16 @@ dependencies = [ "log", ] +[[package]] +name = "field-offset" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf539fba70056b50f40a22e0da30639518a12ee18c35807858a63b158cb6dde7" +dependencies = [ + "memoffset", + "rustc_version 0.3.3", +] + [[package]] name = "filetime" version = "0.2.14" @@ -2332,6 +2342,7 @@ dependencies = [ "cbindgen", "cfg-if 1.0.0", "enumset", + "field-offset", "inline-c", "lazy_static", "libc", diff --git a/lib/c-api/CHANGELOG.md b/lib/c-api/CHANGELOG.md new file mode 100644 index 00000000000..aa9c4dac2d8 --- /dev/null +++ b/lib/c-api/CHANGELOG.md @@ -0,0 +1,22 @@ +# C API Changelog + +*The format is based on [Keep a Changelog].* + +[Keep a Changelog]: http://keepachangelog.com/en/1.0.0/ + +Looking for changes to the Wasmer CLI and the Rust API? See our [Primary Changelog](../../CHANGELOG.md) + + +## **[Unreleased]** + +### Added +- [#2208](https://github.com/wasmerio/wasmer/pull/2208) Add a new CHANGELOG.md specific to our C API to make it easier for users primarily consuming our C API to keep up to date with changes that affect them. + +### Changed + +### Fixed +- [#2208](https://github.com/wasmerio/wasmer/pull/2208) Fix ownership in Wasm C API of `wasm_extern_as_func`, `wasm_extern_as_memory`, `wasm_extern_as_table`, `wasm_extern_as_global`, `wasm_func_as_extern`, `wasm_memory_as_extern`, `wasm_table_as_extern`, and `wasm_global_as_extern`. These functions no longer allocate memory and thus their results should not be freed. This is a breaking change to align more closely with the Wasm C API's stated ownership. + +## Changes before 2020-04-06 + +See the [Primary Changelog](../../CHANGELOG.md). diff --git a/lib/c-api/Cargo.toml b/lib/c-api/Cargo.toml index e8d559a4de9..4f5b8c924a8 100644 --- a/lib/c-api/Cargo.toml +++ b/lib/c-api/Cargo.toml @@ -38,6 +38,7 @@ typetag = { version = "0.1", optional = true } paste = "1.0" [dev-dependencies] +field-offset = "0.3.3" inline-c = "0.1.5" [features] diff --git a/lib/c-api/examples/exports-function.c b/lib/c-api/examples/exports-function.c index e9c1653c050..e7a74f30d9d 100644 --- a/lib/c-api/examples/exports-function.c +++ b/lib/c-api/examples/exports-function.c @@ -77,7 +77,6 @@ int main(int argc, const char* argv[]) { printf("Results of `sum`: %d\n", results_val[0].of.i32); - wasm_func_delete(sum_func); wasm_module_delete(module); wasm_instance_delete(instance); wasm_extern_vec_delete(&exports); diff --git a/lib/c-api/examples/exports-global.c b/lib/c-api/examples/exports-global.c index 7673e39fe1d..ae576038d37 100644 --- a/lib/c-api/examples/exports-global.c +++ b/lib/c-api/examples/exports-global.c @@ -109,8 +109,6 @@ int main(int argc, const char* argv[]) { wasm_global_set(some, &some_set_value); printf("`some` value: %.1f\n", some_value.of.f32); - wasm_global_delete(some); - wasm_global_delete(one); wasm_module_delete(module); wasm_extern_vec_delete(&exports); wasm_instance_delete(instance); diff --git a/lib/c-api/examples/imports-exports.c b/lib/c-api/examples/imports-exports.c index aad86104e18..b233e647f04 100644 --- a/lib/c-api/examples/imports-exports.c +++ b/lib/c-api/examples/imports-exports.c @@ -124,10 +124,6 @@ int main(int argc, const char* argv[]) { printf("Got the exported memory: %p\n", memory); - wasm_func_delete(func); - wasm_global_delete(global); - wasm_table_delete(table); - wasm_memory_delete(memory); wasm_module_delete(module); wasm_extern_vec_delete(&exports); wasm_instance_delete(instance); diff --git a/lib/c-api/examples/memory.c b/lib/c-api/examples/memory.c index 325a41cba1c..33e0e22fbff 100644 --- a/lib/c-api/examples/memory.c +++ b/lib/c-api/examples/memory.c @@ -99,10 +99,6 @@ int main(int argc, const char* argv[]) { printf("Value at 0x%04x: %d\n", mem_addr, get_at_results_val[0].of.i32); - wasm_memory_delete(memory); - wasm_func_delete(mem_size); - wasm_func_delete(set_at); - wasm_func_delete(get_at); wasm_extern_vec_delete(&exports); wasm_module_delete(module); wasm_instance_delete(instance); diff --git a/lib/c-api/src/wasm_c_api/externals/function.rs b/lib/c-api/src/wasm_c_api/externals/function.rs index a2d2b16a005..98d184649cc 100644 --- a/lib/c-api/src/wasm_c_api/externals/function.rs +++ b/lib/c-api/src/wasm_c_api/externals/function.rs @@ -2,17 +2,27 @@ use super::super::store::wasm_store_t; use super::super::trap::wasm_trap_t; use super::super::types::{wasm_functype_t, wasm_valkind_enum}; use super::super::value::{wasm_val_inner, wasm_val_t, wasm_val_vec_t}; +use super::CApiExternTag; use std::convert::TryInto; use std::ffi::c_void; use std::sync::Arc; -use wasmer::{Function, Instance, RuntimeError, Val}; +use wasmer::{Function, RuntimeError, Val}; -#[derive(Debug)] +#[derive(Debug, Clone)] #[allow(non_camel_case_types)] +#[repr(C)] pub struct wasm_func_t { - pub(crate) inner: Function, - // this is how we ensure the instance stays alive - pub(crate) instance: Option>, + pub(crate) tag: CApiExternTag, + pub(crate) inner: Box, +} + +impl wasm_func_t { + pub(crate) fn new(function: Function) -> Self { + Self { + tag: CApiExternTag::Function, + inner: Box::new(function), + } + } } #[allow(non_camel_case_types)] @@ -80,10 +90,7 @@ pub unsafe extern "C" fn wasm_func_new( }; let function = Function::new(&store.inner, func_sig, inner_callback); - Some(Box::new(wasm_func_t { - instance: None, - inner: function, - })) + Some(Box::new(wasm_func_t::new(function))) } #[no_mangle] @@ -171,10 +178,7 @@ pub unsafe extern "C" fn wasm_func_new_with_env( trampoline, ); - Some(Box::new(wasm_func_t { - instance: None, - inner: function, - })) + Some(Box::new(wasm_func_t::new(function))) } #[no_mangle] diff --git a/lib/c-api/src/wasm_c_api/externals/global.rs b/lib/c-api/src/wasm_c_api/externals/global.rs index 624d4620fdd..14b6edb7123 100644 --- a/lib/c-api/src/wasm_c_api/externals/global.rs +++ b/lib/c-api/src/wasm_c_api/externals/global.rs @@ -1,14 +1,26 @@ use super::super::store::wasm_store_t; use super::super::types::wasm_globaltype_t; use super::super::value::wasm_val_t; +use super::CApiExternTag; use crate::error::update_last_error; use std::convert::TryInto; use wasmer::{Global, Val}; #[allow(non_camel_case_types)] +#[repr(C)] +#[derive(Clone, Debug)] pub struct wasm_global_t { - // maybe needs to hold onto instance - pub(crate) inner: Global, + pub(crate) tag: CApiExternTag, + pub(crate) inner: Box, +} + +impl wasm_global_t { + pub(crate) fn new(global: Global) -> Self { + Self { + tag: CApiExternTag::Global, + inner: Box::new(global), + } + } } #[no_mangle] @@ -30,7 +42,7 @@ pub unsafe extern "C" fn wasm_global_new( Global::new(store, wasm_val) }; - Some(Box::new(wasm_global_t { inner: global })) + Some(Box::new(wasm_global_t::new(global))) } #[no_mangle] @@ -40,9 +52,7 @@ pub unsafe extern "C" fn wasm_global_delete(_global: Option>) #[no_mangle] pub unsafe extern "C" fn wasm_global_copy(global: &wasm_global_t) -> Box { // do shallow copy - Box::new(wasm_global_t { - inner: global.inner.clone(), - }) + Box::new(wasm_global_t::new((&*global.inner).clone())) } #[no_mangle] diff --git a/lib/c-api/src/wasm_c_api/externals/memory.rs b/lib/c-api/src/wasm_c_api/externals/memory.rs index e43db12ee69..4e73c6aec5f 100644 --- a/lib/c-api/src/wasm_c_api/externals/memory.rs +++ b/lib/c-api/src/wasm_c_api/externals/memory.rs @@ -1,12 +1,24 @@ use super::super::store::wasm_store_t; use super::super::types::wasm_memorytype_t; +use super::CApiExternTag; use std::mem; use wasmer::{Memory, Pages}; #[allow(non_camel_case_types)] +#[repr(C)] +#[derive(Clone, Debug)] pub struct wasm_memory_t { - // maybe needs to hold onto instance - pub(crate) inner: Memory, + pub(crate) tag: CApiExternTag, + pub(crate) inner: Box, +} + +impl wasm_memory_t { + pub(crate) fn new(memory: Memory) -> Self { + Self { + tag: CApiExternTag::Memory, + inner: Box::new(memory), + } + } } #[no_mangle] @@ -20,7 +32,7 @@ pub unsafe extern "C" fn wasm_memory_new( let memory_type = memory_type.inner().memory_type.clone(); let memory = c_try!(Memory::new(&store.inner, memory_type)); - Some(Box::new(wasm_memory_t { inner: memory })) + Some(Box::new(wasm_memory_t::new(memory))) } #[no_mangle] @@ -30,9 +42,7 @@ pub unsafe extern "C" fn wasm_memory_delete(_memory: Option>) #[no_mangle] pub unsafe extern "C" fn wasm_memory_copy(memory: &wasm_memory_t) -> Box { // do shallow copy - Box::new(wasm_memory_t { - inner: memory.inner.clone(), - }) + Box::new(wasm_memory_t::new((&*memory.inner).clone())) } #[no_mangle] diff --git a/lib/c-api/src/wasm_c_api/externals/mod.rs b/lib/c-api/src/wasm_c_api/externals/mod.rs index 869ae4b6e2a..2b8a670b9cc 100644 --- a/lib/c-api/src/wasm_c_api/externals/mod.rs +++ b/lib/c-api/src/wasm_c_api/externals/mod.rs @@ -6,16 +6,167 @@ mod table; pub use function::*; pub use global::*; pub use memory::*; -use std::sync::Arc; +use std::mem; pub use table::*; -use wasmer::{Extern, Instance}; +use wasmer::{Extern, ExternType}; #[allow(non_camel_case_types)] -#[derive(Clone)] +#[repr(transparent)] pub struct wasm_extern_t { - // this is how we ensure the instance stays alive - pub(crate) instance: Option>, - pub(crate) inner: Extern, + pub(crate) inner: wasm_extern_inner, +} + +/// All elements in this union must be `repr(C)` and have a +/// `CApiExternTag` as their first element. +#[allow(non_camel_case_types)] +pub(crate) union wasm_extern_inner { + function: mem::ManuallyDrop, + memory: mem::ManuallyDrop, + global: mem::ManuallyDrop, + table: mem::ManuallyDrop, +} + +#[cfg(test)] +mod extern_tests { + use super::*; + + #[test] + fn externs_are_the_same_size() { + use std::mem::{align_of, size_of}; + assert_eq!(size_of::(), size_of::()); + assert_eq!(size_of::(), size_of::()); + assert_eq!(size_of::(), size_of::()); + assert_eq!(size_of::(), size_of::()); + + assert_eq!(align_of::(), align_of::()); + assert_eq!(align_of::(), align_of::()); + assert_eq!(align_of::(), align_of::()); + assert_eq!(align_of::(), align_of::()); + } + + #[test] + fn tags_are_the_same_offset_away() { + use field_offset::offset_of; + + let func_tag_offset = offset_of!(wasm_func_t => tag).get_byte_offset(); + let memory_tag_offset = offset_of!(wasm_memory_t => tag).get_byte_offset(); + let global_tag_offset = offset_of!(wasm_global_t => tag).get_byte_offset(); + let table_tag_offset = offset_of!(wasm_table_t => tag).get_byte_offset(); + + assert_eq!(func_tag_offset, memory_tag_offset); + assert_eq!(global_tag_offset, table_tag_offset); + assert_eq!(func_tag_offset, global_tag_offset); + } +} + +impl Drop for wasm_extern_inner { + fn drop(&mut self) { + unsafe { + match self.function.tag { + CApiExternTag::Function => mem::ManuallyDrop::drop(&mut self.function), + CApiExternTag::Global => mem::ManuallyDrop::drop(&mut self.global), + CApiExternTag::Table => mem::ManuallyDrop::drop(&mut self.table), + CApiExternTag::Memory => mem::ManuallyDrop::drop(&mut self.memory), + } + } + } +} + +impl wasm_extern_t { + pub(crate) fn get_tag(&self) -> CApiExternTag { + unsafe { self.inner.function.tag } + } + + pub(crate) fn ty(&self) -> ExternType { + match self.get_tag() { + CApiExternTag::Function => { + ExternType::Function(unsafe { self.inner.function.inner.ty().clone() }) + } + CApiExternTag::Memory => { + ExternType::Memory(unsafe { self.inner.memory.inner.ty().clone() }) + } + CApiExternTag::Global => { + ExternType::Global(unsafe { self.inner.global.inner.ty().clone() }) + } + CApiExternTag::Table => { + ExternType::Table(unsafe { self.inner.table.inner.ty().clone() }) + } + } + } +} + +impl Clone for wasm_extern_t { + fn clone(&self) -> Self { + match self.get_tag() { + CApiExternTag::Function => Self { + inner: wasm_extern_inner { + function: unsafe { self.inner.function.clone() }, + }, + }, + CApiExternTag::Memory => Self { + inner: wasm_extern_inner { + memory: unsafe { self.inner.memory.clone() }, + }, + }, + CApiExternTag::Global => Self { + inner: wasm_extern_inner { + global: unsafe { self.inner.global.clone() }, + }, + }, + CApiExternTag::Table => Self { + inner: wasm_extern_inner { + table: unsafe { self.inner.table.clone() }, + }, + }, + } + } +} + +impl From for wasm_extern_t { + fn from(other: Extern) -> Self { + match other { + Extern::Function(function) => Self { + inner: wasm_extern_inner { + function: mem::ManuallyDrop::new(wasm_func_t::new(function)), + }, + }, + Extern::Memory(memory) => Self { + inner: wasm_extern_inner { + memory: mem::ManuallyDrop::new(wasm_memory_t::new(memory)), + }, + }, + Extern::Table(table) => Self { + inner: wasm_extern_inner { + table: mem::ManuallyDrop::new(wasm_table_t::new(table)), + }, + }, + Extern::Global(global) => Self { + inner: wasm_extern_inner { + global: mem::ManuallyDrop::new(wasm_global_t::new(global)), + }, + }, + } + } +} + +impl From for Extern { + fn from(other: wasm_extern_t) -> Self { + match other.get_tag() { + CApiExternTag::Function => unsafe { (&*other.inner.function.inner).clone().into() }, + CApiExternTag::Memory => unsafe { (&*other.inner.memory.inner).clone().into() }, + CApiExternTag::Table => unsafe { (&*other.inner.table.inner).clone().into() }, + CApiExternTag::Global => unsafe { (&*other.inner.global.inner).clone().into() }, + } + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[repr(C)] +pub(crate) enum CApiExternTag { + Function, + Global, + Table, + Memory, } wasm_declare_boxed_vec!(extern); @@ -31,106 +182,68 @@ pub unsafe extern "C" fn wasm_extern_copy(r#extern: &wasm_extern_t) -> Box>) {} #[no_mangle] -pub unsafe extern "C" fn wasm_func_as_extern( - func: Option<&wasm_func_t>, -) -> Option> { - let func = func?; - - Some(Box::new(wasm_extern_t { - instance: func.instance.clone(), - inner: Extern::Function(func.inner.clone()), - })) +pub extern "C" fn wasm_func_as_extern(func: Option<&wasm_func_t>) -> Option<&wasm_extern_t> { + unsafe { mem::transmute::, Option<&wasm_extern_t>>(func) } } #[no_mangle] -pub unsafe extern "C" fn wasm_global_as_extern( - global: Option<&wasm_global_t>, -) -> Option> { - let global = global?; - - Some(Box::new(wasm_extern_t { - // TODO: update this if global does hold onto an `instance` - instance: None, - inner: Extern::Global(global.inner.clone()), - })) +pub extern "C" fn wasm_global_as_extern(global: Option<&wasm_global_t>) -> Option<&wasm_extern_t> { + unsafe { mem::transmute::, Option<&wasm_extern_t>>(global) } } #[no_mangle] -pub unsafe extern "C" fn wasm_memory_as_extern( - memory: Option<&wasm_memory_t>, -) -> Option> { - let memory = memory?; - - Some(Box::new(wasm_extern_t { - // TODO: update this if global does hold onto an `instance` - instance: None, - inner: Extern::Memory(memory.inner.clone()), - })) +pub extern "C" fn wasm_memory_as_extern(memory: Option<&wasm_memory_t>) -> Option<&wasm_extern_t> { + unsafe { mem::transmute::, Option<&wasm_extern_t>>(memory) } } #[no_mangle] -pub unsafe extern "C" fn wasm_table_as_extern( - table: Option<&wasm_table_t>, -) -> Option> { - let table = table?; - - Some(Box::new(wasm_extern_t { - // TODO: update this if global does hold onto an `instance` - instance: None, - inner: Extern::Table(table.inner.clone()), - })) +pub extern "C" fn wasm_table_as_extern(table: Option<&wasm_table_t>) -> Option<&wasm_extern_t> { + unsafe { mem::transmute::, Option<&wasm_extern_t>>(table) } } #[no_mangle] -pub unsafe extern "C" fn wasm_extern_as_func( - r#extern: Option<&wasm_extern_t>, -) -> Option> { +pub extern "C" fn wasm_extern_as_func(r#extern: Option<&wasm_extern_t>) -> Option<&wasm_func_t> { let r#extern = r#extern?; - if let Extern::Function(f) = &r#extern.inner { - Some(Box::new(wasm_func_t { - inner: f.clone(), - instance: r#extern.instance.clone(), - })) + if r#extern.get_tag() == CApiExternTag::Function { + Some(unsafe { mem::transmute::<&wasm_extern_t, &wasm_func_t>(r#extern) }) } else { None } } #[no_mangle] -pub unsafe extern "C" fn wasm_extern_as_global( +pub extern "C" fn wasm_extern_as_global( r#extern: Option<&wasm_extern_t>, -) -> Option> { +) -> Option<&wasm_global_t> { let r#extern = r#extern?; - if let Extern::Global(g) = &r#extern.inner { - Some(Box::new(wasm_global_t { inner: g.clone() })) + if r#extern.get_tag() == CApiExternTag::Global { + Some(unsafe { mem::transmute::<&wasm_extern_t, &wasm_global_t>(r#extern) }) } else { None } } #[no_mangle] -pub unsafe extern "C" fn wasm_extern_as_memory( +pub extern "C" fn wasm_extern_as_memory( r#extern: Option<&wasm_extern_t>, -) -> Option> { +) -> Option<&wasm_memory_t> { let r#extern = r#extern?; - if let Extern::Memory(m) = &r#extern.inner { - Some(Box::new(wasm_memory_t { inner: m.clone() })) + if r#extern.get_tag() == CApiExternTag::Memory { + Some(unsafe { mem::transmute::<&wasm_extern_t, &wasm_memory_t>(r#extern) }) } else { None } } #[no_mangle] -pub unsafe extern "C" fn wasm_extern_as_table( - r#extern: Option<&wasm_extern_t>, -) -> Option> { +pub extern "C" fn wasm_extern_as_table(r#extern: Option<&wasm_extern_t>) -> Option<&wasm_table_t> { let r#extern = r#extern?; - if let Extern::Table(t) = &r#extern.inner { - Some(Box::new(wasm_table_t { inner: t.clone() })) + if r#extern.get_tag() == CApiExternTag::Table { + Some(unsafe { mem::transmute::<&wasm_extern_t, &wasm_table_t>(r#extern) }) } else { None } diff --git a/lib/c-api/src/wasm_c_api/externals/table.rs b/lib/c-api/src/wasm_c_api/externals/table.rs index d2b23f5fa6f..f90f8607d66 100644 --- a/lib/c-api/src/wasm_c_api/externals/table.rs +++ b/lib/c-api/src/wasm_c_api/externals/table.rs @@ -1,11 +1,23 @@ use super::super::store::wasm_store_t; use super::super::types::{wasm_ref_t, wasm_table_size_t, wasm_tabletype_t}; +use super::CApiExternTag; use wasmer::Table; #[allow(non_camel_case_types)] +#[repr(C)] +#[derive(Clone)] pub struct wasm_table_t { - // maybe needs to hold onto instance - pub(crate) inner: Table, + pub(crate) tag: CApiExternTag, + pub(crate) inner: Box, +} + +impl wasm_table_t { + pub(crate) fn new(table: Table) -> Self { + Self { + tag: CApiExternTag::Table, + inner: Box::new(table), + } + } } #[no_mangle] @@ -32,9 +44,7 @@ pub unsafe extern "C" fn wasm_table_delete(_table: Option>) {} #[no_mangle] pub unsafe extern "C" fn wasm_table_copy(table: &wasm_table_t) -> Box { // do shallow copy - Box::new(wasm_table_t { - inner: table.inner.clone(), - }) + Box::new(wasm_table_t::new((&*table.inner).clone())) } #[no_mangle] diff --git a/lib/c-api/src/wasm_c_api/instance.rs b/lib/c-api/src/wasm_c_api/instance.rs index 9f7bc384243..1ef0b55d3cc 100644 --- a/lib/c-api/src/wasm_c_api/instance.rs +++ b/lib/c-api/src/wasm_c_api/instance.rs @@ -53,9 +53,8 @@ pub unsafe extern "C" fn wasm_instance_new( .into_slice() .map(|imports| imports.iter()) .unwrap_or_else(|| [].iter()) - .map(|imp| &imp.inner) + .map(|imp| Extern::from((&**imp).clone())) .take(module_import_count) - .cloned() .collect(); let instance = match Instance::new(wasm_module, &resolver) { @@ -192,10 +191,7 @@ pub unsafe extern "C" fn wasm_instance_exports( None }; - Box::into_raw(Box::new(wasm_extern_t { - instance: Some(Arc::clone(instance)), - inner: r#extern.clone(), - })) + Box::into_raw(Box::new(r#extern.clone().into())) }) .collect::>(); extern_vec.shrink_to_fit(); diff --git a/lib/c-api/src/wasm_c_api/types/extern_.rs b/lib/c-api/src/wasm_c_api/types/extern_.rs index 4ef1f8cb607..cdf2abca9de 100644 --- a/lib/c-api/src/wasm_c_api/types/extern_.rs +++ b/lib/c-api/src/wasm_c_api/types/extern_.rs @@ -85,12 +85,12 @@ impl From<&ExternType> for wasm_externtype_t { #[no_mangle] pub unsafe extern "C" fn wasm_extern_type(r#extern: &wasm_extern_t) -> Box { - Box::new(wasm_externtype_t::new(r#extern.inner.ty())) + Box::new(wasm_externtype_t::new(r#extern.ty())) } #[no_mangle] pub unsafe extern "C" fn wasm_extern_kind(r#extern: &wasm_extern_t) -> wasm_externkind_t { - wasm_externkind_enum::from(r#extern.inner.ty()) as wasm_externkind_t + wasm_externkind_enum::from(r#extern.ty()) as wasm_externkind_t } #[no_mangle] diff --git a/lib/c-api/src/wasm_c_api/unstable/wasi.rs b/lib/c-api/src/wasm_c_api/unstable/wasi.rs index 8fce17709f1..8b3801be829 100644 --- a/lib/c-api/src/wasm_c_api/unstable/wasi.rs +++ b/lib/c-api/src/wasm_c_api/unstable/wasi.rs @@ -189,10 +189,7 @@ fn wasi_get_unordered_imports_inner( Box::new(wasmer_named_extern_t { module, name, - r#extern: Box::new(wasm_extern_t { - instance: None, - inner: extern_inner, - }), + r#extern: Box::new(extern_inner.into()), }) }) .collect::>() diff --git a/lib/c-api/src/wasm_c_api/wasi/mod.rs b/lib/c-api/src/wasm_c_api/wasi/mod.rs index 5d16fabb022..1097665b201 100644 --- a/lib/c-api/src/wasm_c_api/wasi/mod.rs +++ b/lib/c-api/src/wasm_c_api/wasi/mod.rs @@ -6,7 +6,7 @@ mod capture_files; pub use super::unstable::wasi::wasi_get_unordered_imports; use super::{ - externals::{wasm_extern_t, wasm_extern_vec_t, wasm_func_t, wasm_memory_t}, + externals::{wasm_extern_vec_t, wasm_func_t, wasm_memory_t}, instance::wasm_instance_t, module::wasm_module_t, store::wasm_store_t, @@ -390,10 +390,7 @@ fn wasi_get_imports_inner( })); let inner = Extern::from_vm_export(store, export); - Some(Box::new(wasm_extern_t { - instance: None, - inner, - })) + Some(Box::new(inner.into())) }) .collect::>>()? .into(); @@ -407,10 +404,7 @@ pub unsafe extern "C" fn wasi_get_start_function( ) -> Option> { let start = c_try!(instance.inner.exports.get_function("_start")); - Some(Box::new(wasm_func_t { - inner: start.clone(), - instance: Some(instance.inner.clone()), - })) + Some(Box::new(wasm_func_t::new(start.clone()))) } #[cfg(test)] diff --git a/lib/c-api/tests/test-early-exit.c b/lib/c-api/tests/test-early-exit.c index d98b5417c48..429ef3b96e9 100644 --- a/lib/c-api/tests/test-early-exit.c +++ b/lib/c-api/tests/test-early-exit.c @@ -90,8 +90,6 @@ int main(int argc, const char *argv[]) { return 1; } - wasm_func_delete(host_func); - // Extract export. printf("Extracting export...\n"); own wasm_extern_vec_t exports;