Skip to content

Commit 6494b1f

Browse files
committed
Fix review
- Add test for multiple file flags - Add comments to some code
1 parent 2e9509d commit 6494b1f

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

src/shims/windows/fs.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
204204
throw_unsup_format!("CreateFileW: Template files are not supported");
205205
}
206206

207+
// We need to know if the file is a directory to correctly open directory handles.
208+
// This is racy, but currently the stdlib doesn't appear to offer a better solution.
207209
let is_dir = file_name.is_dir();
208210

209211
// BACKUP_SEMANTICS is how Windows calls the act of opening a directory handle.
@@ -253,7 +255,6 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
253255
}
254256
}
255257
CreateNew => {
256-
// See comments in
257258
options.create_new(true);
258259
// Per `create_new` documentation:
259260
// The file must be opened with write or append access in order to create a new file.
@@ -342,6 +343,9 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
342343
this.eval_windows_u32("c", "FILE_ATTRIBUTE_DEVICE")
343344
};
344345

346+
// Per the Windows documentation:
347+
// "If the underlying file system does not support the [...] time, this member is zero (0)."
348+
// https://learn.microsoft.com/en-us/windows/win32/api/fileapi/ns-fileapi-by_handle_file_information
345349
let created = extract_windows_epoch(this, metadata.created())?.unwrap_or((0, 0));
346350
let accessed = extract_windows_epoch(this, metadata.accessed())?.unwrap_or((0, 0));
347351
let written = extract_windows_epoch(this, metadata.modified())?.unwrap_or((0, 0));

src/shims/windows/handle.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
198198
#[allow(non_snake_case)]
199199
pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
200200
/// Convert a scalar into a structured `Handle`.
201-
/// If the handle is invalid, or references a non-existent item, this returns a machine abort.
201+
/// If the handle is invalid, or references a non-existent item, execution is aborted.
202202
#[track_caller]
203203
fn read_handle(&self, handle: &OpTy<'tcx>, function_name: &str) -> InterpResult<'tcx, Handle> {
204204
let this = self.eval_context_ref();

tests/pass-dep/shims/windows-fs.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ use windows_sys::Win32::Foundation::{
1414
};
1515
use windows_sys::Win32::Storage::FileSystem::{
1616
BY_HANDLE_FILE_INFORMATION, CREATE_ALWAYS, CREATE_NEW, CreateFileW, FILE_ATTRIBUTE_DIRECTORY,
17-
FILE_ATTRIBUTE_NORMAL, FILE_FLAG_BACKUP_SEMANTICS, FILE_SHARE_DELETE, FILE_SHARE_READ,
18-
FILE_SHARE_WRITE, GetFileInformationByHandle, OPEN_ALWAYS, OPEN_EXISTING,
17+
FILE_ATTRIBUTE_NORMAL, FILE_FLAG_BACKUP_SEMANTICS, FILE_FLAG_OPEN_REPARSE_POINT,
18+
FILE_SHARE_DELETE, FILE_SHARE_READ, FILE_SHARE_WRITE, GetFileInformationByHandle, OPEN_ALWAYS,
19+
OPEN_EXISTING,
1920
};
2021

2122
fn main() {
@@ -164,6 +165,31 @@ unsafe fn test_open_always_twice() {
164165
};
165166
}
166167

168+
// TODO: Once we support more of the std API, it would be nice to test against an actual symlink
169+
unsafe fn test_open_dir_reparse() {
170+
let temp = utils::tmp();
171+
let raw_path = to_wide_cstr(&temp);
172+
// Open the `temp` directory.
173+
let handle = CreateFileW(
174+
raw_path.as_ptr(),
175+
GENERIC_READ,
176+
FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
177+
ptr::null_mut(),
178+
OPEN_EXISTING,
179+
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT,
180+
0,
181+
);
182+
assert_ne!(handle, -1, "CreateFileW Failed: {}", GetLastError());
183+
let mut info = std::mem::zeroed::<BY_HANDLE_FILE_INFORMATION>();
184+
if GetFileInformationByHandle(handle, &mut info) == 0 {
185+
panic!("Failed to get file information")
186+
};
187+
assert!(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY != 0);
188+
if CloseHandle(handle) == 0 {
189+
panic!("Failed to close file")
190+
};
191+
}
192+
167193
fn to_wide_cstr(path: &Path) -> Vec<u16> {
168194
let mut raw_path = path.as_os_str().encode_wide().collect::<Vec<_>>();
169195
raw_path.extend([0, 0]);

0 commit comments

Comments
 (0)