-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #3650 - tiif:feat/eventfd, r=oli-obk
Add eventfd shim Fixes #3445 Design docs: https://hackmd.io/`@tiif/rk9hlmP4R`
- Loading branch information
Showing
6 changed files
with
257 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
//@ignore-target-windows: No eventfd on Windows | ||
//@ignore-target-apple: No eventfd in macos | ||
fn main() { | ||
// eventfd read will block when EFD_NONBLOCK flag is clear and counter = 0. | ||
// This will pass when blocking is implemented. | ||
let flags = libc::EFD_CLOEXEC; | ||
let fd = unsafe { libc::eventfd(0, flags) }; | ||
let mut buf: [u8; 8] = [0; 8]; | ||
let _res: i32 = unsafe { | ||
libc::read(fd, buf.as_mut_ptr().cast(), buf.len() as libc::size_t).try_into().unwrap() //~ERROR: blocking is unsupported | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
error: unsupported operation: eventfd: blocking is unsupported | ||
--> $DIR/libc_eventfd_read_block.rs:LL:CC | ||
| | ||
LL | libc::read(fd, buf.as_mut_ptr().cast(), buf.len() as libc::size_t).try_into().unwrap() | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ eventfd: blocking is unsupported | ||
| | ||
= help: this is likely not a bug in the program; it indicates that the program performed an operation that Miri does not support | ||
= note: BACKTRACE: | ||
= note: inside `main` at $DIR/libc_eventfd_read_block.rs:LL:CC | ||
|
||
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace | ||
|
||
error: aborting due to 1 previous error | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//@ignore-target-windows: No eventfd on Windows | ||
//@ignore-target-apple: No eventfd in macos | ||
fn main() { | ||
// eventfd write will block when EFD_NONBLOCK flag is clear | ||
// and the addition caused counter to exceed u64::MAX - 1. | ||
// This will pass when blocking is implemented. | ||
let flags = libc::EFD_CLOEXEC; | ||
let fd = unsafe { libc::eventfd(0, flags) }; | ||
// Write u64 - 1. | ||
let mut sized_8_data: [u8; 8] = (u64::MAX - 1).to_ne_bytes(); | ||
let res: i64 = unsafe { | ||
libc::write(fd, sized_8_data.as_ptr() as *const libc::c_void, 8).try_into().unwrap() | ||
}; | ||
assert_eq!(res, 8); | ||
|
||
// Write 1. | ||
sized_8_data = 1_u64.to_ne_bytes(); | ||
// Write 1 to the counter. | ||
let _res: i64 = unsafe { | ||
libc::write(fd, sized_8_data.as_ptr() as *const libc::c_void, 8).try_into().unwrap() //~ERROR: blocking is unsupported | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
error: unsupported operation: eventfd: blocking is unsupported | ||
--> $DIR/libc_eventfd_write_block.rs:LL:CC | ||
| | ||
LL | libc::write(fd, sized_8_data.as_ptr() as *const libc::c_void, 8).try_into().unwrap() | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ eventfd: blocking is unsupported | ||
| | ||
= help: this is likely not a bug in the program; it indicates that the program performed an operation that Miri does not support | ||
= note: BACKTRACE: | ||
= note: inside `main` at $DIR/libc_eventfd_write_block.rs:LL:CC | ||
|
||
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace | ||
|
||
error: aborting due to 1 previous error | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
//@ignore-target-windows: No eventfd in windows | ||
//@ignore-target-apple: No eventfd in macos | ||
// test_race depends on a deterministic schedule. | ||
//@compile-flags: -Zmiri-preemption-rate=0 | ||
|
||
use std::thread; | ||
|
||
fn main() { | ||
test_read_write(); | ||
test_race(); | ||
} | ||
|
||
fn read_bytes<const N: usize>(fd: i32, buf: &mut [u8; N]) -> i32 { | ||
let res: i32 = unsafe { libc::read(fd, buf.as_mut_ptr().cast(), N).try_into().unwrap() }; | ||
return res; | ||
} | ||
|
||
fn write_bytes<const N: usize>(fd: i32, data: [u8; N]) -> i32 { | ||
let res: i32 = | ||
unsafe { libc::write(fd, data.as_ptr() as *const libc::c_void, N).try_into().unwrap() }; | ||
return res; | ||
} | ||
|
||
fn test_read_write() { | ||
let flags = libc::EFD_NONBLOCK | libc::EFD_CLOEXEC; | ||
let fd = unsafe { libc::eventfd(0, flags) }; | ||
let sized_8_data: [u8; 8] = 1_u64.to_ne_bytes(); | ||
// Write 1 to the counter. | ||
let res = write_bytes(fd, sized_8_data); | ||
assert_eq!(res, 8); | ||
|
||
// Read 1 from the counter. | ||
let mut buf: [u8; 8] = [0; 8]; | ||
let res = read_bytes(fd, &mut buf); | ||
// Read returns number of bytes has been read, which is always 8. | ||
assert_eq!(res, 8); | ||
// Check the value of counter read. | ||
let counter = u64::from_ne_bytes(buf); | ||
assert_eq!(counter, 1); | ||
|
||
// After read, the counter is currently 0, read counter 0 should fail with return | ||
// value -1. | ||
let mut buf: [u8; 8] = [0; 8]; | ||
let res = read_bytes(fd, &mut buf); | ||
assert_eq!(res, -1); | ||
|
||
// Write with supplied buffer that > 8 bytes should be allowed. | ||
let sized_9_data: [u8; 9]; | ||
if cfg!(target_endian = "big") { | ||
// Adjust the data based on the endianness of host system. | ||
sized_9_data = [0, 0, 0, 0, 0, 0, 0, 1, 0]; | ||
} else { | ||
sized_9_data = [1, 0, 0, 0, 0, 0, 0, 0, 0]; | ||
} | ||
let res = write_bytes(fd, sized_9_data); | ||
assert_eq!(res, 8); | ||
|
||
// Read with supplied buffer that < 8 bytes should fail with return | ||
// value -1. | ||
let mut buf: [u8; 7] = [1; 7]; | ||
let res = read_bytes(fd, &mut buf); | ||
assert_eq!(res, -1); | ||
|
||
// Write with supplied buffer that < 8 bytes should fail with return | ||
// value -1. | ||
let size_7_data: [u8; 7] = [1; 7]; | ||
let res = write_bytes(fd, size_7_data); | ||
assert_eq!(res, -1); | ||
|
||
// Read with supplied buffer > 8 bytes should be allowed. | ||
let mut buf: [u8; 9] = [1; 9]; | ||
let res = read_bytes(fd, &mut buf); | ||
assert_eq!(res, 8); | ||
|
||
// Write u64::MAX should fail. | ||
let u64_max_bytes: [u8; 8] = [255; 8]; | ||
let res = write_bytes(fd, u64_max_bytes); | ||
assert_eq!(res, -1); | ||
} | ||
|
||
fn test_race() { | ||
static mut VAL: u8 = 0; | ||
let flags = libc::EFD_NONBLOCK | libc::EFD_CLOEXEC; | ||
let fd = unsafe { libc::eventfd(0, flags) }; | ||
let thread1 = thread::spawn(move || { | ||
let mut buf: [u8; 8] = [0; 8]; | ||
let res = read_bytes(fd, &mut buf); | ||
// read returns number of bytes has been read, which is always 8. | ||
assert_eq!(res, 8); | ||
let counter = u64::from_ne_bytes(buf); | ||
assert_eq!(counter, 1); | ||
unsafe { assert_eq!(VAL, 1) }; | ||
}); | ||
unsafe { VAL = 1 }; | ||
let data: [u8; 8] = 1_u64.to_ne_bytes(); | ||
let res = write_bytes(fd, data); | ||
// write returns number of bytes written, which is always 8. | ||
assert_eq!(res, 8); | ||
thread::yield_now(); | ||
thread1.join().unwrap(); | ||
} |