Skip to content

Commit

Permalink
Merge #997
Browse files Browse the repository at this point in the history
997: Compiler tests r=syrusakbary a=syrusakbary

# Description
This PR simplifies the testing part, making `wasmer_runtime_core` the one responsible for picking the default backend.

This makes tests much simpler, and reliable when trying to run in different backends (this PR solves an issue when trying to run wasitests on ARM machines).

<!-- 
Provide details regarding the change including motivation,
links to related issues, and the context of the PR.
-->

# Review

- [x] Add a short description of the the change to the CHANGELOG.md file


Co-authored-by: Syrus <[email protected]>
Co-authored-by: Mark McCaskey <[email protected]>
Co-authored-by: Mark McCaskey <[email protected]>
  • Loading branch information
4 people authored Nov 22, 2019
2 parents 9fcfbb7 + 499d42a commit 10adb3d
Show file tree
Hide file tree
Showing 17 changed files with 106 additions and 290 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## **[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)
- [#996](https://github.com/wasmerio/wasmer/pull/997) Refactored spectests, emtests and wasitests to use default compiler logic
- [#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.
Expand Down
5 changes: 2 additions & 3 deletions Cargo.lock

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

4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,28 +77,26 @@ default = ["fast-tests", "wasi", "backend-cranelift"]
"loader-kernel" = ["wasmer-kernel-loader"]
debug = ["wasmer-runtime-core/debug"]
trace = ["wasmer-runtime-core/trace"]
docs = ["wasmer-runtime/docs"]
extra-debug = ["wasmer-clif-backend/debug", "wasmer-runtime-core/debug"]
# This feature will allow cargo test to run much faster
fast-tests = []
backend-cranelift = [
"wasmer-runtime-core/backend-cranelift",
"wasmer-runtime/cranelift",
"wasmer-middleware-common-tests/clif",
"wasmer-wasi-tests/clif"
]
backend-llvm = [
"wasmer-llvm-backend",
"wasmer-runtime-core/backend-llvm",
"wasmer-runtime/llvm",
"wasmer-middleware-common-tests/llvm",
"wasmer-wasi-tests/llvm"
]
backend-singlepass = [
"wasmer-singlepass-backend",
"wasmer-runtime-core/backend-singlepass",
"wasmer-runtime/singlepass",
"wasmer-middleware-common-tests/singlepass",
"wasmer-wasi-tests/singlepass"
]
wasi = ["wasmer-wasi"]
managed = ["backend-singlepass", "wasmer-runtime-core/managed"]
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,13 @@ check: check-bench

# Release
release:
cargo build --release --features backend-singlepass,backend-llvm,loader-kernel
cargo build --release --features backend-singlepass,backend-cranelift,backend-llvm,loader-kernel

# Only one backend (cranelift)
release-clif:
# If you are on macOS, you will need mingw-w64 for cross compiling to Windows
# brew install mingw-w64
cargo build --release
cargo build --release --features backend-cranelift

release-singlepass:
cargo build --release --features backend-singlepass
Expand Down Expand Up @@ -265,4 +265,4 @@ dep-graph:
cargo deps --optional-deps --filter wasmer-wasi wasmer-wasi-tests wasmer-kernel-loader wasmer-dev-utils wasmer-llvm-backend wasmer-emscripten wasmer-emscripten-tests wasmer-runtime-core wasmer-runtime wasmer-middleware-common wasmer-middleware-common-tests wasmer-singlepass-backend wasmer-clif-backend wasmer --manifest-path Cargo.toml | dot -Tpng > wasmer_depgraph.png

docs:
cargo doc --features=backend-singlepass,backend-llvm,wasi,managed
cargo doc --features=backend-cranelift,backend-singlepass,backend-llvm,docs,wasi,managed
10 changes: 5 additions & 5 deletions lib/emscripten-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ build = "build/mod.rs"

[dependencies]
wasmer-emscripten = { path = "../emscripten", version = "0.10.2" }
wasmer-runtime-core = { path = "../runtime-core", version = "0.10.2" }
wasmer-clif-backend = { path = "../clif-backend", version = "0.10.2" }
wasmer-runtime = { path = "../runtime", version = "0.10.2", default-features = false }
wasmer-clif-backend = { path = "../clif-backend", version = "0.10.2", optional = true}
wasmer-llvm-backend = { path = "../llvm-backend", version = "0.10.2", optional = true }
wasmer-singlepass-backend = { path = "../singlepass-backend", version = "0.10.2", optional = true }

Expand All @@ -23,6 +23,6 @@ wasmer-dev-utils = { path = "../dev-utils", version = "0.10.2"}
glob = "0.3"

[features]
clif = []
llvm = ["wasmer-llvm-backend"]
singlepass = ["wasmer-singlepass-backend"]
clif = ["wasmer-clif-backend", "wasmer-runtime/default-backend-cranelift"]
singlepass = ["wasmer-singlepass-backend", "wasmer-runtime/default-backend-singlepass"]
llvm = ["wasmer-llvm-backend", "wasmer-runtime/default-backend-llvm"]
34 changes: 3 additions & 31 deletions lib/emscripten-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,13 @@ mod tests {
use std::sync::Arc;
use wabt::wat2wasm;
use wasmer_emscripten::is_emscripten_module;
use wasmer_runtime_core::backend::Compiler;
use wasmer_runtime_core::compile_with;

#[cfg(feature = "clif")]
fn get_compiler() -> impl Compiler {
use wasmer_clif_backend::CraneliftCompiler;
CraneliftCompiler::new()
}

#[cfg(feature = "llvm")]
fn get_compiler() -> impl Compiler {
use wasmer_llvm_backend::LLVMCompiler;
LLVMCompiler::new()
}

#[cfg(feature = "singlepass")]
fn get_compiler() -> impl Compiler {
use wasmer_singlepass_backend::SinglePassCompiler;
SinglePassCompiler::new()
}

#[cfg(not(any(feature = "llvm", feature = "clif", feature = "singlepass")))]
fn get_compiler() -> impl Compiler {
panic!("compiler not specified, activate a compiler via features");
use wasmer_clif_backend::CraneliftCompiler;
CraneliftCompiler::new()
}
use wasmer_runtime::compile;

#[test]
fn should_detect_emscripten_files() {
const WAST_BYTES: &[u8] = include_bytes!("tests/is_emscripten_true.wast");
let wasm_binary = wat2wasm(WAST_BYTES.to_vec()).expect("Can't convert to wasm");
let module =
compile_with(&wasm_binary[..], &get_compiler()).expect("WASM can't be compiled");
let module = compile(&wasm_binary[..]).expect("WASM can't be compiled");
let module = Arc::new(module);
assert!(is_emscripten_module(&module));
}
Expand All @@ -45,8 +18,7 @@ mod tests {
fn should_detect_non_emscripten_files() {
const WAST_BYTES: &[u8] = include_bytes!("tests/is_emscripten_false.wast");
let wasm_binary = wat2wasm(WAST_BYTES.to_vec()).expect("Can't convert to wasm");
let module =
compile_with(&wasm_binary[..], &get_compiler()).expect("WASM can't be compiled");
let module = compile(&wasm_binary[..]).expect("WASM can't be compiled");
let module = Arc::new(module);
assert!(!is_emscripten_module(&module));
}
Expand Down
31 changes: 2 additions & 29 deletions lib/emscripten-tests/tests/emtests/_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,12 @@ macro_rules! assert_emscripten_output {
EmscriptenGlobals,
generate_emscripten_env,
};
use wasmer_runtime_core::{
backend::Compiler,
};
use wasmer_runtime::compile;
use wasmer_dev_utils::stdio::StdioCapturer;

#[cfg(feature = "clif")]
fn get_compiler() -> impl Compiler {
use wasmer_clif_backend::CraneliftCompiler;
CraneliftCompiler::new()
}

#[cfg(feature = "llvm")]
fn get_compiler() -> impl Compiler {
use wasmer_llvm_backend::LLVMCompiler;
LLVMCompiler::new()
}

#[cfg(feature = "singlepass")]
fn get_compiler() -> impl Compiler {
use wasmer_singlepass_backend::SinglePassCompiler;
SinglePassCompiler::new()
}

#[cfg(not(any(feature = "llvm", feature = "clif", feature = "singlepass")))]
fn get_compiler() -> impl Compiler {
panic!("compiler not specified, activate a compiler via features");
use wasmer_clif_backend::CraneliftCompiler;
CraneliftCompiler::new()
}

let wasm_bytes = include_bytes!($file);

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

// let module = compile(&wasm_bytes[..])
Expand Down
1 change: 1 addition & 0 deletions lib/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ optional = true

[features]
default = ["cranelift", "default-backend-cranelift"]
docs = []
cranelift = ["wasmer-clif-backend"]
cache = ["cranelift"]
debug = ["wasmer-clif-backend/debug", "wasmer-runtime-core/debug"]
Expand Down
19 changes: 13 additions & 6 deletions lib/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@
//! [`wasmer-clif-backend`]: https://crates.io/crates/wasmer-clif-backend
//! [`compile_with`]: fn.compile_with.html
pub use wasmer_runtime_core::backend::Backend;
pub use wasmer_runtime_core::backend::{Backend, Features};
pub use wasmer_runtime_core::codegen::{MiddlewareChain, StreamingCompiler};
pub use wasmer_runtime_core::export::Export;
pub use wasmer_runtime_core::global::Global;
pub use wasmer_runtime_core::import::ImportObject;
pub use wasmer_runtime_core::import::{ImportObject, LikeNamespace};
pub use wasmer_runtime_core::instance::{DynFunc, Instance};
pub use wasmer_runtime_core::memory::ptr::{Array, Item, WasmPtr};
pub use wasmer_runtime_core::memory::Memory;
Expand Down Expand Up @@ -131,9 +131,14 @@ pub mod units {
pub use wasmer_runtime_core::units::{Bytes, Pages};
}

pub mod types {
//! Various types.
pub use wasmer_runtime_core::types::*;
}

pub mod cache;

use wasmer_runtime_core::backend::{Compiler, CompilerConfig};
pub use wasmer_runtime_core::backend::{Compiler, CompilerConfig};

/// Compile WebAssembly binary code into a [`Module`].
/// This function is useful if it is necessary to
Expand Down Expand Up @@ -203,12 +208,14 @@ pub fn default_compiler() -> impl Compiler {
#[cfg(any(
all(
feature = "default-backend-llvm",
not(feature = "docs"),
any(
feature = "default-backend-cranelift",
feature = "default-backend-singlepass"
)
),
all(
not(feature = "docs"),
feature = "default-backend-cranelift",
feature = "default-backend-singlepass"
)
Expand All @@ -217,13 +224,13 @@ pub fn default_compiler() -> impl Compiler {
"The `default-backend-X` features are mutually exclusive. Please choose just one"
);

#[cfg(feature = "default-backend-llvm")]
#[cfg(all(feature = "default-backend-llvm", not(feature = "docs")))]
use wasmer_llvm_backend::LLVMCompiler as DefaultCompiler;

#[cfg(feature = "default-backend-singlepass")]
#[cfg(all(feature = "default-backend-singlepass", not(feature = "docs")))]
use wasmer_singlepass_backend::SinglePassCompiler as DefaultCompiler;

#[cfg(feature = "default-backend-cranelift")]
#[cfg(any(feature = "default-backend-cranelift", feature = "docs"))]
use wasmer_clif_backend::CraneliftCompiler as DefaultCompiler;

DefaultCompiler::new()
Expand Down
10 changes: 5 additions & 5 deletions lib/spectests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ edition = "2018"

[dependencies]
glob = "0.3"
wasmer-runtime-core = { path = "../runtime-core", version = "0.10.2" }
wasmer-clif-backend = { path = "../clif-backend", version = "0.10.2" }
wasmer-runtime = { path = "../runtime", version = "0.10.2", default-features = false}
wasmer-clif-backend = { path = "../clif-backend", version = "0.10.2", optional = true}
wasmer-llvm-backend = { path = "../llvm-backend", version = "0.10.2", optional = true }
wasmer-singlepass-backend = { path = "../singlepass-backend", version = "0.10.2", optional = true }

Expand All @@ -23,6 +23,6 @@ wabt = "0.9.1"
[features]
default = ["fast-tests"]
fast-tests = []
clif = []
llvm = ["wasmer-llvm-backend"]
singlepass = ["wasmer-singlepass-backend"]
clif = ["wasmer-clif-backend", "wasmer-runtime/default-backend-cranelift"]
singlepass = ["wasmer-singlepass-backend", "wasmer-runtime/default-backend-singlepass"]
llvm = ["wasmer-llvm-backend", "wasmer-runtime/default-backend-llvm"]
41 changes: 6 additions & 35 deletions lib/spectests/examples/simple/main.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,17 @@
use wabt::wat2wasm;
use wasmer_runtime_core::{
backend::Compiler,
error,
global::Global,
memory::Memory,
prelude::*,
table::Table,
use wasmer_runtime::{
compile, error, func, imports,
types::{ElementType, MemoryDescriptor, TableDescriptor, Value},
units::Pages,
Ctx, Global, Memory, Table,
};

#[cfg(feature = "clif")]
fn get_compiler() -> impl Compiler {
use wasmer_clif_backend::CraneliftCompiler;
CraneliftCompiler::new()
}

#[cfg(feature = "llvm")]
fn get_compiler() -> impl Compiler {
use wasmer_llvm_backend::LLVMCompiler;
LLVMCompiler::new()
}

#[cfg(feature = "singlepass")]
fn get_compiler() -> impl Compiler {
use wasmer_singlepass_backend::SinglePassCompiler;
SinglePassCompiler::new()
}

#[cfg(not(any(feature = "llvm", feature = "clif", feature = "singlepass")))]
fn get_compiler() -> impl Compiler {
panic!("compiler not specified, activate a compiler via features");
use wasmer_clif_backend::CraneliftCompiler;
CraneliftCompiler::new()
}

static EXAMPLE_WASM: &'static [u8] = include_bytes!("simple.wasm");

fn main() -> error::Result<()> {
let wasm_binary = wat2wasm(IMPORT_MODULE.as_bytes()).expect("WAST not valid or malformed");

let inner_module = wasmer_runtime_core::compile_with(&wasm_binary, &get_compiler())?;
let inner_module = compile(&wasm_binary)?;

let memory_desc = MemoryDescriptor::new(Pages(1), Some(Pages(1)), false).unwrap();
let memory = Memory::new(memory_desc).unwrap();
Expand Down Expand Up @@ -71,15 +42,15 @@ fn main() -> error::Result<()> {
"env" => inner_instance,
};

let outer_module = wasmer_runtime_core::compile_with(EXAMPLE_WASM, &get_compiler())?;
let outer_module = compile(EXAMPLE_WASM)?;
let outer_instance = outer_module.instantiate(&outer_imports)?;
let ret = outer_instance.call("main", &[Value::I32(42)])?;
println!("ret: {:?}", ret);

Ok(())
}

fn print_num(ctx: &mut vm::Ctx, n: i32) -> Result<i32, ()> {
fn print_num(ctx: &mut Ctx, n: i32) -> Result<i32, ()> {
println!("print_num({})", n);

let memory: &Memory = ctx.memory(0);
Expand Down
Loading

0 comments on commit 10adb3d

Please sign in to comment.