Add wasmtime as default runtime for integration tests
This commit is contained in:
11
Cargo.toml
11
Cargo.toml
@@ -23,6 +23,17 @@ nix = "0.13"
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
winapi = { version = "0.3", features=["std", "handleapi", "processthreadsapi", "winbase", "ws2def", "fileapi"] }
|
||||
|
||||
[dev-dependencies]
|
||||
wasmtime-runtime = { git = "https://github.com/cranestation/wasmtime" }
|
||||
wasmtime-environ = { git = "https://github.com/cranestation/wasmtime" }
|
||||
wasmtime-jit = { git = "https://github.com/cranestation/wasmtime" }
|
||||
cranelift-codegen = "0.30.0"
|
||||
cranelift-entity = "0.30.0"
|
||||
cranelift-wasm = "0.30.0"
|
||||
cranelift-native = "0.30.0"
|
||||
target-lexicon = "0.3.0"
|
||||
pretty_env_logger = "0.3"
|
||||
|
||||
[lib]
|
||||
name = "wasi_common"
|
||||
crate-type = ["rlib", "staticlib", "cdylib"]
|
||||
|
||||
54
tests/misc_tests.rs
Normal file
54
tests/misc_tests.rs
Normal file
@@ -0,0 +1,54 @@
|
||||
mod runtime;
|
||||
|
||||
use std::sync::{Once, ONCE_INIT};
|
||||
|
||||
static INIT: Once = ONCE_INIT;
|
||||
|
||||
fn setup() {
|
||||
INIT.call_once(|| {
|
||||
std::env::set_var("RUST_LOG", "wasi_common=trace");
|
||||
pretty_env_logger::init_custom_env("RUST_LOG");
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sched_yield() {
|
||||
setup();
|
||||
runtime::run_wasm("tests/misc-testsuite/sched_yield.wasm")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncation_rights() {
|
||||
setup();
|
||||
runtime::run_wasm("tests/misc-testsuite/truncation_rights.wasm")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unlink_dir() {
|
||||
setup();
|
||||
runtime::run_wasm("tests/misc-testsuite/unlink_dir.wasm")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn remove_nonempty_dir() {
|
||||
setup();
|
||||
runtime::run_wasm("tests/misc-testsuite/remove_nonempty_dir.wasm")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn interesting_paths() {
|
||||
setup();
|
||||
runtime::run_wasm("tests/misc-testsuite/interesting_paths.wasm")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nofollow_errors() {
|
||||
setup();
|
||||
runtime::run_wasm("tests/misc-testsuite/nofollow_errors.wasm")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn symlink_loop() {
|
||||
setup();
|
||||
runtime::run_wasm("tests/misc-testsuite/symlink_loop.wasm")
|
||||
}
|
||||
111
tests/runtime/mod.rs
Normal file
111
tests/runtime/mod.rs
Normal file
@@ -0,0 +1,111 @@
|
||||
mod wasi;
|
||||
|
||||
use cranelift_codegen::settings;
|
||||
use cranelift_native;
|
||||
use std::env;
|
||||
use std::ffi::OsStr;
|
||||
use std::fs::{self, File};
|
||||
use std::io;
|
||||
use std::io::prelude::*;
|
||||
use std::path::{Component, Path, PathBuf};
|
||||
use std::process::exit;
|
||||
use std::time::SystemTime;
|
||||
use wasmtime_jit::Context;
|
||||
|
||||
fn read_to_end(path: PathBuf) -> Result<Vec<u8>, io::Error> {
|
||||
let mut buf: Vec<u8> = Vec::new();
|
||||
let mut file = File::open(path)?;
|
||||
file.read_to_end(&mut buf)?;
|
||||
Ok(buf)
|
||||
}
|
||||
|
||||
fn read_wasm(path: PathBuf) -> Result<Vec<u8>, String> {
|
||||
let data = read_to_end(path).map_err(|err| err.to_string())?;
|
||||
if data.starts_with(&[b'\0', b'a', b's', b'm']) {
|
||||
Ok(data)
|
||||
} else {
|
||||
Err("Invalid Wasm file encountered".to_owned())
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_module(context: &mut Context, path: &Path) -> Result<(), String> {
|
||||
// Read the wasm module binary.
|
||||
let data = read_wasm(path.to_path_buf())?;
|
||||
|
||||
// Compile and instantiating a wasm module.
|
||||
context
|
||||
.instantiate_module(None, &data)
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn prepare_workspace(exe_name: &str) -> Result<String, String> {
|
||||
let mut workspace = env::temp_dir();
|
||||
let time_now = SystemTime::now()
|
||||
.duration_since(SystemTime::UNIX_EPOCH)
|
||||
.map_err(|err| err.to_string())?;
|
||||
let subdir = format!("wasi_common_tests_{}_{}", exe_name, time_now.as_secs());
|
||||
workspace.push(subdir);
|
||||
fs::create_dir(workspace.as_path()).map_err(|err| err.to_string())?;
|
||||
|
||||
Ok(workspace
|
||||
.as_os_str()
|
||||
.to_str()
|
||||
.expect("could convert to string")
|
||||
.to_string())
|
||||
}
|
||||
|
||||
fn preopen_workspace(workspace: String) -> (String, File) {
|
||||
let preopen_dir = wasi_common::preopen_dir(&workspace).unwrap_or_else(|err| {
|
||||
println!("error while preopening directory {}: {}", workspace, err);
|
||||
exit(1);
|
||||
});
|
||||
(".".to_owned(), preopen_dir)
|
||||
}
|
||||
|
||||
pub fn run_wasm<P: AsRef<Path>>(path: P) {
|
||||
let isa_builder = cranelift_native::builder().unwrap_or_else(|_| {
|
||||
panic!("host machine is not a supported target");
|
||||
});
|
||||
let flag_builder = settings::builder();
|
||||
let isa = isa_builder.finish(settings::Flags::new(flag_builder));
|
||||
let mut context = Context::with_isa(isa);
|
||||
|
||||
let global_exports = context.get_global_exports();
|
||||
|
||||
// extract exe name from path
|
||||
let exe_name = Path::new(path.as_ref())
|
||||
.components()
|
||||
.next_back()
|
||||
.map(Component::as_os_str)
|
||||
.and_then(OsStr::to_str)
|
||||
.unwrap_or("unknown")
|
||||
.to_owned();
|
||||
|
||||
let workspace = match prepare_workspace(&exe_name) {
|
||||
Ok(workspace) => workspace,
|
||||
Err(message) => {
|
||||
println!("error while processing preopen dirs: {}", message);
|
||||
exit(1);
|
||||
}
|
||||
};
|
||||
let preopen_dirs = &[preopen_workspace(workspace)];
|
||||
let argv = vec![exe_name, ".".to_owned()];
|
||||
|
||||
context.name_instance(
|
||||
"wasi_unstable".to_owned(),
|
||||
wasi::instantiate_wasi("", global_exports, preopen_dirs, &argv, &[])
|
||||
.expect("instantiating wasi"),
|
||||
);
|
||||
|
||||
// Load the main wasm module.
|
||||
match handle_module(&mut context, path.as_ref()) {
|
||||
Ok(()) => {}
|
||||
Err(message) => {
|
||||
let name = path.as_ref().as_os_str().to_string_lossy();
|
||||
println!("error while processing main module {}: {}", name, message);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
1157
tests/runtime/wasi.rs
Normal file
1157
tests/runtime/wasi.rs
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user