Skip to content

Commit

Permalink
Auto merge of rust-lang#3836 - tiif:einval_ctl, r=oli-obk
Browse files Browse the repository at this point in the history
epoll: Add a EINVAL case

In ``epoll_ctl`` documentation, it is mentioned that:
> EINVAL epfd is not an epoll file descriptor, or fd is the same as epfd, or the requested operation op is not supported by this interface.

So I added this EINVAL case for ``epfd == fd`` in ``epoll_ctl``
  • Loading branch information
bors committed Aug 24, 2024
2 parents 17659eb + 14f22c6 commit 3a9e63c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/tools/miri/src/shims/unix/linux/epoll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,13 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
throw_unsup_format!("epoll_ctl: encountered unknown unsupported operation {:#x}", op);
}

// Throw EINVAL if epfd and fd have the same value.
if epfd_value == fd {
let einval = this.eval_libc("EINVAL");
this.set_last_error(einval)?;
return Ok(Scalar::from_i32(-1));
}

// Check if epfd is a valid epoll file descriptor.
let Some(epfd) = this.machine.fds.get(epfd_value) else {
return Ok(Scalar::from_i32(this.fd_not_found()?));
Expand Down
14 changes: 14 additions & 0 deletions src/tools/miri/tests/pass-dep/libc/libc-epoll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ fn main() {
test_socketpair_epollerr();
test_epoll_lost_events();
test_ready_list_fetching_logic();
test_epoll_ctl_epfd_equal_fd();
}

// Using `as` cast since `EPOLLET` wraps around
Expand Down Expand Up @@ -630,3 +631,16 @@ fn test_ready_list_fetching_logic() {
let expected_value1 = fd1 as u64;
check_epoll_wait::<1>(epfd, &[(expected_event1, expected_value1)]);
}

// In epoll_ctl, if the value of epfd equals to fd, EINVAL should be returned.
fn test_epoll_ctl_epfd_equal_fd() {
// Create an epoll instance.
let epfd = unsafe { libc::epoll_create1(0) };
assert_ne!(epfd, -1);

let array_ptr = std::ptr::without_provenance_mut::<libc::epoll_event>(0x100);
let res = unsafe { libc::epoll_ctl(epfd, libc::EPOLL_CTL_ADD, epfd, array_ptr) };
let e = std::io::Error::last_os_error();
assert_eq!(e.raw_os_error(), Some(libc::EINVAL));
assert_eq!(res, -1);
}

0 comments on commit 3a9e63c

Please sign in to comment.