Skip to content

Commit

Permalink
Upgrade to witx 0.8.0 with tagged unions (bytecodealliance#921)
Browse files Browse the repository at this point in the history
* witx tagged unions: updates to wig to use new semantics

* wig: emit a `#variant: ()` union variant for empty variants

* wasi-common: translate to use tagged unions

* update to flattened layout of event struct

* wig: generate layout tests, and delete bindgen ones

the bindgen tests became out-of-date with the latest changes to the
representation of unions, and the re-jiggering of various struct
definitions that went along with it.

* wasi: point at master with tagged union PR merged

* fix event struct repr on windows
  • Loading branch information
pchickey committed Feb 20, 2020
1 parent 80b095f commit 4460e56
Show file tree
Hide file tree
Showing 19 changed files with 267 additions and 2,167 deletions.
14 changes: 10 additions & 4 deletions Cargo.lock

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

88 changes: 0 additions & 88 deletions crates/wasi-common/src/host.rs
Expand Up @@ -78,91 +78,3 @@ impl Dirent {
Ok(raw)
}
}

#[cfg(test)]
mod test {
use super::*;
#[test]
fn bindgen_test_layout___wasi_prestat_t() {
assert_eq!(
::std::mem::size_of::<__wasi_prestat_t>(),
16usize,
concat!("Size of: ", stringify!(__wasi_prestat_t))
);
assert_eq!(
::std::mem::align_of::<__wasi_prestat_t>(),
8usize,
concat!("Alignment of ", stringify!(__wasi_prestat_t))
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<__wasi_prestat_t>())).pr_type as *const _ as usize },
0usize,
concat!(
"Offset of field: ",
stringify!(__wasi_prestat_t),
"::",
stringify!(pr_type)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<__wasi_prestat_t>())).u as *const _ as usize },
8usize,
concat!(
"Offset of field: ",
stringify!(__wasi_prestat_t),
"::",
stringify!(u)
)
);
}

#[test]
fn bindgen_test_layout___wasi_prestat_t___wasi_prestat_u___wasi_prestat_u_dir_t() {
assert_eq!(
::std::mem::size_of::<__wasi_prestat_dir_t>(),
8usize,
concat!("Size of: ", stringify!(__wasi_prestat_dir_t))
);
assert_eq!(
::std::mem::align_of::<__wasi_prestat_dir_t>(),
8usize,
concat!("Alignment of ", stringify!(__wasi_prestat_dir_t))
);
assert_eq!(
unsafe {
&(*(::std::ptr::null::<__wasi_prestat_dir_t>())).pr_name_len as *const _ as usize
},
0usize,
concat!(
"Offset of field: ",
stringify!(__wasi_prestat_dir_t),
"::",
stringify!(pr_name_len)
)
);
}

