Jakub Konka
5 years ago
committed by
Dan Gohman
3 changed files with 93 additions and 106 deletions
@ -1,66 +1,66 @@ |
|||
mod runtime; |
|||
|
|||
#[test] |
|||
fn sched_yield() { |
|||
fn sched_yield() -> Result<(), String> { |
|||
runtime::run_test("tests/misc-tests/bin/sched_yield.wasm") |
|||
} |
|||
|
|||
#[test] |
|||
fn truncation_rights() { |
|||
fn truncation_rights() -> Result<(), String> { |
|||
runtime::run_test("tests/misc-tests/bin/truncation_rights.wasm") |
|||
} |
|||
|
|||
#[test] |
|||
fn unlink_directory() { |
|||
fn unlink_directory() -> Result<(), String> { |
|||
runtime::run_test("tests/misc-tests/bin/unlink_directory.wasm") |
|||
} |
|||
|
|||
#[test] |
|||
fn remove_nonempty_directory() { |
|||
fn remove_nonempty_directory() -> Result<(), String> { |
|||
runtime::run_test("tests/misc-tests/bin/remove_nonempty_directory.wasm") |
|||
} |
|||
|
|||
#[test] |
|||
fn interesting_paths() { |
|||
fn interesting_paths() -> Result<(), String> { |
|||
runtime::run_test("tests/misc-tests/bin/interesting_paths.wasm") |
|||
} |
|||
|
|||
#[test] |
|||
fn nofollow_errors() { |
|||
fn nofollow_errors() -> Result<(), String> { |
|||
runtime::run_test("tests/misc-tests/bin/nofollow_errors.wasm") |
|||
} |
|||
|
|||
#[test] |
|||
fn symlink_loop() { |
|||
fn symlink_loop() -> Result<(), String> { |
|||
runtime::run_test("tests/misc-tests/bin/symlink_loop.wasm") |
|||
} |
|||
|
|||
#[test] |
|||
fn close_preopen() { |
|||
fn close_preopen() -> Result<(), String> { |
|||
runtime::run_test("tests/misc-tests/bin/close_preopen.wasm") |
|||
} |
|||
|
|||
#[test] |
|||
fn clock_time_get() { |
|||
fn clock_time_get() -> Result<(), String> { |
|||
runtime::run_test("tests/misc-tests/bin/clock_time_get.wasm") |
|||
} |
|||
|
|||
#[test] |
|||
fn readlink_no_buffer() { |
|||
fn readlink_no_buffer() -> Result<(), String> { |
|||
runtime::run_test("tests/misc-tests/bin/readlink_no_buffer.wasm") |
|||
} |
|||
|
|||
#[test] |
|||
fn isatty() { |
|||
fn isatty() -> Result<(), String> { |
|||
runtime::run_test("tests/misc-tests/bin/isatty.wasm") |
|||
} |
|||
|
|||
#[test] |
|||
fn directory_seek() { |
|||
fn directory_seek() -> Result<(), String> { |
|||
runtime::run_test("tests/misc-tests/bin/directory_seek.wasm") |
|||
} |
|||
|
|||
#[test] |
|||
fn big_random_buf() { |
|||
fn big_random_buf() -> Result<(), String> { |
|||
runtime::run_test("tests/misc-tests/bin/big_random_buf.wasm") |
|||
} |
|||
|
@ -1,111 +1,44 @@ |
|||
mod utils; |
|||
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 std::path::Path; |
|||
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(()) |
|||
} |
|||
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())?; |
|||
|
|||
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())?; |
|||
// Prepare workspace
|
|||
let workspace = utils::prepare_workspace(&bin_name)?; |
|||
|
|||
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_test<P: AsRef<Path>>(path: P) { |
|||
let isa_builder = cranelift_native::builder().unwrap_or_else(|_| { |
|||
panic!("host machine is not a supported target"); |
|||
}); |
|||
// Prepare runtime
|
|||
let isa_builder = |
|||
cranelift_native::builder().map_err(|_| "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()]; |
|||
let preopen_dir = wasi_common::preopen_dir(&workspace) |
|||
.map_err(|e| format!("error while preopening directory '{}': {}", workspace, e))?; |
|||
|
|||
context.name_instance( |
|||
"wasi_unstable".to_owned(), |
|||
wasi::instantiate_wasi("", global_exports, preopen_dirs, &argv, &[]) |
|||
.expect("instantiating wasi"), |
|||
wasi::instantiate_wasi( |
|||
"", |
|||
global_exports, |
|||
&[(".".to_owned(), preopen_dir)], |
|||
&[bin_name.clone(), ".".to_owned()], |
|||
&[], |
|||
) |
|||
.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); |
|||
} |
|||
} |
|||
// Compile and instantiating a wasm module.
|
|||
context |
|||
.instantiate_module(None, &data) |
|||
.map(|_| ()) |
|||
.map_err(|e| format!("error while processing main module '{}': {}", bin_name, e)) |
|||
} |
|||
|
@ -0,0 +1,54 @@ |
|||
use std::env; |
|||
use std::ffi::OsStr; |
|||
use std::fs::{self, File}; |
|||
use std::io; |
|||
use std::io::prelude::*; |
|||
use std::path::{Component, Path}; |
|||
use std::time::SystemTime; |
|||
|
|||
fn read_to_end<P: AsRef<Path>>(path: P) -> 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) |
|||
} |
|||
|
|||
pub fn read_wasm<P: AsRef<Path>>(path: P) -> 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()) |
|||
} |
|||
} |
|||
|
|||
pub fn prepare_workspace<S: AsRef<str>>(exe_name: S) -> 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.as_ref(), |
|||
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() |
|||
.ok_or("couldn't convert to str".to_owned())? |
|||
.to_string()) |
|||
} |
|||
|
|||
pub fn extract_exec_name_from_path<P: AsRef<Path>>(path: P) -> Result<String, String> { |
|||
Ok(path |
|||
.as_ref() |
|||
.components() |
|||
.next_back() |
|||
.map(Component::as_os_str) |
|||
.and_then(OsStr::to_str) |
|||
.ok_or("couldn't convert to str".to_owned())? |
|||
.to_owned()) |
|||
} |
Loading…
Reference in new issue