Merge remote-tracking branch 'upstream/master' into poll

This commit is contained in:
Marcin Mielniczuk
2020-01-14 16:40:23 +01:00
104 changed files with 2056 additions and 1607 deletions

View File

@@ -1,6 +1,6 @@
[package]
name = "test-programs"
version = "0.7.0"
version = "0.9.0"
authors = ["The Wasmtime Project Developers"]
readme = "README.md"
edition = "2018"
@@ -10,13 +10,13 @@ publish = false
cfg-if = "0.1.9"
[dev-dependencies]
wasi-common = { path = "../wasi-common" }
wasmtime-runtime = { path = "../runtime" }
wasmtime-environ = { path = "../environ" }
wasmtime-jit = { path = "../jit" }
wasmtime-wasi = { path = "../wasi" }
wasmtime = { path = "../api" }
target-lexicon = "0.9.0"
wasi-common = { path = "../wasi-common", version = "0.9.0" }
wasmtime-runtime = { path = "../runtime", version = "0.9.0" }
wasmtime-environ = { path = "../environ", version = "0.9.0" }
wasmtime-jit = { path = "../jit", version = "0.9.0" }
wasmtime-wasi = { path = "../wasi", version = "0.9.0" }
wasmtime = { path = "../api", version = "0.9.0" }
target-lexicon = "0.10.0"
pretty_env_logger = "0.3.0"
tempfile = "3.1.0"
os_pipe = "0.9"

View File

