-
Notifications
You must be signed in to change notification settings - Fork 360
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
Support F_GETFL and F_SETFL for fcntl #4212
Open
tiif
wants to merge
10
commits into
rust-lang:master
Choose a base branch
from
tiif:setfl
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d7f0d20
Add F_SETFL flag
tiif e177d69
Add SETFL
tiif fd668d7
Add basic test for setfl
tiif 26831b0
Add test for threaded setfl
tiif 54b38d4
Add comment to test
tiif 091a151
Fix the blocking test
tiif 5bc4382
Add a FIXME
tiif 62510f7
Shift comment to the right place
tiif a5106f1
Remove spurious change
tiif 1df03f9
macos don't have SOCK_NONBLOCK
tiif 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 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 |
---|---|---|
|
@@ -22,7 +22,7 @@ const MAX_SOCKETPAIR_BUFFER_CAPACITY: usize = 212992; | |
|
||
/// One end of a pair of connected unnamed sockets. | ||
#[derive(Debug)] | ||
struct AnonSocket { | ||
pub struct AnonSocket { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is needed for // We only support F_GETFL for socketpair and pipe.
let anonsocket_fd = fd.downcast::<AnonSocket>().ok_or_else(|| {
err_unsup_format!("fcntl: only socketpair / pipe are supported for F_SETFL")
})?; |
||
/// The buffer we are reading from, or `None` if this is the writing end of a pipe. | ||
/// (In that case, the peer FD will be the reading end of that pipe.) | ||
readbuf: Option<RefCell<Buffer>>, | ||
|
@@ -40,7 +40,7 @@ struct AnonSocket { | |
/// A list of thread ids blocked because the buffer was full. | ||
/// Once another thread reads some bytes, these threads will be unblocked. | ||
blocked_write_tid: RefCell<Vec<ThreadId>>, | ||
is_nonblock: bool, | ||
is_nonblock: Cell<bool>, | ||
} | ||
|
||
#[derive(Debug)] | ||
|
@@ -59,6 +59,14 @@ impl AnonSocket { | |
fn peer_fd(&self) -> &WeakFileDescriptionRef<AnonSocket> { | ||
self.peer_fd.get().unwrap() | ||
} | ||
|
||
pub fn is_nonblock(&self) -> bool { | ||
self.is_nonblock.get() | ||
} | ||
|
||
pub fn set_nonblock(&self) { | ||
self.is_nonblock.set(true); | ||
} | ||
} | ||
|
||
impl FileDescription for AnonSocket { | ||
|
@@ -141,7 +149,7 @@ fn anonsocket_write<'tcx>( | |
// Let's see if we can write. | ||
let available_space = MAX_SOCKETPAIR_BUFFER_CAPACITY.strict_sub(writebuf.borrow().buf.len()); | ||
if available_space == 0 { | ||
if self_ref.is_nonblock { | ||
if self_ref.is_nonblock() { | ||
// Non-blocking socketpair with a full buffer. | ||
return finish.call(ecx, Err(ErrorKind::WouldBlock.into())); | ||
} else { | ||
|
@@ -223,7 +231,7 @@ fn anonsocket_read<'tcx>( | |
// Socketpair with no peer and empty buffer. | ||
// 0 bytes successfully read indicates end-of-file. | ||
return finish.call(ecx, Ok(0)); | ||
} else if self_ref.is_nonblock { | ||
} else if self_ref.is_nonblock() { | ||
// Non-blocking socketpair with writer and empty buffer. | ||
// https://linux.die.net/man/2/read | ||
// EAGAIN or EWOULDBLOCK can be returned for socket, | ||
|
@@ -407,15 +415,15 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { | |
peer_lost_data: Cell::new(false), | ||
blocked_read_tid: RefCell::new(Vec::new()), | ||
blocked_write_tid: RefCell::new(Vec::new()), | ||
is_nonblock: is_sock_nonblock, | ||
is_nonblock: Cell::new(is_sock_nonblock), | ||
}); | ||
let fd1 = fds.new_ref(AnonSocket { | ||
readbuf: Some(RefCell::new(Buffer::new())), | ||
peer_fd: OnceCell::new(), | ||
peer_lost_data: Cell::new(false), | ||
blocked_read_tid: RefCell::new(Vec::new()), | ||
blocked_write_tid: RefCell::new(Vec::new()), | ||
is_nonblock: is_sock_nonblock, | ||
is_nonblock: Cell::new(is_sock_nonblock), | ||
}); | ||
|
||
// Make the file descriptions point to each other. | ||
|
@@ -475,15 +483,15 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { | |
peer_lost_data: Cell::new(false), | ||
blocked_read_tid: RefCell::new(Vec::new()), | ||
blocked_write_tid: RefCell::new(Vec::new()), | ||
is_nonblock, | ||
is_nonblock: Cell::new(is_nonblock), | ||
}); | ||
let fd1 = fds.new_ref(AnonSocket { | ||
readbuf: None, | ||
peer_fd: OnceCell::new(), | ||
peer_lost_data: Cell::new(false), | ||
blocked_read_tid: RefCell::new(Vec::new()), | ||
blocked_write_tid: RefCell::new(Vec::new()), | ||
is_nonblock, | ||
is_nonblock: Cell::new(is_nonblock), | ||
}); | ||
|
||
// Make the file descriptions point to each other. | ||
|
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,21 @@ | ||
//@ignore-target: windows # File handling is not implemented yet | ||
//~^ ERROR: deadlock: the evaluated program deadlocked | ||
//@compile-flags: -Zmiri-disable-isolation -Zmiri-preemption-rate=0 | ||
use std::thread; | ||
|
||
/// If an O_NONBLOCK flag is set while the fd is blocking, that fd will not be woken up. | ||
fn main() { | ||
let mut fds = [-1, -1]; | ||
let res = unsafe { libc::pipe(fds.as_mut_ptr()) }; | ||
assert_eq!(res, 0); | ||
let mut buf: [u8; 5] = [0; 5]; | ||
let _thread1 = thread::spawn(move || { | ||
// Add O_NONBLOCK flag while pipe is still block on read. | ||
let new_flag = libc::O_NONBLOCK; | ||
let res = unsafe { libc::fcntl(fds[0], libc::F_SETFL, new_flag) }; | ||
assert_eq!(res, 0); | ||
}); | ||
// Main thread will block on read. | ||
let _res = unsafe { libc::read(fds[0], buf.as_mut_ptr().cast(), buf.len() as libc::size_t) }; | ||
//~^ ERROR: deadlock: the evaluated program deadlocked | ||
} |
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,19 @@ | ||
error: deadlock: the evaluated program deadlocked | ||
| | ||
= note: the evaluated program deadlocked | ||
= note: (no span available) | ||
= note: BACKTRACE on thread `unnamed-ID`: | ||
|
||
error: deadlock: the evaluated program deadlocked | ||
--> tests/fail-dep/libc/fcntl_fsetfl_while_blocking.rs:LL:CC | ||
| | ||
LL | let _res = unsafe { libc::read(fds[0], buf.as_mut_ptr().cast(), buf.len() as libc::size_t) }; | ||
| ^ the evaluated program deadlocked | ||
| | ||
= note: BACKTRACE: | ||
= note: inside `main` at tests/fail-dep/libc/fcntl_fsetfl_while_blocking.rs:LL:CC | ||
|
||
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace | ||
|
||
error: aborting due to 2 previous errors | ||
|
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From the man page:
There are quite a few file access mode and file creation flags, would prefer to open an issue for this and do it later.