|
| 1 | +//@only-target: linux |
| 2 | +// test_epoll_race depends on a deterministic schedule. |
| 3 | +//@compile-flags: -Zmiri-preemption-rate=0 |
| 4 | + |
| 5 | +use std::convert::TryInto; |
| 6 | +use std::thread; |
| 7 | + |
| 8 | +fnmain(){ |
| 9 | +test_epoll_race(); |
| 10 | +} |
| 11 | + |
| 12 | +// Using `as` cast since `EPOLLET` wraps around |
| 13 | +constEPOLL_IN_OUT_ET:u32 = (libc::EPOLLIN | libc::EPOLLOUT | libc::EPOLLET)as_; |
| 14 | + |
| 15 | +#[track_caller] |
| 16 | +fncheck_epoll_wait<constN:usize>( |
| 17 | +epfd:i32, |
| 18 | +expected_notifications:&[(u32,u64)], |
| 19 | +timeout:i32, |
| 20 | +){ |
| 21 | +let epoll_event = libc::epoll_event{events:0,u64:0}; |
| 22 | +letmut array:[libc::epoll_event;N] = [epoll_event;N]; |
| 23 | +let maxsize = N; |
| 24 | +let array_ptr = array.as_mut_ptr(); |
| 25 | +let res = unsafe{ libc::epoll_wait(epfd, array_ptr, maxsize.try_into().unwrap(), timeout)}; |
| 26 | +if res < 0{ |
| 27 | +panic!("epoll_wait failed: {}", std::io::Error::last_os_error()); |
| 28 | +} |
| 29 | +assert_eq!( |
| 30 | + res, |
| 31 | + expected_notifications.len().try_into().unwrap(), |
| 32 | +"got wrong number of notifications" |
| 33 | +); |
| 34 | +let slice = unsafe{ std::slice::from_raw_parts(array_ptr, res.try_into().unwrap())}; |
| 35 | +for(return_event, expected_event)in slice.iter().zip(expected_notifications.iter()){ |
| 36 | +let event = return_event.events; |
| 37 | +let data = return_event.u64; |
| 38 | +assert_eq!(event, expected_event.0,"got wrong events"); |
| 39 | +assert_eq!(data, expected_event.1,"got wrong data"); |
| 40 | +} |
| 41 | +} |
| 42 | + |
| 43 | +// This test shows a data_race before epoll had vector clocks added. |
| 44 | +fntest_epoll_race(){ |
| 45 | +// Create an epoll instance. |
| 46 | +let epfd = unsafe{ libc::epoll_create1(0)}; |
| 47 | +assert_ne!(epfd, -1); |
| 48 | + |
| 49 | +// Create an eventfd instance. |
| 50 | +let flags = libc::EFD_NONBLOCK | libc::EFD_CLOEXEC; |
| 51 | +let fd = unsafe{ libc::eventfd(0, flags)}; |
| 52 | + |
| 53 | +// Register eventfd with the epoll instance. |
| 54 | +letmut ev = libc::epoll_event{events:EPOLL_IN_OUT_ET,u64: fd asu64}; |
| 55 | +let res = unsafe{ libc::epoll_ctl(epfd, libc::EPOLL_CTL_ADD, fd,&mut ev)}; |
| 56 | +assert_eq!(res,0); |
| 57 | + |
| 58 | +staticmutVAL:u8 = 0; |
| 59 | +let thread1 = thread::spawn(move || { |
| 60 | +// Write to the static mut variable. |
| 61 | +unsafe{VAL = 1}; |
| 62 | +// Write to the eventfd instance. |
| 63 | +let sized_8_data:[u8;8] = 1_u64.to_ne_bytes(); |
| 64 | +let res = unsafe{ libc::write(fd, sized_8_data.as_ptr()as*const libc::c_void,8)}; |
| 65 | +// read returns number of bytes that have been read, which is always 8. |
| 66 | +assert_eq!(res,8); |
| 67 | +}); |
| 68 | + thread::yield_now(); |
| 69 | +// epoll_wait for the event to happen. |
| 70 | +let expected_event = u32::try_from(libc::EPOLLIN | libc::EPOLLOUT).unwrap(); |
| 71 | +let expected_value = u64::try_from(fd).unwrap(); |
| 72 | +check_epoll_wait::<8>(epfd,&[(expected_event, expected_value)], -1); |
| 73 | +// Read from the static mut variable. |
| 74 | +#[allow(static_mut_refs)] |
| 75 | +unsafe{ |
| 76 | +assert_eq!(VAL,1)//~ ERROR: Data race detected |
| 77 | +}; |
| 78 | + thread1.join().unwrap(); |
| 79 | +} |
0 commit comments