@@ -1,7 +1,7 @@
use anyhow::{bail, Context};
use std::fs::File;
use std::path::Path;
use wasmtime::{HostRef, Instance, Module, Store};
use wasmtime::{Instance, Module, Store};
pub fn instantiate(data: &[u8], bin_name: &str, workspace: Option<&Path>) -> anyhow::Result<()> {
let store = Store::default();
@@ -61,13 +61,12 @@ pub fn instantiate(data: &[u8], bin_name: &str, workspace: Option<&Path>) -> any
})
.collect::<Result<Vec<_>, _>>()?;
let instance = HostRef::new(Instance::new(&store, &module, &imports).context(format!(
let instance = Instance::new(&module, &imports).context(format!(
"error while instantiating Wasm module '{}'",
bin_name,
))?);
))?;
let export = instance
.borrow()
.find_export_by_name("_start")
.context("expected a _start export")?
.clone();
@@ -75,7 +74,6 @@ pub fn instantiate(data: &[u8], bin_name: &str, workspace: Option<&Path>) -> any
if let Err(trap) = export
.func()
.context("expected export to be a func")?
.borrow()
.call(&[])
{
bail!("trapped: {:?}", trap);

View File

@@ -0,0 +1,30 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "libc"
version = "0.2.66"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "more-asserts"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "wasi"
version = "0.9.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "wasi-tests"
version = "0.9.0"
dependencies = [
"libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)",
"more-asserts 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"wasi 0.9.0+wasi-snapshot-preview1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[metadata]
"checksum libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)" = "d515b1f41455adea1313a4a2ac8a8a477634fbae63cc6100e3aebb207ce61558"
"checksum more-asserts 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0debeb9fcf88823ea64d64e4a815ab1643f33127d995978e099942ce38f25238"
"checksum wasi 0.9.0+wasi-snapshot-preview1 (registry+https://github.com/rust-lang/crates.io-index)" = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"

View File

@@ -1,6 +1,6 @@
[package]
name = "wasi-tests"
version = "0.7.0"
version = "0.9.0"
authors = ["The Wasmtime Project Developers"]
readme = "README.md"
edition = "2018"

View File

@@ -9,7 +9,11 @@ unsafe fn test_fd_advise(dir_fd: wasi::Fd) {
0,
"file",
wasi::OFLAGS_CREAT,
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
wasi::RIGHTS_FD_READ
| wasi::RIGHTS_FD_WRITE
| wasi::RIGHTS_FD_ADVISE
| wasi::RIGHTS_FD_FILESTAT_GET
| wasi::RIGHTS_FD_ALLOCATE,
0,
0,
)

View File

@@ -9,7 +9,11 @@ unsafe fn test_fd_filestat_set(dir_fd: wasi::Fd) {
0,
"file",
wasi::OFLAGS_CREAT,
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
wasi::RIGHTS_FD_READ
| wasi::RIGHTS_FD_WRITE
| wasi::RIGHTS_FD_FILESTAT_GET
| wasi::RIGHTS_FD_FILESTAT_SET_SIZE
| wasi::RIGHTS_FD_FILESTAT_SET_TIMES,
0,
0,
)

View File

@@ -0,0 +1,165 @@
use std::{env, process};
use wasi;
use wasi_tests::open_scratch_directory;
unsafe fn test_fd_fdstat_set_flags(dir_fd: wasi::Fd) {
const FILE_NAME: &str = "file";
let data = &[0u8; 100];
let file_fd = wasi::path_open(
dir_fd,
0,
FILE_NAME,
wasi::OFLAGS_CREAT,
wasi::RIGHTS_FD_READ
| wasi::RIGHTS_FD_WRITE
| wasi::RIGHTS_FD_SEEK
| wasi::RIGHTS_FD_TELL
| wasi::RIGHTS_FD_FDSTAT_SET_FLAGS,
0,
wasi::FDFLAGS_APPEND,
)
.expect("opening a file");
// Write some data and then verify the written data
assert_eq!(
wasi::fd_write(
file_fd,
&[wasi::Ciovec {
buf: data.as_ptr(),
buf_len: data.len(),
}],
)
.expect("writing to a file"),
data.len(),
"should write {} bytes",
data.len(),
);
wasi::fd_seek(file_fd, 0, wasi::WHENCE_SET).expect("seeking file");
let buffer = &mut [0u8; 100];
assert_eq!(
wasi::fd_read(
file_fd,
&[wasi::Iovec {
buf: buffer.as_mut_ptr(),
buf_len: buffer.len(),
}]
)
.expect("reading file"),
buffer.len(),
"shoudl read {} bytes",
buffer.len()
);
assert_eq!(&data[..], &buffer[..]);
let data = &[1u8; 100];
// Seek back to the start to ensure we're in append-only mode
wasi::fd_seek(file_fd, 0, wasi::WHENCE_SET).expect("seeking file");
assert_eq!(
wasi::fd_write(
file_fd,
&[wasi::Ciovec {
buf: data.as_ptr(),
buf_len: data.len(),
}],
)
.expect("writing to a file"),
data.len(),
"should write {} bytes",
data.len(),
);
wasi::fd_seek(file_fd, 100, wasi::WHENCE_SET).expect("seeking file");
assert_eq!(
wasi::fd_read(
file_fd,
&[wasi::Iovec {
buf: buffer.as_mut_ptr(),
buf_len: buffer.len(),
}]
)
.expect("reading file"),
buffer.len(),
"shoudl read {} bytes",
buffer.len()
);
assert_eq!(&data[..], &buffer[..]);
wasi::fd_fdstat_set_flags(file_fd, 0).expect("disabling flags");
// Overwrite some existing data to ensure the append mode is now off
wasi::fd_seek(file_fd, 0, wasi::WHENCE_SET).expect("seeking file");
let data = &[2u8; 100];
assert_eq!(
wasi::fd_write(
file_fd,
&[wasi::Ciovec {
buf: data.as_ptr(),
buf_len: data.len(),
}],
)
.expect("writing to a file"),
data.len(),
"should write {} bytes",
data.len(),
);
wasi::fd_seek(file_fd, 0, wasi::WHENCE_SET).expect("seeking file");
assert_eq!(
wasi::fd_read(
file_fd,
&[wasi::Iovec {
buf: buffer.as_mut_ptr(),
buf_len: buffer.len(),
}]
)
.expect("reading file"),
buffer.len(),
"shoudl read {} bytes",
buffer.len()
);
assert_eq!(&data[..], &buffer[..]);
wasi::fd_close(file_fd).expect("close file");
let stat = wasi::path_filestat_get(dir_fd, 0, FILE_NAME).expect("stat path");
assert_eq!(stat.size, 200, "expected a file size of 200");
wasi::path_unlink_file(dir_fd, FILE_NAME).expect("unlinking file");
}
fn main() {
let mut args = env::args();
let prog = args.next().unwrap();
let arg = if let Some(arg) = args.next() {
arg
} else {
eprintln!("usage: {} <scratch directory>", prog);
process::exit(1);
};
let dir_fd = match open_scratch_directory(&arg) {
Ok(dir_fd) => dir_fd,
Err(err) => {
eprintln!("{}", err);
process::exit(1)
}
};
unsafe {
test_fd_fdstat_set_flags(dir_fd);
}
}

View File

@@ -90,7 +90,10 @@ unsafe fn test_fd_readdir(dir_fd: wasi::Fd) {
0,
"file",
wasi::OFLAGS_CREAT,
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
wasi::RIGHTS_FD_READ
| wasi::RIGHTS_FD_WRITE
| wasi::RIGHTS_FD_READDIR
| wasi::RIGHTS_FD_FILESTAT_GET,
0,
0,
)

View File

@@ -9,7 +9,10 @@ unsafe fn test_file_allocate(dir_fd: wasi::Fd) {
0,
"file",
wasi::OFLAGS_CREAT,
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
wasi::RIGHTS_FD_READ
| wasi::RIGHTS_FD_WRITE
| wasi::RIGHTS_FD_ALLOCATE
| wasi::RIGHTS_FD_FILESTAT_GET,
0,
0,
)

View File

@@ -9,7 +9,7 @@ unsafe fn test_file_seek_tell(dir_fd: wasi::Fd) {
0,
"file",
wasi::OFLAGS_CREAT,
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE | wasi::RIGHTS_FD_SEEK | wasi::RIGHTS_FD_TELL,
0,
0,
)

View File

@@ -2,8 +2,15 @@ use more_asserts::assert_gt;
use std::{env, process};
use wasi_tests::{create_file, open_scratch_directory};
const TEST_RIGHTS: wasi::Rights = wasi::RIGHTS_FD_READ
| wasi::RIGHTS_PATH_LINK_SOURCE
| wasi::RIGHTS_PATH_LINK_TARGET
| wasi::RIGHTS_FD_FILESTAT_GET
| wasi::RIGHTS_PATH_OPEN
| wasi::RIGHTS_PATH_UNLINK_FILE;
unsafe fn create_or_open(dir_fd: wasi::Fd, name: &str, flags: wasi::Oflags) -> wasi::Fd {
let file_fd = wasi::path_open(dir_fd, 0, name, flags, 0, 0, 0)
let file_fd = wasi::path_open(dir_fd, 0, name, flags, TEST_RIGHTS, TEST_RIGHTS, 0)
.unwrap_or_else(|_| panic!("opening '{}'", name));
assert_gt!(
file_fd,
@@ -14,7 +21,7 @@ unsafe fn create_or_open(dir_fd: wasi::Fd, name: &str, flags: wasi::Oflags) -> w
}
unsafe fn open_link(dir_fd: wasi::Fd, name: &str) -> wasi::Fd {
let file_fd = wasi::path_open(dir_fd, 0, name, 0, 0, 0, 0)
let file_fd = wasi::path_open(dir_fd, 0, name, 0, TEST_RIGHTS, TEST_RIGHTS, 0)
.unwrap_or_else(|_| panic!("opening a link '{}'", name));
assert_gt!(
file_fd,

View File

@@ -0,0 +1,79 @@
use std::{env, process};
use wasi_tests::open_scratch_directory;
use wasi_tests::{create_file, drop_rights, fd_get_rights};
const TEST_FILENAME: &'static str = "file";
unsafe fn try_read_file(dir_fd: wasi::Fd) {
let fd = wasi::path_open(dir_fd, 0, TEST_FILENAME, 0, 0, 0, 0).expect("opening the file");
// Check that we don't have the right to exeucute fd_read
let (rbase, rinher) = fd_get_rights(fd);
assert_eq!(
rbase & wasi::RIGHTS_FD_READ,
0,
"should not have base RIGHTS_FD_READ"
);
assert_eq!(
rinher & wasi::RIGHTS_FD_READ,
0,
"should not have inheriting RIGHTS_FD_READ"
);
let contents = &mut [0u8; 1];
let iovec = wasi::Iovec {
buf: contents.as_mut_ptr() as *mut _,
buf_len: contents.len(),
};
// Since we no longer have the right to fd_read, trying to read a file
// should be an error.
assert_eq!(
wasi::fd_read(fd, &[iovec])
.expect_err("reading bytes from file should fail")
.raw_error(),
wasi::ERRNO_NOTCAPABLE,
"the errno should be ENOTCAPABLE"
);
}
unsafe fn test_read_rights(dir_fd: wasi::Fd) {
create_file(dir_fd, TEST_FILENAME);
drop_rights(dir_fd, wasi::RIGHTS_FD_READ, wasi::RIGHTS_FD_READ);
let (rbase, rinher) = fd_get_rights(dir_fd);
assert_eq!(
rbase & wasi::RIGHTS_FD_READ,
0,
"dir should not have base RIGHTS_FD_READ"
);
assert_eq!(
rinher & wasi::RIGHTS_FD_READ,
0,
"dir should not have inheriting RIGHTS_FD_READ"
);
try_read_file(dir_fd);
}
fn main() {
let mut args = env::args();
let prog = args.next().unwrap();
let arg = if let Some(arg) = args.next() {
arg
} else {
eprintln!("usage: {} <scratch directory>", prog);
process::exit(1);
};
// Open scratch directory
let dir_fd = match open_scratch_directory(&arg) {
Ok(dir_fd) => dir_fd,
Err(err) => {
eprintln!("{}", err);
process::exit(1)
}
};
// Run the tests.
unsafe { test_read_rights(dir_fd) }
}

View File

@@ -198,7 +198,7 @@ unsafe fn test_fd_readwrite_valid_fd(dir_fd: wasi::Fd) {
0,
"file",
wasi::OFLAGS_CREAT,
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE | wasi::RIGHTS_POLL_FD_READWRITE,
0,
0,
)

View File

@@ -24,8 +24,11 @@ pub fn open_scratch_directory(path: &str) -> Result<wasi::Fd, String> {
}
dst.set_len(stat.u.dir.pr_name_len);
if dst == path.as_bytes() {
return Ok(wasi::path_open(i, 0, ".", wasi::OFLAGS_DIRECTORY, 0, 0, 0)
.expect("failed to open dir"));
let (base, inherit) = fd_get_rights(i);
return Ok(
wasi::path_open(i, 0, ".", wasi::OFLAGS_DIRECTORY, base, inherit, 0)
.expect("failed to open dir"),
);
}
}
@@ -43,3 +46,18 @@ pub unsafe fn create_file(dir_fd: wasi::Fd, filename: &str) {
);
wasi::fd_close(file_fd).expect("closing a file");
}
// Returns: (rights_base, rights_inheriting)
pub unsafe fn fd_get_rights(fd: wasi::Fd) -> (wasi::Rights, wasi::Rights) {
let fdstat = wasi::fd_fdstat_get(fd).expect("fd_fdstat_get failed");
(fdstat.fs_rights_base, fdstat.fs_rights_inheriting)
}
pub unsafe fn drop_rights(fd: wasi::Fd, drop_base: wasi::Rights, drop_inheriting: wasi::Rights) {
let (current_base, current_inheriting) = fd_get_rights(fd);
let new_base = current_base & !drop_base;
let new_inheriting = current_inheriting & !drop_inheriting;
wasi::fd_fdstat_set_rights(fd, new_base, new_inheriting).expect("dropping fd rights");
}