|
| 1 | +//! This ensures that when an epoll_wait wakes up and there are multiple events, |
| 2 | +//! and we only read one of them, we do not synchronize with the other events |
| 3 | +//! and therefore still report a data race for things that need to see the second event |
| 4 | +//! to be considered synchronized. |
| 5 | +//@only-target: linux |
| 6 | +// ensure deterministic schedule |
| 7 | +//@compile-flags: -Zmiri-preemption-rate=0 |
| 8 | + |
| 9 | +use std::convert::TryInto; |
| 10 | +use std::thread; |
| 11 | +use std::thread::spawn; |
| 12 | + |
| 13 | +#[track_caller] |
| 14 | +fn check_epoll_wait<const N: usize>(epfd: i32, expected_notifications: &[(u32, u64)]) { |
| 15 | + let epoll_event = libc::epoll_event { events: 0, u64: 0 }; |
| 16 | + let mut array: [libc::epoll_event; N] = [epoll_event; N]; |
| 17 | + let maxsize = N; |
| 18 | + let array_ptr = array.as_mut_ptr(); |
| 19 | + let res = unsafe { libc::epoll_wait(epfd, array_ptr, maxsize.try_into().unwrap(), 0) }; |
| 20 | + if res < 0 { |
| 21 | + panic!("epoll_wait failed: {}", std::io::Error::last_os_error()); |
| 22 | + } |
| 23 | + assert_eq!( |
| 24 | + res, |
| 25 | + expected_notifications.len().try_into().unwrap(), |
| 26 | + "got wrong number of notifications" |
| 27 | + ); |
| 28 | + let slice = unsafe { std::slice::from_raw_parts(array_ptr, res.try_into().unwrap()) }; |
| 29 | + for (return_event, expected_event) in slice.iter().zip(expected_notifications.iter()) { |
| 30 | + let event = return_event.events; |
| 31 | + let data = return_event.u64; |
| 32 | + assert_eq!(event, expected_event.0, "got wrong events"); |
| 33 | + assert_eq!(data, expected_event.1, "got wrong data"); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +fn main() { |
| 38 | + // Create an epoll instance. |
| 39 | + let epfd = unsafe { libc::epoll_create1(0) }; |
| 40 | + assert_ne!(epfd, -1); |
| 41 | + |
| 42 | + // Create two socketpair instances. |
| 43 | + let mut fds_a = [-1, -1]; |
| 44 | + let res = unsafe { libc::socketpair(libc::AF_UNIX, libc::SOCK_STREAM, 0, fds_a.as_mut_ptr()) }; |
| 45 | + assert_eq!(res, 0); |
| 46 | + |
| 47 | + let mut fds_b = [-1, -1]; |
| 48 | + let res = unsafe { libc::socketpair(libc::AF_UNIX, libc::SOCK_STREAM, 0, fds_b.as_mut_ptr()) }; |
| 49 | + assert_eq!(res, 0); |
| 50 | + |
| 51 | + // Register both pipe read ends. |
| 52 | + let mut ev = libc::epoll_event { |
| 53 | + events: (libc::EPOLLIN | libc::EPOLLET) as _, |
| 54 | + u64: u64::try_from(fds_a[1]).unwrap(), |
| 55 | + }; |
| 56 | + let res = unsafe { libc::epoll_ctl(epfd, libc::EPOLL_CTL_ADD, fds_a[1], &mut ev) }; |
| 57 | + assert_eq!(res, 0); |
| 58 | + |
| 59 | + let mut ev = libc::epoll_event { |
| 60 | + events: (libc::EPOLLIN | libc::EPOLLET) as _, |
| 61 | + u64: u64::try_from(fds_b[1]).unwrap(), |
| 62 | + }; |
| 63 | + let res = unsafe { libc::epoll_ctl(epfd, libc::EPOLL_CTL_ADD, fds_b[1], &mut ev) }; |
| 64 | + assert_eq!(res, 0); |
| 65 | + |
| 66 | + static mut VAL_ONE: u8 = 40; // This one will be read soundly. |
| 67 | + static mut VAL_TWO: u8 = 50; // This one will be read unsoundly. |
| 68 | + let thread1 = spawn(move || { |
| 69 | + unsafe { VAL_ONE = 41 }; |
| 70 | + |
| 71 | + let data = "abcde".as_bytes().as_ptr(); |
| 72 | + let res = unsafe { libc::write(fds_a[0], data as *const libc::c_void, 5) }; |
| 73 | + assert_eq!(res, 5); |
| 74 | + |
| 75 | + unsafe { VAL_TWO = 51 }; |
| 76 | + |
| 77 | + let res = unsafe { libc::write(fds_b[0], data as *const libc::c_void, 5) }; |
| 78 | + assert_eq!(res, 5); |
| 79 | + }); |
| 80 | + thread::yield_now(); |
| 81 | + |
| 82 | + // With room for one event: check result from epoll_wait. |
| 83 | + let expected_event = u32::try_from(libc::EPOLLIN).unwrap(); |
| 84 | + let expected_value = u64::try_from(fds_a[1]).unwrap(); |
| 85 | + check_epoll_wait::<1>(epfd, &[(expected_event, expected_value)]); |
| 86 | + |
| 87 | + // Since we only received one event, we have synchronized with |
| 88 | + // the write to VAL_ONE but not with the one to VAL_TWO. |
| 89 | + unsafe { |
| 90 | + assert_eq!({ VAL_ONE }, 41) // This one is not UB |
| 91 | + }; |
| 92 | + unsafe { |
| 93 | + assert_eq!({ VAL_TWO }, 51) //~ERROR: Data race detected |
| 94 | + }; |
| 95 | + |
| 96 | + thread1.join().unwrap(); |
| 97 | +} |
0 commit comments