diff --git a/lib/wasi/src/syscalls/types.rs b/lib/wasi/src/syscalls/types.rs index 19c825057..378098bcb 100644 --- a/lib/wasi/src/syscalls/types.rs +++ b/lib/wasi/src/syscalls/types.rs @@ -1,8 +1,8 @@ #![allow(non_camel_case_types)] use crate::ptr::{Array, WasmPtr}; -use wasmer_runtime_core::types::{ValueError, ValueType}; use std::mem; +use wasmer_runtime_core::types::{ValueError, ValueType}; pub type __wasi_advice_t = u8; pub const __WASI_ADVICE_DONTNEED: u8 = 0; @@ -178,7 +178,7 @@ impl ValueType for __wasi_prestat_u_dir_t { } fn from_le(buffer: &[u8]) -> Result { - if buffer.len() >= mem::size_of::<__wasi_iovec_t>() { + if buffer.len() >= mem::size_of::<__wasi_prestat_u_dir_t>() { let pr_name_len = ValueType::from_le(&buffer[..mem::size_of::()])?; Ok(Self { pr_name_len }) } else { @@ -195,12 +195,14 @@ pub union __wasi_prestat_u { impl ValueType for __wasi_prestat_u { fn into_le(self, buffer: &mut [u8]) { - unsafe {self.dir - .into_le(&mut buffer[..mem::size_of::<__wasi_prestat_u_dir_t>()])}; + unsafe { + self.dir + .into_le(&mut buffer[..mem::size_of::<__wasi_prestat_u_dir_t>()]) + }; } fn from_le(buffer: &[u8]) -> Result { - if buffer.len() >= mem::size_of::<__wasi_iovec_t>() { + if buffer.len() >= mem::size_of::<__wasi_prestat_u>() { let dir = ValueType::from_le(&buffer[..mem::size_of::<__wasi_prestat_u_dir_t>()])?; Ok(Self { dir }) } else { @@ -225,7 +227,7 @@ impl ValueType for __wasi_prestat_t { } fn from_le(buffer: &[u8]) -> Result { - if buffer.len() >= mem::size_of::<__wasi_iovec_t>() { + if buffer.len() >= mem::size_of::<__wasi_prestat_t>() { let pr_type = ValueType::from_le(&buffer[..mem::size_of::<__wasi_preopentype_t>()])?; let u = ValueType::from_le(&buffer[mem::size_of::<__wasi_preopentype_t>()..])?; Ok(Self { pr_type, u }) @@ -244,6 +246,51 @@ pub struct __wasi_fdstat_t { pub fs_rights_inheriting: __wasi_rights_t, } +impl ValueType for __wasi_fdstat_t { + fn into_le(self, buffer: &mut [u8]) { + self.fs_filetype + .into_le(&mut buffer[..mem::size_of::<__wasi_filetype_t>()]); + self.fs_flags.into_le( + &mut buffer[mem::size_of::<__wasi_filetype_t>()..mem::size_of::<__wasi_fdflags_t>()], + ); + self.fs_rights_base.into_le( + &mut buffer[(mem::size_of::<__wasi_filetype_t>() + mem::size_of::<__wasi_fdflags_t>()) + ..mem::size_of::<__wasi_rights_t>()], + ); + self.fs_rights_inheriting.into_le( + &mut buffer[(mem::size_of::<__wasi_filetype_t>() + + mem::size_of::<__wasi_fdflags_t>() + + mem::size_of::<__wasi_rights_t>())..], + ); + } + + fn from_le(buffer: &[u8]) -> Result { + if buffer.len() >= mem::size_of::<__wasi_fdstat_t>() { + let fs_filetype = ValueType::from_le(&buffer[..mem::size_of::<__wasi_filetype_t>()])?; + let fs_flags = ValueType::from_le( + &buffer[mem::size_of::<__wasi_filetype_t>()..mem::size_of::<__wasi_fdflags_t>()], + )?; + let fs_rights_base = ValueType::from_le( + &buffer[(mem::size_of::<__wasi_filetype_t>() + mem::size_of::<__wasi_fdflags_t>()) + ..mem::size_of::<__wasi_rights_t>()], + )?; + let fs_rights_inheriting = ValueType::from_le( + &buffer[(mem::size_of::<__wasi_filetype_t>() + + mem::size_of::<__wasi_fdflags_t>() + + mem::size_of::<__wasi_rights_t>())..], + )?; + Ok(Self { + fs_filetype, + fs_flags, + fs_rights_base, + fs_rights_inheriting, + }) + } else { + Err(ValueError::BufferTooSmall) + } + } +} + pub type __wasi_filedelta_t = i64; pub type __wasi_filesize_t = u64;