Skip to content
This repository has been archived by the owner on Nov 7, 2019. It is now read-only.

Commit

Permalink
Use the wasi crate.
Browse files Browse the repository at this point in the history
Switch from depending on libc to depending on the new wasi crate to provide
the low-level WASI interfaces.

See also rust-lang/libc#1461.
  • Loading branch information
sunfishcode committed Aug 19, 2019
1 parent f64e071 commit 6b2ce3a
Show file tree
Hide file tree
Showing 28 changed files with 524 additions and 553 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ edition = "2018"

[dependencies]
libc = { git = "https://github.com/rust-lang/libc" }
wasi = "0.5.0"
7 changes: 4 additions & 3 deletions src/bin/big_random_buf.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use wasi::wasi_unstable;

fn test_big_random_buf() {
let mut buf = Vec::new();
buf.resize(1024, 0);
let status =
unsafe { libc::__wasi_random_get(buf.as_mut_ptr() as *mut libc::c_void, buf.len()) };
let status = wasi_unstable::random_get(&mut buf);
assert_eq!(
status,
libc::__WASI_ESUCCESS,
wasi_unstable::ESUCCESS,
"calling get_random on a large buffer"
);
// Chances are pretty good that at least *one* byte will be non-zero in
Expand Down
14 changes: 7 additions & 7 deletions src/bin/clock_time_get.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
use libc;
use misc_tests::wasi::wasi_clock_time_get;
use misc_tests::wasi_wrappers::wasi_clock_time_get;
use wasi::wasi_unstable;

fn test_clock_time_get() {
// Test that clock_time_get succeeds. Even in environments where it's not
// desirable to expose high-precision timers, it should still succeed.
// clock_res_get is where information about precision can be provided.
let mut time: libc::__wasi_timestamp_t = 0;
let mut status = wasi_clock_time_get(libc::__WASI_CLOCK_MONOTONIC, 0, &mut time);
let mut time: wasi_unstable::Timestamp = 0;
let mut status = wasi_clock_time_get(wasi_unstable::CLOCK_MONOTONIC, 0, &mut time);
assert_eq!(
status,
libc::__WASI_ESUCCESS,
wasi_unstable::ESUCCESS,
"clock_time_get with a precision of 0"
);

status = wasi_clock_time_get(libc::__WASI_CLOCK_MONOTONIC, 1, &mut time);
status = wasi_clock_time_get(wasi_unstable::CLOCK_MONOTONIC, 1, &mut time);
assert_eq!(
status,
libc::__WASI_ESUCCESS,
wasi_unstable::ESUCCESS,
"clock_time_get with a precision of 1"
);
}
Expand Down
29 changes: 15 additions & 14 deletions src/bin/close_preopen.rs
Original file line number Diff line number Diff line change
@@ -1,59 +1,60 @@
use libc;
use misc_tests::open_scratch_directory;
use misc_tests::wasi::{wasi_fd_close, wasi_fd_fdstat_get, wasi_fd_renumber};
use misc_tests::wasi_wrappers::wasi_fd_fdstat_get;
use std::{env, mem, process};
use wasi::wasi_unstable;

fn test_close_preopen(dir_fd: libc::__wasi_fd_t) {
let pre_fd: libc::__wasi_fd_t = (libc::STDERR_FILENO + 1) as libc::__wasi_fd_t;
fn test_close_preopen(dir_fd: wasi_unstable::Fd) {
let pre_fd: wasi_unstable::Fd = (libc::STDERR_FILENO + 1) as wasi_unstable::Fd;

assert!(dir_fd > pre_fd, "dir_fd number");

// Try to close a preopened directory handle.
let mut status = wasi_fd_close(pre_fd);
let mut status = wasi_unstable::fd_close(pre_fd);
assert_eq!(
status,
libc::__WASI_ENOTSUP,
wasi_unstable::ENOTSUP,
"closing a preopened file descriptor",
);

// Try to renumber over a preopened directory handle.
status = wasi_fd_renumber(dir_fd, pre_fd);
status = wasi_unstable::fd_renumber(dir_fd, pre_fd);
assert_eq!(
status,
libc::__WASI_ENOTSUP,
wasi_unstable::ENOTSUP,
"renumbering over a preopened file descriptor",
);

// Ensure that dir_fd is still open.
let mut dir_fdstat: libc::__wasi_fdstat_t = unsafe { mem::zeroed() };
let mut dir_fdstat: wasi_unstable::FdStat = unsafe { mem::zeroed() };
status = wasi_fd_fdstat_get(dir_fd, &mut dir_fdstat);
assert_eq!(
status,
libc::__WASI_ESUCCESS,
wasi_unstable::ESUCCESS,
"calling fd_fdstat on the scratch directory"
);
assert!(
dir_fdstat.fs_filetype == libc::__WASI_FILETYPE_DIRECTORY,
dir_fdstat.fs_filetype == wasi_unstable::FILETYPE_DIRECTORY,
"expected the scratch directory to be a directory",
);

// Try to renumber a preopened directory handle.
status = wasi_fd_renumber(pre_fd, dir_fd);
status = wasi_unstable::fd_renumber(pre_fd, dir_fd);
assert_eq!(
status,
libc::__WASI_ENOTSUP,
wasi_unstable::ENOTSUP,
"renumbering over a preopened file descriptor",
);

// Ensure that dir_fd is still open.
status = wasi_fd_fdstat_get(dir_fd, &mut dir_fdstat);
assert_eq!(
status,
libc::__WASI_ESUCCESS,
wasi_unstable::ESUCCESS,
"calling fd_fdstat on the scratch directory"
);
assert!(
dir_fdstat.fs_filetype == libc::__WASI_FILETYPE_DIRECTORY,
dir_fdstat.fs_filetype == wasi_unstable::FILETYPE_DIRECTORY,
"expected the scratch directory to be a directory",
);
}
Expand Down
16 changes: 8 additions & 8 deletions src/bin/dangling_symlink.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
use libc;
use misc_tests::open_scratch_directory;
use misc_tests::utils::cleanup_file;
use misc_tests::wasi::{wasi_path_open, wasi_path_symlink};
use misc_tests::wasi_wrappers::{wasi_path_open, wasi_path_symlink};
use std::{env, process};
use wasi::wasi_unstable;

fn test_dangling_symlink(dir_fd: libc::__wasi_fd_t) {
fn test_dangling_symlink(dir_fd: wasi_unstable::Fd) {
// First create a dangling symlink.
let mut status = wasi_path_symlink("target", dir_fd, "symlink");
assert_eq!(status, libc::__WASI_ESUCCESS, "creating a symlink");
assert_eq!(status, wasi_unstable::ESUCCESS, "creating a symlink");

// Try to open it as a directory with O_NOFOLLOW.
let mut file_fd: libc::__wasi_fd_t = libc::__wasi_fd_t::max_value() - 1;
let mut file_fd: wasi_unstable::Fd = wasi_unstable::Fd::max_value() - 1;
status = wasi_path_open(
dir_fd,
0,
"symlink",
libc::__WASI_O_DIRECTORY,
wasi_unstable::O_DIRECTORY,
0,
0,
0,
&mut file_fd,
);
assert_eq!(
status,
libc::__WASI_ELOOP,
wasi_unstable::ELOOP,
"opening a dangling symlink as a directory",
);
assert_eq!(
file_fd,
libc::__wasi_fd_t::max_value(),
wasi_unstable::Fd::max_value(),
"failed open should set the file descriptor to -1",
);

Expand Down
25 changes: 13 additions & 12 deletions src/bin/directory_seek.rs
Original file line number Diff line number Diff line change
@@ -1,50 +1,51 @@
use libc;
use misc_tests::open_scratch_directory;
use misc_tests::utils::{cleanup_dir, close_fd, create_dir};
use misc_tests::wasi::{wasi_fd_fdstat_get, wasi_fd_seek, wasi_path_open};
use misc_tests::wasi_wrappers::{wasi_fd_fdstat_get, wasi_fd_seek, wasi_path_open};
use std::{env, mem, process};
use wasi::wasi_unstable;

fn test_directory_seek(dir_fd: libc::__wasi_fd_t) {
fn test_directory_seek(dir_fd: wasi_unstable::Fd) {
// Create a directory in the scratch directory.
create_dir(dir_fd, "dir");

// Open the directory and attempt to request rights for seeking.
let mut fd: libc::__wasi_fd_t = libc::__wasi_fd_t::max_value() - 1;
let mut fd: wasi_unstable::Fd = wasi_unstable::Fd::max_value() - 1;
let mut status = wasi_path_open(
dir_fd,
0,
"dir",
0,
libc::__WASI_RIGHT_FD_SEEK,
wasi_unstable::RIGHT_FD_SEEK,
0,
0,
&mut fd,
);
assert_eq!(status, libc::__WASI_ESUCCESS, "opening a file");
assert_eq!(status, wasi_unstable::ESUCCESS, "opening a file");
assert!(
fd > libc::STDERR_FILENO as libc::__wasi_fd_t,
fd > libc::STDERR_FILENO as wasi_unstable::Fd,
"file descriptor range check",
);

// Attempt to seek.
let mut newoffset = 1;
status = wasi_fd_seek(fd, 0, libc::__WASI_WHENCE_CUR, &mut newoffset);
assert_eq!(status, libc::__WASI_ENOTCAPABLE, "seek on a directory");
status = wasi_fd_seek(fd, 0, wasi_unstable::WHENCE_CUR, &mut newoffset);
assert_eq!(status, wasi_unstable::ENOTCAPABLE, "seek on a directory");

// Check if we obtained the right to seek.
let mut fdstat: libc::__wasi_fdstat_t = unsafe { mem::zeroed() };
let mut fdstat: wasi_unstable::FdStat = unsafe { mem::zeroed() };
status = wasi_fd_fdstat_get(fd, &mut fdstat);
assert_eq!(
status,
libc::__WASI_ESUCCESS,
wasi_unstable::ESUCCESS,
"calling fd_fdstat on a directory"
);
assert!(
fdstat.fs_filetype == libc::__WASI_FILETYPE_DIRECTORY,
fdstat.fs_filetype == wasi_unstable::FILETYPE_DIRECTORY,
"expected the scratch directory to be a directory",
);
assert_eq!(
(fdstat.fs_rights_base & libc::__WASI_RIGHT_FD_SEEK),
(fdstat.fs_rights_base & wasi_unstable::RIGHT_FD_SEEK),
0,
"directory has the seek right",
);
Expand Down
43 changes: 23 additions & 20 deletions src/bin/fd_filestat_set.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
use libc;
use misc_tests::open_scratch_directory;
use misc_tests::utils::{cleanup_file, close_fd};
use misc_tests::wasi::{
wasi_fd_filestat_get, wasi_fd_filestat_set_size, wasi_fd_filestat_set_times, wasi_path_open,
};
use misc_tests::wasi_wrappers::{wasi_fd_filestat_get, wasi_path_open};
use std::{env, process};
use wasi::wasi_unstable;

fn test_fd_filestat_set(dir_fd: libc::__wasi_fd_t) {
fn test_fd_filestat_set(dir_fd: wasi_unstable::Fd) {
// Create a file in the scratch directory.
let mut file_fd = libc::__wasi_fd_t::max_value() - 1;
let mut file_fd = wasi_unstable::Fd::max_value() - 1;
let status = wasi_path_open(
dir_fd,
0,
"file",
libc::__WASI_O_CREAT,
libc::__WASI_RIGHT_FD_READ | libc::__WASI_RIGHT_FD_WRITE,
wasi_unstable::O_CREAT,
wasi_unstable::RIGHT_FD_READ | wasi_unstable::RIGHT_FD_WRITE,
0,
0,
&mut file_fd,
);
assert_eq!(status, libc::__WASI_ESUCCESS, "opening a file");
assert_eq!(status, wasi_unstable::ESUCCESS, "opening a file");
assert!(
file_fd > libc::STDERR_FILENO as libc::__wasi_fd_t,
file_fd > libc::STDERR_FILENO as wasi_unstable::Fd,
"file descriptor range check",
);

// Check file size
let mut stat = libc::__wasi_filestat_t {
let mut stat = wasi_unstable::FileStat {
st_dev: 0,
st_ino: 0,
st_filetype: 0,
Expand All @@ -37,32 +36,36 @@ fn test_fd_filestat_set(dir_fd: libc::__wasi_fd_t) {
st_ctim: 0,
};
let status = wasi_fd_filestat_get(file_fd, &mut stat);
assert_eq!(status, libc::__WASI_ESUCCESS, "reading file stats");
assert_eq!(status, wasi_unstable::ESUCCESS, "reading file stats");
assert_eq!(stat.st_size, 0, "file size should be 0");

// Check fd_filestat_set_size
let status = wasi_fd_filestat_set_size(file_fd, 100);
assert_eq!(status, libc::__WASI_ESUCCESS, "fd_filestat_set_size");
let status = wasi_unstable::fd_filestat_set_size(file_fd, 100);
assert_eq!(status, wasi_unstable::ESUCCESS, "fd_filestat_set_size");

let status = wasi_fd_filestat_get(file_fd, &mut stat);
assert_eq!(
status,
libc::__WASI_ESUCCESS,
wasi_unstable::ESUCCESS,
"reading file stats after fd_filestat_set_size"
);
assert_eq!(stat.st_size, 100, "file size should be 100");

// Check fd_filestat_set_times
let old_atim = stat.st_atim;
let new_mtim = stat.st_mtim - 100;
let status =
wasi_fd_filestat_set_times(file_fd, new_mtim, new_mtim, libc::__WASI_FILESTAT_SET_MTIM);
assert_eq!(status, libc::__WASI_ESUCCESS, "fd_filestat_set_times");
let status = wasi_unstable::fd_filestat_set_times(
file_fd,
new_mtim,
new_mtim,
wasi_unstable::FILESTAT_SET_MTIM,
);
assert_eq!(status, wasi_unstable::ESUCCESS, "fd_filestat_set_times");

let status = wasi_fd_filestat_get(file_fd, &mut stat);
assert_eq!(
status,
libc::__WASI_ESUCCESS,
wasi_unstable::ESUCCESS,
"reading file stats after fd_filestat_set_times"
);
assert_eq!(
Expand All @@ -72,8 +75,8 @@ fn test_fd_filestat_set(dir_fd: libc::__wasi_fd_t) {
assert_eq!(stat.st_mtim, new_mtim, "mtim should change");
assert_eq!(stat.st_atim, old_atim, "atim should not change");

// let status = wasi_fd_filestat_set_times(file_fd, new_mtim, new_mtim, libc::__WASI_FILESTAT_SET_MTIM | libc::__WASI_FILESTAT_SET_MTIM_NOW);
// assert_eq!(status, libc::__WASI_EINVAL, "ATIM & ATIM_NOW can't both be set");
// let status = wasi_fd_filestat_set_times(file_fd, new_mtim, new_mtim, wasi_unstable::FILESTAT_SET_MTIM | wasi_unstable::FILESTAT_SET_MTIM_NOW);
// assert_eq!(status, wasi_unstable::EINVAL, "ATIM & ATIM_NOW can't both be set");

close_fd(file_fd);
cleanup_file(dir_fd, "file");
Expand Down
Loading

0 comments on commit 6b2ce3a

Please sign in to comment.