-
Notifications
You must be signed in to change notification settings - Fork 10
Fcntl_syscall updates #259
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
Conversation
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.
The testing is good. I have a major style comment in the code itself, but much of it looks sound.
src/safeposix/syscalls/fs_calls.rs
Outdated
//instead, I propose using the 'if let' construct to be able to report an error to | ||
//the user instead of simply panicking | ||
//otherwise, file descriptor table entry is stored in 'checkedfd' | ||
if let Ok(checkedfd) = self.get_filedescriptor(fd) { |
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.
So I like the philosophy here, but I think you want to reverse the if statement behavior so that you error out quickly, rather than continually nesting your if statement. This way one can much more easily read the code.
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.
If I write something like "if let Err() = self.get_filedescriptor(fd)", then in the "else", I will have to do pattern matching once again to extract checkedfd, which seems redundant. Should I use match instead?
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.
That would be an option.
I'm not a Rust expert, but code like this looks more "correct" to me. I'm very open to hearing from others, but certainly in Python, C, etc. it is preferred to do it in the style I've written.
src/safeposix/syscalls/fs_calls.rs
Outdated
//currently, O_CLOEXEC is the only defined file descriptor flag, thus only this flag is | ||
//masked when using F_GETFD or F_SETFD | ||
(F_GETFD, ..) => *flags & O_CLOEXEC, | ||
// set the flags but make sure that the flags are valid |
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.
I'm not sure how this achieves your goal. Can you say more?
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.
F_GETFD command should return file descriptor flags only, which means that the returned value should not include access mode flags and file status flags, and because O_CLOEXEC is the only implemented file descriptor as of now, we can get it by bit-wise and'ing. Same logic applies to F_SETFD: because O_CLOEXEC is the only existing file descriptor flag, we need to check only it.
src/safeposix/syscalls/fs_calls.rs
Outdated
0 //TO DO: traditional SIGIO behavior | ||
} | ||
(F_SETOWN, arg) if arg >= 0 => { | ||
0 //this would return the PID if positive and the process group if negative, |
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.
Would it set this or return it? Why do we do nothing?
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.
When I was talking to Yash, he suggested me that I should not focus on things that are not yet implemented, and that is why I left this part with F_SETOWN and F_GETOWN commands untouched. Should I work on them?
src/safeposix/syscalls/fs_calls.rs
Outdated
0 | ||
} | ||
(F_DUPFD, arg) if arg >= 0 => self._dup2_helper(&filedesc_enum, arg, false), | ||
//TO DO: implement. this one is saying get the signals |
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.
Which is saying to get the signals? What signals?
src/safeposix/syscalls/fs_calls.rs
Outdated
_ => syscall_error( | ||
Errno::EINVAL, | ||
"fcntl", | ||
"Arguments provided do not match implemented parameters", |
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.
Can we list anything else useful like the arguments themselves?
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.
Thank you! I will include a (cmd, arg) tuple in the error message.
src/tests/fs_tests.rs
Outdated
assert_eq!(cage.fcntl_syscall(filefd, F_GETFL, 0), 2048); | ||
|
||
//when provided with 'F_GETFD' or 'F_GETFL' command, 'arg' should be ignored, thus even | ||
//negative arg values should produce nomal behavior | ||
//However, testing results in two errors |
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.
Okay, great! So this test will fail now, but after we fix the code later it will pass, right?
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.
This is my bad: everything works now and the test passes. I just forgot to delete this part of the comment.
Please add documentation about the syscall, its arguments, return values and manpage link above the function definition. |
@@ -1898,87 +1898,126 @@ impl Cage { | |||
} | |||
|
|||
//------------------------------------FCNTL SYSCALL------------------------------------ | |||
|
|||
//fcntl performs operations, like returning or setting file status flags, |
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.
Looks great!
@yashaswi2000 , do you want to have the rustdoc changes be done in these edits or a different set of PRs? I do think that for new PRs, we likely should include them...
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.
I will refactor these first 3 PRs. and any new ones coming in will follow the template I will share with them.
src/safeposix/syscalls/fs_calls.rs
Outdated
|
||
if let Some(ins) = &mut sockhandle.innersocket { | ||
let fcntlret; | ||
if arg & O_NONBLOCK == O_NONBLOCK { |
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.
Deep nesting. Is there a way we can avoid this?
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.
No not easily. The rustfmt makes it looks worse than it is.
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.
I think clippy will suggest a way to change this pattern:
match fd_table_entry_ptr {
Err(()) => {
syscall_error(Errno::EBADF, "fcntl", "File descriptor is out of range")
},
Ok(checkedfd) => {
let mut unlocked_fd = checkedfd.write();
...
into an if let Ok(checkedfd) = fd_table_entry_ptr {...}
in at least some cases. Here, it would unfortunately move the error condition to the end, so while this would reduce indentation, it isn't a 100% win...
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.
This seems fine as it for now but may be part of a later refactor.
src/safeposix/syscalls/fs_calls.rs
Outdated
&mut sockfdobj.flags | ||
} | ||
}; | ||
&mut sockfdobj.flags |
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.
remove extra whitespace
…emoved an extra whitespace
… commented out thefailing test
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.
LGTM!
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.
Approved!
Description
Fixes # (issue)
The following changes include more elaborate comments and new unit tests for fcntl_syscall. Moreover, it was proposed to use "if let" construct instead of "unwrap" to get an entry from a file descriptor table to avoid "panic!" when provided with a file descriptor outside of the allowed range.
Type of change
How Has This Been Tested?
To run the tests, we need to run cargo test --lib command inside the safeposix-rust directory.
All the tests are present under this directory: lind_project/src/safeposix-rust/src/tests/fs_tests.rs
ut_lind_fs_fcntl_valid_args()
ut_lind_fs_fcntl_invalid_args()
ut_lind_fs_fcntl_invalid_fd()
ut_lind_fs_fcntl_dup()
Checklist: