Initial reorg.
This is largely the same as #305, but updated for the current tree.
This commit is contained in:
6
crates/wasi-c/src/host.rs
Normal file
6
crates/wasi-c/src/host.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
#![allow(non_camel_case_types, non_snake_case, dead_code)]
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/wasmtime_ssp.rs"));
|
||||
|
||||
pub type char = ::std::os::raw::c_char;
|
||||
pub type void = ::std::os::raw::c_void;
|
||||
205
crates/wasi-c/src/instantiate.rs
Normal file
205
crates/wasi-c/src/instantiate.rs
Normal file
@@ -0,0 +1,205 @@
|
||||
use crate::host::{
|
||||
argv_environ_init, argv_environ_values, fd_prestats, fd_prestats_init, fd_prestats_insert,
|
||||
fd_table, fd_table_init, fd_table_insert_existing,
|
||||
};
|
||||
use crate::syscalls;
|
||||
use alloc::rc::Rc;
|
||||
use core::cell::RefCell;
|
||||
use cranelift_codegen::ir::types;
|
||||
use cranelift_codegen::{ir, isa};
|
||||
use cranelift_entity::PrimaryMap;
|
||||
use cranelift_wasm::DefinedFuncIndex;
|
||||
use std::collections::HashMap;
|
||||
use std::ffi::CString;
|
||||
use std::fs::File;
|
||||
use std::mem;
|
||||
use std::os::unix::io::AsRawFd;
|
||||
use target_lexicon::HOST;
|
||||
use wasmtime_environ::{translate_signature, Export, Module};
|
||||
use wasmtime_runtime::{Imports, InstanceHandle, InstantiationError, VMFunctionBody};
|
||||
|
||||
pub(crate) struct WASIState {
|
||||
pub curfds: Box<fd_table>,
|
||||
pub prestats: Box<fd_prestats>,
|
||||
pub argv_environ: Box<argv_environ_values>,
|
||||
}
|
||||
|
||||
/// Return an instance implementing the "wasi" interface.
|
||||
pub fn instantiate_wasi_c(
|
||||
prefix: &str,
|
||||
global_exports: Rc<RefCell<HashMap<String, Option<wasmtime_runtime::Export>>>>,
|
||||
preopened_dirs: &[(String, File)],
|
||||
argv: &[String],
|
||||
environ: &[(String, String)],
|
||||
) -> Result<InstanceHandle, InstantiationError> {
|
||||
let pointer_type = types::Type::triple_pointer_type(&HOST);
|
||||
let mut module = Module::new();
|
||||
let mut finished_functions: PrimaryMap<DefinedFuncIndex, *const VMFunctionBody> =
|
||||
PrimaryMap::new();
|
||||
let call_conv = isa::CallConv::triple_default(&HOST);
|
||||
|
||||
macro_rules! signature {
|
||||
($name:ident) => {{
|
||||
let sig = module.signatures.push(translate_signature(
|
||||
ir::Signature {
|
||||
params: syscalls::$name::params()
|
||||
.into_iter()
|
||||
.map(ir::AbiParam::new)
|
||||
.collect(),
|
||||
returns: syscalls::$name::results()
|
||||
.into_iter()
|
||||
.map(ir::AbiParam::new)
|
||||
.collect(),
|
||||
call_conv,
|
||||
},
|
||||
pointer_type,
|
||||
));
|
||||
let func = module.functions.push(sig);
|
||||
module.exports.insert(
|
||||
prefix.to_owned() + stringify!($name),
|
||||
Export::Function(func),
|
||||
);
|
||||
finished_functions.push(syscalls::$name::SHIM as *const VMFunctionBody);
|
||||
}};
|
||||
}
|
||||
|
||||
signature!(args_get);
|
||||
signature!(args_sizes_get);
|
||||
signature!(clock_res_get);
|
||||
signature!(clock_time_get);
|
||||
signature!(environ_get);
|
||||
signature!(environ_sizes_get);
|
||||
signature!(fd_prestat_get);
|
||||
signature!(fd_prestat_dir_name);
|
||||
signature!(fd_close);
|
||||
signature!(fd_datasync);
|
||||
signature!(fd_pread);
|
||||
signature!(fd_pwrite);
|
||||
signature!(fd_read);
|
||||
signature!(fd_renumber);
|
||||
signature!(fd_seek);
|
||||
signature!(fd_tell);
|
||||
signature!(fd_fdstat_get);
|
||||
signature!(fd_fdstat_set_flags);
|
||||
signature!(fd_fdstat_set_rights);
|
||||
signature!(fd_sync);
|
||||
signature!(fd_write);
|
||||
signature!(fd_advise);
|
||||
signature!(fd_allocate);
|
||||
signature!(path_create_directory);
|
||||
signature!(path_link);
|
||||
signature!(path_open);
|
||||
signature!(fd_readdir);
|
||||
signature!(path_readlink);
|
||||
signature!(path_rename);
|
||||
signature!(fd_filestat_get);
|
||||
signature!(fd_filestat_set_times);
|
||||
signature!(fd_filestat_set_size);
|
||||
signature!(path_filestat_get);
|
||||
signature!(path_filestat_set_times);
|
||||
signature!(path_symlink);
|
||||
signature!(path_unlink_file);
|
||||
signature!(path_remove_directory);
|
||||
signature!(poll_oneoff);
|
||||
signature!(proc_exit);
|
||||
signature!(proc_raise);
|
||||
signature!(random_get);
|
||||
signature!(sched_yield);
|
||||
signature!(sock_recv);
|
||||
signature!(sock_send);
|
||||
signature!(sock_shutdown);
|
||||
|
||||
let imports = Imports::none();
|
||||
let data_initializers = Vec::new();
|
||||
let signatures = PrimaryMap::new();
|
||||
let mut curfds = Box::new(unsafe { mem::zeroed::<fd_table>() });
|
||||
let mut prestats = Box::new(unsafe { mem::zeroed::<fd_prestats>() });
|
||||
let mut argv_environ = Box::new(unsafe { mem::zeroed::<argv_environ_values>() });
|
||||
|
||||
unsafe {
|
||||
let argv_environ: &mut argv_environ_values = &mut *argv_environ;
|
||||
let (argv_offsets, argv_buf, environ_offsets, environ_buf) =
|
||||
allocate_argv_environ(argv, environ);
|
||||
argv_environ_init(
|
||||
argv_environ,
|
||||
argv_offsets.as_ptr(),
|
||||
argv_offsets.len(),
|
||||
argv_buf.as_ptr(),
|
||||
argv_buf.len(),
|
||||
environ_offsets.as_ptr(),
|
||||
environ_offsets.len(),
|
||||
environ_buf.as_ptr(),
|
||||
environ_buf.len(),
|
||||
);
|
||||
|
||||
let curfds: *mut fd_table = &mut *curfds;
|
||||
fd_table_init(curfds);
|
||||
|
||||
let prestats: *mut fd_prestats = &mut *prestats;
|
||||
fd_prestats_init(prestats);
|
||||
|
||||
// Prepopulate curfds with stdin, stdout, and stderr file descriptors.
|
||||
assert!(fd_table_insert_existing(curfds, 0, 0));
|
||||
assert!(fd_table_insert_existing(curfds, 1, 1));
|
||||
assert!(fd_table_insert_existing(curfds, 2, 2));
|
||||
|
||||
let mut wasm_fd = 3;
|
||||
for (dir, file) in preopened_dirs {
|
||||
assert!(fd_table_insert_existing(curfds, wasm_fd, file.as_raw_fd()));
|
||||
assert!(fd_prestats_insert(
|
||||
prestats,
|
||||
CString::new(dir.as_str()).unwrap().as_ptr(),
|
||||
wasm_fd,
|
||||
));
|
||||
wasm_fd += 1;
|
||||
}
|
||||
}
|
||||
|
||||
let host_state = WASIState {
|
||||
curfds,
|
||||
prestats,
|
||||
argv_environ,
|
||||
};
|
||||
|
||||
InstanceHandle::new(
|
||||
Rc::new(module),
|
||||
global_exports,
|
||||
finished_functions.into_boxed_slice(),
|
||||
imports,
|
||||
&data_initializers,
|
||||
signatures.into_boxed_slice(),
|
||||
None,
|
||||
Box::new(host_state),
|
||||
)
|
||||
}
|
||||
|
||||
fn allocate_argv_environ(
|
||||
argv: &[String],
|
||||
environ: &[(String, String)],
|
||||
) -> (Vec<usize>, Vec<libc::c_char>, Vec<usize>, Vec<libc::c_char>) {
|
||||
let mut argv_offsets = Vec::new();
|
||||
let mut argv_buf = Vec::new();
|
||||
let mut environ_offsets = Vec::new();
|
||||
let mut environ_buf = Vec::new();
|
||||
|
||||
for arg in argv {
|
||||
argv_offsets.push(argv_buf.len());
|
||||
for c in arg.bytes() {
|
||||
argv_buf.push(c as libc::c_char);
|
||||
}
|
||||
argv_buf.push('\0' as libc::c_char);
|
||||
}
|
||||
for (key, value) in environ {
|
||||
environ_offsets.push(environ_buf.len());
|
||||
for c in key.bytes() {
|
||||
environ_buf.push(c as libc::c_char);
|
||||
}
|
||||
environ_buf.push('=' as libc::c_char);
|
||||
for c in value.bytes() {
|
||||
environ_buf.push(c as libc::c_char);
|
||||
}
|
||||
environ_buf.push('\0' as libc::c_char);
|
||||
}
|
||||
|
||||
(argv_offsets, argv_buf, environ_offsets, environ_buf)
|
||||
}
|
||||
12
crates/wasi-c/src/lib.rs
Normal file
12
crates/wasi-c/src/lib.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
extern crate alloc;
|
||||
extern crate core;
|
||||
|
||||
mod host;
|
||||
mod instantiate;
|
||||
mod syscalls;
|
||||
mod translate;
|
||||
mod wasm32;
|
||||
|
||||
pub use instantiate::instantiate_wasi_c;
|
||||
1556
crates/wasi-c/src/syscalls.rs
Normal file
1556
crates/wasi-c/src/syscalls.rs
Normal file
File diff suppressed because it is too large
Load Diff
576
crates/wasi-c/src/translate.rs
Normal file
576
crates/wasi-c/src/translate.rs
Normal file
@@ -0,0 +1,576 @@
|
||||
use crate::host;
|
||||
use crate::wasm32;
|
||||
use core::convert::TryFrom;
|
||||
use std::mem::{align_of, size_of, zeroed};
|
||||
use std::slice;
|
||||
use wasmtime_runtime::{Export, VMContext};
|
||||
|
||||
/// Translate a wasm pointer into a native pointer.
|
||||
///
|
||||
/// This is unsafe due to trusting the contents of vmctx. The pointer result
|
||||
/// is bounds and alignment checked.
|
||||
unsafe fn decode_ptr(
|
||||
vmctx: &mut VMContext,
|
||||
ptr: wasm32::uintptr_t,
|
||||
len: usize,
|
||||
align: usize,
|
||||
) -> Result<*mut u8, host::__wasi_errno_t> {
|
||||
match vmctx.lookup_global_export("memory") {
|
||||
Some(Export::Memory {
|
||||
definition,
|
||||
vmctx: _,
|
||||
memory: _,
|
||||
}) => {
|
||||
if len > 0 {
|
||||
// Check for overflow within the access.
|
||||
let last = match (ptr as usize).checked_add(len - 1) {
|
||||
Some(sum) => sum,
|
||||
None => {
|
||||
debug!("overflow in decode_ptr");
|
||||
return Err(host::__WASI_EFAULT as host::__wasi_errno_t);
|
||||
}
|
||||
};
|
||||
// Check for out of bounds.
|
||||
if last >= (*definition).current_length {
|
||||
debug!("out of bounds in decode_ptr");
|
||||
return Err(host::__WASI_EFAULT as host::__wasi_errno_t);
|
||||
}
|
||||
}
|
||||
// Check alignment.
|
||||
if (ptr as usize) % align != 0 {
|
||||
debug!("bad alignment in decode_ptr: {} % {}", ptr, align);
|
||||
return Err(host::__WASI_EINVAL as host::__wasi_errno_t);
|
||||
}
|
||||
// Ok, translate the address.
|
||||
Ok((((*definition).base as usize) + (ptr as usize)) as *mut u8)
|
||||
}
|
||||
// No export named "memory", or the export isn't a memory.
|
||||
// FIXME: Is EINVAL the best code here?
|
||||
x => {
|
||||
error!(
|
||||
"no export named \"memory\", or the export isn't a mem: {:?}",
|
||||
x
|
||||
);
|
||||
Err(host::__WASI_EINVAL as host::__wasi_errno_t)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn decode_ptr_to<T>(
|
||||
vmctx: &mut VMContext,
|
||||
ptr: wasm32::uintptr_t,
|
||||
) -> Result<*mut T, host::__wasi_errno_t> {
|
||||
decode_ptr(vmctx, ptr, size_of::<T>(), align_of::<T>()).map(|ptr| ptr as *mut T)
|
||||
}
|
||||
|
||||
unsafe fn decode_pointee<T>(
|
||||
vmctx: &mut VMContext,
|
||||
ptr: wasm32::uintptr_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
// Check bounds and alignment.
|
||||
decode_ptr_to::<T>(vmctx, ptr)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub unsafe fn encode_pointee<T>(vmctx: &mut VMContext, ptr: wasm32::uintptr_t, t: T) {
|
||||
// Bounds and alignment are checked in `decode_pointee`.
|
||||
let ptr = decode_ptr_to::<T>(vmctx, ptr).unwrap();
|
||||
|
||||
ptr.write(t);
|
||||
}
|
||||
|
||||
pub unsafe fn decode_slice_of<T>(
|
||||
vmctx: &mut VMContext,
|
||||
ptr: wasm32::uintptr_t,
|
||||
len: wasm32::size_t,
|
||||
) -> Result<(*mut T, usize), host::__wasi_errno_t> {
|
||||
let len = usize::try_from(len).unwrap();
|
||||
|
||||
let ptr = decode_ptr(
|
||||
vmctx,
|
||||
ptr,
|
||||
size_of::<T>().checked_mul(len).unwrap(),
|
||||
align_of::<T>(),
|
||||
)? as *mut T;
|
||||
|
||||
Ok((ptr, len))
|
||||
}
|
||||
|
||||
pub fn encode_usize(len: usize) -> wasm32::size_t {
|
||||
u32::try_from(len).unwrap()
|
||||
}
|
||||
|
||||
pub fn encode_device(device: host::__wasi_device_t) -> wasm32::__wasi_device_t {
|
||||
device
|
||||
}
|
||||
|
||||
pub fn encode_inode(inode: host::__wasi_inode_t) -> wasm32::__wasi_inode_t {
|
||||
inode
|
||||
}
|
||||
|
||||
pub fn encode_linkcount(linkcount: host::__wasi_linkcount_t) -> wasm32::__wasi_linkcount_t {
|
||||
linkcount
|
||||
}
|
||||
|
||||
pub fn decode_userdata(userdata: wasm32::__wasi_userdata_t) -> host::__wasi_userdata_t {
|
||||
userdata
|
||||
}
|
||||
|
||||
pub fn encode_userdata(userdata: host::__wasi_userdata_t) -> wasm32::__wasi_userdata_t {
|
||||
userdata
|
||||
}
|
||||
|
||||
pub fn decode_eventtype(eventtype: wasm32::__wasi_eventtype_t) -> host::__wasi_eventtype_t {
|
||||
eventtype
|
||||
}
|
||||
|
||||
pub fn encode_eventtype(eventtype: host::__wasi_eventtype_t) -> wasm32::__wasi_eventtype_t {
|
||||
eventtype
|
||||
}
|
||||
|
||||
pub fn decode_filesize(filesize: wasm32::__wasi_filesize_t) -> host::__wasi_filesize_t {
|
||||
filesize
|
||||
}
|
||||
|
||||
pub fn encode_filesize(filesize: host::__wasi_filesize_t) -> wasm32::__wasi_filesize_t {
|
||||
filesize
|
||||
}
|
||||
|
||||
pub fn encode_eventrwflags(
|
||||
eventrwflags: host::__wasi_eventrwflags_t,
|
||||
) -> wasm32::__wasi_eventrwflags_t {
|
||||
eventrwflags
|
||||
}
|
||||
|
||||
pub fn decode_subclockflags(
|
||||
subclockflags: wasm32::__wasi_subclockflags_t,
|
||||
) -> host::__wasi_subclockflags_t {
|
||||
subclockflags
|
||||
}
|
||||
|
||||
pub fn decode_fd(fd: wasm32::__wasi_fd_t) -> host::__wasi_fd_t {
|
||||
fd
|
||||
}
|
||||
|
||||
pub fn decode_filedelta(filedelta: wasm32::__wasi_filedelta_t) -> host::__wasi_filedelta_t {
|
||||
filedelta
|
||||
}
|
||||
|
||||
pub fn decode_whence(whence: wasm32::__wasi_whence_t) -> host::__wasi_whence_t {
|
||||
whence
|
||||
}
|
||||
|
||||
pub fn decode_clockid(clockid: wasm32::__wasi_clockid_t) -> host::__wasi_clockid_t {
|
||||
clockid
|
||||
}
|
||||
|
||||
pub fn decode_timestamp(timestamp: wasm32::__wasi_timestamp_t) -> host::__wasi_timestamp_t {
|
||||
timestamp
|
||||
}
|
||||
|
||||
pub fn encode_timestamp(timestamp: host::__wasi_timestamp_t) -> wasm32::__wasi_timestamp_t {
|
||||
timestamp
|
||||
}
|
||||
|
||||
pub fn decode_exitcode(exitcode: wasm32::__wasi_exitcode_t) -> host::__wasi_exitcode_t {
|
||||
exitcode
|
||||
}
|
||||
|
||||
pub fn decode_lookupflags(lookupflags: wasm32::__wasi_lookupflags_t) -> host::__wasi_lookupflags_t {
|
||||
lookupflags
|
||||
}
|
||||
|
||||
pub fn decode_oflags(oflags: wasm32::__wasi_oflags_t) -> host::__wasi_oflags_t {
|
||||
oflags
|
||||
}
|
||||
|
||||
pub fn decode_advice(advice: wasm32::__wasi_advice_t) -> host::__wasi_advice_t {
|
||||
advice
|
||||
}
|
||||
|
||||
pub fn decode_dircookie(dircookie: wasm32::__wasi_dircookie_t) -> host::__wasi_dircookie_t {
|
||||
dircookie
|
||||
}
|
||||
|
||||
pub fn encode_preopentype(preopentype: host::__wasi_preopentype_t) -> wasm32::__wasi_preopentype_t {
|
||||
preopentype
|
||||
}
|
||||
|
||||
pub fn encode_filetype(filetype: host::__wasi_filetype_t) -> wasm32::__wasi_filetype_t {
|
||||
filetype
|
||||
}
|
||||
|
||||
pub fn decode_fstflags(fstflags: wasm32::__wasi_fstflags_t) -> host::__wasi_fstflags_t {
|
||||
fstflags
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn encode_fstflags(fstflags: host::__wasi_fstflags_t) -> wasm32::__wasi_fstflags_t {
|
||||
fstflags
|
||||
}
|
||||
|
||||
pub fn decode_fdflags(fdflags: wasm32::__wasi_fdflags_t) -> host::__wasi_fdflags_t {
|
||||
fdflags
|
||||
}
|
||||
|
||||
pub fn encode_fdflags(fdflags: host::__wasi_fdflags_t) -> wasm32::__wasi_fdflags_t {
|
||||
fdflags
|
||||
}
|
||||
|
||||
pub fn decode_sdflags(sdflags: wasm32::__wasi_sdflags_t) -> host::__wasi_sdflags_t {
|
||||
sdflags
|
||||
}
|
||||
|
||||
pub fn decode_rights(rights: wasm32::__wasi_rights_t) -> host::__wasi_rights_t {
|
||||
rights
|
||||
}
|
||||
|
||||
pub fn encode_rights(rights: host::__wasi_rights_t) -> wasm32::__wasi_rights_t {
|
||||
rights
|
||||
}
|
||||
|
||||
pub fn decode_riflags(riflags: wasm32::__wasi_riflags_t) -> host::__wasi_riflags_t {
|
||||
riflags
|
||||
}
|
||||
|
||||
pub fn decode_siflags(siflags: wasm32::__wasi_siflags_t) -> host::__wasi_siflags_t {
|
||||
siflags
|
||||
}
|
||||
|
||||
pub unsafe fn decode_char_slice(
|
||||
vmctx: &mut VMContext,
|
||||
ptr: wasm32::uintptr_t,
|
||||
len: wasm32::size_t,
|
||||
) -> Result<(*mut host::char, usize), host::__wasi_errno_t> {
|
||||
decode_slice_of::<wasm32::char>(vmctx, ptr, len)
|
||||
}
|
||||
|
||||
pub unsafe fn decode_charstar_slice(
|
||||
vmctx: &mut VMContext,
|
||||
ptr: wasm32::uintptr_t,
|
||||
count: wasm32::size_t,
|
||||
) -> Result<(*mut wasm32::uintptr_t, usize), host::__wasi_errno_t> {
|
||||
decode_slice_of::<wasm32::uintptr_t>(vmctx, ptr, count)
|
||||
}
|
||||
|
||||
pub unsafe fn encode_charstar_slice(
|
||||
ptr: *mut wasm32::uintptr_t,
|
||||
host_vec: Vec<*mut libc::c_char>,
|
||||
guest_base: wasm32::uintptr_t,
|
||||
host_base: *mut libc::c_char,
|
||||
) {
|
||||
for (i, host) in host_vec.iter().enumerate() {
|
||||
let guest = if host.is_null() {
|
||||
0
|
||||
} else {
|
||||
guest_base + (*host as usize - host_base as usize) as wasm32::uintptr_t
|
||||
};
|
||||
ptr.add(i).write(guest);
|
||||
}
|
||||
}
|
||||
|
||||
pub unsafe fn decode_ciovec(
|
||||
vmctx: &mut VMContext,
|
||||
ciovec: &wasm32::__wasi_ciovec_t,
|
||||
) -> Result<host::__wasi_ciovec_t, host::__wasi_errno_t> {
|
||||
let len = usize::try_from(ciovec.buf_len).unwrap();
|
||||
Ok(host::__wasi_ciovec_t {
|
||||
buf: decode_ptr(vmctx, ciovec.buf, len, 1)? as *const host::void,
|
||||
buf_len: len,
|
||||
})
|
||||
}
|
||||
|
||||
pub unsafe fn decode_iovec(
|
||||
vmctx: &mut VMContext,
|
||||
iovec: &wasm32::__wasi_iovec_t,
|
||||
) -> Result<host::__wasi_iovec_t, host::__wasi_errno_t> {
|
||||
let len = usize::try_from(iovec.buf_len).unwrap();
|
||||
Ok(host::__wasi_iovec_t {
|
||||
buf: decode_ptr(vmctx, iovec.buf, len, 1)? as *mut host::void,
|
||||
buf_len: len,
|
||||
})
|
||||
}
|
||||
|
||||
pub unsafe fn decode_ciovec_slice(
|
||||
vmctx: &mut VMContext,
|
||||
ptr: wasm32::uintptr_t,
|
||||
len: wasm32::size_t,
|
||||
) -> Result<Vec<host::__wasi_ciovec_t>, host::__wasi_errno_t> {
|
||||
let slice = decode_slice_of::<wasm32::__wasi_ciovec_t>(vmctx, ptr, len)?;
|
||||
let slice = slice::from_raw_parts(slice.0, slice.1);
|
||||
slice.iter().map(|iov| decode_ciovec(vmctx, iov)).collect()
|
||||
}
|
||||
|
||||
pub unsafe fn decode_iovec_slice(
|
||||
vmctx: &mut VMContext,
|
||||
ptr: wasm32::uintptr_t,
|
||||
len: wasm32::size_t,
|
||||
) -> Result<Vec<host::__wasi_iovec_t>, host::__wasi_errno_t> {
|
||||
let slice = decode_slice_of::<wasm32::__wasi_iovec_t>(vmctx, ptr, len)?;
|
||||
let slice = slice::from_raw_parts(slice.0, slice.1);
|
||||
slice.iter().map(|iov| decode_iovec(vmctx, iov)).collect()
|
||||
}
|
||||
|
||||
pub fn decode_subscription(
|
||||
guest_subscription: wasm32::__wasi_subscription_t,
|
||||
) -> Result<host::__wasi_subscription_t, host::__wasi_errno_t> {
|
||||
let mut host_subscription = host::__wasi_subscription_t {
|
||||
userdata: decode_userdata(guest_subscription.userdata),
|
||||
type_: decode_eventtype(guest_subscription.type_),
|
||||
u: unsafe { zeroed() },
|
||||
};
|
||||
|
||||
match guest_subscription.type_ {
|
||||
wasm32::__WASI_EVENTTYPE_CLOCK => unsafe {
|
||||
host_subscription.u.clock =
|
||||
host::__wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_clock_t {
|
||||
identifier: decode_userdata(guest_subscription.u.clock.identifier),
|
||||
clock_id: decode_clockid(guest_subscription.u.clock.clock_id),
|
||||
timeout: decode_timestamp(guest_subscription.u.clock.timeout),
|
||||
precision: decode_timestamp(guest_subscription.u.clock.precision),
|
||||
flags: decode_subclockflags(guest_subscription.u.clock.flags),
|
||||
};
|
||||
},
|
||||
wasm32::__WASI_EVENTTYPE_FD_READ | wasm32::__WASI_EVENTTYPE_FD_WRITE => unsafe {
|
||||
host_subscription
|
||||
.u
|
||||
.fd_readwrite =
|
||||
host::__wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_fd_readwrite_t {
|
||||
fd: decode_fd(guest_subscription.u.fd_readwrite.fd),
|
||||
}
|
||||
},
|
||||
_ => return Err(host::__WASI_EINVAL as host::__wasi_errno_t),
|
||||
};
|
||||
|
||||
Ok(host_subscription)
|
||||
}
|
||||
|
||||
pub unsafe fn decode_subscription_slice(
|
||||
vmctx: &mut VMContext,
|
||||
ptr: wasm32::uintptr_t,
|
||||
len: wasm32::size_t,
|
||||
) -> Result<Vec<host::__wasi_subscription_t>, host::__wasi_errno_t> {
|
||||
let slice = decode_slice_of::<wasm32::__wasi_subscription_t>(vmctx, ptr, len)?;
|
||||
let slice = slice::from_raw_parts(slice.0, slice.1);
|
||||
slice
|
||||
.iter()
|
||||
.map(|subscription| decode_subscription(*subscription))
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn encode_event(host_event: host::__wasi_event_t) -> wasm32::__wasi_event_t {
|
||||
let mut guest_event = wasm32::__wasi_event_t {
|
||||
userdata: encode_userdata(host_event.userdata),
|
||||
error: encode_errno(host_event.error),
|
||||
type_: encode_eventtype(host_event.type_),
|
||||
u: unsafe { zeroed() },
|
||||
__bindgen_padding_0: 0,
|
||||
};
|
||||
|
||||
match u32::from(host_event.type_) {
|
||||
host::__WASI_EVENTTYPE_CLOCK => {}
|
||||
host::__WASI_EVENTTYPE_FD_READ | host::__WASI_EVENTTYPE_FD_WRITE => unsafe {
|
||||
guest_event.u.fd_readwrite =
|
||||
wasm32::__wasi_event_t___wasi_event_u___wasi_event_u_fd_readwrite_t {
|
||||
nbytes: encode_filesize(host_event.u.fd_readwrite.nbytes),
|
||||
flags: encode_eventrwflags(host_event.u.fd_readwrite.flags),
|
||||
__bindgen_padding_0: zeroed(),
|
||||
}
|
||||
},
|
||||
_ => panic!("unrecognized event type"),
|
||||
};
|
||||
|
||||
guest_event
|
||||
}
|
||||
|
||||
pub unsafe fn decode_event_slice(
|
||||
vmctx: &mut VMContext,
|
||||
ptr: wasm32::uintptr_t,
|
||||
len: wasm32::size_t,
|
||||
) -> Result<(*mut wasm32::__wasi_event_t, usize), host::__wasi_errno_t> {
|
||||
decode_slice_of::<wasm32::__wasi_event_t>(vmctx, ptr, len)
|
||||
}
|
||||
|
||||
pub unsafe fn encode_event_slice(
|
||||
ptr: *mut wasm32::__wasi_event_t,
|
||||
host_vec: Vec<host::__wasi_event_t>,
|
||||
) {
|
||||
for (i, host) in host_vec.iter().enumerate() {
|
||||
let guest = encode_event(*host);
|
||||
|
||||
ptr.add(i * size_of::<wasm32::__wasi_event_t>())
|
||||
.write(guest);
|
||||
}
|
||||
}
|
||||
|
||||
pub unsafe fn decode_fd_byref(
|
||||
vmctx: &mut VMContext,
|
||||
fd_ptr: wasm32::uintptr_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
decode_pointee::<wasm32::__wasi_fd_t>(vmctx, fd_ptr)
|
||||
}
|
||||
|
||||
pub unsafe fn encode_fd_byref(
|
||||
vmctx: &mut VMContext,
|
||||
fd_ptr: wasm32::uintptr_t,
|
||||
fd: host::__wasi_fd_t,
|
||||
) {
|
||||
encode_pointee::<wasm32::__wasi_fd_t>(vmctx, fd_ptr, wasm32::size_t::try_from(fd).unwrap())
|
||||
}
|
||||
|
||||
pub unsafe fn decode_timestamp_byref(
|
||||
vmctx: &mut VMContext,
|
||||
timestamp_ptr: wasm32::uintptr_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
decode_pointee::<wasm32::__wasi_timestamp_t>(vmctx, timestamp_ptr)
|
||||
}
|
||||
|
||||
pub unsafe fn encode_timestamp_byref(
|
||||
vmctx: &mut VMContext,
|
||||
timestamp_ptr: wasm32::uintptr_t,
|
||||
host_timestamp: host::__wasi_timestamp_t,
|
||||
) {
|
||||
encode_pointee::<wasm32::__wasi_timestamp_t>(
|
||||
vmctx,
|
||||
timestamp_ptr,
|
||||
wasm32::__wasi_timestamp_t::try_from(host_timestamp).unwrap(),
|
||||
)
|
||||
}
|
||||
|
||||
pub unsafe fn decode_filesize_byref(
|
||||
vmctx: &mut VMContext,
|
||||
filesize_ptr: wasm32::uintptr_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
decode_pointee::<wasm32::__wasi_filesize_t>(vmctx, filesize_ptr)
|
||||
}
|
||||
|
||||
pub unsafe fn encode_filesize_byref(
|
||||
vmctx: &mut VMContext,
|
||||
filesize_ptr: wasm32::uintptr_t,
|
||||
host_filesize: host::__wasi_filesize_t,
|
||||
) {
|
||||
encode_pointee::<wasm32::__wasi_filesize_t>(
|
||||
vmctx,
|
||||
filesize_ptr,
|
||||
wasm32::__wasi_filesize_t::try_from(host_filesize).unwrap(),
|
||||
)
|
||||
}
|
||||
|
||||
pub unsafe fn decode_roflags_byref(
|
||||
vmctx: &mut VMContext,
|
||||
roflags_ptr: wasm32::uintptr_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
decode_pointee::<wasm32::__wasi_roflags_t>(vmctx, roflags_ptr)
|
||||
}
|
||||
|
||||
pub unsafe fn encode_roflags_byref(
|
||||
vmctx: &mut VMContext,
|
||||
roflags_ptr: wasm32::uintptr_t,
|
||||
host_roflags: host::__wasi_roflags_t,
|
||||
) {
|
||||
encode_pointee::<wasm32::__wasi_roflags_t>(
|
||||
vmctx,
|
||||
roflags_ptr,
|
||||
wasm32::__wasi_roflags_t::try_from(host_roflags).unwrap(),
|
||||
)
|
||||
}
|
||||
|
||||
pub unsafe fn decode_usize_byref(
|
||||
vmctx: &mut VMContext,
|
||||
usize_ptr: wasm32::uintptr_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
decode_pointee::<wasm32::size_t>(vmctx, usize_ptr)
|
||||
}
|
||||
|
||||
pub unsafe fn encode_usize_byref(
|
||||
vmctx: &mut VMContext,
|
||||
usize_ptr: wasm32::uintptr_t,
|
||||
host_usize: usize,
|
||||
) {
|
||||
encode_pointee::<wasm32::size_t>(
|
||||
vmctx,
|
||||
usize_ptr,
|
||||
wasm32::size_t::try_from(host_usize).unwrap(),
|
||||
)
|
||||
}
|
||||
|
||||
pub unsafe fn decode_prestat_byref(
|
||||
vmctx: &mut VMContext,
|
||||
prestat_ptr: wasm32::uintptr_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
decode_pointee::<wasm32::__wasi_prestat_t>(vmctx, prestat_ptr)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub unsafe fn encode_prestat_byref(
|
||||
vmctx: &mut VMContext,
|
||||
prestat_ptr: wasm32::uintptr_t,
|
||||
host_prestat: host::__wasi_prestat_t,
|
||||
) {
|
||||
let wasm32_prestat = wasm32::__wasi_prestat_t {
|
||||
pr_type: encode_preopentype(host_prestat.pr_type),
|
||||
u: wasm32::__wasi_prestat_t___wasi_prestat_u {
|
||||
dir: wasm32::__wasi_prestat_t___wasi_prestat_u___wasi_prestat_u_dir_t {
|
||||
pr_name_len: encode_usize(host_prestat.u.dir.pr_name_len),
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
encode_pointee::<wasm32::__wasi_prestat_t>(vmctx, prestat_ptr, wasm32_prestat)
|
||||
}
|
||||
|
||||
pub unsafe fn decode_fdstat_byref(
|
||||
vmctx: &mut VMContext,
|
||||
fdstat_ptr: wasm32::uintptr_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
decode_pointee::<wasm32::__wasi_fdstat_t>(vmctx, fdstat_ptr)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub unsafe fn encode_fdstat_byref(
|
||||
vmctx: &mut VMContext,
|
||||
fdstat_ptr: wasm32::uintptr_t,
|
||||
host_fdstat: host::__wasi_fdstat_t,
|
||||
) {
|
||||
let wasm32_fdstat = wasm32::__wasi_fdstat_t {
|
||||
fs_filetype: encode_filetype(host_fdstat.fs_filetype),
|
||||
fs_flags: encode_fdflags(host_fdstat.fs_flags),
|
||||
__bindgen_padding_0: 0,
|
||||
fs_rights_base: encode_rights(host_fdstat.fs_rights_base),
|
||||
fs_rights_inheriting: encode_rights(host_fdstat.fs_rights_inheriting),
|
||||
};
|
||||
|
||||
encode_pointee::<wasm32::__wasi_fdstat_t>(vmctx, fdstat_ptr, wasm32_fdstat)
|
||||
}
|
||||
|
||||
pub unsafe fn decode_filestat_byref(
|
||||
vmctx: &mut VMContext,
|
||||
filestat_ptr: wasm32::uintptr_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
decode_pointee::<wasm32::__wasi_filestat_t>(vmctx, filestat_ptr)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub unsafe fn encode_filestat_byref(
|
||||
vmctx: &mut VMContext,
|
||||
filestat_ptr: wasm32::uintptr_t,
|
||||
host_filestat: host::__wasi_filestat_t,
|
||||
) {
|
||||
let wasm32_filestat = wasm32::__wasi_filestat_t {
|
||||
st_dev: encode_device(host_filestat.st_dev),
|
||||
st_ino: encode_inode(host_filestat.st_ino),
|
||||
st_filetype: encode_filetype(host_filestat.st_filetype),
|
||||
st_nlink: encode_linkcount(host_filestat.st_nlink),
|
||||
st_size: encode_filesize(host_filestat.st_size),
|
||||
st_atim: encode_timestamp(host_filestat.st_atim),
|
||||
st_mtim: encode_timestamp(host_filestat.st_mtim),
|
||||
st_ctim: encode_timestamp(host_filestat.st_ctim),
|
||||
};
|
||||
|
||||
encode_pointee::<wasm32::__wasi_filestat_t>(vmctx, filestat_ptr, wasm32_filestat)
|
||||
}
|
||||
|
||||
pub fn encode_errno(e: host::__wasi_errno_t) -> wasm32::__wasi_errno_t {
|
||||
assert!(e <= wasm32::__WASI_ENOTCAPABLE);
|
||||
e
|
||||
}
|
||||
1443
crates/wasi-c/src/wasm32.rs
Normal file
1443
crates/wasi-c/src/wasm32.rs
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user