-
Notifications
You must be signed in to change notification settings - Fork 395
Implement SetFileInformationByHandle, as well as direct shim test for it #4547
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 | ||||
---|---|---|---|---|---|---|
|
@@ -5,6 +5,7 @@ use std::path::PathBuf; | |||||
use std::time::SystemTime; | ||||||
|
||||||
use bitflags::bitflags; | ||||||
use rustc_abi::Size; | ||||||
|
||||||
use crate::shims::files::{FileDescription, FileHandle}; | ||||||
use crate::shims::windows::handle::{EvalContextExt as _, Handle}; | ||||||
|
@@ -373,6 +374,64 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { | |||||
interp_ok(this.eval_windows("c", "TRUE")) | ||||||
} | ||||||
|
||||||
fn SetFileInformationByHandle( | ||||||
&mut self, | ||||||
file: &OpTy<'tcx>, // HANDLE | ||||||
class: &OpTy<'tcx>, // FILE_INFO_BY_HANDLE_CLASS | ||||||
file_information: &OpTy<'tcx>, // LPVOID | ||||||
buffer_size: &OpTy<'tcx>, // DWORD | ||||||
) -> InterpResult<'tcx, Scalar> { | ||||||
// ^ Returns BOOL (i32 on Windows) | ||||||
let this = self.eval_context_mut(); | ||||||
this.assert_target_os("windows", "SetFileInformationByHandle"); | ||||||
this.check_no_isolation("`SetFileInformationByHandle`")?; | ||||||
|
||||||
let class = this.read_scalar(class)?.to_u32()?; | ||||||
let buffer_size = this.read_scalar(buffer_size)?.to_u32()?; | ||||||
let file_info = this.read_pointer(file_information)?; | ||||||
this.check_ptr_access( | ||||||
file_info, | ||||||
Size::from_bytes(buffer_size), | ||||||
CheckInAllocMsg::MemoryAccess, | ||||||
)?; | ||||||
|
||||||
let file = this.read_handle(file, "GetFileInformationByHandle")?; | ||||||
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.
Suggested change
Happens a few more times below. |
||||||
let fd_num = if let Handle::File(fd_num) = file { | ||||||
fd_num | ||||||
} else { | ||||||
this.invalid_handle("GetFileInformationByHandle")? | ||||||
}; | ||||||
let Some(desc) = this.machine.fds.get(fd_num) else { | ||||||
this.invalid_handle("GetFileInformationByHandle")? | ||||||
}; | ||||||
let file = desc.downcast::<FileHandle>().ok_or_else(|| { | ||||||
err_unsup_format!( | ||||||
"`SetFileInformationByHandle` is only supported on file-backed file descriptors" | ||||||
) | ||||||
})?; | ||||||
|
||||||
if class == this.eval_windows_u32("c", "FileEndOfFileInfo") { | ||||||
let ptr = this.deref_pointer_as( | ||||||
file_information, | ||||||
this.windows_ty_layout("FILE_END_OF_FILE_INFO"), | ||||||
)?; | ||||||
Comment on lines
+414
to
+417
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 re-reads the pointer. Since you already read the pointer, you can just call |
||||||
let new_len = | ||||||
this.read_scalar(&this.project_field_named(&ptr, "EndOfFile")?)?.to_i64()?; | ||||||
match file.file.set_len(new_len.try_into().unwrap()) { | ||||||
Ok(_) => interp_ok(this.eval_windows("c", "TRUE")), | ||||||
Err(e) => { | ||||||
this.set_last_error(e)?; | ||||||
interp_ok(this.eval_windows("c", "FALSE")) | ||||||
} | ||||||
} | ||||||
} else { | ||||||
throw_unsup_format!( | ||||||
"SetFileInformationByHandle: Unsupported `FileInformationClass` value {}", | ||||||
class | ||||||
) | ||||||
} | ||||||
} | ||||||
|
||||||
fn DeleteFileW( | ||||||
&mut self, | ||||||
file_name: &OpTy<'tcx>, // LPCWSTR | ||||||
|
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. Can you also add a |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,9 +27,9 @@ fn main() { | |
test_errors(); | ||
test_from_raw_os_error(); | ||
test_file_clone(); | ||
test_file_set_len(); | ||
// Windows file handling is very incomplete. | ||
if cfg!(not(windows)) { | ||
test_file_set_len(); | ||
test_file_sync(); | ||
test_rename(); | ||
test_directory(); | ||
|
@@ -85,7 +85,6 @@ fn test_file_partial_reads_writes() { | |
|
||
// Ensure we sometimes do incomplete writes. | ||
let got_short_write = (0..16).any(|_| { | ||
let _ = remove_file(&path); // FIXME(win, issue #4483): errors if the file already exists | ||
let mut file = File::create(&path).unwrap(); | ||
file.write(&[0; 4]).unwrap() != 4 | ||
}); | ||
|
@@ -209,7 +208,10 @@ fn test_file_set_len() { | |
|
||
// Can't use set_len on a file not opened for writing | ||
let file = OpenOptions::new().read(true).open(&path).unwrap(); | ||
assert_eq!(ErrorKind::InvalidInput, file.set_len(14).unwrap_err().kind()); | ||
assert_eq!( | ||
if cfg!(windows) { ErrorKind::PermissionDenied } else { ErrorKind::InvalidInput }, | ||
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. Since we pass through the host OS error, I doubt this will work properly. Just check that it's one of the two (with a comment explaining why). |
||
file.set_len(14).unwrap_err().kind() | ||
); | ||
|
||
remove_file(&path).unwrap(); | ||
} | ||
|
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.
Please pick a consistent name (
file_information
vsfile_info
).