Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return EINVAL in poll_oneoff with no events. #838

Merged
merged 2 commits into from
Jan 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions crates/test-programs/wasi-tests/src/bin/poll_oneoff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ unsafe fn poll_oneoff_impl(r#in: &[wasi::Subscription], nexpected: usize) -> Vec
out
}

unsafe fn test_empty_poll() {
let r#in = [];
let mut out: Vec<wasi::Event> = Vec::new();
let error = wasi::poll_oneoff(r#in.as_ptr(), out.as_mut_ptr(), r#in.len())
.expect_err("empty poll_oneoff should fail");
assert_eq!(
error.raw_error(),
wasi::ERRNO_INVAL,
"error should be EINVAL"
);
}

unsafe fn test_timeout() {
let clock = wasi::SubscriptionClock {
id: wasi::CLOCKID_MONOTONIC,
Expand Down Expand Up @@ -220,6 +232,7 @@ unsafe fn test_fd_readwrite_invalid_fd() {

unsafe fn test_poll_oneoff(dir_fd: wasi::Fd) {
test_timeout();
test_empty_poll();
// NB we assume that stdin/stdout/stderr are valid and open
// for the duration of the test case
test_stdin_read();
Expand Down
9 changes: 9 additions & 0 deletions crates/wasi-common/src/hostcalls_impl/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,12 @@ pub(crate) fn poll_oneoff(

let mut timeout: Option<ClockEventData> = None;
let mut fd_events = Vec::new();

// As mandated by the WASI spec:
// > If `nsubscriptions` is 0, returns `errno::inval`.
if subscriptions.is_empty() {
return Err(Error::EINVAL);
}
for subscription in subscriptions {
match subscription.r#type {
wasi::__WASI_EVENTTYPE_CLOCK => {
Expand Down Expand Up @@ -280,6 +286,9 @@ pub(crate) fn poll_oneoff(
log::debug!("poll_oneoff timeout = {:?}", timeout);
log::debug!("poll_oneoff fd_events = {:?}", fd_events);

// The underlying implementation should successfully and immediately return
// if no events have been passed. Such situation may occur if all provided
// events have been filtered out as errors in the code above.
hostcalls_impl::poll_oneoff(timeout, fd_events, &mut events)?;

let events_count = u32::try_from(events.len()).map_err(|_| Error::EOVERFLOW)?;
Expand Down