#[test]
fn bindgen_test_layout___wasi_prestat_t___wasi_prestat_u() {
assert_eq!(
::std::mem::size_of::<__wasi_prestat_u_t>(),
8usize,
concat!("Size of: ", stringify!(__wasi_prestat_u))
);
assert_eq!(
::std::mem::align_of::<__wasi_prestat_u_t>(),
8usize,
concat!("Alignment of ", stringify!(__wasi_prestat_u))
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<__wasi_prestat_u_t>())).dir as *const _ as usize },
0usize,
concat!(
"Offset of field: ",
stringify!(__wasi_prestat_u_t),
"::",
stringify!(dir)
)
);
}
}
2 changes: 1 addition & 1 deletion crates/wasi-common/src/hostcalls_impl/fs.rs
Expand Up @@ -1002,7 +1002,7 @@ pub(crate) unsafe fn fd_prestat_get(
memory,
prestat_ptr,
host::__wasi_prestat_t {
pr_type: wasi::__WASI_PREOPENTYPE_DIR,
tag: wasi::__WASI_PREOPENTYPE_DIR,
u: host::__wasi_prestat_u_t {
dir: host::__wasi_prestat_dir_t {
pr_name_len: path.len(),
Expand Down
55 changes: 37 additions & 18 deletions crates/wasi-common/src/hostcalls_impl/misc.rs
Expand Up @@ -225,9 +225,9 @@ pub(crate) fn poll_oneoff(
return Err(Error::EINVAL);
}
for subscription in subscriptions {
match subscription.r#type {
match subscription.u.tag {
wasi::__WASI_EVENTTYPE_CLOCK => {
let clock = unsafe { subscription.u.clock };
let clock = unsafe { subscription.u.u.clock };
let delay = wasi_clock_to_relative_ns_delay(clock)?;

log::debug!("poll_oneoff event.u.clock = {:?}", clock);
Expand All @@ -242,43 +242,62 @@ pub(crate) fn poll_oneoff(
*timeout = current;
}
}
r#type
if r#type == wasi::__WASI_EVENTTYPE_FD_READ
|| r#type == wasi::__WASI_EVENTTYPE_FD_WRITE =>
{
let wasi_fd = unsafe { subscription.u.fd_readwrite.file_descriptor };
let rights = if r#type == wasi::__WASI_EVENTTYPE_FD_READ {
wasi::__WASI_RIGHTS_FD_READ | wasi::__WASI_RIGHTS_POLL_FD_READWRITE
} else {
wasi::__WASI_RIGHTS_FD_WRITE | wasi::__WASI_RIGHTS_POLL_FD_READWRITE
wasi::__WASI_EVENTTYPE_FD_READ => {
let wasi_fd = unsafe { subscription.u.u.fd_read.file_descriptor };
let rights = wasi::__WASI_RIGHTS_FD_READ | wasi::__WASI_RIGHTS_POLL_FD_READWRITE;
match unsafe {
wasi_ctx
.get_fd_entry(wasi_fd)
.and_then(|fe| fe.as_descriptor(rights, 0))
} {
Ok(descriptor) => fd_events.push(FdEventData {
descriptor,
r#type: wasi::__WASI_EVENTTYPE_FD_READ,
userdata: subscription.userdata,
}),
Err(err) => {
let event = wasi::__wasi_event_t {
userdata: subscription.userdata,
error: err.as_wasi_error().as_raw_errno(),
r#type: wasi::__WASI_EVENTTYPE_FD_READ,
fd_readwrite: wasi::__wasi_event_fd_readwrite_t {
nbytes: 0,
flags: 0,
},
};
events.push(event);
}
};
}

wasi::__WASI_EVENTTYPE_FD_WRITE => {
let wasi_fd = unsafe { subscription.u.u.fd_write.file_descriptor };
let rights = wasi::__WASI_RIGHTS_FD_WRITE | wasi::__WASI_RIGHTS_POLL_FD_READWRITE;
match unsafe {
wasi_ctx
.get_fd_entry(wasi_fd)
.and_then(|fe| fe.as_descriptor(rights, 0))
} {
Ok(descriptor) => fd_events.push(FdEventData {
descriptor,
r#type: subscription.r#type,
r#type: wasi::__WASI_EVENTTYPE_FD_WRITE,
userdata: subscription.userdata,
}),
Err(err) => {
let event = wasi::__wasi_event_t {
userdata: subscription.userdata,
r#type,
error: err.as_wasi_error().as_raw_errno(),
u: wasi::__wasi_event_u_t {
fd_readwrite: wasi::__wasi_event_fd_readwrite_t {
nbytes: 0,
flags: 0,
},
r#type: wasi::__WASI_EVENTTYPE_FD_WRITE,
fd_readwrite: wasi::__wasi_event_fd_readwrite_t {
nbytes: 0,
flags: 0,
},
};
events.push(event);
}
};
}

_ => unreachable!(),
}
}
Expand Down
59 changes: 30 additions & 29 deletions crates/wasi-common/src/memory.rs
Expand Up @@ -329,9 +329,9 @@ pub(crate) fn dec_prestat_byref(
) -> Result<host::__wasi_prestat_t> {
let raw = dec_raw_byref::<wasi32::__wasi_prestat_t>(memory, prestat_ptr)?;

match PrimInt::from_le(raw.pr_type) {
match PrimInt::from_le(raw.tag) {
wasi::__WASI_PREOPENTYPE_DIR => Ok(host::__wasi_prestat_t {
pr_type: wasi::__WASI_PREOPENTYPE_DIR,
tag: wasi::__WASI_PREOPENTYPE_DIR,
u: host::__wasi_prestat_u_t {
dir: host::__wasi_prestat_dir_t {
pr_name_len: dec_usize(PrimInt::from_le(unsafe { raw.u.dir.pr_name_len })),
Expand All @@ -347,9 +347,9 @@ pub(crate) fn enc_prestat_byref(
prestat_ptr: wasi32::uintptr_t,
prestat: host::__wasi_prestat_t,
) -> Result<()> {
let raw = match prestat.pr_type {
let raw = match prestat.tag {
wasi::__WASI_PREOPENTYPE_DIR => Ok(wasi32::__wasi_prestat_t {
pr_type: PrimInt::to_le(wasi::__WASI_PREOPENTYPE_DIR),
tag: PrimInt::to_le(wasi::__WASI_PREOPENTYPE_DIR),
u: wasi32::__wasi_prestat_u_t {
dir: wasi32::__wasi_prestat_dir_t {
pr_name_len: enc_usize(unsafe { prestat.u.dir.pr_name_len }),
Expand Down Expand Up @@ -410,10 +410,10 @@ pub(crate) fn dec_subscriptions(
.into_iter()
.map(|raw_subscription| {
let userdata = PrimInt::from_le(raw_subscription.userdata);
let r#type = PrimInt::from_le(raw_subscription.r#type);
let raw_u = raw_subscription.u;
let u = match r#type {
wasi::__WASI_EVENTTYPE_CLOCK => wasi::__wasi_subscription_u_t {
let tag = PrimInt::from_le(raw_subscription.u.tag);
let raw_u = raw_subscription.u.u;
let u = match tag {
wasi::__WASI_EVENTTYPE_CLOCK => wasi::__wasi_subscription_u_u_t {
clock: unsafe {
wasi::__wasi_subscription_clock_t {
id: PrimInt::from_le(raw_u.clock.id),
Expand All @@ -423,21 +423,23 @@ pub(crate) fn dec_subscriptions(
}
},
},
wasi::__WASI_EVENTTYPE_FD_READ | wasi::__WASI_EVENTTYPE_FD_WRITE => {
wasi::__wasi_subscription_u_t {
fd_readwrite: wasi::__wasi_subscription_fd_readwrite_t {
file_descriptor: PrimInt::from_le(unsafe {
raw_u.fd_readwrite.file_descriptor
}),
},
}
}
wasi::__WASI_EVENTTYPE_FD_READ => wasi::__wasi_subscription_u_u_t {
fd_read: wasi::__wasi_subscription_fd_readwrite_t {
file_descriptor: PrimInt::from_le(unsafe { raw_u.fd_read.file_descriptor }),
},
},
wasi::__WASI_EVENTTYPE_FD_WRITE => wasi::__wasi_subscription_u_u_t {
fd_write: wasi::__wasi_subscription_fd_readwrite_t {
file_descriptor: PrimInt::from_le(unsafe {
raw_u.fd_write.file_descriptor
}),
},
},
_ => return Err(Error::EINVAL),
};
Ok(wasi::__wasi_subscription_t {
userdata,
r#type,
u,
u: wasi::__wasi_subscription_u_t { tag, u },
})
})
.collect::<Result<Vec<_>>>()
Expand All @@ -456,17 +458,16 @@ pub(crate) fn enc_events(
*raw_output_iter
.next()
.expect("the number of events cannot exceed the number of subscriptions") = {
let fd_readwrite = unsafe { event.u.fd_readwrite };
let userdata = PrimInt::to_le(event.userdata);
let error = PrimInt::to_le(event.error);
let r#type = PrimInt::to_le(event.r#type);
let flags = PrimInt::to_le(event.fd_readwrite.flags);
let nbytes = PrimInt::to_le(event.fd_readwrite.nbytes);
wasi::__wasi_event_t {
userdata: PrimInt::to_le(event.userdata),
r#type: PrimInt::to_le(event.r#type),
error: PrimInt::to_le(event.error),
u: wasi::__wasi_event_u_t {
fd_readwrite: wasi::__wasi_event_fd_readwrite_t {
nbytes: PrimInt::to_le(fd_readwrite.nbytes),
flags: PrimInt::to_le(fd_readwrite.flags),
},
},
userdata,
error,
r#type,
fd_readwrite: wasi::__wasi_event_fd_readwrite_t { flags, nbytes },
}
};
}
Expand Down

0 comments on commit 4460e56

Please sign in to comment.