Skip to content

Commit 2e9509d

Browse files
committed
Fix review
- comment improvements - Fix FileAttributes using else when it shouldn't
1 parent fa3b49a commit 2e9509d

File tree

5 files changed

+21
-13
lines changed

5 files changed

+21
-13
lines changed

src/shims/unix/fd.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
122122
};
123123

124124
let result = fd.as_unix(this).flock(this.machine.communicate(), parsed_op)?;
125-
drop(fd);
126125
// return `0` if flock is successful
127126
let result = result.map(|()| 0i32);
128127
interp_ok(Scalar::from_i32(this.try_unwrap_io_result(result)?))

src/shims/windows/fs.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,12 @@ impl FileAttributes {
124124
if value & file_flag_backup_semantics != 0 {
125125
value &= !file_flag_backup_semantics;
126126
out |= FileAttributes::BACKUP_SEMANTICS;
127-
} else if value & file_flag_open_reparse_point != 0 {
127+
}
128+
if value & file_flag_open_reparse_point != 0 {
128129
value &= !file_flag_open_reparse_point;
129130
out |= FileAttributes::OPEN_REPARSE;
130-
} else if value & file_attribute_normal != 0 {
131+
}
132+
if value & file_attribute_normal != 0 {
131133
value &= !file_attribute_normal;
132134
out |= FileAttributes::NORMAL;
133135
}
@@ -137,6 +139,7 @@ impl FileAttributes {
137139
}
138140

139141
if out == FileAttributes::ZERO {
142+
// NORMAL is equivalent to 0. Avoid needing to check both cases by unifying the two.
140143
out = FileAttributes::NORMAL;
141144
}
142145
interp_ok(out)
@@ -162,6 +165,9 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
162165
let this = self.eval_context_mut();
163166
this.assert_target_os("windows", "CreateFileW");
164167
this.check_no_isolation("`CreateFileW`")?;
168+
169+
// This function appears to always set the error to 0. This is important for some flag
170+
// combinations, which may set error code on success.
165171
this.set_last_error(IoError::Raw(Scalar::from_i32(0)))?;
166172

167173
let file_name = this.read_path_from_wide_str(this.read_pointer(file_name)?)?;
@@ -200,6 +206,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
200206

201207
let is_dir = file_name.is_dir();
202208

209+
// BACKUP_SEMANTICS is how Windows calls the act of opening a directory handle.
203210
if !attributes.contains(FileAttributes::BACKUP_SEMANTICS) && is_dir {
204211
this.set_last_error(IoError::WindowsError("ERROR_ACCESS_DENIED"))?;
205212
return interp_ok(Handle::Invalid);
@@ -226,20 +233,19 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
226233

227234
match creation_disposition {
228235
CreateAlways | OpenAlways => {
229-
// This is racy, but there doesn't appear to be an std API that both succeeds if a
230-
// file exists but tells us it isn't new. Either we accept racing one way or another,
231-
// or we use an iffy heuristic like file creation time. This implementation prefers
232-
// to fail in the direction of erroring more often.
233236
// Per the documentation:
234237
// If the specified file exists and is writable, the function truncates the file,
235238
// the function succeeds, and last-error code is set to ERROR_ALREADY_EXISTS.
236239
// If the specified file does not exist and is a valid path, a new file is created,
237240
// the function succeeds, and the last-error code is set to zero.
238241
// https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilew
242+
//
243+
// This is racy, but there doesn't appear to be an std API that both succeeds if a
244+
// file exists but tells us it isn't new. Either we accept racing one way or another,
245+
// or we use an iffy heuristic like file creation time. This implementation prefers
246+
// to fail in the direction of erroring more often.
239247
if file_name.exists() {
240248
this.set_last_error(IoError::WindowsError("ERROR_ALREADY_EXISTS"))?;
241-
} else {
242-
this.set_last_error(IoError::Raw(Scalar::from_u32(0)))?;
243249
}
244250
options.create(true);
245251
if creation_disposition == CreateAlways {

src/shims/windows/handle.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl Handle {
8787
// INVALID_HANDLE_VALUE.
8888
let variant_count = variant_count::<Self>();
8989

90-
// However, std's ilog2 is floor(log2(x))
90+
// However, std's ilog2 is floor(log2(x)).
9191
let floor_log2 = variant_count.ilog2();
9292

9393
// We need to add one for non powers of two to compensate for the difference.
@@ -165,7 +165,7 @@ impl Handle {
165165
/// Structurally invalid handles return [`HandleError::InvalidHandle`].
166166
/// If the handle is structurally valid but semantically invalid, e.g. a for non-existent thread
167167
/// ID, returns [`HandleError::ThreadNotFound`].
168-
pub fn try_from_scalar<'tcx>(
168+
fn try_from_scalar<'tcx>(
169169
handle: Scalar,
170170
cx: &MiriInterpCx<'tcx>,
171171
) -> InterpResult<'tcx, Result<Self, HandleError>> {
@@ -197,6 +197,8 @@ impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
197197

198198
#[allow(non_snake_case)]
199199
pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
200+
/// Convert a scalar into a structured `Handle`.
201+
/// If the handle is invalid, or references a non-existent item, this returns a machine abort.
200202
#[track_caller]
201203
fn read_handle(&self, handle: &OpTy<'tcx>, function_name: &str) -> InterpResult<'tcx, Handle> {
202204
let this = self.eval_context_ref();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ fn main() {
3030
unsafe fn test_create_dir_file() {
3131
let temp = utils::tmp();
3232
let raw_path = to_wide_cstr(&temp);
33+
// Open the `temp` directory.
3334
let handle = CreateFileW(
3435
raw_path.as_ptr(),
3536
GENERIC_READ,

tests/pass/shims/fs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,10 @@ fn test_metadata() {
145145
let path = utils::prepare_with_content("miri_test_fs_metadata.txt", bytes);
146146

147147
// Test that metadata of an absolute path is correct.
148-
check_metadata(bytes, &path).expect("Absolute path metadata");
148+
check_metadata(bytes, &path).expect("absolute path metadata");
149149
// Test that metadata of a relative path is correct.
150150
std::env::set_current_dir(path.parent().unwrap()).unwrap();
151-
check_metadata(bytes, Path::new(path.file_name().unwrap())).expect("Relative path metadata");
151+
check_metadata(bytes, Path::new(path.file_name().unwrap())).expect("relative path metadata");
152152

153153
// Removing file should succeed.
154154
remove_file(&path).unwrap();

0 commit comments

Comments
 (0)