Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wasmtime: Rip out incomplete/incorrect externref "host info" support #1922

Merged
merged 1 commit into from
Jun 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions crates/c-api/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,13 @@ pub fn declare_ref(input: proc_macro::TokenStream) -> proc_macro::TokenStream {

#[no_mangle]
pub extern fn #get_host_info(a: &#ty) -> *mut std::os::raw::c_void {
crate::r#ref::get_host_info(&a.externref())
std::ptr::null_mut()
}

#[no_mangle]
pub extern fn #set_host_info(a: &#ty, info: *mut std::os::raw::c_void) {
crate::r#ref::set_host_info(&a.externref(), info, None)
eprintln!("`{}` is not implemented", stringify!(#set_host_info));
std::process::abort();
}

#[no_mangle]
Expand All @@ -91,7 +92,8 @@ pub fn declare_ref(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
info: *mut std::os::raw::c_void,
finalizer: Option<extern "C" fn(*mut std::os::raw::c_void)>,
) {
crate::r#ref::set_host_info(&a.externref(), info, finalizer)
eprintln!("`{}` is not implemented", stringify!(#set_host_info_final));
std::process::abort();
}

#[no_mangle]
Expand Down
6 changes: 3 additions & 3 deletions crates/c-api/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{wasm_name_t, wasm_trap_t};
use anyhow::{anyhow, Error, Result};
use wasmtime::{Store, Trap};
use wasmtime::Trap;

#[repr(C)]
pub struct wasmtime_error_t {
Expand All @@ -10,8 +10,8 @@ pub struct wasmtime_error_t {
wasmtime_c_api_macros::declare_own!(wasmtime_error_t);

impl wasmtime_error_t {
pub(crate) fn to_trap(self, store: &Store) -> Box<wasm_trap_t> {
Box::new(wasm_trap_t::new(store, Trap::from(self.error)))
pub(crate) fn to_trap(self) -> Box<wasm_trap_t> {
Box::new(wasm_trap_t::new(Trap::from(self.error)))
}
}

Expand Down
18 changes: 8 additions & 10 deletions crates/c-api/src/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ fn create_function(
}
Ok(())
});
Box::new(HostRef::new(store, func).into())
Box::new(HostRef::new(func).into())
}

#[no_mangle]
Expand Down Expand Up @@ -183,7 +183,7 @@ pub unsafe extern "C" fn wasm_func_call(
&mut trap,
);
match error {
Some(err) => Box::into_raw(err.to_trap(&wasm_func.ext.externref().store().unwrap())),
Some(err) => Box::into_raw(err.to_trap()),
None => trap,
}
}
Expand Down Expand Up @@ -211,7 +211,6 @@ fn _wasmtime_func_call(
results: &mut [wasm_val_t],
trap_ptr: &mut *mut wasm_trap_t,
) -> Option<Box<wasmtime_error_t>> {
let store = &func.ext.externref().store().unwrap();
let func = func.func().borrow();
if results.len() != func.result_arity() {
return Some(Box::new(anyhow!("wrong number of results provided").into()));
Expand All @@ -232,7 +231,7 @@ fn _wasmtime_func_call(
}
Ok(Err(trap)) => match trap.downcast::<Trap>() {
Ok(trap) => {
*trap_ptr = Box::into_raw(Box::new(wasm_trap_t::new(store, trap)));
*trap_ptr = Box::into_raw(Box::new(wasm_trap_t::new(trap)));
None
}
Err(err) => Some(Box::new(err.into())),
Expand All @@ -245,7 +244,7 @@ fn _wasmtime_func_call(
} else {
Trap::new("rust panic happened")
};
let trap = Box::new(wasm_trap_t::new(store, trap));
let trap = Box::new(wasm_trap_t::new(trap));
*trap_ptr = Box::into_raw(trap);
None
}
Expand Down Expand Up @@ -279,12 +278,11 @@ pub extern "C" fn wasmtime_caller_export_get(
) -> Option<Box<wasm_extern_t>> {
let name = str::from_utf8(name.as_slice()).ok()?;
let export = caller.caller.get_export(name)?;
let store = caller.caller.store();
let which = match export {
Extern::Func(f) => ExternHost::Func(HostRef::new(&store, f)),
Extern::Global(g) => ExternHost::Global(HostRef::new(&store, g)),
Extern::Memory(m) => ExternHost::Memory(HostRef::new(&store, m)),
Extern::Table(t) => ExternHost::Table(HostRef::new(&store, t)),
Extern::Func(f) => ExternHost::Func(HostRef::new(f)),
Extern::Global(g) => ExternHost::Global(HostRef::new(g)),
Extern::Memory(m) => ExternHost::Memory(HostRef::new(m)),
Extern::Table(t) => ExternHost::Table(HostRef::new(t)),
};
Some(Box::new(wasm_extern_t { which }))
}
2 changes: 1 addition & 1 deletion crates/c-api/src/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub extern "C" fn wasmtime_global_new(
handle_result(global, |global| {
*ret = Box::into_raw(Box::new(wasm_global_t {
ext: wasm_extern_t {
which: ExternHost::Global(HostRef::new(&store.store, global)),
which: ExternHost::Global(HostRef::new(global)),
},
}));
})
Expand Down
6 changes: 3 additions & 3 deletions crates/c-api/src/host_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::any::Any;
use std::cell::{self, RefCell};
use std::convert::TryFrom;
use std::marker::PhantomData;
use wasmtime::{ExternRef, Store};
use wasmtime::ExternRef;

/// Represents a piece of data located in the host environment.
#[derive(Debug)]
Expand All @@ -19,9 +19,9 @@ where
T: 'static + Any,
{
/// Creates a new `HostRef<T>` from `T`.
pub fn new(store: &Store, item: T) -> HostRef<T> {
pub fn new(item: T) -> HostRef<T> {
HostRef {
externref: ExternRef::new(store, RefCell::new(item)),
externref: ExternRef::new(RefCell::new(item)),
_phantom: PhantomData,
}
}
Expand Down
19 changes: 8 additions & 11 deletions crates/c-api/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{wasm_store_t, wasmtime_error_t, ExternHost};
use anyhow::Result;
use std::cell::RefCell;
use std::ptr;
use wasmtime::{Extern, Instance, Store, Trap};
use wasmtime::{Extern, Instance, Trap};

#[repr(C)]
#[derive(Clone)]
Expand All @@ -17,9 +17,8 @@ wasmtime_c_api_macros::declare_ref!(wasm_instance_t);

impl wasm_instance_t {
pub(crate) fn new(instance: Instance) -> wasm_instance_t {
let store = instance.store().clone();
wasm_instance_t {
instance: HostRef::new(&store, instance),
instance: HostRef::new(instance),
exports_cache: RefCell::new(None),
}
}
Expand Down Expand Up @@ -51,7 +50,7 @@ pub unsafe extern "C" fn wasm_instance_new(
assert!(trap.is_null());
assert!(instance.is_null());
if let Some(result) = result {
*result = Box::into_raw(err.to_trap(&store.store));
*result = Box::into_raw(err.to_trap());
}
None
}
Expand Down Expand Up @@ -109,15 +108,13 @@ fn _wasmtime_instance_new(
.collect::<Vec<_>>();
let module = &module.module.borrow();
handle_instantiate(
store,
Instance::new(store, module, &imports),
instance_ptr,
trap_ptr,
)
}

pub fn handle_instantiate(
store: &Store,
instance: Result<Instance>,
instance_ptr: &mut *mut wasm_instance_t,
trap_ptr: &mut *mut wasm_trap_t,
Expand All @@ -133,7 +130,7 @@ pub fn handle_instantiate(
}
Err(e) => match e.downcast::<Trap>() {
Ok(trap) => {
write(trap_ptr, wasm_trap_t::new(store, trap));
write(trap_ptr, wasm_trap_t::new(trap));
None
}
Err(e) => Some(Box::new(e.into())),
Expand All @@ -149,10 +146,10 @@ pub extern "C" fn wasm_instance_exports(instance: &wasm_instance_t, out: &mut wa
instance
.exports()
.map(|e| match e.into_extern() {
Extern::Func(f) => ExternHost::Func(HostRef::new(instance.store(), f)),
Extern::Global(f) => ExternHost::Global(HostRef::new(instance.store(), f)),
Extern::Memory(f) => ExternHost::Memory(HostRef::new(instance.store(), f)),
Extern::Table(f) => ExternHost::Table(HostRef::new(instance.store(), f)),
Extern::Func(f) => ExternHost::Func(HostRef::new(f)),
Extern::Global(f) => ExternHost::Global(HostRef::new(f)),
Extern::Memory(f) => ExternHost::Memory(HostRef::new(f)),
Extern::Table(f) => ExternHost::Table(HostRef::new(f)),
})
.collect()
});
Expand Down
13 changes: 6 additions & 7 deletions crates/c-api/src/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub extern "C" fn wasmtime_linker_instantiate(
trap_ptr: &mut *mut wasm_trap_t,
) -> Option<Box<wasmtime_error_t>> {
let result = linker.linker.instantiate(&module.module.borrow());
super::instance::handle_instantiate(linker.linker.store(), result, instance_ptr, trap_ptr)
super::instance::handle_instantiate(result, instance_ptr, trap_ptr)
}

#[no_mangle]
Expand Down Expand Up @@ -117,7 +117,7 @@ pub extern "C" fn wasmtime_linker_get_default(
Err(_) => return bad_utf8(),
};
handle_result(linker.get_default(name), |f| {
*func = Box::into_raw(Box::new(HostRef::new(linker.store(), f).into()))
*func = Box::into_raw(Box::new(HostRef::new(f).into()))
})
}

Expand All @@ -138,12 +138,11 @@ pub extern "C" fn wasmtime_linker_get_one_by_name(
Err(_) => return bad_utf8(),
};
handle_result(linker.get_one_by_name(module, name), |item| {
let store = linker.store();
let which = match item {
Extern::Func(f) => ExternHost::Func(HostRef::new(&store, f)),
Extern::Global(g) => ExternHost::Global(HostRef::new(&store, g)),
Extern::Memory(m) => ExternHost::Memory(HostRef::new(&store, m)),
Extern::Table(t) => ExternHost::Table(HostRef::new(&store, t)),
Extern::Func(f) => ExternHost::Func(HostRef::new(f)),
Extern::Global(g) => ExternHost::Global(HostRef::new(g)),
Extern::Memory(m) => ExternHost::Memory(HostRef::new(m)),
Extern::Table(t) => ExternHost::Table(HostRef::new(t)),
};
*item_ptr = Box::into_raw(Box::new(wasm_extern_t { which }))
})
Expand Down
2 changes: 1 addition & 1 deletion crates/c-api/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub extern "C" fn wasm_memory_new(
store: &wasm_store_t,
mt: &wasm_memorytype_t,
) -> Box<wasm_memory_t> {
let memory = HostRef::new(&store.store, Memory::new(&store.store, mt.ty().ty.clone()));
let memory = HostRef::new(Memory::new(&store.store, mt.ty().ty.clone()));
Box::new(wasm_memory_t {
ext: wasm_extern_t {
which: ExternHost::Memory(memory),
Expand Down
4 changes: 2 additions & 2 deletions crates/c-api/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub extern "C" fn wasmtime_module_new(
.map(|e| wasm_exporttype_t::new(e.name().to_owned(), e.ty()))
.collect::<Vec<_>>();
let module = Box::new(wasm_module_t {
module: HostRef::new(store, module),
module: HostRef::new(module),
imports,
exports,
});
Expand Down Expand Up @@ -130,7 +130,7 @@ pub extern "C" fn wasm_module_obtain(
.map(|e| wasm_exporttype_t::new(e.name().to_owned(), e.ty()))
.collect::<Vec<_>>();
Some(Box::new(wasm_module_t {
module: HostRef::new(&store.store, module),
module: HostRef::new(module),
imports,
exports,
}))
Expand Down
45 changes: 10 additions & 35 deletions crates/c-api/src/ref.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::HostInfoState;
use std::os::raw::c_void;
use wasmtime::ExternRef;

Expand All @@ -24,47 +23,23 @@ pub extern "C" fn wasm_ref_same(a: &wasm_ref_t, b: &wasm_ref_t) -> bool {
}
}

pub(crate) fn get_host_info(r: &ExternRef) -> *mut c_void {
let host_info = match r.host_info() {
Some(info) => info,
None => return std::ptr::null_mut(),
};
let host_info = host_info.borrow();
match host_info.downcast_ref::<HostInfoState>() {
Some(state) => state.info,
None => std::ptr::null_mut(),
}
}

#[no_mangle]
pub extern "C" fn wasm_ref_get_host_info(a: &wasm_ref_t) -> *mut c_void {
a.r.as_ref()
.map_or(std::ptr::null_mut(), |r| get_host_info(r))
}

pub(crate) fn set_host_info(
r: &ExternRef,
info: *mut c_void,
finalizer: Option<extern "C" fn(*mut c_void)>,
) {
let info = if info.is_null() && finalizer.is_none() {
None
} else {
Some(Box::new(crate::HostInfoState { info, finalizer }) as Box<dyn std::any::Any>)
};
r.set_host_info(info);
pub extern "C" fn wasm_ref_get_host_info(_ref: &wasm_ref_t) -> *mut c_void {
std::ptr::null_mut()
}

#[no_mangle]
pub extern "C" fn wasm_ref_set_host_info(a: &wasm_ref_t, info: *mut c_void) {
a.r.as_ref().map(|r| set_host_info(r, info, None));
pub extern "C" fn wasm_ref_set_host_info(_ref: &wasm_ref_t, _info: *mut c_void) {
eprintln!("`wasm_ref_set_host_info` is not implemented");
std::process::abort();
}

#[no_mangle]
pub extern "C" fn wasm_ref_set_host_info_with_finalizer(
a: &wasm_ref_t,
info: *mut c_void,
finalizer: Option<extern "C" fn(*mut c_void)>,
_ref: &wasm_ref_t,
_info: *mut c_void,
_finalizer: Option<extern "C" fn(*mut c_void)>,
) {
a.r.as_ref().map(|r| set_host_info(r, info, finalizer));
eprintln!("`wasm_ref_set_host_info_with_finalizer` is not implemented");
std::process::abort();
}
12 changes: 3 additions & 9 deletions crates/c-api/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub extern "C" fn wasm_table_new(
let table = Table::new(&store.store, tt.ty().ty.clone(), init).ok()?;
Some(Box::new(wasm_table_t {
ext: wasm_extern_t {
which: ExternHost::Table(HostRef::new(&store.store, table)),
which: ExternHost::Table(HostRef::new(table)),
},
}))
}
Expand All @@ -68,7 +68,7 @@ pub extern "C" fn wasmtime_funcref_table_new(
|table| {
*out = Box::into_raw(Box::new(wasm_table_t {
ext: wasm_extern_t {
which: ExternHost::Table(HostRef::new(&store.store, table)),
which: ExternHost::Table(HostRef::new(table)),
},
}));
},
Expand Down Expand Up @@ -100,13 +100,7 @@ pub extern "C" fn wasmtime_funcref_table_get(
*ptr = match val {
// TODO: what do do about creating new `HostRef` handles here?
Val::FuncRef(None) => ptr::null_mut(),
Val::FuncRef(Some(f)) => {
let store = match t.table().as_ref().store() {
None => return false,
Some(store) => store,
};
Box::into_raw(Box::new(HostRef::new(&store, f).into()))
}
Val::FuncRef(Some(f)) => Box::into_raw(Box::new(HostRef::new(f).into())),
_ => return false,
};
}
Expand Down
Loading