Skip to content

Implement file read/write/seek in Windows #4275

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

Open
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

CraftSpider
Copy link
Contributor

I can split seek off if desired, though that means I can't enable the whole file read/write shim test in fs.rs in one go

@CraftSpider
Copy link
Contributor Author

@rustbot ready

@rustbot rustbot added the S-waiting-on-review Status: Waiting for a review to complete label Apr 20, 2025
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.

Looks great overall, thanks! Just some minor comments. Nice to see that the blocking read/write API works for the Windows functions as well.

@rustbot author

@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 29, 2025
@rustbot
Copy link
Collaborator

rustbot commented Apr 29, 2025

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

@RalfJung
Copy link
Member

@CraftSpider what's the status of this? A standard library test started using NtReadFile directly, and I think this PR would let us run that test (rather than having to disable it in Miri which I will do now). :)

@RalfJung
Copy link
Member

RalfJung commented May 18, 2025

There's a FIXME added in rust-lang/miri-test-libstd#96 that we can hopefully remove when this lands.

@CraftSpider
Copy link
Contributor Author

I'll try to get this updated soon, I've just been doing other stuff - hopefully later today or tomorrow.

@rustbot

This comment has been minimized.

@CraftSpider
Copy link
Contributor Author

(modulo tests passing)
@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 May 19, 2025
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.

Actually it seems there's one pretty big issue, see the comments regarding when the callback is called...
@rustbot author

Comment on lines +492 to +494
// Return whether this was a success. 0 is a success.
// We wrote the return status into the IO_STATUS_BLOCK in the above callback,
// just pull it from there.
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't work -- the entire reason that that's a callback is that desc.write may block the current thread, return immediately, and run the callback later!

This function needs to take a dest argument to capture the return place, pass that into the callback, and then write the return value that way.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This took me a moment to get - for future reference when I'm fixing this: if the callback doesn't happen immediately, we will return the (possibly uninit) value that is in the IO_STATUS_BLOCK we were passed, then before the user code resumes, the callback will happen, write the value into IO_STATUS_BLOCK, then resume execution. We have to pass the return place into the callback to avoid that problem.

This is fairly obvious in retrospect, but it wasn't clear to me until I reread your comment a couple times.

Comment on lines 590 to 591
// See NtWriteFile for commentary on this
interp_ok(this.read_scalar(&io_status)?)
Copy link
Member

Choose a reason for hiding this comment

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

Same here regarding the callback being potentially invoked later.

@@ -230,19 +230,11 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
let stderr = this.eval_windows("c", "STD_ERROR_HANDLE").to_i32()?;

let fd_num = if which == stdin {
Copy link
Member

Choose a reason for hiding this comment

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

Please add a comment saying where those numbers come from. AFAIK those are the typical Unix numbers, right? Miri just happens to set them up always, even on Windows.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do. And yeah, this is just taking advantage of the existing setup. The user won't even see these values directly, due to handle encoding.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we added support for SetStdHandle, then we'd need to add state to track the current values of these handles. But since we don't support that, this will just only ever return these values. Adding such support is completely possible, but not really important for std as far as I can tell.

@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 May 20, 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.

3 participants