Skip to content

Commit

Permalink
Merge branch 'master' into compiler-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkMcCaskey authored Nov 22, 2019
2 parents 31437a1 + b9138aa commit 44f2e92
Show file tree
Hide file tree
Showing 13 changed files with 582 additions and 138 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

## **[Unreleased]**

- [#995](https://github.com/wasmerio/wasmer/pull/995) Detect when a global is read without being initialized (emit a proper error instead of panicking)
- [#992](https://github.com/wasmerio/wasmer/pull/992) Updates WAPM version to 0.4.1, fix arguments issue introduced in #990
- [#990](https://github.com/wasmerio/wasmer/pull/990) Default wasmer CLI to `run`. Wasmer will now attempt to parse unrecognized command line options as if they were applied to the run command: `wasmer mywasm.wasm --dir=.` now works!
- [#987](https://github.com/wasmerio/wasmer/pull/987) Fix `runtime-c-api` header files when compiled by gnuc.
- [#957](https://github.com/wasmerio/wasmer/pull/957) Change the meaning of `wasmer_wasi::is_wasi_module` to detect any type of WASI module, add support for new wasi snapshot_preview1

## 0.10.2 - 2019-11-18

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ members = [
"lib/wasi-tests",
"lib/emscripten-tests",
"lib/middleware-common-tests",
"examples/plugin-for-example",
"examples/parallel",
"examples/plugin-for-example",
"examples/parallel-guest",
]

Expand Down
15 changes: 12 additions & 3 deletions lib/runtime-core/src/backing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl LocalBacking {
}
};
let mut tables = Self::generate_tables(module);
let mut globals = Self::generate_globals(module, imports);
let mut globals = Self::generate_globals(module, imports)?;

// Ensure all initializers are valid before running finalizers
Self::validate_memories(module, imports)?;
Expand Down Expand Up @@ -443,13 +443,22 @@ impl LocalBacking {
fn generate_globals(
module: &ModuleInner,
imports: &ImportBacking,
) -> BoxedMap<LocalGlobalIndex, Global> {
) -> LinkResult<BoxedMap<LocalGlobalIndex, Global>> {
let mut globals = Map::with_capacity(module.info.globals.len());

for (_, global_init) in module.info.globals.iter() {
let value = match &global_init.init {
Initializer::Const(value) => value.clone(),
Initializer::GetGlobal(import_global_index) => {
if imports.globals.len() <= import_global_index.index() {
return Err(vec![LinkError::Generic {
message: format!(
"Trying to read the `{:?}` global that is not properly initialized.",
import_global_index.index()
),
}]);
}

imports.globals[*import_global_index].get()
}
};
Expand All @@ -463,7 +472,7 @@ impl LocalBacking {
globals.push(global);
}

globals.into_boxed_map()
Ok(globals.into_boxed_map())
}

fn finalize_globals(
Expand Down
11 changes: 7 additions & 4 deletions lib/wasi-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ fn serializing_works() {
b"GOROOT=$HOME/.cargo/bin".into_iter().cloned().collect(),
];
let wasm_binary = include_bytes!("../wasitests/fd_read.wasm");
let import_object = generate_import_object(
let module = compile(&wasm_binary[..])
.map_err(|e| format!("Can't compile module: {:?}", e))
.unwrap();

let wasi_version = get_wasi_version(&module).expect("WASI module");
let import_object = generate_import_object_for_version(
wasi_version,
args.clone(),
envs.clone(),
vec![],
Expand All @@ -24,9 +30,6 @@ fn serializing_works() {
std::path::PathBuf::from("wasitests/test_fs/hamlet"),
)],
);
let module = compile(&wasm_binary[..])
.map_err(|e| format!("Can't compile module: {:?}", e))
.unwrap();

let state_bytes = {
let instance = module.instantiate(&import_object).unwrap();
Expand Down
15 changes: 12 additions & 3 deletions lib/wasi-tests/tests/wasitests/_common.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
macro_rules! assert_wasi_output {
($file:expr, $name:expr, $po_dir_args: expr, $mapdir_args:expr, $envvar_args:expr, $expected:expr) => {{
use wasmer_dev_utils::stdio::StdioCapturer;
use wasmer_runtime::Func;
use wasmer_wasi::generate_import_object;
use wasmer_runtime_core::Func;
use wasmer_wasi::{generate_import_object_for_version, get_wasi_version};

let wasm_bytes = include_bytes!($file);

let module = wasmer_runtime::compile(&wasm_bytes[..]).expect("WASM can't be compiled");

let import_object = generate_import_object(vec![], vec![], $po_dir_args, $mapdir_args);
let wasi_version = get_wasi_version(&module).expect("WASI module");

let import_object = generate_import_object_for_version(
wasi_version,
vec![],
vec![],
$po_dir_args,
$mapdir_args,
);

let instance = module
.instantiate(&import_object)
Expand Down
2 changes: 1 addition & 1 deletion lib/wasi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ serde = { version = "1", features = ["derive"] }
wasmer-runtime-core = { path = "../runtime-core", version = "0.10.2" }

[target.'cfg(windows)'.dependencies]
winapi = "0.3"
winapi = "0.3"
107 changes: 103 additions & 4 deletions lib/wasi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

//! Wasmer's WASI implementation
//!
//! Use `generate_import_object` to create an `ImportObject`. This `ImportObject`
//! Use `generate_import_object` to create an [`ImportObject`]. This [`ImportObject`]
//! can be combined with a module to create an `Instance` which can execute WASI
//! Wasm functions.
//!
Expand All @@ -37,7 +37,7 @@ use self::syscalls::*;
use std::ffi::c_void;
use std::path::PathBuf;

pub use self::utils::is_wasi_module;
pub use self::utils::{get_wasi_version, is_wasi_module, WasiVersion};

use wasmer_runtime_core::{func, import::ImportObject, imports};

Expand All @@ -47,7 +47,8 @@ pub struct ExitCode {
pub code: syscalls::types::__wasi_exitcode_t,
}

/// Creates a Wasi [`ImportObject`] with [`WasiState`].
/// Creates a Wasi [`ImportObject`] with [`WasiState`] with the latest snapshot
/// of WASI.
pub fn generate_import_object(
args: Vec<Vec<u8>>,
envs: Vec<Vec<u8>>,
Expand Down Expand Up @@ -79,7 +80,7 @@ pub fn generate_import_object(
imports! {
// This generates the wasi state.
state_gen,
"wasi_unstable" => {
"wasi_snapshot_preview1" => {
"args_get" => func!(args_get),
"args_sizes_get" => func!(args_sizes_get),
"clock_res_get" => func!(clock_res_get),
Expand Down Expand Up @@ -128,3 +129,101 @@ pub fn generate_import_object(
},
}
}

/// Creates a Wasi [`ImportObject`] with [`WasiState`] for the given [`WasiVersion`].
pub fn generate_import_object_for_version(
version: WasiVersion,
args: Vec<Vec<u8>>,
envs: Vec<Vec<u8>>,
preopened_files: Vec<PathBuf>,
mapped_dirs: Vec<(String, PathBuf)>,
) -> ImportObject {
match version {
WasiVersion::Snapshot0 => {
generate_import_object_snapshot0(args, envs, preopened_files, mapped_dirs)
}
WasiVersion::Snapshot1 => generate_import_object(args, envs, preopened_files, mapped_dirs),
}
}

/// Creates a legacy Wasi [`ImportObject`] with [`WasiState`].
fn generate_import_object_snapshot0(
args: Vec<Vec<u8>>,
envs: Vec<Vec<u8>>,
preopened_files: Vec<PathBuf>,
mapped_dirs: Vec<(String, PathBuf)>,
) -> ImportObject {
let state_gen = move || {
// TODO: look into removing all these unnecessary clones
fn state_destructor(data: *mut c_void) {
unsafe {
drop(Box::from_raw(data as *mut WasiState));
}
}
let preopened_files = preopened_files.clone();
let mapped_dirs = mapped_dirs.clone();
//let wasi_builder = create_wasi_instance();

let state = Box::new(WasiState {
fs: WasiFs::new(&preopened_files, &mapped_dirs).expect("Could not create WASI FS"),
args: args.clone(),
envs: envs.clone(),
});

(
Box::into_raw(state) as *mut c_void,
state_destructor as fn(*mut c_void),
)
};
imports! {
// This generates the wasi state.
state_gen,
"wasi_unstable" => {
"args_get" => func!(args_get),
"args_sizes_get" => func!(args_sizes_get),
"clock_res_get" => func!(clock_res_get),
"clock_time_get" => func!(clock_time_get),
"environ_get" => func!(environ_get),
"environ_sizes_get" => func!(environ_sizes_get),
"fd_advise" => func!(fd_advise),
"fd_allocate" => func!(fd_allocate),
"fd_close" => func!(fd_close),
"fd_datasync" => func!(fd_datasync),
"fd_fdstat_get" => func!(fd_fdstat_get),
"fd_fdstat_set_flags" => func!(fd_fdstat_set_flags),
"fd_fdstat_set_rights" => func!(fd_fdstat_set_rights),
"fd_filestat_get" => func!(legacy::snapshot0::fd_filestat_get),
"fd_filestat_set_size" => func!(fd_filestat_set_size),
"fd_filestat_set_times" => func!(fd_filestat_set_times),
"fd_pread" => func!(fd_pread),
"fd_prestat_get" => func!(fd_prestat_get),
"fd_prestat_dir_name" => func!(fd_prestat_dir_name),
"fd_pwrite" => func!(fd_pwrite),
"fd_read" => func!(fd_read),
"fd_readdir" => func!(fd_readdir),
"fd_renumber" => func!(fd_renumber),
"fd_seek" => func!(legacy::snapshot0::fd_seek),
"fd_sync" => func!(fd_sync),
"fd_tell" => func!(fd_tell),
"fd_write" => func!(fd_write),
"path_create_directory" => func!(path_create_directory),
"path_filestat_get" => func!(legacy::snapshot0::path_filestat_get),
"path_filestat_set_times" => func!(path_filestat_set_times),
"path_link" => func!(path_link),
"path_open" => func!(path_open),
"path_readlink" => func!(path_readlink),
"path_remove_directory" => func!(path_remove_directory),
"path_rename" => func!(path_rename),
"path_symlink" => func!(path_symlink),
"path_unlink_file" => func!(path_unlink_file),
"poll_oneoff" => func!(legacy::snapshot0::poll_oneoff),
"proc_exit" => func!(proc_exit),
"proc_raise" => func!(proc_raise),
"random_get" => func!(random_get),
"sched_yield" => func!(sched_yield),
"sock_recv" => func!(sock_recv),
"sock_send" => func!(sock_send),
"sock_shutdown" => func!(sock_shutdown),
},
}
}
5 changes: 5 additions & 0 deletions lib/wasi/src/syscalls/legacy/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//! These modules provide wrappers and implementations for older version of WASI.
//!
//! If you are relying on legacy WASI, please upgrade for the best experience.
pub mod snapshot0;
Loading

0 comments on commit 44f2e92

Please sign in to comment.