Add template for Windows impl

This commit is contained in:
Jakub Konka
2019-05-20 11:04:51 +02:00
committed by Dan Gohman
parent 7605584691
commit c3ff3cf075
20 changed files with 575 additions and 339 deletions

View File

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

View File

@@ -1,195 +0,0 @@
#![allow(non_camel_case_types)]
use crate::ctx::WasiCtx;
use crate::memory::*;
use crate::wasm32;
use crate::sys::hostcalls as hostcalls_impl;
use cast::From as _0;
use wasi_common_cbindgen::wasi_common_cbindgen;
#[wasi_common_cbindgen]
pub fn args_get(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
argv_ptr: wasm32::uintptr_t,
argv_buf: wasm32::uintptr_t,
) -> wasm32::__wasi_errno_t {
let mut argv_buf_offset = 0;
let mut argv = vec![];
for arg in wasi_ctx.args.iter() {
let arg_bytes = arg.as_bytes_with_nul();
let arg_ptr = argv_buf + argv_buf_offset;
if let Err(e) = enc_slice_of(memory, arg_bytes, arg_ptr) {
return enc_errno(e);
}
argv.push(arg_ptr);
argv_buf_offset = if let Some(new_offset) = argv_buf_offset.checked_add(
wasm32::uintptr_t::cast(arg_bytes.len())
.expect("cast overflow would have been caught by `enc_slice_of` above"),
) {
new_offset
} else {
return wasm32::__WASI_EOVERFLOW;
}
}
enc_slice_of(memory, argv.as_slice(), argv_ptr)
.map(|_| wasm32::__WASI_ESUCCESS)
.unwrap_or_else(|e| e)
}
#[wasi_common_cbindgen]
pub fn args_sizes_get(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
argc_ptr: wasm32::uintptr_t,
argv_buf_size_ptr: wasm32::uintptr_t,
) -> wasm32::__wasi_errno_t {
let argc = wasi_ctx.args.len();
let argv_size = wasi_ctx
.args
.iter()
.map(|arg| arg.as_bytes_with_nul().len())
.sum();
if let Err(e) = enc_usize_byref(memory, argc_ptr, argc) {
return enc_errno(e);
}
if let Err(e) = enc_usize_byref(memory, argv_buf_size_ptr, argv_size) {
return enc_errno(e);
}
wasm32::__WASI_ESUCCESS
}
#[wasi_common_cbindgen]
pub fn clock_res_get(
memory: &mut [u8],
clock_id: wasm32::__wasi_clockid_t,
resolution_ptr: wasm32::uintptr_t,
) -> wasm32::__wasi_errno_t {
hostcalls_impl::clock_res_get(memory, clock_id, resolution_ptr)
}
#[wasi_common_cbindgen]
pub fn clock_time_get(
memory: &mut [u8],
clock_id: wasm32::__wasi_clockid_t,
precision: wasm32::__wasi_timestamp_t,
time_ptr: wasm32::uintptr_t,
) -> wasm32::__wasi_errno_t {
hostcalls_impl::clock_time_get(memory, clock_id, precision, time_ptr)
}
#[wasi_common_cbindgen]
pub fn environ_get(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
environ_ptr: wasm32::uintptr_t,
environ_buf: wasm32::uintptr_t,
) -> wasm32::__wasi_errno_t {
let mut environ_buf_offset = 0;
let mut environ = vec![];
for pair in wasi_ctx.env.iter() {
let env_bytes = pair.as_bytes_with_nul();
let env_ptr = environ_buf + environ_buf_offset;
if let Err(e) = enc_slice_of(memory, env_bytes, env_ptr) {
return enc_errno(e);
}
environ.push(env_ptr);
environ_buf_offset = if let Some(new_offset) = environ_buf_offset.checked_add(
wasm32::uintptr_t::cast(env_bytes.len())
.expect("cast overflow would have been caught by `enc_slice_of` above"),
) {
new_offset
} else {
return wasm32::__WASI_EOVERFLOW;
}
}
enc_slice_of(memory, environ.as_slice(), environ_ptr)
.map(|_| wasm32::__WASI_ESUCCESS)
.unwrap_or_else(|e| e)
}
#[wasi_common_cbindgen]
pub fn environ_sizes_get(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
environ_count_ptr: wasm32::uintptr_t,
environ_size_ptr: wasm32::uintptr_t,
) -> wasm32::__wasi_errno_t {
let environ_count = wasi_ctx.env.len();
if let Some(environ_size) = wasi_ctx.env.iter().try_fold(0, |acc: u32, pair| {
acc.checked_add(pair.as_bytes_with_nul().len() as u32)
}) {
if let Err(e) = enc_usize_byref(memory, environ_count_ptr, environ_count) {
return enc_errno(e);
}
if let Err(e) = enc_usize_byref(memory, environ_size_ptr, environ_size as usize) {
return enc_errno(e);
}
wasm32::__WASI_ESUCCESS
} else {
wasm32::__WASI_EOVERFLOW
}
}
#[wasi_common_cbindgen]
pub fn poll_oneoff(
memory: &mut [u8],
input: wasm32::uintptr_t,
output: wasm32::uintptr_t,
nsubscriptions: wasm32::size_t,
nevents: wasm32::uintptr_t,
) -> wasm32::__wasi_errno_t {
hostcalls_impl::poll_oneoff(memory, input, output, nsubscriptions, nevents)
}
#[wasi_common_cbindgen]
pub fn proc_exit(rval: wasm32::__wasi_exitcode_t) -> () {
// TODO: Rather than call std::process::exit here, we should trigger a
// stack unwind similar to a trap.
std::process::exit(dec_exitcode(rval) as i32);
}
#[wasi_common_cbindgen]
pub fn proc_raise(
_wasi_ctx: &WasiCtx,
_memory: &mut [u8],
_sig: wasm32::__wasi_signal_t,
) -> wasm32::__wasi_errno_t {
unimplemented!("proc_raise")
}
#[wasi_common_cbindgen]
pub fn sched_yield() -> wasm32::__wasi_errno_t {
hostcalls_impl::sched_yield()
}
#[wasi_common_cbindgen]
pub fn random_get(
memory: &mut [u8],
buf_ptr: wasm32::uintptr_t,
buf_len: wasm32::size_t,
) -> wasm32::__wasi_errno_t {
use rand::{thread_rng, RngCore};
let buf = match dec_slice_of_mut::<u8>(memory, buf_ptr, buf_len) {
Ok(buf) => buf,
Err(e) => return enc_errno(e),
};
thread_rng().fill_bytes(buf);
return wasm32::__WASI_ESUCCESS;
}

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,61 +0,0 @@
#![allow(non_camel_case_types)]
use crate::ctx::WasiCtx;
use crate::wasm32;
use crate::sys::hostcalls as hostcalls_impl;
use wasi_common_cbindgen::wasi_common_cbindgen;
#[wasi_common_cbindgen]
pub fn sock_recv(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
sock: wasm32::__wasi_fd_t,
ri_data: wasm32::uintptr_t,
ri_data_len: wasm32::size_t,
ri_flags: wasm32::__wasi_riflags_t,
ro_datalen: wasm32::uintptr_t,
ro_flags: wasm32::uintptr_t,
) -> wasm32::__wasi_errno_t {
hostcalls_impl::sock_recv(
wasi_ctx,
memory,
sock,
ri_data,
ri_data_len,
ri_flags,
ro_datalen,
ro_flags,
)
}
#[wasi_common_cbindgen]
pub fn sock_send(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
sock: wasm32::__wasi_fd_t,
si_data: wasm32::uintptr_t,
si_data_len: wasm32::size_t,
si_flags: wasm32::__wasi_siflags_t,
so_datalen: wasm32::uintptr_t,
) -> wasm32::__wasi_errno_t {
hostcalls_impl::sock_send(
wasi_ctx,
memory,
sock,
si_data,
si_data_len,
si_flags,
so_datalen,
)
}
#[wasi_common_cbindgen]
pub fn sock_shutdown(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
sock: wasm32::__wasi_fd_t,
how: wasm32::__wasi_sdflags_t,
) -> wasm32::__wasi_errno_t {
hostcalls_impl::sock_shutdown(wasi_ctx, memory, sock, how)
}