From 09511078810a88116fad8fcc7a6e84ce182b825b Mon Sep 17 00:00:00 2001 From: tiif Date: Sat, 17 Aug 2024 02:08:31 +0800 Subject: [PATCH] Add epoll EPOLLHUP flag support --- src/tools/miri/src/shims/unix/linux/epoll.rs | 20 +++++++++++++++++-- src/tools/miri/src/shims/unix/socket.rs | 5 +++-- .../miri/tests/pass-dep/libc/libc-epoll.rs | 6 ++++-- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/tools/miri/src/shims/unix/linux/epoll.rs b/src/tools/miri/src/shims/unix/linux/epoll.rs index d529a9b63e6..a47d9fe0826 100644 --- a/src/tools/miri/src/shims/unix/linux/epoll.rs +++ b/src/tools/miri/src/shims/unix/linux/epoll.rs @@ -69,17 +69,21 @@ pub struct EpollReadyEvents { /// Stream socket peer closed connection, or shut down writing /// half of connection. pub epollrdhup: bool, + /// For stream socket, this event merely indicates that the peer + /// closed its end of the channel. + pub epollhup: bool, } impl EpollReadyEvents { pub fn new() -> Self { - EpollReadyEvents { epollin: false, epollout: false, epollrdhup: false } + EpollReadyEvents { epollin: false, epollout: false, epollrdhup: false, epollhup: false } } pub fn get_event_bitmask<'tcx>(&self, ecx: &MiriInterpCx<'tcx>) -> u32 { let epollin = ecx.eval_libc_u32("EPOLLIN"); let epollout = ecx.eval_libc_u32("EPOLLOUT"); let epollrdhup = ecx.eval_libc_u32("EPOLLRDHUP"); + let epollhup = ecx.eval_libc_u32("EPOLLHUP"); let mut bitmask = 0; if self.epollin { @@ -91,6 +95,9 @@ pub fn get_event_bitmask<'tcx>(&self, ecx: &MiriInterpCx<'tcx>) -> u32 { if self.epollrdhup { bitmask |= epollrdhup; } + if self.epollhup { + bitmask |= epollhup; + } bitmask } } @@ -217,6 +224,7 @@ fn epoll_ctl( let epollout = this.eval_libc_u32("EPOLLOUT"); let epollrdhup = this.eval_libc_u32("EPOLLRDHUP"); let epollet = this.eval_libc_u32("EPOLLET"); + let epollhup = this.eval_libc_u32("EPOLLHUP"); // Fail on unsupported operations. if op & epoll_ctl_add != epoll_ctl_add @@ -244,11 +252,16 @@ fn epoll_ctl( if op == epoll_ctl_add || op == epoll_ctl_mod { // Read event bitmask and data from epoll_event passed by caller. - let events = this.read_scalar(&this.project_field(&event, 0)?)?.to_u32()?; + let mut events = this.read_scalar(&this.project_field(&event, 0)?)?.to_u32()?; let data = this.read_scalar(&this.project_field(&event, 1)?)?.to_u64()?; // Unset the flag we support to discover if any unsupported flags are used. let mut flags = events; + // epoll_wait(2) will always wait for epollhup; it is not + // necessary to set it in events when calling epoll_ctl(). + // So we will always set this event type. + events |= epollhup; + if events & epollet != epollet { // We only support edge-triggered notification for now. throw_unsup_format!("epoll_ctl: epollet flag must be included."); @@ -264,6 +277,9 @@ fn epoll_ctl( if flags & epollrdhup == epollrdhup { flags &= !epollrdhup; } + if flags & epollhup == epollhup { + flags &= !epollhup; + } if flags != 0 { throw_unsup_format!( "epoll_ctl: encountered unknown unsupported flags {:#x}", diff --git a/src/tools/miri/src/shims/unix/socket.rs b/src/tools/miri/src/shims/unix/socket.rs index 75d4d0dbbe4..cf3f65f69b2 100644 --- a/src/tools/miri/src/shims/unix/socket.rs +++ b/src/tools/miri/src/shims/unix/socket.rs @@ -49,8 +49,8 @@ fn name(&self) -> &'static str { } fn get_epoll_ready_events<'tcx>(&self) -> InterpResult<'tcx, EpollReadyEvents> { - // We only check the status of EPOLLIN, EPOLLOUT and EPOLLRDHUP flags. If other event flags - // need to be supported in the future, the check should be added here. + // We only check the status of EPOLLIN, EPOLLOUT, EPOLLHUP and EPOLLRDHUP flags. + // If other event flags need to be supported in the future, the check should be added here. let mut epoll_ready_events = EpollReadyEvents::new(); @@ -76,6 +76,7 @@ fn get_epoll_ready_events<'tcx>(&self) -> InterpResult<'tcx, EpollReadyEvents> { // for read and write. epoll_ready_events.epollin = true; epoll_ready_events.epollout = true; + epoll_ready_events.epollhup = true; } Ok(epoll_ready_events) } diff --git a/src/tools/miri/tests/pass-dep/libc/libc-epoll.rs b/src/tools/miri/tests/pass-dep/libc/libc-epoll.rs index 841761e53dd..8d7d559c6aa 100644 --- a/src/tools/miri/tests/pass-dep/libc/libc-epoll.rs +++ b/src/tools/miri/tests/pass-dep/libc/libc-epoll.rs @@ -97,7 +97,8 @@ fn test_epoll_socketpair() { // Check result from epoll_wait. // We expect to get a read, write, HUP notification from the close since closing an FD always unblocks reads and writes on its peer. - let expected_event = u32::try_from(libc::EPOLLRDHUP | libc::EPOLLIN | libc::EPOLLOUT).unwrap(); + let expected_event = + u32::try_from(libc::EPOLLRDHUP | libc::EPOLLIN | libc::EPOLLOUT | libc::EPOLLHUP).unwrap(); let expected_value = u64::try_from(fds[1]).unwrap(); assert!(check_epoll_wait::<8>(epfd, &[(expected_event, expected_value)])); } @@ -141,7 +142,8 @@ fn test_epoll_ctl_mod() { assert_eq!(res, 0); // Check result from epoll_wait. - let expected_event = u32::try_from(libc::EPOLLRDHUP | libc::EPOLLIN | libc::EPOLLOUT).unwrap(); + let expected_event = + u32::try_from(libc::EPOLLRDHUP | libc::EPOLLIN | libc::EPOLLOUT | libc::EPOLLHUP).unwrap(); let expected_value = u64::try_from(fds[1]).unwrap(); assert!(check_epoll_wait::<8>(epfd, &[(expected_event, expected_value)])); }