Skip to content

Conversation

@LorrensP-2158466
Copy link
Contributor

@LorrensP-2158466 LorrensP-2158466 commented Nov 4, 2025

should close #4464.

I used this man page for the implementation.
Changes included in this pr:

  • impl posix_fallocate and posix_fallocate64 (same as truncate versions because of libc::off_t)
  • use the impl in unix/foreign_items.
  • tests in pass-dep/libc/libc-fs.rs.

@rustbot
Copy link
Collaborator

rustbot commented Nov 4, 2025

Thank you for contributing to Miri!
Please remember to not force-push to the PR branch except when you need to rebase due to a conflict or when the reviewer asks you for it.

@rustbot rustbot added the S-waiting-on-review Status: Waiting for a review to complete label Nov 4, 2025
}

// only macos doesn't support `posix_fallocate`
"posix_fallocate" if &*this.tcx.sess.target.os != "macos" => {
Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, please check pipe2 for how we make a shim available only on some OSes. Please do not use a match guard.

Copy link
Member

@RalfJung RalfJung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

View changes since this review

Comment on lines +1210 to +1212
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"));
Copy link
Member

Choose a reason for hiding this comment

The 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 eval_libc instead of Scalar::from_i32(eval_libc_i32).

return interp_ok(einval);
}

if file.writable {
Copy link
Member

Choose a reason for hiding this comment

The 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

Err(err) => return this.io_error_to_errnum(err),
};
let new_size = match offset.checked_add(len) {
Some(size) => size.try_into().unwrap(), // We just checked negative `offset` and `len`.
Copy link
Member

Choose a reason for hiding this comment

The 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.

// `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))?;
Copy link
Member

Choose a reason for hiding this comment

The 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?

Ok(metadata) => metadata.len(),
Err(err) => return this.io_error_to_errnum(err),
};
let new_size = match offset.checked_add(len) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC, if this operation overflows we should return EFBIG.

@rustbot rustbot added S-waiting-on-author Status: Waiting for the PR author to address review comments and removed S-waiting-on-review Status: Waiting for a review to complete labels Nov 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: Waiting for the PR author to address review comments

Projects

None yet

Development

Successfully merging this pull request may close these issues.

posix_fallocate not supported

3 participants