Move workspace creation outside of runtime instantiation
This commit is contained in:
@@ -1,78 +1,102 @@
|
|||||||
mod runtime;
|
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))]
|
#[cfg(all(unix))]
|
||||||
#[test]
|
#[test]
|
||||||
fn sched_yield() -> Result<(), String> {
|
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))]
|
#[cfg(all(unix))]
|
||||||
#[test]
|
#[test]
|
||||||
fn truncation_rights() -> Result<(), String> {
|
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))]
|
#[cfg(all(unix))]
|
||||||
#[test]
|
#[test]
|
||||||
fn unlink_directory() -> Result<(), String> {
|
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))]
|
#[cfg(all(unix))]
|
||||||
#[test]
|
#[test]
|
||||||
fn remove_nonempty_directory() -> Result<(), String> {
|
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))]
|
#[cfg(all(unix))]
|
||||||
#[test]
|
#[test]
|
||||||
fn interesting_paths() -> Result<(), String> {
|
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))]
|
#[cfg(all(unix))]
|
||||||
#[test]
|
#[test]
|
||||||
fn nofollow_errors() -> Result<(), String> {
|
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))]
|
#[cfg(all(unix))]
|
||||||
#[test]
|
#[test]
|
||||||
fn symlink_loop() -> Result<(), String> {
|
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))]
|
#[cfg(all(unix))]
|
||||||
#[test]
|
#[test]
|
||||||
fn close_preopen() -> Result<(), String> {
|
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))]
|
#[cfg(all(unix))]
|
||||||
#[test]
|
#[test]
|
||||||
fn clock_time_get() -> Result<(), String> {
|
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))]
|
#[cfg(all(unix))]
|
||||||
#[test]
|
#[test]
|
||||||
fn readlink_no_buffer() -> Result<(), String> {
|
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))]
|
#[cfg(all(unix))]
|
||||||
#[test]
|
#[test]
|
||||||
fn isatty() -> Result<(), String> {
|
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))]
|
#[cfg(all(unix))]
|
||||||
#[test]
|
#[test]
|
||||||
fn directory_seek() -> Result<(), String> {
|
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]
|
#[test]
|
||||||
fn big_random_buf() -> Result<(), String> {
|
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;
|
mod wasi;
|
||||||
|
|
||||||
use cranelift_codegen::settings;
|
use cranelift_codegen::settings;
|
||||||
use cranelift_native;
|
use cranelift_native;
|
||||||
use std::path::Path;
|
|
||||||
use wasmtime_jit::Context;
|
use wasmtime_jit::Context;
|
||||||
|
|
||||||
pub fn run_test<P: AsRef<Path>>(path: P) -> Result<(), String> {
|
pub fn instantiate<S: AsRef<str>>(
|
||||||
// Load in the wasm testcase
|
data: &[u8],
|
||||||
let data = utils::read_wasm(path.as_ref())?;
|
bin_name: S,
|
||||||
let bin_name = utils::extract_exec_name_from_path(path.as_ref())?;
|
workspace: Option<S>,
|
||||||
|
) -> Result<(), String> {
|
||||||
// Prepare workspace
|
|
||||||
let workspace = utils::prepare_workspace(&bin_name)?;
|
|
||||||
|
|
||||||
// Prepare runtime
|
// Prepare runtime
|
||||||
let isa_builder =
|
let isa_builder =
|
||||||
cranelift_native::builder().map_err(|_| "host machine is not a supported target")?;
|
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 isa = isa_builder.finish(settings::Flags::new(flag_builder));
|
||||||
let mut context = Context::with_isa(isa);
|
let mut context = Context::with_isa(isa);
|
||||||
let global_exports = context.get_global_exports();
|
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(
|
context.name_instance(
|
||||||
"wasi_unstable".to_owned(),
|
"wasi_unstable".to_owned(),
|
||||||
wasi::instantiate_wasi(
|
wasi::instantiate_wasi(
|
||||||
"",
|
"",
|
||||||
global_exports,
|
global_exports,
|
||||||
&[(".".to_owned(), preopen_dir)],
|
&get_preopens(workspace)?,
|
||||||
&[bin_name.clone(), ".".to_owned()],
|
&[bin_name.as_ref().to_owned(), ".".to_owned()],
|
||||||
&[],
|
&[],
|
||||||
)
|
)
|
||||||
.expect("instantiating wasi"),
|
.expect("instantiating wasi"),
|
||||||
@@ -40,5 +49,11 @@ pub fn run_test<P: AsRef<Path>>(path: P) -> Result<(), String> {
|
|||||||
context
|
context
|
||||||
.instantiate_module(None, &data)
|
.instantiate_module(None, &data)
|
||||||
.map(|_| ())
|
.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