Auto-generate the hostcalls module of wasi-common (#846)

* Auto-generate shims for old `wasi_unstable` module

This commit is effectively just doing what #707 already did, but
applying it to the `snapshot_0` module as well. The end result is the
same, where we cut down on all the boilerplate in `snapshot_0` and bring
it in line with the main `wasi_snapshot_preview1` implementation. The
goal here is to make it easier to change the two in tandem since they're
both doing the same thing.

* Migrate `wasi_common::hostcalls` to a macro

This commit migrates the `hostcalls` module to being auto-generated by a
macro rather than duplicating a handwritten signature for each wasi
syscall.

* Auto-generate snapshot_0's `hostcalls` module

Similar to the previous commit, but for `snapshot_0`

* Delete the `wasi-common-cbindgen` crate

This is no longer needed with the hostcalls macro now, we can easily
fold the definition of the cbindgen macro into the same crate.

* Rustfmt

* Fix windows build errors

* Rustfmt

* Remove now no-longer-necessary code

* rustfmt
This commit is contained in:
Alex Crichton
2020-01-22 14:54:39 -06:00
committed by GitHub
parent 5d7635c351
commit 5953215bac
44 changed files with 391 additions and 2742 deletions

View File

@@ -1,256 +0,0 @@
#![allow(non_camel_case_types)]
use crate::old::snapshot_0::ctx::WasiCtx;
use crate::old::snapshot_0::{wasi, wasi32};
hostcalls_old! {
pub unsafe fn fd_close(wasi_ctx: &mut WasiCtx, fd: wasi::__wasi_fd_t,) -> wasi::__wasi_errno_t;
pub unsafe fn fd_datasync(wasi_ctx: &WasiCtx, fd: wasi::__wasi_fd_t,) -> wasi::__wasi_errno_t;
pub unsafe fn fd_pread(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
fd: wasi::__wasi_fd_t,
iovs_ptr: wasi32::uintptr_t,
iovs_len: wasi32::size_t,
offset: wasi::__wasi_filesize_t,
nread: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_pwrite(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
fd: wasi::__wasi_fd_t,
iovs_ptr: wasi32::uintptr_t,
iovs_len: wasi32::size_t,
offset: wasi::__wasi_filesize_t,
nwritten: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_read(
wasi_ctx: &mut WasiCtx,
memory: &mut [u8],
fd: wasi::__wasi_fd_t,
iovs_ptr: wasi32::uintptr_t,
iovs_len: wasi32::size_t,
nread: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_renumber(
wasi_ctx: &mut WasiCtx,
from: wasi::__wasi_fd_t,
to: wasi::__wasi_fd_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_seek(
wasi_ctx: &mut WasiCtx,
memory: &mut [u8],
fd: wasi::__wasi_fd_t,
offset: wasi::__wasi_filedelta_t,
whence: wasi::__wasi_whence_t,
newoffset: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_tell(
wasi_ctx: &mut WasiCtx,
memory: &mut [u8],
fd: wasi::__wasi_fd_t,
newoffset: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_fdstat_get(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
fd: wasi::__wasi_fd_t,
fdstat_ptr: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_fdstat_set_flags(
wasi_ctx: &WasiCtx,
fd: wasi::__wasi_fd_t,
fdflags: wasi::__wasi_fdflags_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_fdstat_set_rights(
wasi_ctx: &mut WasiCtx,
fd: wasi::__wasi_fd_t,
fs_rights_base: wasi::__wasi_rights_t,
fs_rights_inheriting: wasi::__wasi_rights_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_sync(wasi_ctx: &WasiCtx, fd: wasi::__wasi_fd_t,) -> wasi::__wasi_errno_t;
pub unsafe fn fd_write(
wasi_ctx: &mut WasiCtx,
memory: &mut [u8],
fd: wasi::__wasi_fd_t,
iovs_ptr: wasi32::uintptr_t,
iovs_len: wasi32::size_t,
nwritten: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_advise(
wasi_ctx: &WasiCtx,
fd: wasi::__wasi_fd_t,
offset: wasi::__wasi_filesize_t,
len: wasi::__wasi_filesize_t,
advice: wasi::__wasi_advice_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_allocate(
wasi_ctx: &WasiCtx,
fd: wasi::__wasi_fd_t,
offset: wasi::__wasi_filesize_t,
len: wasi::__wasi_filesize_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn path_create_directory(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
dirfd: wasi::__wasi_fd_t,
path_ptr: wasi32::uintptr_t,
path_len: wasi32::size_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn path_link(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
old_dirfd: wasi::__wasi_fd_t,
old_flags: wasi::__wasi_lookupflags_t,
old_path_ptr: wasi32::uintptr_t,
old_path_len: wasi32::size_t,
new_dirfd: wasi::__wasi_fd_t,
new_path_ptr: wasi32::uintptr_t,
new_path_len: wasi32::size_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn path_open(
wasi_ctx: &mut WasiCtx,
memory: &mut [u8],
dirfd: wasi::__wasi_fd_t,
dirflags: wasi::__wasi_lookupflags_t,
path_ptr: wasi32::uintptr_t,
path_len: wasi32::size_t,
oflags: wasi::__wasi_oflags_t,
fs_rights_base: wasi::__wasi_rights_t,
fs_rights_inheriting: wasi::__wasi_rights_t,
fs_flags: wasi::__wasi_fdflags_t,
fd_out_ptr: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_readdir(
wasi_ctx: &mut WasiCtx,
memory: &mut [u8],
fd: wasi::__wasi_fd_t,
buf: wasi32::uintptr_t,
buf_len: wasi32::size_t,
cookie: wasi::__wasi_dircookie_t,
buf_used: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn path_readlink(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
dirfd: wasi::__wasi_fd_t,
path_ptr: wasi32::uintptr_t,
path_len: wasi32::size_t,
buf_ptr: wasi32::uintptr_t,
buf_len: wasi32::size_t,
buf_used: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn path_rename(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
old_dirfd: wasi::__wasi_fd_t,
old_path_ptr: wasi32::uintptr_t,
old_path_len: wasi32::size_t,
new_dirfd: wasi::__wasi_fd_t,
new_path_ptr: wasi32::uintptr_t,
new_path_len: wasi32::size_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_filestat_get(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
fd: wasi::__wasi_fd_t,
filestat_ptr: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_filestat_set_times(
wasi_ctx: &WasiCtx,
fd: wasi::__wasi_fd_t,
st_atim: wasi::__wasi_timestamp_t,
st_mtim: wasi::__wasi_timestamp_t,
fst_flags: wasi::__wasi_fstflags_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_filestat_set_size(
wasi_ctx: &WasiCtx,
fd: wasi::__wasi_fd_t,
st_size: wasi::__wasi_filesize_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn path_filestat_get(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
dirfd: wasi::__wasi_fd_t,
dirflags: wasi::__wasi_lookupflags_t,
path_ptr: wasi32::uintptr_t,
path_len: wasi32::size_t,
filestat_ptr: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn path_filestat_set_times(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
dirfd: wasi::__wasi_fd_t,
dirflags: wasi::__wasi_lookupflags_t,
path_ptr: wasi32::uintptr_t,
path_len: wasi32::size_t,
st_atim: wasi::__wasi_timestamp_t,
st_mtim: wasi::__wasi_timestamp_t,
fst_flags: wasi::__wasi_fstflags_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn path_symlink(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
old_path_ptr: wasi32::uintptr_t,
old_path_len: wasi32::size_t,
dirfd: wasi::__wasi_fd_t,
new_path_ptr: wasi32::uintptr_t,
new_path_len: wasi32::size_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn path_unlink_file(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
dirfd: wasi::__wasi_fd_t,
path_ptr: wasi32::uintptr_t,
path_len: wasi32::size_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn path_remove_directory(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
dirfd: wasi::__wasi_fd_t,
path_ptr: wasi32::uintptr_t,
path_len: wasi32::size_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_prestat_get(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
fd: wasi::__wasi_fd_t,
prestat_ptr: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_prestat_dir_name(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
fd: wasi::__wasi_fd_t,
path_ptr: wasi32::uintptr_t,
path_len: wasi32::size_t,
) -> wasi::__wasi_errno_t;
}

View File

@@ -1,82 +0,0 @@
#![allow(non_camel_case_types)]
use crate::old::snapshot_0::ctx::WasiCtx;
use crate::old::snapshot_0::{wasi, wasi32};
use log::trace;
use wasi_common_cbindgen::wasi_common_cbindgen_old;
#[wasi_common_cbindgen_old]
pub unsafe fn proc_exit(rval: wasi::__wasi_exitcode_t) {
trace!("proc_exit(rval={:?})", rval);
// TODO: Rather than call std::process::exit here, we should trigger a
// stack unwind similar to a trap.
std::process::exit(rval as i32);
}
#[wasi_common_cbindgen_old]
pub unsafe fn proc_raise(
_wasi_ctx: &WasiCtx,
_memory: &mut [u8],
_sig: wasi::__wasi_signal_t,
) -> wasi::__wasi_errno_t {
unimplemented!("proc_raise")
}
hostcalls_old! {
pub unsafe fn args_get(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
argv_ptr: wasi32::uintptr_t,
argv_buf: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn args_sizes_get(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
argc_ptr: wasi32::uintptr_t,
argv_buf_size_ptr: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn environ_get(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
environ_ptr: wasi32::uintptr_t,
environ_buf: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn environ_sizes_get(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
environ_count_ptr: wasi32::uintptr_t,
environ_size_ptr: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn random_get(
memory: &mut [u8],
buf_ptr: wasi32::uintptr_t,
buf_len: wasi32::size_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn clock_res_get(
memory: &mut [u8],
clock_id: wasi::__wasi_clockid_t,
resolution_ptr: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn clock_time_get(
memory: &mut [u8],
clock_id: wasi::__wasi_clockid_t,
precision: wasi::__wasi_timestamp_t,
time_ptr: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn poll_oneoff(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
input: wasi32::uintptr_t,
output: wasi32::uintptr_t,
nsubscriptions: wasi32::size_t,
nevents: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn sched_yield() -> wasi::__wasi_errno_t;
}

View File

@@ -1,7 +0,0 @@
mod fs;
mod misc;
mod sock;
pub use self::fs::*;
pub use self::misc::*;
pub use self::sock::*;

View File

@@ -1,43 +0,0 @@
#![allow(non_camel_case_types)]
#![allow(unused_unsafe)]
#![allow(unused)]
use crate::old::snapshot_0::ctx::WasiCtx;
use crate::old::snapshot_0::{wasi, wasi32};
use wasi_common_cbindgen::wasi_common_cbindgen_old;
#[wasi_common_cbindgen_old]
pub unsafe fn sock_recv(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
sock: wasi::__wasi_fd_t,
ri_data: wasi32::uintptr_t,
ri_data_len: wasi32::size_t,
ri_flags: wasi::__wasi_riflags_t,
ro_datalen: wasi32::uintptr_t,
ro_flags: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t {
unimplemented!("sock_recv")
}
#[wasi_common_cbindgen_old]
pub unsafe fn sock_send(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
sock: wasi::__wasi_fd_t,
si_data: wasi32::uintptr_t,
si_data_len: wasi32::size_t,
si_flags: wasi::__wasi_siflags_t,
so_datalen: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t {
unimplemented!("sock_send")
}
#[wasi_common_cbindgen_old]
pub unsafe fn sock_shutdown(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
sock: wasi::__wasi_fd_t,
how: wasi::__wasi_sdflags_t,
) -> wasi::__wasi_errno_t {
unimplemented!("sock_shutdown")
}

View File

@@ -16,7 +16,11 @@ use std::io::{self, Read, Seek, SeekFrom, Write};
use std::ops::DerefMut;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
pub(crate) unsafe fn fd_close(wasi_ctx: &mut WasiCtx, fd: wasi::__wasi_fd_t) -> Result<()> {
pub(crate) unsafe fn fd_close(
wasi_ctx: &mut WasiCtx,
_mem: &mut [u8],
fd: wasi::__wasi_fd_t,
) -> Result<()> {
trace!("fd_close(fd={:?})", fd);
if let Ok(fe) = wasi_ctx.get_fd_entry(fd) {
@@ -30,7 +34,11 @@ pub(crate) unsafe fn fd_close(wasi_ctx: &mut WasiCtx, fd: wasi::__wasi_fd_t) ->
Ok(())
}
pub(crate) unsafe fn fd_datasync(wasi_ctx: &WasiCtx, fd: wasi::__wasi_fd_t) -> Result<()> {
pub(crate) unsafe fn fd_datasync(
wasi_ctx: &WasiCtx,
_mem: &mut [u8],
fd: wasi::__wasi_fd_t,
) -> Result<()> {
trace!("fd_datasync(fd={:?})", fd);
let fd = wasi_ctx
@@ -175,6 +183,7 @@ pub(crate) unsafe fn fd_read(
pub(crate) unsafe fn fd_renumber(
wasi_ctx: &mut WasiCtx,
_mem: &mut [u8],
from: wasi::__wasi_fd_t,
to: wasi::__wasi_fd_t,
) -> Result<()> {
@@ -291,6 +300,7 @@ pub(crate) unsafe fn fd_fdstat_get(
pub(crate) unsafe fn fd_fdstat_set_flags(
wasi_ctx: &WasiCtx,
_mem: &mut [u8],
fd: wasi::__wasi_fd_t,
fdflags: wasi::__wasi_fdflags_t,
) -> Result<()> {
@@ -306,6 +316,7 @@ pub(crate) unsafe fn fd_fdstat_set_flags(
pub(crate) unsafe fn fd_fdstat_set_rights(
wasi_ctx: &mut WasiCtx,
_mem: &mut [u8],
fd: wasi::__wasi_fd_t,
fs_rights_base: wasi::__wasi_rights_t,
fs_rights_inheriting: wasi::__wasi_rights_t,
@@ -329,7 +340,11 @@ pub(crate) unsafe fn fd_fdstat_set_rights(
Ok(())
}
pub(crate) unsafe fn fd_sync(wasi_ctx: &WasiCtx, fd: wasi::__wasi_fd_t) -> Result<()> {
pub(crate) unsafe fn fd_sync(
wasi_ctx: &WasiCtx,
_mem: &mut [u8],
fd: wasi::__wasi_fd_t,
) -> Result<()> {
trace!("fd_sync(fd={:?})", fd);
let fd = wasi_ctx
@@ -397,6 +412,7 @@ pub(crate) unsafe fn fd_write(
pub(crate) unsafe fn fd_advise(
wasi_ctx: &WasiCtx,
_mem: &mut [u8],
fd: wasi::__wasi_fd_t,
offset: wasi::__wasi_filesize_t,
len: wasi::__wasi_filesize_t,
@@ -420,6 +436,7 @@ pub(crate) unsafe fn fd_advise(
pub(crate) unsafe fn fd_allocate(
wasi_ctx: &WasiCtx,
_mem: &mut [u8],
fd: wasi::__wasi_fd_t,
offset: wasi::__wasi_filesize_t,
len: wasi::__wasi_filesize_t,
@@ -702,6 +719,7 @@ pub(crate) unsafe fn fd_filestat_get(
pub(crate) unsafe fn fd_filestat_set_times(
wasi_ctx: &WasiCtx,
_mem: &mut [u8],
fd: wasi::__wasi_fd_t,
st_atim: wasi::__wasi_timestamp_t,
st_mtim: wasi::__wasi_timestamp_t,
@@ -761,6 +779,7 @@ pub(crate) fn fd_filestat_set_times_impl(
pub(crate) unsafe fn fd_filestat_set_size(
wasi_ctx: &WasiCtx,
_mem: &mut [u8],
fd: wasi::__wasi_fd_t,
st_size: wasi::__wasi_filesize_t,
) -> Result<()> {

View File

@@ -128,6 +128,7 @@ pub(crate) fn environ_sizes_get(
}
pub(crate) fn random_get(
_wasi_ctx: &WasiCtx,
memory: &mut [u8],
buf_ptr: wasi32::uintptr_t,
buf_len: wasi32::size_t,
@@ -143,6 +144,7 @@ pub(crate) fn random_get(
}
pub(crate) fn clock_res_get(
_wasi_ctx: &WasiCtx,
memory: &mut [u8],
clock_id: wasi::__wasi_clockid_t,
resolution_ptr: wasi32::uintptr_t,
@@ -161,6 +163,7 @@ pub(crate) fn clock_res_get(
}
pub(crate) fn clock_time_get(
_wasi_ctx: &WasiCtx,
memory: &mut [u8],
clock_id: wasi::__wasi_clockid_t,
precision: wasi::__wasi_timestamp_t,
@@ -180,7 +183,7 @@ pub(crate) fn clock_time_get(
enc_timestamp_byref(memory, time_ptr, time)
}
pub(crate) fn sched_yield() -> Result<()> {
pub(crate) fn sched_yield(_wasi_ctx: &WasiCtx, _memory: &mut [u8]) -> Result<()> {
trace!("sched_yield()");
std::thread::yield_now();
@@ -314,3 +317,18 @@ pub(crate) struct FdEventData<'a> {
pub(crate) r#type: wasi::__wasi_eventtype_t,
pub(crate) userdata: wasi::__wasi_userdata_t,
}
pub(crate) fn proc_exit(_wasi_ctx: &WasiCtx, _memory: &mut [u8], rval: wasi::__wasi_exitcode_t) {
trace!("proc_exit(rval={:?})", rval);
// TODO: Rather than call std::process::exit here, we should trigger a
// stack unwind similar to a trap.
std::process::exit(rval as i32);
}
pub(crate) fn proc_raise(
_wasi_ctx: &WasiCtx,
_memory: &mut [u8],
_sig: wasi::__wasi_signal_t,
) -> Result<()> {
unimplemented!("proc_raise")
}

View File

@@ -1,7 +1,9 @@
mod fs;
mod fs_helpers;
mod misc;
mod sock;
pub(crate) use self::fs::*;
pub(crate) use self::fs_helpers::PathGet;
pub(crate) use self::misc::*;
pub(crate) use self::sock::*;

View File

@@ -0,0 +1,35 @@
use crate::old::snapshot_0::{wasi, wasi32, Result, WasiCtx};
pub fn sock_recv(
_wasi_ctx: &WasiCtx,
_memory: &mut [u8],
_sock: wasi::__wasi_fd_t,
_ri_data: wasi32::uintptr_t,
_ri_data_len: wasi32::size_t,
_ri_flags: wasi::__wasi_riflags_t,
_ro_datalen: wasi32::uintptr_t,
_ro_flags: wasi32::uintptr_t,
) -> Result<()> {
unimplemented!("sock_recv")
}
pub fn sock_send(
_wasi_ctx: &WasiCtx,
_memory: &mut [u8],
_sock: wasi::__wasi_fd_t,
_si_data: wasi32::uintptr_t,
_si_data_len: wasi32::size_t,
_si_flags: wasi::__wasi_siflags_t,
_so_datalen: wasi32::uintptr_t,
) -> Result<()> {
unimplemented!("sock_send")
}
pub fn sock_shutdown(
_wasi_ctx: &WasiCtx,
_memory: &mut [u8],
_sock: wasi::__wasi_fd_t,
_how: wasi::__wasi_sdflags_t,
) -> Result<()> {
unimplemented!("sock_shutdown")
}

View File

@@ -3,13 +3,16 @@ mod error;
mod fdentry;
mod helpers;
mod host;
pub mod hostcalls;
mod hostcalls_impl;
mod memory;
mod sys;
pub mod wasi;
pub mod wasi32;
pub mod hostcalls {
wig::define_hostcalls!("old/snapshot_0" "wasi_unstable");
}
pub use ctx::{WasiCtx, WasiCtxBuilder};
pub type Error = error::Error;