Skip to content

Commit aea3cfd

Browse files
committed
Auto merge of #3824 - tiif:maxeventeinval, r=RalfJung
Set EINVAL for epoll_wait maxevent value 0 Fixes #3821
2 parents 8821108 + f918de8 commit aea3cfd

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

src/tools/miri/src/shims/unix/linux/epoll.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -402,18 +402,23 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
402402
let this = self.eval_context_mut();
403403

404404
let epfd = this.read_scalar(epfd)?.to_i32()?;
405+
let events = this.read_immediate(events_op)?;
405406
let maxevents = this.read_scalar(maxevents)?.to_i32()?;
406-
let event = this.deref_pointer_as(
407-
events_op,
408-
this.libc_array_ty_layout("epoll_event", maxevents.try_into().unwrap()),
409-
)?;
410407
let timeout = this.read_scalar(timeout)?.to_i32()?;
411408

412-
if epfd <= 0 {
409+
if epfd <= 0 || maxevents <= 0 {
413410
let einval = this.eval_libc("EINVAL");
414411
this.set_last_error(einval)?;
415412
return Ok(Scalar::from_i32(-1));
416413
}
414+
415+
// This needs to come after the maxevents value check, or else maxevents.try_into().unwrap()
416+
// will fail.
417+
let events = this.deref_pointer_as(
418+
&events,
419+
this.libc_array_ty_layout("epoll_event", maxevents.try_into().unwrap()),
420+
)?;
421+
417422
// FIXME: Implement blocking support
418423
if timeout != 0 {
419424
throw_unsup_format!("epoll_wait: timeout value can only be 0");
@@ -429,7 +434,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
429434
let ready_list = epoll_file_description.get_ready_list();
430435
let mut ready_list = ready_list.borrow_mut();
431436
let mut num_of_events: i32 = 0;
432-
let mut array_iter = this.project_array_fields(&event)?;
437+
let mut array_iter = this.project_array_fields(&events)?;
433438

434439
while let Some((epoll_key, epoll_return)) = ready_list.pop_first() {
435440
// If the file description is fully close, the entry for corresponding FdID in the

src/tools/miri/tests/pass-dep/libc/libc-epoll.rs

+14
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ fn main() {
1919
test_epoll_ctl_del();
2020
test_pointer();
2121
test_two_same_fd_in_same_epoll_instance();
22+
test_epoll_wait_maxevent_zero();
2223
}
2324

2425
// Using `as` cast since `EPOLLET` wraps around
@@ -528,3 +529,16 @@ fn test_no_notification_for_unregister_flag() {
528529
let expected_value = u64::try_from(fds[0]).unwrap();
529530
check_epoll_wait::<8>(epfd, &[(expected_event, expected_value)]);
530531
}
532+
533+
fn test_epoll_wait_maxevent_zero() {
534+
// Create an epoll instance.
535+
let epfd = unsafe { libc::epoll_create1(0) };
536+
assert_ne!(epfd, -1);
537+
// It is ok to use uninitialised pointer here because it will error out before the
538+
// pointer actually get accessed.
539+
let array_ptr = MaybeUninit::<libc::epoll_event>::uninit().as_mut_ptr();
540+
let res = unsafe { libc::epoll_wait(epfd, array_ptr, 0, 0) };
541+
let e = std::io::Error::last_os_error();
542+
assert_eq!(e.raw_os_error(), Some(libc::EINVAL));
543+
assert_eq!(res, -1);
544+
}

0 commit comments

Comments
 (0)