Move workspace creation outside of runtime instantiation
This commit is contained in:
@@ -1,78 +1,102 @@
|
||||
mod runtime;
|
||||
mod utils;
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
fn run_test_with_workspace<P: AsRef<Path>>(path: P) -> Result<(), String> {
|
||||
// Load in the wasm testcase
|
||||
let data = utils::read_wasm(path.as_ref())?;
|
||||
let bin_name = utils::extract_exec_name_from_path(path.as_ref())?;
|
||||
|
||||
// Prepare workspace
|
||||
let workspace = utils::prepare_workspace(&bin_name)?;
|
||||
|
||||
// Run!
|
||||
runtime::instantiate(&data, bin_name, Some(workspace))
|
||||
}
|
||||
|
||||
fn run_test_without_workspace<P: AsRef<Path>>(path: P) -> Result<(), String> {
|
||||
// Load in the wasm testcase
|
||||
let data = utils::read_wasm(path.as_ref())?;
|
||||
let bin_name = utils::extract_exec_name_from_path(path.as_ref())?;
|
||||
|
||||
// Run!
|
||||
runtime::instantiate(&data, bin_name, None)
|
||||
}
|
||||
|
||||
#[cfg(all(unix))]
|
||||
#[test]
|
||||
fn sched_yield() -> Result<(), String> {
|
||||
runtime::run_test("tests/misc-tests/bin/sched_yield.wasm")
|
||||
run_test_without_workspace("tests/misc-tests/bin/sched_yield.wasm")
|
||||
}
|
||||
|
||||
#[cfg(all(unix))]
|
||||
#[test]
|
||||
fn truncation_rights() -> Result<(), String> {
|
||||
runtime::run_test("tests/misc-tests/bin/truncation_rights.wasm")
|
||||
run_test_with_workspace("tests/misc-tests/bin/truncation_rights.wasm")
|
||||
}
|
||||
|
||||
#[cfg(all(unix))]
|
||||
#[test]
|
||||
fn unlink_directory() -> Result<(), String> {
|
||||
runtime::run_test("tests/misc-tests/bin/unlink_directory.wasm")
|
||||
run_test_with_workspace("tests/misc-tests/bin/unlink_directory.wasm")
|
||||
}
|
||||
|
||||
#[cfg(all(unix))]
|
||||
#[test]
|
||||
fn remove_nonempty_directory() -> Result<(), String> {
|
||||
runtime::run_test("tests/misc-tests/bin/remove_nonempty_directory.wasm")
|
||||
run_test_with_workspace("tests/misc-tests/bin/remove_nonempty_directory.wasm")
|
||||
}
|
||||
|
||||
#[cfg(all(unix))]
|
||||
#[test]
|
||||
fn interesting_paths() -> Result<(), String> {
|
||||
runtime::run_test("tests/misc-tests/bin/interesting_paths.wasm")
|
||||
run_test_with_workspace("tests/misc-tests/bin/interesting_paths.wasm")
|
||||
}
|
||||
|
||||
#[cfg(all(unix))]
|
||||
#[test]
|
||||
fn nofollow_errors() -> Result<(), String> {
|
||||
runtime::run_test("tests/misc-tests/bin/nofollow_errors.wasm")
|
||||
run_test_with_workspace("tests/misc-tests/bin/nofollow_errors.wasm")
|
||||
}
|
||||
|
||||
#[cfg(all(unix))]
|
||||
#[test]
|
||||
fn symlink_loop() -> Result<(), String> {
|
||||
runtime::run_test("tests/misc-tests/bin/symlink_loop.wasm")
|
||||
run_test_with_workspace("tests/misc-tests/bin/symlink_loop.wasm")
|
||||
}
|
||||
|
||||
#[cfg(all(unix))]
|
||||
#[test]
|
||||
fn close_preopen() -> Result<(), String> {
|
||||
runtime::run_test("tests/misc-tests/bin/close_preopen.wasm")
|
||||
run_test_with_workspace("tests/misc-tests/bin/close_preopen.wasm")
|
||||
}
|
||||
|
||||
#[cfg(all(unix))]
|
||||
#[test]
|
||||
fn clock_time_get() -> Result<(), String> {
|
||||
runtime::run_test("tests/misc-tests/bin/clock_time_get.wasm")
|
||||
run_test_without_workspace("tests/misc-tests/bin/clock_time_get.wasm")
|
||||
}
|
||||
|
||||
#[cfg(all(unix))]
|
||||
#[test]
|
||||
fn readlink_no_buffer() -> Result<(), String> {
|
||||
runtime::run_test("tests/misc-tests/bin/readlink_no_buffer.wasm")
|
||||
run_test_with_workspace("tests/misc-tests/bin/readlink_no_buffer.wasm")
|
||||
}
|
||||
|
||||
#[cfg(all(unix))]
|
||||
#[test]
|
||||
fn isatty() -> Result<(), String> {
|
||||
runtime::run_test("tests/misc-tests/bin/isatty.wasm")
|
||||
run_test_with_workspace("tests/misc-tests/bin/isatty.wasm")
|
||||
}
|
||||
|
||||
#[cfg(all(unix))]
|
||||
#[test]
|
||||
fn directory_seek() -> Result<(), String> {
|
||||
runtime::run_test("tests/misc-tests/bin/directory_seek.wasm")
|
||||
run_test_with_workspace("tests/misc-tests/bin/directory_seek.wasm")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn big_random_buf() -> Result<(), String> {
|
||||
runtime::run_test("tests/misc-tests/bin/big_random_buf.wasm")
|
||||
run_test_without_workspace("tests/misc-tests/bin/big_random_buf.wasm")
|
||||
}
|
||||
|
||||
@@ -1,19 +1,14 @@
|
||||
mod utils;
|
||||
mod wasi;
|
||||
|
||||
use cranelift_codegen::settings;
|
||||
use cranelift_native;
|
||||
use std::path::Path;
|
||||
use wasmtime_jit::Context;
|
||||
|
||||
pub fn run_test<P: AsRef<Path>>(path: P) -> Result<(), String> {
|
||||
// Load in the wasm testcase
|
||||
let data = utils::read_wasm(path.as_ref())?;
|
||||
let bin_name = utils::extract_exec_name_from_path(path.as_ref())?;
|
||||
|
||||
// Prepare workspace
|
||||
let workspace = utils::prepare_workspace(&bin_name)?;
|
||||
|
||||
pub fn instantiate<S: AsRef<str>>(
|
||||
data: &[u8],
|
||||
bin_name: S,
|
||||
workspace: Option<S>,
|
||||
) -> Result<(), String> {
|
||||
// Prepare runtime
|
||||
let isa_builder =
|
||||
cranelift_native::builder().map_err(|_| "host machine is not a supported target")?;
|
||||
@@ -21,16 +16,30 @@ pub fn run_test<P: AsRef<Path>>(path: P) -> Result<(), String> {
|
||||
let isa = isa_builder.finish(settings::Flags::new(flag_builder));
|
||||
let mut context = Context::with_isa(isa);
|
||||
let global_exports = context.get_global_exports();
|
||||
let preopen_dir = wasi_common::preopen_dir(&workspace)
|
||||
.map_err(|e| format!("error while preopening directory '{}': {}", workspace, e))?;
|
||||
|
||||
let get_preopens = |workspace: Option<S>| -> Result<Vec<_>, String> {
|
||||
if let Some(workspace) = workspace {
|
||||
let preopen_dir = wasi_common::preopen_dir(workspace.as_ref()).map_err(|e| {
|
||||
format!(
|
||||
"error while preopening directory '{}': {}",
|
||||
workspace.as_ref(),
|
||||
e
|
||||
)
|
||||
})?;
|
||||
|
||||
Ok(vec![(".".to_owned(), preopen_dir)])
|
||||
} else {
|
||||
Ok(vec![])
|
||||
}
|
||||
};
|
||||
|
||||
context.name_instance(
|
||||
"wasi_unstable".to_owned(),
|
||||
wasi::instantiate_wasi(
|
||||
"",
|
||||
global_exports,
|
||||
&[(".".to_owned(), preopen_dir)],
|
||||
&[bin_name.clone(), ".".to_owned()],
|
||||
&get_preopens(workspace)?,
|
||||
&[bin_name.as_ref().to_owned(), ".".to_owned()],
|
||||
&[],
|
||||
)
|
||||
.expect("instantiating wasi"),
|
||||
@@ -40,5 +49,11 @@ pub fn run_test<P: AsRef<Path>>(path: P) -> Result<(), String> {
|
||||
context
|
||||
.instantiate_module(None, &data)
|
||||
.map(|_| ())
|
||||
.map_err(|e| format!("error while processing main module '{}': {}", bin_name, e))
|
||||
.map_err(|e| {
|
||||
format!(
|
||||
"error while processing main module '{}': {}",
|
||||
bin_name.as_ref(),
|
||||
e
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user