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

Take into account requirements for "isolate" or single-threaded runtimes #5284

Draft
wants to merge 19 commits into
base: wasmer-5.1.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
915252e
feat(api): Add function to get the runtime kind from an engine
xdoardo Nov 27, 2024
bf76d61
fix(api/v8): Don't allow creation and instantiation on two different …
xdoardo Dec 2, 2024
f4cd60b
fix(api/c_api): Take into account callbacks during function calls
xdoardo Dec 2, 2024
4a564b1
feat(api): Rename `Runtime` to `RuntimeKind`
xdoardo Dec 4, 2024
86833eb
fix(api/v8): Better message when an export is not found
xdoardo Dec 4, 2024
fe17f57
fix(api): Make `StoreInner` sync
xdoardo Dec 5, 2024
2218373
fix(cli): Move `StoreOptions` to `RuntimeOptions`
xdoardo Dec 5, 2024
9221246
fix(wasix): Add better message when a non-errno error happens
xdoardo Dec 5, 2024
c04fffc
fix: Remove top level constructions of `stores`
xdoardo Dec 5, 2024
a352602
fix(wasix): Create store and instances in the executor thread
xdoardo Dec 5, 2024
20c161a
chore: Make linter happy
xdoardo Dec 5, 2024
cff7a39
fix(cli-compiler): Revert to `StoreOptions` in cli-compiler
xdoardo Dec 5, 2024
5277d33
chore: Make linter happy
xdoardo Dec 5, 2024
c64f421
fix(cli): Use appropriate calls to `RuntimeOptions`
xdoardo Dec 5, 2024
c463031
fix(api): Remove unnecessary `Send` and `Sync` for `StoreInner`
xdoardo Dec 6, 2024
1aaf081
fix(api/js): Fix missing qualification
xdoardo Dec 6, 2024
89c4a4c
fix(api/jsc): Fix missing qualification
xdoardo Dec 6, 2024
9038db9
fix(api): Add `non_exhaustive` to public `RuntimeKind` enum
xdoardo Dec 6, 2024
c2815b3
fix(wasix): Remove comments and move `SpawnMemoryTypeOrStore` to visi…
xdoardo Dec 6, 2024
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
18 changes: 18 additions & 0 deletions lib/api/src/entities/engine/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@ use crate::{
gen_rt_ty!(Engine @derives Debug, Clone);

impl RuntimeEngine {
/// Returns the [`crate::Runtime`] kind this engine belongs to.
pub fn get_rt_kind(&self) -> crate::RuntimeKind {
match self {
#[cfg(feature = "sys")]
Self::Sys(_) => crate::RuntimeKind::Sys,
#[cfg(feature = "v8")]
Self::V8(_) => crate::RuntimeKind::V8,
#[cfg(feature = "wamr")]
Self::Wamr(_) => crate::RuntimeKind::Wamr,
#[cfg(feature = "wasmi")]
Self::Wasmi(_) => crate::RuntimeKind::Wasmi,
#[cfg(feature = "js")]
Self::Js(_) => crate::RuntimeKind::Js,
#[cfg(feature = "jsc")]
Self::Jsc(_) => crate::RuntimeKind::Jsc,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is public, it needs to be #[non_exhaustive]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}

/// Returns the deterministic id of this engine.
pub fn deterministic_id(&self) -> &str {
match_rt!(on self => s {
Expand Down
5 changes: 5 additions & 0 deletions lib/api/src/entities/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ impl Engine {
ENGINE_ID_COUNTER.fetch_add(1, std::sync::atomic::Ordering::SeqCst)
}

/// Returns the [`crate::RuntimeKind`] kind this engine belongs to.
pub fn get_rt_kind(&self) -> crate::RuntimeKind {
self.rt.get_rt_kind()
}

/// Returns the deterministic id of this engine.
pub fn deterministic_id(&self) -> &str {
self.rt.deterministic_id()
Expand Down
36 changes: 18 additions & 18 deletions lib/api/src/entities/function/host/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,31 @@ impl< $( $x, )* Rets, RetsAsResult, T, Func> crate::HostFunction<T, ( $( $x ),*
{

#[allow(non_snake_case)]
fn function_callback(&self, rt: crate::rt::Runtime) -> crate::vm::VMFunctionCallback {
fn function_callback(&self, rt: crate::rt::RuntimeKind) -> crate::vm::VMFunctionCallback {
paste::paste!{
match rt {
#[cfg(feature = "sys")]
crate::rt::Runtime::Sys => crate::vm::VMFunctionCallback::Sys(crate::rt::sys::function::[<gen_fn_callback_ $c_struct_name:lower >](self)),
crate::rt::RuntimeKind::Sys => crate::vm::VMFunctionCallback::Sys(crate::rt::sys::function::[<gen_fn_callback_ $c_struct_name:lower >](self)),
#[cfg(feature = "js")]
crate::rt::Runtime::Js => crate::vm::VMFunctionCallback::Js(crate::rt::js::function::[<gen_fn_callback_ $c_struct_name:lower >](self)),
crate::rt::RuntimeKind::Js => crate::vm::VMFunctionCallback::Js(crate::rt::js::function::[<gen_fn_callback_ $c_struct_name:lower >](self)),
#[cfg(feature = "jsc")]
crate::rt::Runtime::Jsc => crate::vm::VMFunctionCallback::Jsc(crate::rt::jsc::function::[<gen_fn_callback_ $c_struct_name:lower >](self)),
crate::rt::RuntimeKind::Jsc => crate::vm::VMFunctionCallback::Jsc(crate::rt::jsc::function::[<gen_fn_callback_ $c_struct_name:lower >](self)),
#[cfg(feature = "wamr")]
crate::rt::Runtime::Wamr => crate::vm::VMFunctionCallback::Wamr(crate::rt::wamr::function::[<gen_fn_callback_ $c_struct_name:lower >](self)),
crate::rt::RuntimeKind::Wamr => crate::vm::VMFunctionCallback::Wamr(crate::rt::wamr::function::[<gen_fn_callback_ $c_struct_name:lower >](self)),
#[cfg(feature = "wasmi")]
crate::rt::Runtime::Wasmi => crate::vm::VMFunctionCallback::Wasmi(crate::rt::wasmi::function::[<gen_fn_callback_ $c_struct_name:lower >](self)),
crate::rt::RuntimeKind::Wasmi => crate::vm::VMFunctionCallback::Wasmi(crate::rt::wasmi::function::[<gen_fn_callback_ $c_struct_name:lower >](self)),
#[cfg(feature = "v8")]
crate::rt::Runtime::V8 => crate::vm::VMFunctionCallback::V8(crate::rt::v8::function::[<gen_fn_callback_ $c_struct_name:lower >](self))
crate::rt::RuntimeKind::V8 => crate::vm::VMFunctionCallback::V8(crate::rt::v8::function::[<gen_fn_callback_ $c_struct_name:lower >](self))
}
}
}

#[allow(non_snake_case)]
fn call_trampoline_address(rt: crate::rt::Runtime) -> crate::vm::VMTrampoline {
fn call_trampoline_address(rt: crate::rt::RuntimeKind) -> crate::vm::VMTrampoline {
paste::paste!{
match rt {
#[cfg(feature = "sys")]
crate::rt::Runtime::Sys => crate::vm::VMTrampoline::Sys(crate::rt::sys::function::[<gen_call_trampoline_address_ $c_struct_name:lower >]::<$($x,)* Rets>()),
crate::rt::RuntimeKind::Sys => crate::vm::VMTrampoline::Sys(crate::rt::sys::function::[<gen_call_trampoline_address_ $c_struct_name:lower >]::<$($x,)* Rets>()),
_ => unimplemented!()
}
}
Expand All @@ -68,31 +68,31 @@ where


#[allow(non_snake_case)]
fn function_callback(&self, rt: crate::rt::Runtime) -> crate::vm::VMFunctionCallback {
fn function_callback(&self, rt: crate::rt::RuntimeKind) -> crate::vm::VMFunctionCallback {
paste::paste!{
match rt {
#[cfg(feature = "sys")]
crate::rt::Runtime::Sys => crate::vm::VMFunctionCallback::Sys(crate::rt::sys::function::[<gen_fn_callback_ $c_struct_name:lower _no_env>](self)),
crate::rt::RuntimeKind::Sys => crate::vm::VMFunctionCallback::Sys(crate::rt::sys::function::[<gen_fn_callback_ $c_struct_name:lower _no_env>](self)),
#[cfg(feature = "js")]
crate::rt::Runtime::Js => crate::vm::VMFunctionCallback::Js(crate::rt::js::function::[<gen_fn_callback_ $c_struct_name:lower _no_env>](self)),
crate::rt::RuntimeKind::Js => crate::vm::VMFunctionCallback::Js(crate::rt::js::function::[<gen_fn_callback_ $c_struct_name:lower _no_env>](self)),
#[cfg(feature = "jsc")]
crate::rt::Runtime::Jsc => crate::vm::VMFunctionCallback::Jsc(crate::rt::jsc::function::[<gen_fn_callback_ $c_struct_name:lower _no_env>](self)),
crate::rt::RuntimeKind::Jsc => crate::vm::VMFunctionCallback::Jsc(crate::rt::jsc::function::[<gen_fn_callback_ $c_struct_name:lower _no_env>](self)),
#[cfg(feature = "wamr")]
crate::rt::Runtime::Wamr => crate::vm::VMFunctionCallback::Wamr(crate::rt::wamr::function::[<gen_fn_callback_ $c_struct_name:lower _no_env>](self)),
crate::rt::RuntimeKind::Wamr => crate::vm::VMFunctionCallback::Wamr(crate::rt::wamr::function::[<gen_fn_callback_ $c_struct_name:lower _no_env>](self)),
#[cfg(feature = "wasmi")]
crate::rt::Runtime::Wasmi => crate::vm::VMFunctionCallback::Wasmi(crate::rt::wasmi::function::[<gen_fn_callback_ $c_struct_name:lower _no_env>](self)),
crate::rt::RuntimeKind::Wasmi => crate::vm::VMFunctionCallback::Wasmi(crate::rt::wasmi::function::[<gen_fn_callback_ $c_struct_name:lower _no_env>](self)),
#[cfg(feature = "v8")]
crate::rt::Runtime::V8 => crate::vm::VMFunctionCallback::V8(crate::rt::v8::function::[<gen_fn_callback_ $c_struct_name:lower _no_env>](self))
crate::rt::RuntimeKind::V8 => crate::vm::VMFunctionCallback::V8(crate::rt::v8::function::[<gen_fn_callback_ $c_struct_name:lower _no_env>](self))
}
}
}

#[allow(non_snake_case)]
fn call_trampoline_address(rt: crate::rt::Runtime) -> crate::vm::VMTrampoline {
fn call_trampoline_address(rt: crate::rt::RuntimeKind) -> crate::vm::VMTrampoline {
paste::paste!{
match rt {
#[cfg(feature = "sys")]
crate::rt::Runtime::Sys => crate::vm::VMTrampoline::Sys(crate::rt::sys::function::[<gen_call_trampoline_address_ $c_struct_name:lower _no_env>]::<$($x,)* Rets>()),
crate::rt::RuntimeKind::Sys => crate::vm::VMTrampoline::Sys(crate::rt::sys::function::[<gen_call_trampoline_address_ $c_struct_name:lower _no_env>]::<$($x,)* Rets>()),
_ => unimplemented!()
}
}
Expand Down
6 changes: 3 additions & 3 deletions lib/api/src/entities/function/host/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod imp;

use crate::{
vm::{VMFunctionCallback, VMTrampoline},
Runtime, WasmTypeList,
RuntimeKind, WasmTypeList,
};

/// The `HostFunction` trait represents the set of functions that
Expand All @@ -16,10 +16,10 @@ where
Kind: HostFunctionKind,
{
/// Get the pointer to the function body for a given runtime.
fn function_callback(&self, rt: Runtime) -> crate::vm::VMFunctionCallback;
fn function_callback(&self, rt: RuntimeKind) -> crate::vm::VMFunctionCallback;

/// Get the pointer to the function call trampoline for a given runtime.
fn call_trampoline_address(rt: Runtime) -> crate::vm::VMTrampoline;
fn call_trampoline_address(rt: RuntimeKind) -> crate::vm::VMTrampoline;
}

/// Empty trait to specify the kind of `HostFunction`: With or
Expand Down
5 changes: 5 additions & 0 deletions lib/api/src/rt/js/entities/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ impl crate::Engine {
_ => panic!("Not a `js` engine!"),
}
}

/// Return true if [`self`] is an engine from the `js` runtime.
pub fn is_js(&self) -> bool {
matches!(self.rt, crate::RuntimeEngine::Js(_))
}
}

impl Into<crate::Engine> for Engine {
Expand Down
2 changes: 1 addition & 1 deletion lib/api/src/rt/js/entities/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ where
T: Sized,
{
Self {
address: function.function_callback(crate::Runtime::Js).into_js(),
address: function.function_callback(crate::RuntimeKind::Js).into_js(),
_phantom: PhantomData,
}
}
Expand Down
5 changes: 5 additions & 0 deletions lib/api/src/rt/jsc/entities/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ impl crate::Engine {
_ => panic!("Not a `jsc` engine!"),
}
}

/// Return true if [`self`] is an engine from the `jsc` runtime.
pub fn is_jsc(&self) -> bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does the js engine have a is_jsc method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is in jsc/entities/engine.rs. Am I missing something?

matches!(self.rt, crate::RuntimeEngine::Jsc(_))
}
}

impl Into<crate::Engine> for Engine {
Expand Down
4 changes: 3 additions & 1 deletion lib/api/src/rt/jsc/entities/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,9 @@ where
T: Sized,
{
Self {
callback: function.function_callback(crate::Runtime::Jsc).into_jsc(),
callback: function
.function_callback(crate::RuntimeKind::Jsc)
.into_jsc(),
_phantom: PhantomData,
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/api/src/rt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ pub mod js;
#[cfg(feature = "jsc")]
pub mod jsc;

#[non_exhaustive]
#[derive(Debug, Clone, Copy)]
/// An enumeration over all the supported runtimes.
pub enum Runtime {
pub enum RuntimeKind {
#[cfg(feature = "sys")]
/// The `sys` runtime.
Sys,
Expand Down
8 changes: 4 additions & 4 deletions lib/api/src/rt/sys/entities/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl Function {
Rets: WasmTypeList,
{
let env = FunctionEnv::new(store, ());
let func_ptr = func.function_callback(crate::Runtime::Sys).into_sys();
let func_ptr = func.function_callback(crate::RuntimeKind::Sys).into_sys();
let host_data = Box::new(StaticFunction {
raw_store: store.as_store_mut().as_raw() as *mut u8,
env,
Expand All @@ -150,7 +150,7 @@ impl Function {
};
let call_trampoline =
<F as HostFunction<(), Args, Rets, WithoutEnv>>::call_trampoline_address(
crate::Runtime::Sys,
crate::RuntimeKind::Sys,
)
.into_sys();
let anyfunc = VMCallerCheckedAnyfunc {
Expand Down Expand Up @@ -181,7 +181,7 @@ impl Function {
Args: WasmTypeList,
Rets: WasmTypeList,
{
let func_ptr = func.function_callback(crate::Runtime::Sys).into_sys();
let func_ptr = func.function_callback(crate::RuntimeKind::Sys).into_sys();
let host_data = Box::new(StaticFunction {
raw_store: store.as_store_mut().as_raw() as *mut u8,
env: env.as_sys().clone().into(),
Expand All @@ -198,7 +198,7 @@ impl Function {
host_env: host_data.as_ref() as *const _ as *mut c_void,
};
let call_trampoline = <F as HostFunction<T, Args, Rets, WithEnv>>::call_trampoline_address(
crate::Runtime::Sys,
crate::RuntimeKind::Sys,
)
.into_sys();
let anyfunc = VMCallerCheckedAnyfunc {
Expand Down
82 changes: 70 additions & 12 deletions lib/api/src/rt/v8/entities/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ impl Function {
+ Send
+ Sync,
{
let mut store = store.as_store_mut();
let v8_store = store.inner.store.as_v8();

if v8_store.thread_id != std::thread::current().id() {
panic!("Cannot create new function: current thread is different from the thread the store was created in!");
}

let fn_ty: FunctionType = ty.into();
let params = fn_ty.params();

Expand Down Expand Up @@ -121,8 +128,7 @@ impl Function {
)
};

let mut store = store.as_store_mut();
let inner = store.inner.store.as_v8().inner;
let inner = v8_store.inner;

let callback: CCallback = make_fn_callback(&func, param_types.len());

Expand Down Expand Up @@ -159,6 +165,13 @@ impl Function {
Args: WasmTypeList,
Rets: WasmTypeList,
{
let mut store = store.as_store_mut();
let v8_store = store.inner.store.as_v8();

if v8_store.thread_id != std::thread::current().id() {
panic!("Cannot create new typed function: current thread is different from the thread the store was created in!");
}

let mut param_types = Args::wasm_types()
.into_iter()
.map(|param| {
Expand Down Expand Up @@ -190,11 +203,11 @@ impl Function {
let wasm_functype =
unsafe { wasm_functype_new(&mut wasm_param_types, &mut wasm_result_types) };

let mut store = store.as_store_mut();
let inner = store.inner.store.as_v8().inner;
let inner = v8_store.inner;

let callback: CCallback =
unsafe { std::mem::transmute(func.function_callback(crate::Runtime::V8).into_v8()) };
let callback: CCallback = unsafe {
std::mem::transmute(func.function_callback(crate::RuntimeKind::V8).into_v8())
};

let mut callback_env: *mut FunctionCallbackEnv<'_, F> =
Box::into_raw(Box::new(FunctionCallbackEnv {
Expand Down Expand Up @@ -233,6 +246,13 @@ impl Function {
Rets: WasmTypeList,
T: Send + 'static,
{
let mut store = store.as_store_mut();
let v8_store = store.inner.store.as_v8();

if v8_store.thread_id != std::thread::current().id() {
panic!("Cannot create new typed function with env: current thread is different from the thread the store was created in!");
}

let mut param_types = Args::wasm_types()
.into_iter()
.map(|param| {
Expand Down Expand Up @@ -268,11 +288,11 @@ impl Function {
)
};

let mut store = store.as_store_mut();
let inner = store.inner.store.as_v8().inner;
let inner = v8_store.inner;

let callback: CCallback =
unsafe { std::mem::transmute(func.function_callback(crate::Runtime::V8).into_v8()) };
let callback: CCallback = unsafe {
std::mem::transmute(func.function_callback(crate::RuntimeKind::V8).into_v8())
};

let mut callback_env: *mut FunctionCallbackEnv<'_, F> =
Box::into_raw(Box::new(FunctionCallbackEnv {
Expand Down Expand Up @@ -300,7 +320,13 @@ impl Function {
}
}

pub fn ty(&self, _store: &impl AsStoreRef) -> FunctionType {
pub fn ty(&self, store: &impl AsStoreRef) -> FunctionType {
let store_ref = store.as_store_ref();
let v8_store = store_ref.inner.store.as_v8();

if v8_store.thread_id != std::thread::current().id() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is used a lot... how about making it a method?

panic!("Cannot get the function's type: current thread is different from the thread the store was created in!");
}
let type_ = unsafe { wasm_func_type(self.handle) };
let params: *const wasm_valtype_vec_t = unsafe { wasm_functype_params(type_) };
let returns: *const wasm_valtype_vec_t = unsafe { wasm_functype_results(type_) };
Expand Down Expand Up @@ -341,6 +367,11 @@ impl Function {
params: &[Value],
) -> Result<Box<[Value]>, RuntimeError> {
let store_mut = store.as_store_mut();
let v8_store = store_mut.inner.store.as_v8();

if v8_store.thread_id != std::thread::current().id() {
return Err(RuntimeError::new("Cannot call function: current thread is different from the thread the store was created in!"));
}

let mut args = {
let params = params
Expand All @@ -366,7 +397,34 @@ impl Function {
ptr
};

let trap = unsafe { wasm_func_call(self.handle, args as *const _, results as *mut _) };
let mut trap;

loop {
trap = unsafe { wasm_func_call(self.handle, args as *const _, results) };
let store_mut = store.as_store_mut();
if let Some(callback) = store_mut.inner.on_called.take() {
match callback(store_mut) {
Ok(wasmer_types::OnCalledAction::InvokeAgain) => {
continue;
}
Ok(wasmer_types::OnCalledAction::Finish) => {
break;
}
Ok(wasmer_types::OnCalledAction::Trap(trap)) => {
return Err(RuntimeError::user(trap))
}
Err(trap) => return Err(RuntimeError::user(trap)),
}
}
break;
}

if !trap.is_null() {
unsafe {
let trap: Trap = trap.into();
return Err(RuntimeError::from(trap));
}
}

if !trap.is_null() {
return Err(Into::<Trap>::into(trap).into());
Expand Down
Loading
Loading