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

Add support for exceptions in LLVM #5347

Open
wants to merge 9 commits into
base: wasmer-5.1.0
Choose a base branch
from
1,077 changes: 566 additions & 511 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,11 @@ hyper = "1"
reqwest = { version = "0.12.0", default-features = false }
enumset = "1.1.0"
memoffset = "0.9.0"
wasmparser = { version = "0.216.0", default-features = false, features = [
wasmparser = { version = "0.221.0", default-features = false, features = [
"validate",
"features",
"component-model",
"simd",
] }
rkyv = { version = "0.8.8", features = ["indexmap-2", "bytes-1"] }
memmap2 = { version = "0.6.2" }
Expand Down Expand Up @@ -155,7 +158,7 @@ tracing-subscriber = { version = "0.3", default-features = false, features = [
"env-filter",
"fmt",
] }
reqwest = {workspace = true, features = ["blocking", "rustls-tls"]}
reqwest = { workspace = true, features = ["blocking", "rustls-tls"] }

[features]
# Don't add the compiler features in default, please add them on the Makefile
Expand Down
6 changes: 6 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ fn main() -> anyhow::Result<()> {
.expect("Can't get directory");
build_deps::rerun_if_changed_paths("tests/wasi-wast/wasi/nightly-2022-10-18/*")
.expect("Can't get directory");
build_deps::rerun_if_changed_paths("tests/wast/spec/proposals/*").expect("Can't get directory");

let out_dir = PathBuf::from(
env::var_os("OUT_DIR").expect("The OUT_DIR environment variable must be set"),
Expand All @@ -42,6 +43,11 @@ fn main() -> anyhow::Result<()> {
wast_processor,
)?;
test_directory_module(spectests, "tests/wast/spec/proposals/simd", wast_processor)?;
test_directory_module(
spectests,
"tests/wast/spec/proposals/exception-handling",
wast_processor,
)?;
test_directory_module(
spectests,
"tests/wast/spec/proposals/threads",
Expand Down
135 changes: 135 additions & 0 deletions lib/api/src/entities/exception/exnref/inner.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
use std::any::Any;

use crate::entities::store::{AsStoreMut, AsStoreRef};
use crate::macros::rt::{gen_rt_ty, match_rt};
use crate::vm::VMExceptionRef;
use crate::StoreRef;

gen_rt_ty!(ExceptionRef @derives derive_more::From, Debug, Clone ; @path exception);

impl RuntimeExceptionRef {
/// Make a new extern reference
pub fn new<T>(store: &mut impl AsStoreMut, value: T) -> Self
where
T: Any + Send + Sync + 'static + Sized,
{
match &store.as_store_mut().inner.store {
#[cfg(feature = "sys")]
crate::RuntimeStore::Sys(s) => Self::Sys(
crate::rt::sys::entities::exception::ExceptionRef::new(store, value),
),
#[cfg(feature = "wamr")]
crate::RuntimeStore::Wamr(s) => Self::Wamr(
crate::rt::wamr::entities::exception::ExceptionRef::new(store, value),
),
#[cfg(feature = "wasmi")]
crate::RuntimeStore::Wasmi(s) => Self::Wasmi(
crate::rt::wasmi::entities::exception::ExceptionRef::new(store, value),
),
#[cfg(feature = "v8")]
crate::RuntimeStore::V8(s) => Self::V8(
crate::rt::v8::entities::exception::ExceptionRef::new(store, value),
),
#[cfg(feature = "js")]
crate::RuntimeStore::Js(s) => Self::Js(
crate::rt::js::entities::exception::ExceptionRef::new(store, value),
),
#[cfg(feature = "jsc")]
crate::RuntimeStore::Jsc(s) => Self::Jsc(
crate::rt::jsc::entities::exception::ExceptionRef::new(store, value),
),
}
}

/// Try to downcast to the given value.
pub fn downcast<'a, T>(&self, store: &'a impl AsStoreRef) -> Option<&'a T>
where
T: Any + Send + Sync + 'static + Sized,
{
match_rt!(on self => r {
r.downcast::<T>(store)
})
}

/// Create a [`VMExceptionRef`] from [`Self`].
pub(crate) fn vm_exceptionref(&self) -> VMExceptionRef {
match self {
#[cfg(feature = "sys")]
Self::Sys(r) => VMExceptionRef::Sys(r.vm_exceptionref()),
#[cfg(feature = "wamr")]
Self::Wamr(r) => VMExceptionRef::Wamr(r.vm_exceptionref()),
#[cfg(feature = "wasmi")]
Self::Wasmi(r) => VMExceptionRef::Wasmi(r.vm_exceptionref()),
#[cfg(feature = "v8")]
Self::V8(r) => VMExceptionRef::V8(r.vm_exceptionref()),
#[cfg(feature = "js")]
Self::Js(r) => VMExceptionRef::Js(r.vm_exceptionref()),
#[cfg(feature = "jsc")]
Self::Jsc(r) => VMExceptionRef::Jsc(r.vm_exceptionref()),
}
}

/// Create an instance of [`Self`] from a [`VMExceptionRef`].
pub(crate) unsafe fn from_vm_exceptionref(
store: &mut impl AsStoreMut,
vm_externref: VMExceptionRef,
) -> Self {
match &store.as_store_mut().inner.store {
#[cfg(feature = "sys")]
crate::RuntimeStore::Sys(_) => Self::Sys(
crate::rt::sys::entities::exception::ExceptionRef::from_vm_exceptionref(
store,
vm_externref.into_sys(),
),
),
#[cfg(feature = "wamr")]
crate::RuntimeStore::Wamr(_) => Self::Wamr(
crate::rt::wamr::entities::exception::ExceptionRef::from_vm_exceptionref(
store,
vm_externref.into_wamr(),
),
),
#[cfg(feature = "wasmi")]
crate::RuntimeStore::Wasmi(_) => Self::Wasmi(
crate::rt::wasmi::entities::exception::ExceptionRef::from_vm_exceptionref(
store,
vm_externref.into_wasmi(),
),
),
#[cfg(feature = "v8")]
crate::RuntimeStore::V8(_) => Self::V8(
crate::rt::v8::entities::exception::ExceptionRef::from_vm_exceptionref(
store,
vm_externref.into_v8(),
),
),
#[cfg(feature = "js")]
crate::RuntimeStore::Js(_) => Self::Js(
crate::rt::js::entities::exception::ExceptionRef::from_vm_exceptionref(
store,
vm_externref.into_js(),
),
),
#[cfg(feature = "jsc")]
crate::RuntimeStore::Jsc(_) => Self::Jsc(
crate::rt::jsc::entities::exception::ExceptionRef::from_vm_exceptionref(
store,
vm_externref.into_jsc(),
),
),
}
}

/// Checks whether this `ExceptionRef` can be used with the given context.
///
/// Primitive (`i32`, `i64`, etc) and null funcref/externref values are not
/// tied to a context and can be freely shared between contexts.
///
/// Externref and funcref values are tied to a context and can only be used
/// with that context.
pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool {
match_rt!(on self => r {
r.is_from_store(store)
})
}
}
57 changes: 57 additions & 0 deletions lib/api/src/entities/exception/exnref/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use std::any::Any;

use crate::entities::store::{AsStoreMut, AsStoreRef};
use crate::vm::{VMExceptionRef, VMExternRef};
use crate::StoreRef;

pub(crate) mod inner;
pub(crate) use inner::*;

#[derive(Debug, Clone, derive_more::From)]
/// An opaque reference to some data. This reference can be passed through Wasm.
pub struct ExceptionRef(pub(crate) RuntimeExceptionRef);
Copy link
Member

Choose a reason for hiding this comment

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

This should be opaque (pub only for the crate)


impl ExceptionRef {
/// Make a new extern reference
pub fn new<T>(store: &mut impl AsStoreMut, value: T) -> Self
where
T: Any + Send + Sync + 'static + Sized,
{
Self(RuntimeExceptionRef::new(store, value))
}

/// Try to downcast to the given value.
pub fn downcast<'a, T>(&self, store: &'a impl AsStoreRef) -> Option<&'a T>
where
T: Any + Send + Sync + 'static + Sized,
{
self.0.downcast(store)
}

/// Create a [`VMExceptionRef`] from [`Self`].
pub(crate) fn vm_exceptionref(&self) -> VMExceptionRef {
self.0.vm_exceptionref()
}

/// Create an instance of [`Self`] from a [`VMExceptionRef`].
pub(crate) unsafe fn from_vm_exceptionref(
store: &mut impl AsStoreMut,
vm_externref: VMExceptionRef,
) -> Self {
Self(RuntimeExceptionRef::from_vm_exceptionref(
store,
vm_externref,
))
}

/// Checks whether this `ExceptionRef` can be used with the given context.
///
/// Primitive (`i32`, `i64`, etc) and null funcref/externref values are not
/// tied to a context and can be freely shared between contexts.
///
/// Externref and funcref values are tied to a context and can only be used
/// with that context.
pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool {
self.0.is_from_store(store)
}
}
53 changes: 53 additions & 0 deletions lib/api/src/entities/exception/inner.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use crate::{
macros::rt::{gen_rt_ty, match_rt},
vm::{VMExtern, VMExternTag},
AsStoreMut, AsStoreRef, ExportError, Exportable, Extern, Tag, Value,
};

/// A WebAssembly `global` instance.
///
/// A global instance is the runtime representation of a global variable.
/// It consists of an individual value and a flag indicating whether it is mutable.
///
/// Spec: <https://webassembly.github.io/spec/core/exec/runtime.html#global-instances>
gen_rt_ty!(Exception
@cfg feature = "artifact-size" => derive(loupe::MemoryUsage)
@derives Debug, Clone, PartialEq, Eq, derive_more::From
);

impl RuntimeException {
/// Create a new exception with the given tag type and payload.
pub fn new(store: &mut impl AsStoreMut, tag: Tag, payload: &[Value]) -> Self {
match &store.as_store_mut().inner.store {
#[cfg(feature = "sys")]
crate::RuntimeStore::Sys(_) => Self::Sys(crate::rt::sys::exception::Exception::new(
store, tag, payload,
)),
#[cfg(feature = "wamr")]
crate::RuntimeStore::Wamr(_) => Self::Wamr(crate::rt::wamr::exception::Exception::new(
store, tag, payload,
)),
#[cfg(feature = "wasmi")]
crate::RuntimeStore::Wasmi(_) => Self::Wasmi(
crate::rt::wasmi::exception::Exception::new(store, tag, payload),
),
#[cfg(feature = "v8")]
crate::RuntimeStore::V8(_) => Self::V8(crate::rt::v8::exception::Exception::new(
store, tag, payload,
)),
#[cfg(feature = "js")]
crate::RuntimeStore::Js(_) => Self::Js(crate::rt::js::exception::Exception::new(
store, tag, payload,
)),
#[cfg(feature = "jsc")]
crate::RuntimeStore::Jsc(_) => Self::Jsc(crate::rt::jsc::exception::Exception::new(
store, tag, payload,
)),
}
}

/// Checks whether this `Exception` can be used with the given store.
pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool {
todo!()
}
}
33 changes: 33 additions & 0 deletions lib/api/src/entities/exception/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
pub(crate) mod exnref;
pub use exnref::*;

pub(crate) mod inner;
pub(crate) use inner::*;
use wasmer_types::{TagType, Type};

use crate::{
vm::{VMExtern, VMExternTag},
AsStoreMut, AsStoreRef, ExportError, Exportable, Extern, Tag, Value,
};

/// A WebAssembly `exception` instance.
///
/// An exception is an internal construct in WebAssembly that represents a runtime object that can
/// be thrown. A WebAssembly exception consists of an exception tag and its runtime arguments.
///
/// Spec: <https://github.com/WebAssembly/exception-handling/blob/main/proposals/exception-handling/Exceptions.md#exceptions>
#[derive(Debug, Clone, PartialEq, Eq, derive_more::From)]
#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
pub struct Exception(pub(crate) RuntimeException);

impl Exception {
/// Create a new exception with the given tag type and payload.
pub fn new(store: &mut impl AsStoreMut, tag: Tag, payload: &[Value]) -> Self {
Self(RuntimeException::new(store, tag, payload))
}

/// Checks whether this `Exception` comes from the given store.
pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool {
self.0.is_from_store(store)
}
}
Loading
Loading