Skip to content

shims: improve Linux statx#4913

Open
bourumir-wyngs wants to merge 20 commits intorust-lang:masterfrom
bourumir-wyngs:bw/statx
Open

shims: improve Linux statx#4913
bourumir-wyngs wants to merge 20 commits intorust-lang:masterfrom
bourumir-wyngs:bw/statx

Conversation

@bourumir-wyngs
Copy link
Copy Markdown
Contributor

@bourumir-wyngs bourumir-wyngs commented Mar 21, 2026

shims/unix: improve Linux statx

- Track more fields in `FileMetadata`.
- Populate these new fields in the `statx` shim and update the returned mask.
- Implement `linux_decode_dev` helper to split `dev_t` using glibc / musl layout (initially, now removed)
- Add an integration test in `tests/pass-dep/libc/libc-fs.rs` and remove
  the FIXME in `foreign_items.rs`.

@rustbot rustbot added the S-waiting-on-review Status: Waiting for a review to complete label Mar 21, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Mar 21, 2026

Thank you for contributing to Miri! A reviewer will take a look at your PR, typically within a week or two.
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

This comment has been minimized.

Copy link
Copy Markdown
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.

Sorry for the slow review!

I left some comments. Let's narrow the scope of the PR a bit and avoid those weird device number hacks. The rest looks like with a bit of cleanup it should work nicely. :)

@rustbot author

View changes since this review

Comment thread src/shims/unix/fs.rs Outdated
Comment on lines +755 to +758
unix => {
mask |= this.eval_libc_u32("STATX_INO")
| this.eval_libc_u32("STATX_NLINK")
| this.eval_libc_u32("STATX_UID")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It seems you are making a salient assumption here that every FileMetadata constructed on a unix host has all these fields. First of all, this took me some time to figure out since you didn't didn't document this assumption anywhere -- please ensure to leave more comments in the code to explain your reasoning.

Secondly, I don't think this is the right approach. Instead it'd be better if the FileMetadata type knew whether all these fields are present. That avoids non-local assumptions that could be easily broken in the future. In fact there's a concurrent PR that's about to land (#4812) that would have caused the assumption to be broken.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Working on this. Thanks for hearing from you!

Comment thread src/shims/unix/fs.rs Outdated
Comment on lines +223 to +236
fn linux_decode_dev(&self, dev: u64) -> (u32, u32) {
// Linux glibc and musl encode major and minor in a non-contiguous layout.
// In hex nybbles, the layout is: MMMM Mmmm mmmM MMmm

let major = ((dev & 0x0000_0000_000f_ff00) >> 8) | ((dev & 0xffff_f000_0000_0000) >> 32);
let minor = (dev & 0x0000_0000_0000_00ff) | ((dev & 0x0000_0fff_fff0_0000) >> 12);

(u32::try_from(major).unwrap(), u32::try_from(minor).unwrap())
}

#[cfg(not(target_os = "linux"))]
fn linux_decode_dev(&self, _dev: u64) -> (u32, u32) {
(0, 0)
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Wow, what a mess...

Unless there's a concrete need, I'd prefer to skip handling device numbers in this PR and focus on things that don't need hacks like this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The source code I used can be found here for glibc and here for musl. Surely we can remove from this PR for now and think about this later. Working on this.

@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 Apr 26, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 26, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@bourumir-wyngs
Copy link
Copy Markdown
Contributor Author

Has conflicts, but no worries, I work on resolving them

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 26, 2026

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rustbot

This comment has been minimized.

@bourumir-wyngs
Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Waiting for a review to complete and removed S-waiting-on-author Status: Waiting for the PR author to address review comments labels Apr 26, 2026
@bourumir-wyngs
Copy link
Copy Markdown
Contributor Author

Let's continue on #4983 that I did on the tip of the recent master branch. This looks for me safer, I messed up too much to undo reliably.

@rustbot rustbot removed the S-waiting-on-review Status: Waiting for a review to complete label Apr 26, 2026
@RalfJung
Copy link
Copy Markdown
Member

RalfJung commented Apr 27, 2026

Continuing in the same PR would make it much easier for me to track what changed since I first looked at the PR. If you make a new PR I have to basically redo the entire review from scratch.

So I'd prefer if you could figure out how to properly rebase this PR. The bot posted some instructions above and the internet has tons of git tutorials as well. Please note that you have to reopen this PR before pushing to the old branch or else github won't let you reopen.

@rustbot rustbot added the S-waiting-on-author Status: Waiting for the PR author to address review comments label Apr 27, 2026
audrius and others added 9 commits April 27, 2026 14:19
- Track more fields in `FileMetadata` (`ino`, `nlink`, `rdev`, `blksize`, `blocks`).
- Populate these new fields in the `statx` shim and update the returned mask.
- Implement `unix_dev_major`/`unix_dev_minor` helpers to split `dev_t` using glibc layout.
- Add an integration test in `tests/pass-dep/libc/libc-fs.rs` and remove
  the FIXME in `foreign_items.rs`.
Address PR feedback by dynamically checking the presence of Unix-specific
metadata fields instead of making unconditional platform assumptions.

- Update `FileMetadata` to wrap fields like `ino`, `nlink`, `uid`, `gid`,
  `rdev`, `blksize`, and `blocks` in `Option`.
- Only advertise `STATX_*` flags in the `statx` syscall if the corresponding
  data is actually present in the `FileMetadata`.
- Provide zero fallbacks for unavailable fields when writing to `stat`
  and `statx` buffers.
@bourumir-wyngs
Copy link
Copy Markdown
Contributor Author

bourumir-wyngs commented Apr 27, 2026

A risk to address is not to touch recently merged change that modifies overlapping things. I added an extra regression test.

@bourumir-wyngs bourumir-wyngs marked this pull request as ready for review April 27, 2026 14:31
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 27, 2026

Thank you for contributing to Miri! A reviewer will take a look at your PR, typically within a week or two.
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 S-waiting-on-review Status: Waiting for a review to complete and removed S-waiting-on-author Status: Waiting for the PR author to address review comments labels Apr 27, 2026
@bourumir-wyngs
Copy link
Copy Markdown
Contributor Author

@rustbot ready

@RalfJung
Copy link
Copy Markdown
Member

Thanks for figuring out how to rescue your old PR. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Waiting for a review to complete

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants