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

Try to use wasmtime on x86_64 #367

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
669 changes: 668 additions & 1 deletion Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ wasmi = { git = "https://github.com/tomaka/wasmi", branch = "no-std", default-fe
criterion = "0.3"
futures = { version = "0.3.1", default-features = false, features = ["executor"] }

[target.'cfg(target_arch = "x86_64")'.dependencies]
wasmtime = { version = "0.13.0", default-features = false }

[[bench]]
name = "keccak"
harness = false
5 changes: 4 additions & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,15 @@
//! handler.
//!

#![feature(asm, global_asm, naked_functions)]
#![feature(new_uninit)] // TODO: no; definitely can be circumvented too
#![warn(missing_docs)]
//#![deny(unsafe_code)] // TODO: 🤷
#![allow(dead_code)] // TODO: temporary during development

// The crate uses the stdlib for testing purposes.
#![cfg_attr(not(test), no_std)]
// TODO: restore no_std
//#![cfg_attr(not(test), no_std)]

extern crate alloc;

Expand Down
20 changes: 18 additions & 2 deletions core/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ use core::fmt;
/// This is the equivalent of an [ELF](https://en.wikipedia.org/wiki/Executable_and_Linkable_Format)
/// or a [PE](https://en.wikipedia.org/wiki/Portable_Executable).
pub struct Module {
#[cfg(not(target_arch = "x86_64"))]
inner: wasmi::Module,
#[cfg(target_arch = "x86_64")]
bytes: Vec<u8>,
hash: ModuleHash,
}

Expand All @@ -39,17 +42,30 @@ pub struct FromBase58Error {}
impl Module {
/// Parses a module from WASM bytes.
pub fn from_bytes(buffer: impl AsRef<[u8]>) -> Result<Self, FromBytesError> {
let inner = wasmi::Module::from_buffer(buffer.as_ref()).map_err(|_| FromBytesError {})?;
let buffer = buffer.as_ref();
let hash = ModuleHash::from_bytes(buffer);

Ok(Module { inner, hash })
Ok(Module {
#[cfg(not(target_arch = "x86_64"))]
inner: wasmi::Module::from_buffer(buffer.as_ref()).map_err(|_| FromBytesError {})?,
#[cfg(target_arch = "x86_64")]
bytes: buffer.to_owned(),
hash,
})
}

/// Returns a reference to the internal module.
#[cfg(not(target_arch = "x86_64"))]
pub(crate) fn as_ref(&self) -> &wasmi::Module {
&self.inner
}

/// Returns the Wasm binary.
#[cfg(target_arch = "x86_64")]
pub(crate) fn as_ref(&self) -> &[u8] {
&self.bytes
}

/// Returns the hash of that module.
///
/// This gives the same result as calling `ModuleHash::from_bytes` on the original input.
Expand Down
45 changes: 45 additions & 0 deletions core/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,19 @@ impl<'a> From<&'a wasmi::Signature> for Signature {
}
}

impl<'a> From<&'a wasmtime::FuncType> for Signature {
fn from(sig: &'a wasmtime::FuncType) -> Signature {
// TODO: we only support one return type at the moment; what even is multiple
// return types?
assert!(sig.results().len() <= 1);

Signature::new(
sig.params().iter().cloned().map(ValueType::from),
sig.results().get(0).cloned().map(ValueType::from),
)
}
}

impl From<wasmi::Signature> for Signature {
fn from(sig: wasmi::Signature) -> Signature {
Signature::from(&sig)
Expand Down Expand Up @@ -198,6 +211,26 @@ impl From<WasmValue> for wasmi::RuntimeValue {
}
}

impl From<WasmValue> for wasmtime::Val {
fn from(val: WasmValue) -> Self {
match val {
WasmValue::I32(v) => wasmtime::Val::I32(v),
WasmValue::I64(v) => wasmtime::Val::I64(v),
_ => unimplemented!(),
}
}
}

impl From<wasmtime::Val> for WasmValue {
fn from(val: wasmtime::Val) -> Self {
match val {
wasmtime::Val::I32(v) => WasmValue::I32(v),
wasmtime::Val::I64(v) => WasmValue::I64(v),
_ => unimplemented!(),
}
}
}

impl From<wasmi::ValueType> for ValueType {
fn from(val: wasmi::ValueType) -> Self {
match val {
Expand All @@ -208,3 +241,15 @@ impl From<wasmi::ValueType> for ValueType {
}
}
}

impl From<wasmtime::ValType> for ValueType {
fn from(val: wasmtime::ValType) -> Self {
match val {
wasmtime::ValType::I32 => ValueType::I32,
wasmtime::ValType::I64 => ValueType::I64,
wasmtime::ValType::F32 => ValueType::F32,
wasmtime::ValType::F64 => ValueType::F64,
_ => unimplemented!(), // TODO:
}
}
}
Loading