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

Added support for calling dynamic functions defined on the host #2511

Merged
merged 8 commits into from
Aug 11, 2021
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
472 changes: 247 additions & 225 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ test-generator = { path = "tests/lib/test-generator" }
build-deps = "0.1.4"
anyhow = "1.0"
glob = "0.3"
rustc_version = "0.3"
rustc_version = "0.4"

[dev-dependencies]
anyhow = "1.0"
Expand Down
2 changes: 2 additions & 0 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ deny = [
]
# Certain crates/versions that will be skipped when doing duplicate detection.
skip = [
{ name = "ahash", version = "=0.4.7" },
{ name = "hashbrown", version = "=0.9.1" },
{ name = "cfg-if", version = "0.1.10" },
{ name = "strsim", version = "=0.8.0" },
{ name = "semver", version = "=0.11.0" },
Expand Down
2 changes: 1 addition & 1 deletion lib/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ js-sys = "0.3.51"
wasmer-derive = { path = "../derive", version = "2.0.0" }
# - Optional dependencies for `js`.
wasmparser = { version = "0.78", default-features = false, optional = true }
hashbrown = { version = "0.9", optional = true }
hashbrown = { version = "0.11", optional = true }
# - Development Dependencies for `js`.
[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
wat = "1.0"
Expand Down
15 changes: 14 additions & 1 deletion lib/api/src/sys/externals/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,13 +522,26 @@ impl Function {
/// assert_eq!(sum.call(&[Value::I32(1), Value::I32(2)]).unwrap().to_vec(), vec![Value::I32(3)]);
/// ```
pub fn call(&self, params: &[Val]) -> Result<Box<[Val]>, RuntimeError> {
// If it's a function defined in the Wasm, it will always have a call_trampoline
if let Some(trampoline) = self.exported.vm_function.call_trampoline {
let mut results = vec![Val::null(); self.result_arity()];
self.call_wasm(trampoline, params, &mut results)?;
return Ok(results.into_boxed_slice());
}

unimplemented!("The function definition isn't supported for the moment");
// If it's a function defined in the host
match self.exported.vm_function.kind {
VMFunctionKind::Dynamic => unsafe {
type VMContextWithEnv = VMDynamicFunctionContext<DynamicFunction<std::ffi::c_void>>;
let ctx = self.exported.vm_function.vmctx.host_env as *mut VMContextWithEnv;
Ok((*ctx).ctx.call(&params)?.into_boxed_slice())
},
VMFunctionKind::Static => {
unimplemented!(
"Native function definitions can't be directly called from the host yet"
);
}
}
}

pub(crate) fn from_vm_export(store: &Store, wasmer_export: ExportFunction) -> Self {
Expand Down
25 changes: 25 additions & 0 deletions lib/api/tests/js_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,31 @@ mod js {
assert_eq!(exported.call(&[Val::I32(4)]), Ok(expected));
}

#[wasm_bindgen_test]
fn test_unit_native_function_env() {
let store = Store::default();
#[derive(WasmerEnv, Clone)]
struct Env {
multiplier: u32,
}

fn imported_fn(env: &Env, args: &[Val]) -> Result<Vec<Val>, RuntimeError> {
let value = env.multiplier * args[0].unwrap_i32() as u32;
return Ok(vec![Val::I32(value as _)]);
}

let imported_signature = FunctionType::new(vec![Type::I32], vec![Type::I32]);
let imported = Function::new_with_env(
&store,
imported_signature,
Env { multiplier: 3 },
imported_fn,
);

let expected = vec![Val::I32(12)].into_boxed_slice();
assert_eq!(imported.call(&[Val::I32(4)]), Ok(expected));
}

#[wasm_bindgen_test]
fn test_imported_function_with_wasmer_env() {
let store = Store::default();
Expand Down
File renamed without changes.
File renamed without changes.
28 changes: 28 additions & 0 deletions lib/api/tests/sys/instance.rs → lib/api/tests/sys_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,32 @@ mod sys {

Ok(())
}

#[test]
fn unit_native_function_env() -> Result<()> {
let store = Store::default();
#[derive(WasmerEnv, Clone)]
struct Env {
multiplier: u32,
}

fn imported_fn(env: &Env, args: &[Val]) -> Result<Vec<Val>, RuntimeError> {
let value = env.multiplier * args[0].unwrap_i32() as u32;
return Ok(vec![Val::I32(value as _)]);
}

let imported_signature = FunctionType::new(vec![Type::I32], vec![Type::I32]);
let imported = Function::new_with_env(
&store,
imported_signature,
Env { multiplier: 3 },
imported_fn,
);

let expected = vec![Val::I32(12)].into_boxed_slice();
let result = imported.call(&[Val::I32(4)])?;
assert_eq!(result, expected);

Ok(())
}
}
File renamed without changes.
12 changes: 6 additions & 6 deletions lib/compiler-cranelift/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@ edition = "2018"
wasmer-compiler = { path = "../compiler", version = "2.0.0", features = ["translator"], default-features = false }
wasmer-vm = { path = "../vm", version = "2.0.0" }
wasmer-types = { path = "../types", version = "2.0.0", default-features = false, features = ["std"] }
cranelift-entity = { version = "0.74", default-features = false }
cranelift-codegen = { version = "0.74", default-features = false, features = ["x86", "arm64"] }
cranelift-frontend = { version = "0.74", default-features = false }
cranelift-entity = { version = "0.76", default-features = false }
cranelift-codegen = { version = "0.76", default-features = false, features = ["x86", "arm64"] }
cranelift-frontend = { version = "0.76", default-features = false }
tracing = "0.1"
hashbrown = { version = "0.9", optional = true }
hashbrown = { version = "0.11", optional = true }
rayon = "1.5"
more-asserts = "0.2"
gimli = { version = "0.24", optional = true }
gimli = { version = "0.25", optional = true }
smallvec = "1.6"
loupe = "0.1"

[dev-dependencies]
target-lexicon = { version = "0.12", default-features = false }
cranelift-codegen = { version = "0.74", features = ["all-arch"] }
cranelift-codegen = { version = "0.76", features = ["all-arch"] }
lazy_static = "1.4"

[badges]
Expand Down
2 changes: 1 addition & 1 deletion lib/compiler-cranelift/src/address_map.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This file contains code from external sources.
// Attributions: https://github.com/wasmerio/wasmer/blob/master/ATTRIBUTIONS.md

use cranelift_codegen::machinst::buffer::MachSrcLoc;
use cranelift_codegen::MachSrcLoc;
use cranelift_codegen::{isa, Context};
use wasmer_compiler::{wasmparser::Range, FunctionAddressMap, InstructionAddressMap, SourceLoc};

Expand Down
7 changes: 1 addition & 6 deletions lib/compiler-cranelift/src/func_environ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ pub fn get_function_name(func_index: FunctionIndex) -> ir::ExternalName {
ir::ExternalName::user(0, func_index.as_u32())
}

/// The type of the `current_length` field.
pub fn type_of_vmmemory_definition_current_length(vmoffsets: &VMOffsets) -> ir::Type {
ir::Type::int(u16::from(vmoffsets.size_of_vmmemory_definition_current_length()) * 8).unwrap()
}

/// The type of the `current_elements` field.
pub fn type_of_vmtable_definition_current_elements(vmoffsets: &VMOffsets) -> ir::Type {
ir::Type::int(u16::from(vmoffsets.size_of_vmtable_definition_current_elements()) * 8).unwrap()
Expand Down Expand Up @@ -1049,7 +1044,7 @@ impl<'module_environment> BaseFuncEnvironment for FuncEnvironment<'module_enviro
let heap_bound = func.create_global_value(ir::GlobalValueData::Load {
base: ptr,
offset: Offset32::new(current_length_offset),
global_type: type_of_vmmemory_definition_current_length(&self.offsets),
global_type: pointer_type,
readonly: false,
});
(
Expand Down
4 changes: 2 additions & 2 deletions lib/compiler-llvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ wasmer-vm = { path = "../vm", version = "2.0.0" }
wasmer-types = { path = "../types", version = "2.0.0" }
target-lexicon = { version = "0.12", default-features = false }
smallvec = "1.6"
object = { version = "0.25", default-features = false, features = ["read"] }
object = { version = "0.26", default-features = false, features = ["read"] }
libc = { version = "^0.2", default-features = false }
byteorder = "1"
itertools = "0.10"
Expand All @@ -35,7 +35,7 @@ cc = "1.0"
lazy_static = "1.4"
regex = "1.3"
semver = "1.0"
rustc_version = "0.2"
rustc_version = "0.4"

[features]
test = []
2 changes: 1 addition & 1 deletion lib/compiler-singlepass/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ wasmer-compiler = { path = "../compiler", version = "2.0.0", features = ["transl
wasmer-vm = { path = "../vm", version = "2.0.0" }
wasmer-types = { path = "../types", version = "2.0.0", default-features = false, features = ["std"] }
rayon = { version = "1.5", optional = true }
hashbrown = { version = "0.9", optional = true }
hashbrown = { version = "0.11", optional = true }
more-asserts = "0.2"
dynasm = "1.0"
dynasmrt = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion lib/compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ wasmer-types = { path = "../types", version = "2.0.0", default-features = false
wasmparser = { version = "0.78", optional = true, default-features = false }
target-lexicon = { version = "0.12", default-features = false }
enumset = "1.0"
hashbrown = { version = "0.9", optional = true }
hashbrown = { version = "0.11", optional = true }
serde = { version = "1.0", features = ["derive"], optional = true }
thiserror = "1.0"
serde_bytes = { version = "0.11", optional = true }
Expand Down
2 changes: 1 addition & 1 deletion lib/object/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ wasmer-compiler = { path = "../compiler", version = "2.0.0", default-features =
"std",
"translator"
] }
object = { version = "0.25", default-features = false, features = ["write"] }
object = { version = "0.26", default-features = false, features = ["write"] }
thiserror = "1.0"
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.51
1.53
2 changes: 2 additions & 0 deletions tests/ignores.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ cranelift multi_value_imports::dylib
singlepass multi_value_imports::dylib
singlepass multi_value_imports::dynamic

# Memory load doesn't trap as expected when out out bounds in Windows
windows+cranelift spec::memory_grow
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 a hard one :-/


# LLVM/Universal doesn't work in macOS M1. Skip all tests
llvm+universal+macos+aarch64 *
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/wast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ edition = "2018"
anyhow = "1.0"
wasmer = { path = "../../../lib/api", version = "2.0.0", default-features = false, features = ["experimental-reference-types-extern-ref"] }
wasmer-wasi = { path = "../../../lib/wasi", version = "2.0.0" }
wast = "35.0"
wast = "37.0"
serde = "1"
tempfile = "3"
thiserror = "1.0"
Expand Down
45 changes: 37 additions & 8 deletions tests/lib/wast/src/wast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl Wast {
bail!("expected '{}', got '{}'", expected, actual)
}

fn run_directive(&mut self, directive: wast::WastDirective) -> Result<()> {
fn run_directive(&mut self, test: &Path, directive: wast::WastDirective) -> Result<()> {
use wast::WastDirective::*;

match directive {
Expand Down Expand Up @@ -197,11 +197,14 @@ impl Wast {
}
AssertInvalid {
span: _,
mut module,
module,
message,
} => {
let bytes = module.encode()?;
let err = match self.module(None, &bytes) {
let wasm = match module {
wast::QuoteModule::Module(mut m) => m.encode()?,
wast::QuoteModule::Quote(list) => self.parse_quote_module(test, &list)?,
};
let err = match self.module(None, &wasm) {
Ok(()) => bail!("expected module to fail to build"),
Err(e) => e,
};
Expand All @@ -217,6 +220,9 @@ impl Wast {
QuoteModule { .. } => {
// Do nothing
}
AssertUncaughtException { .. } => {
// Do nothing for now
}
AssertMalformed {
module,
span: _,
Expand Down Expand Up @@ -258,9 +264,9 @@ impl Wast {
}

/// Run a wast script from a byte buffer.
pub fn run_buffer(&mut self, filename: &str, wast: &[u8]) -> Result<()> {
pub fn run_buffer(&mut self, test: &Path, wast: &[u8]) -> Result<()> {
let wast = str::from_utf8(wast)?;

let filename = test.to_str().unwrap();
let adjust_wast = |mut err: wast::Error| {
err.set_path(filename.as_ref());
err.set_text(wast);
Expand All @@ -272,7 +278,7 @@ impl Wast {
let mut errors = Vec::with_capacity(ast.directives.len());
for directive in ast.directives {
let sp = directive.span();
if let Err(e) = self.run_directive(directive) {
if let Err(e) = self.run_directive(&test, directive) {
let message = format!("{}", e);
// If depends on an instance that doesn't exist
if message.contains("no previous instance found") {
Expand Down Expand Up @@ -304,10 +310,33 @@ impl Wast {
Ok(())
}

fn parse_quote_module(&self, test: &Path, source: &[&[u8]]) -> Result<Vec<u8>> {
let mut ret = String::new();
for src in source {
match str::from_utf8(src) {
Ok(s) => ret.push_str(s),
Err(_) => bail!("malformed UTF-8 encoding"),
}
ret.push_str(" ");
}
let buf = wast::parser::ParseBuffer::new(&ret)?;
let mut wat = wast::parser::parse::<wast::Wat>(&buf)?;

// TODO: when memory64 merges into the proper spec then this should be
// removed since it will presumably no longer be a text-format error but
// rather a validation error. Currently all non-memory64 proposals
// assert that this offset is a text-parser error, whereas with memory64
// support that error is deferred until later.
if ret.contains("offset=4294967296") && !test.iter().any(|t| t == "memory64") {
bail!("i32 constant out of bounds");
}
Ok(wat.module.encode()?)
}

/// Run a wast script from a file.
pub fn run_file(&mut self, path: &Path) -> Result<()> {
let bytes = std::fs::read(path)?;
self.run_buffer(path.to_str().unwrap(), &bytes)
self.run_buffer(path, &bytes)
}
}

Expand Down