Skip to content

Commit

Permalink
Fixed some merge conflicts on the Cargo.toml file
Browse files Browse the repository at this point in the history
Added unit tests for multithreading and WASIX
  • Loading branch information
john-sharratt committed Aug 1, 2022
1 parent 01048b3 commit 771a87f
Show file tree
Hide file tree
Showing 13 changed files with 365 additions and 71 deletions.
87 changes: 63 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 28 additions & 1 deletion lib/api/src/sys/imports.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//! The import module contains the implementation data structures and helper functions used to
//! manipulate and access a wasm module's imports including memories, tables, globals, and
//! functions.
use crate::{Exports, Extern, Module};
use crate::{Exports, Extern, Module, AsStoreRef, Memory, AsStoreMut};
use std::collections::HashMap;
use std::fmt;
use wasmer_compiler::LinkError;
use wasmer_types::ImportError;
use tracing::trace;
use wasmer_vm::VMMemory;

/// All of the import data used when instantiating.
///
Expand Down Expand Up @@ -113,6 +114,32 @@ impl Imports {
.insert((ns.to_string(), name.to_string()), val.into());
}

/// Imports (any) shared memory into the imports.
/// (if the module does not import memory then this function is ignored)
pub fn import_shared_memory(&mut self, module: &Module, store: &mut impl AsStoreMut) -> Option<VMMemory> {
// Determine if shared memory needs to be created and imported
let shared_memory = module
.imports()
.memories()
.next()
.map(|a| *a.ty())
.map(|ty| {
let style = store
.as_store_ref()
.tunables()
.memory_style(&ty);
VMMemory::new(&ty, &style)
.unwrap()
});

if let Some(memory) = shared_memory {
self.define("env", "memory", Memory::new_from_existing(store, memory.clone()));
Some(memory)
} else {
None
}
}

