-
Notifications
You must be signed in to change notification settings - Fork 385
Add eventfd shim #3650
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add eventfd shim #3650
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 | ||
tiif marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 | ||
tiif marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// value -1. | ||
let size_7_data: [u8; 7] = [1; 7]; | ||
let res = write_bytes(fd, size_7_data); | ||
assert_eq!(res, -1); | ||
tiif marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// 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(); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.