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

Better to be loud that we don't support attaching arbitrary host info to
`externref`s than to limp along and pretend we do support it. Supporting it
properly won't reuse any of this code anyways.
This commit is contained in:
Nick Fitzgerald
2020-06-25 10:24:40 -07:00
parent 9ce67d8cad
commit e40c039e65
19 changed files with 80 additions and 199 deletions

View File

@@ -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]
@@ -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]

View File

@@ -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 {
@@ -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)))
}
}

View File

@@ -108,7 +108,7 @@ fn create_function(
}
Ok(())
});
Box::new(HostRef::new(store, func).into())
Box::new(HostRef::new(func).into())
}
#[no_mangle]
@@ -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,
}
}
@@ -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()));
@@ -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())),
@@ -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
}
@@ -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 }))
}

View File

@@ -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)),
},
}));
})

View File

@@ -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)]
@@ -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,
}
}

View File

@@ -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)]
@@ -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),
}
}
@@ -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
}
@@ -109,7 +108,6 @@ fn _wasmtime_instance_new(
.collect::<Vec<_>>();
let module = &module.module.borrow();
handle_instantiate(
store,
Instance::new(store, module, &imports),
instance_ptr,
trap_ptr,
@@ -117,7 +115,6 @@ fn _wasmtime_instance_new(
}
pub fn handle_instantiate(
store: &Store,
instance: Result<Instance>,
instance_ptr: &mut *mut wasm_instance_t,
trap_ptr: &mut *mut wasm_trap_t,
@@ -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())),
@@ -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()
});

View File

@@ -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]
@@ -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()))
})
}
@@ -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 }))
})

View File

@@ -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),

View File

@@ -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,
});
@@ -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,
}))

View File

@@ -1,4 +1,3 @@
use crate::HostInfoState;
use std::os::raw::c_void;
use wasmtime::ExternRef;
@@ -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(_ref: &wasm_ref_t) -> *mut c_void {
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);
}
#[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();
}

View File

@@ -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)),
},
}))
}
@@ -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)),
},
}));
},
@@ -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,
};
}

View File

@@ -1,7 +1,7 @@
use crate::host_ref::HostRef;
use crate::{wasm_frame_vec_t, wasm_instance_t, wasm_name_t, wasm_store_t};
use once_cell::unsync::OnceCell;
use wasmtime::{Store, Trap};
use wasmtime::Trap;
#[repr(C)]
#[derive(Clone)]
@@ -12,9 +12,9 @@ pub struct wasm_trap_t {
wasmtime_c_api_macros::declare_ref!(wasm_trap_t);
impl wasm_trap_t {
pub(crate) fn new(store: &Store, trap: Trap) -> wasm_trap_t {
pub(crate) fn new(trap: Trap) -> wasm_trap_t {
wasm_trap_t {
trap: HostRef::new(store, trap),
trap: HostRef::new(trap),
}
}
@@ -38,7 +38,7 @@ pub type wasm_message_t = wasm_name_t;
#[no_mangle]
pub extern "C" fn wasm_trap_new(
store: &wasm_store_t,
_store: &wasm_store_t,
message: &wasm_message_t,
) -> Box<wasm_trap_t> {
let message = message.as_slice();
@@ -47,7 +47,7 @@ pub extern "C" fn wasm_trap_new(
}
let message = String::from_utf8_lossy(&message[..message.len() - 1]);
Box::new(wasm_trap_t {
trap: HostRef::new(&store.store, Trap::new(message)),
trap: HostRef::new(Trap::new(message)),
})
}

View File

@@ -314,7 +314,7 @@ pub unsafe extern "C" fn wasi_instance_new(
})),
Err(e) => {
*trap = Box::into_raw(Box::new(wasm_trap_t {
trap: HostRef::new(store, Trap::new(e)),
trap: HostRef::new(Trap::new(e)),
}));
None
@@ -352,14 +352,13 @@ pub extern "C" fn wasi_instance_bind_import<'a>(
if &export.ty() != import.ty.func()? {
return None;
}
let store = export.store();
let entry = instance
.export_cache
.entry(name.to_string())
.or_insert_with(|| {
Box::new(wasm_extern_t {
which: ExternHost::Func(HostRef::new(store, export.clone())),
which: ExternHost::Func(HostRef::new(export.clone())),
})
});
Some(entry)