/// Returns the contents of a namespace as an `Exports`.
///
/// Returns `None` if the namespace doesn't exist.
Expand Down
2 changes: 1 addition & 1 deletion lib/api/src/sys/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub enum IoCompileError {
/// Cloning a module is cheap: it does a shallow copy of the compiled
/// contents rather than a deep copy.
#[derive(Clone)]
pub struct Module {
pub struct Module {
// The field ordering here is actually significant because of the drop
// order: we want to drop the artifact before dropping the engine.
//
Expand Down
1 change: 1 addition & 0 deletions lib/api/src/sys/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ impl StoreInner
}

/// Represents a packaged store that can be passed around and then created locally
#[derive(Clone)]
pub struct PackagedStore
{
engine: Engine,
Expand Down
2 changes: 1 addition & 1 deletion lib/api/tests/js_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ mod js {
let exported = instance.exports.get_function("exported").unwrap();

let expected = vec![Val::I32(12)].into_boxed_slice();
env.as_mut(&mut store).multiplier = 3;
env.data_mut(&mut store).multiplier = 3;
assert_eq!(exported.call(&mut store, &[Val::I32(4)]), Ok(expected));
}

Expand Down
23 changes: 3 additions & 20 deletions lib/cli/src/commands/run/wasi.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::utils::{parse_envvar, parse_mapdir};
use anyhow::Result;
use wasmer_vm::VMMemory;
use std::collections::BTreeSet;
use std::path::PathBuf;
use wasmer::{AsStoreMut, FunctionEnv, Instance, Module, RuntimeError, Value, Memory};
use wasmer::{AsStoreMut, FunctionEnv, Instance, Module, RuntimeError, Value};
use wasmer_wasi::{
get_wasi_versions, import_object_for_all_wasi_versions, WasiEnv, WasiError,
WasiState, WasiVersion,
Expand Down Expand Up @@ -101,26 +100,10 @@ impl Wasi {
}
}

// Determine if shared memory needs to be created and imported
let shared_memory = module
.imports()
.memories()
.next()
.map(|a| *a.ty())
.map(|ty| {
let style = store
.as_store_ref()
.tunables()
.memory_style(&ty);
VMMemory::new(&ty, &style)
.unwrap()
});

let mut wasi_env = wasi_state_builder.finalize(store)?;
let mut import_object = import_object_for_all_wasi_versions(store, &wasi_env.env);
if let Some(memory) = shared_memory {
import_object.define("env", "memory", Memory::new_from_existing(&mut store, memory));
}
import_object.import_shared_memory(module, &mut store);

let instance = Instance::new(store, module, &import_object)?;
wasi_env.initialize(&mut store, &instance)?;
Ok((wasi_env.env, instance))
Expand Down
9 changes: 4 additions & 5 deletions lib/wasi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ bincode = { version = "1.3", optional = true }
chrono = { version = "^0.4", default-features = false, features = [ "wasmbind", "std", "clock" ], optional = true }
derivative = { version = "^2" }
bytes = "1"
<<<<<<< HEAD
thread-id = { version = "4", optional = true }
=======
>>>>>>> 1a05fe46c80e67a79e3c4cd574b73399f3e530d5
sha2 = { version = "0.10" }

[target.'cfg(unix)'.dependencies]
Expand All @@ -50,11 +46,14 @@ wasm-bindgen = "0.2.74"
wasm-bindgen-test = "0.3.0"
tracing-wasm = "0.2"

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
tracing-subscriber = { version = "^0.2" }

[features]
default = ["sys-default"]

sys = ["wasmer/sys"]
sys-default = ["wasmer/sys-default", "wasmer/wat", "wasmer/compiler", "sys", "logging", "host-fs", "sys-poll", "sys-thread", "host-vnet", "host-threads", "thread-id" ]
sys-default = ["wasmer/sys-default", "wasmer/wat", "wasmer/compiler", "sys", "logging", "host-fs", "sys-poll", "sys-thread", "host-vnet", "host-threads" ]
sys-poll = []
sys-thread = []

Expand Down
8 changes: 3 additions & 5 deletions lib/wasi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ pub use crate::syscalls::types;
pub use crate::utils::{
get_wasi_version, get_wasi_versions, is_wasi_module, is_wasix_module, WasiVersion,
};
use bytes::Bytes;
use derivative::Derivative;
pub use wasmer_vbus::{UnsupportedVirtualBus, VirtualBus};
#[deprecated(since = "2.1.0", note = "Please use `wasmer_vfs::FsError`")]
Expand Down Expand Up @@ -132,8 +131,6 @@ pub struct WasiEnvInner
memory: Memory,
/// Represents the reactors used to sleep and wake
reactors: Reactors,
/// Compiled bytes for the module so it can be recreated
module_bytes: Bytes,
/// Represents the callback for spawning a thread (name = "_start_thread")
thread_spawn: Option<TypedFunction<i64, ()>>,
/// Represents the callback for spawning a reactor (name = "_react")
Expand All @@ -149,7 +146,8 @@ pub struct WasiEnvInner
/// The environment provided to the WASI imports.
#[derive(Derivative, Clone)]
#[derivative(Debug)]
pub struct WasiEnv {
pub struct WasiEnv
where Self: Send {
/// ID of this thread (zero is the main thread)
id: WasiThreadId,
/// Shared state of the WASI system. Manages all the data that the
Expand Down Expand Up @@ -356,7 +354,7 @@ impl WasiFunctionEnv {
let memory = instance.exports.get_memory("memory")?.clone();
let new_inner = WasiEnvInner {
reactors: Default::default(),
module_bytes: Bytes::from(instance.module().serialize().unwrap()),
module: instance.module().clone(),
memory,
thread_spawn: instance.exports.get_typed_function(store, "_start_thread").ok(),
react: instance.exports.get_typed_function(store, "_react").ok(),
Expand Down
Loading

0 comments on commit 771a87f

Please sign in to comment.