-
Notifications
You must be signed in to change notification settings - Fork 402
Implement posix_fallocate with set_len() functionality
#4664
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1197,6 +1197,65 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { | |
| } | ||
| } | ||
|
|
||
| fn posix_fallocate( | ||
| &mut self, | ||
| fd_num: i32, | ||
| offset: i128, | ||
| len: i128, | ||
| ) -> InterpResult<'tcx, Scalar> { | ||
| let this = self.eval_context_mut(); | ||
|
|
||
| // According to the man page of `possix_fallocate`, it returns the error code instead | ||
| // of setting `errno`. | ||
| let ebadf = Scalar::from_i32(this.eval_libc_i32("EBADF")); | ||
| let einval = Scalar::from_i32(this.eval_libc_i32("EINVAL")); | ||
| let enodev = Scalar::from_i32(this.eval_libc_i32("ENODEV")); | ||
|
Comment on lines
+1210
to
+1212
Member
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. I think it's better to just inline these where they are needed; the error path shouldn't take so much space on the happy path. Also, you can use |
||
|
|
||
| // Reject if isolation is enabled. | ||
| if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op { | ||
| this.reject_in_isolation("`posix_fallocate`", reject_with)?; | ||
| // Set error code as "EBADF" (bad fd) | ||
| return interp_ok(ebadf); | ||
| } | ||
|
|
||
| let Some(fd) = this.machine.fds.get(fd_num) else { | ||
| return interp_ok(ebadf); | ||
| }; | ||
|
|
||
| let file = match fd.downcast::<FileHandle>() { | ||
| Some(file_handle) => file_handle, | ||
| // Man page specifies to return ENODEV if `fd` is not a regular file. | ||
| None => return interp_ok(enodev), | ||
| }; | ||
|
|
||
| // EINVAL is returned when: "offset was less than 0, or len was less than or equal to 0". | ||
| if offset < 0 || len <= 0 { | ||
| return interp_ok(einval); | ||
| } | ||
|
|
||
| if file.writable { | ||
|
Member
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. Please make this more like if !file.writable { return error; }to avoid rightwards drift |
||
| let current_size = match file.file.metadata() { | ||
| Ok(metadata) => metadata.len(), | ||
| Err(err) => return this.io_error_to_errnum(err), | ||
| }; | ||
| let new_size = match offset.checked_add(len) { | ||
|
Member
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. IIUC, if this operation overflows we should return EFBIG. |
||
| Some(size) => size.try_into().unwrap(), // We just checked negative `offset` and `len`. | ||
|
Member
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. Please change this code so it is clear what type we are converting this into. Also the comment does not explain why the size would fit into the target type. |
||
| None => return interp_ok(einval), | ||
| }; | ||
| // `posix_fallocate` only specifies increasing the file size. | ||
| if current_size < new_size { | ||
| let result = file.file.set_len(new_size); | ||
| let result = this.try_unwrap_io_result(result.map(|_| 0i32))?; | ||
|
Member
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 can set the errno. Didn't you say the function does not do that? |
||
| interp_ok(Scalar::from_i32(result)) | ||
| } else { | ||
| interp_ok(Scalar::from_i32(0)) | ||
| } | ||
| } else { | ||
| // The file is not writable. | ||
| interp_ok(ebadf) | ||
| } | ||
| } | ||
|
|
||
| fn fsync(&mut self, fd_op: &OpTy<'tcx>) -> InterpResult<'tcx, Scalar> { | ||
| // On macOS, `fsync` (unlike `fcntl(F_FULLFSYNC)`) does not wait for the | ||
| // underlying disk to finish writing. In the interest of host compatibility, | ||
|
|
||
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.
Not sure if this is the correct way, from what I could find online, only macos doesn't implement
posix_fallocate.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 that is the case then the test should also be for all OSes except macos.
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.
Also, please check
pipe2for how we make a shim available only on some OSes. Please do not use a match guard.