Skip to content

Commit

Permalink
epoll test: avoid some subtly dangling pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Aug 21, 2024
1 parent 41c65e6 commit fbdc191
Showing 1 changed file with 4 additions and 24 deletions.
28 changes: 4 additions & 24 deletions src/tools/miri/tests/pass-dep/libc/libc-epoll.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//@only-target-linux

#![feature(exposed_provenance)] // Needed for fn test_pointer()
#![feature(strict_provenance)]
use std::convert::TryInto;
use std::mem::MaybeUninit;

fn main() {
test_epoll_socketpair();
Expand All @@ -17,7 +16,6 @@ fn main() {
test_no_notification_for_unregister_flag();
test_epoll_ctl_mod();
test_epoll_ctl_del();
test_pointer();
test_two_same_fd_in_same_epoll_instance();
test_epoll_wait_maxevent_zero();
test_socketpair_epollerr();
Expand Down Expand Up @@ -261,24 +259,6 @@ fn test_epoll_eventfd() {
check_epoll_wait::<8>(epfd, &[(expected_event, expected_value)]);
}

fn test_pointer() {
// Create an epoll instance.
let epfd = unsafe { libc::epoll_create1(0) };
assert_ne!(epfd, -1);

// Create a socketpair instance.
let mut fds = [-1, -1];
let res = unsafe { libc::socketpair(libc::AF_UNIX, libc::SOCK_STREAM, 0, fds.as_mut_ptr()) };
assert_eq!(res, 0);

// Register fd[1] with EPOLLIN|EPOLLOUT|EPOLLET
let data = MaybeUninit::<u64>::uninit().as_ptr();
let mut ev =
libc::epoll_event { events: EPOLL_IN_OUT_ET, u64: data.expose_provenance() as u64 };
let res = unsafe { libc::epoll_ctl(epfd, libc::EPOLL_CTL_ADD, fds[1], &mut ev) };
assert_eq!(res, 0);
}

// When read/write happened on one side of the socketpair, only the other side will be notified.
fn test_epoll_socketpair_both_sides() {
// Create an epoll instance.
Expand Down Expand Up @@ -543,9 +523,9 @@ fn test_epoll_wait_maxevent_zero() {
// Create an epoll instance.
let epfd = unsafe { libc::epoll_create1(0) };
assert_ne!(epfd, -1);
// It is ok to use uninitialised pointer here because it will error out before the
// pointer actually get accessed.
let array_ptr = MaybeUninit::<libc::epoll_event>::uninit().as_mut_ptr();
// It is ok to use a dangling pointer here because it will error out before the
// pointer actually gets accessed.
let array_ptr = std::ptr::without_provenance_mut::<libc::epoll_event>(0x100);
let res = unsafe { libc::epoll_wait(epfd, array_ptr, 0, 0) };
let e = std::io::Error::last_os_error();
assert_eq!(e.raw_os_error(), Some(libc::EINVAL));
Expand Down

0 comments on commit fbdc191

Please sign in to comment.