Skip to content

Commit 0b060c7

Browse files
committed
Review comments
1 parent 87c4694 commit 0b060c7

File tree

4 files changed

+33
-23
lines changed

4 files changed

+33
-23
lines changed

src/shims/foreign_items/posix.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,13 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
137137
this.write_scalar(Scalar::from_i64(result), dest)?;
138138
}
139139
"fsync" => {
140-
let result = this.fsync(args[0])?;
140+
let &[fd] = check_arg_count(args)?;
141+
let result = this.fsync(fd)?;
141142
this.write_scalar(Scalar::from_i32(result), dest)?;
142143
}
143144
"fdatasync" => {
144-
let result = this.fdatasync(args[0])?;
145+
let &[fd] = check_arg_count(args)?;
146+
let result = this.fdatasync(fd)?;
145147
this.write_scalar(Scalar::from_i32(result), dest)?;
146148
}
147149

src/shims/foreign_items/posix/linux.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
5454
// fadvise is only informational, we can ignore it.
5555
this.write_null(dest)?;
5656
}
57-
// Linux-only
5857
"sync_file_range" => {
59-
let result = this.sync_file_range(args[0], args[1], args[2], args[3])?;
58+
let &[fd, offset, nbytes, flags] = check_arg_count(args)?;
59+
let result = this.sync_file_range(fd, offset, nbytes, flags)?;
6060
this.write_scalar(Scalar::from_i32(result), dest)?;
6161
}
6262

src/shims/fs.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
378378
} else if this.tcx.sess.target.target.target_os == "macos"
379379
&& cmd == this.eval_libc_i32("F_FULLFSYNC")?
380380
{
381+
// On macOS, fsync does not wait for the underlying disk to finish writing, while this
382+
// F_FULLFSYNC operation does. The standard library uses F_FULLFSYNC for both
383+
// File::sync_data() and File::sync_all().
384+
let &[_, _] = check_arg_count(args)?;
381385
if let Some(FileHandle { file, writable: _ }) = this.machine.file_handler.handles.get_mut(&fd) {
382386
let result = file.sync_all();
383387
this.try_unwrap_io_result(result.map(|_| 0i32))
@@ -1153,9 +1157,24 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
11531157
this.check_no_isolation("sync_file_range")?;
11541158

11551159
let fd = this.read_scalar(fd_op)?.to_i32()?;
1156-
let _offset = this.read_scalar(offset_op)?.to_i64()?;
1157-
let _nbytes = this.read_scalar(nbytes_op)?.to_i64()?;
1158-
let _flags = this.read_scalar(flags_op)?.to_u32()?;
1160+
let offset = this.read_scalar(offset_op)?.to_i64()?;
1161+
let nbytes = this.read_scalar(nbytes_op)?.to_i64()?;
1162+
let flags = this.read_scalar(flags_op)?.to_i32()?;
1163+
1164+
if offset < 0 || nbytes < 0 {
1165+
let einval = this.eval_libc("EINVAL")?;
1166+
this.set_last_error(einval)?;
1167+
return Ok(-1);
1168+
}
1169+
let allowed_flags = this.eval_libc_i32("SYNC_FILE_RANGE_WAIT_BEFORE")?
1170+
| this.eval_libc_i32("SYNC_FILE_RANGE_WRITE")?
1171+
| this.eval_libc_i32("SYNC_FILE_RANGE_WAIT_AFTER")?;
1172+
if flags & allowed_flags != flags {
1173+
let einval = this.eval_libc("EINVAL")?;
1174+
this.set_last_error(einval)?;
1175+
return Ok(-1);
1176+
}
1177+
11591178
if let Some(FileHandle { file, writable: _ }) = this.machine.file_handler.handles.get_mut(&fd) {
11601179
// In the interest of host compatibility, we conservatively ignore
11611180
// offset, nbytes, and flags, and sync the entire file.

tests/run-pass/fs.rs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ fn main() {
1414
test_seek();
1515
test_metadata();
1616
test_file_set_len();
17-
test_file_sync_all();
18-
test_file_sync_data();
17+
test_file_sync();
1918
test_symlink();
2019
test_errors();
2120
test_rename();
@@ -184,24 +183,14 @@ fn test_file_set_len() {
184183
remove_file(&path).unwrap();
185184
}
186185

187-
fn test_file_sync_all() {
186+
fn test_file_sync() {
188187
let bytes = b"Hello, World!\n";
189-
let path = prepare_with_content("miri_test_fs_sync_all.txt", bytes);
188+
let path = prepare_with_content("miri_test_fs_sync.txt", bytes);
190189

191-
// Test that we can call sync_all (can't readily test effects of this operation)
192-
let file = File::open(&path).unwrap();
193-
file.sync_all().unwrap();
194-
195-
remove_file(&path).unwrap();
196-
}
197-
198-
fn test_file_sync_data() {
199-
let bytes = b"Hello, World!\n";
200-
let path = prepare_with_content("miri_test_fs_sync_data.txt", bytes);
201-
202-
// Test that we can call sync_data (can't readily test effects of this operation)
190+
// Test that we can call sync_data and sync_all (can't readily test effects of this operation)
203191
let file = File::open(&path).unwrap();
204192
file.sync_data().unwrap();
193+
file.sync_all().unwrap();
205194

206195
remove_file(&path).unwrap();
207196
}

0 commit comments

Comments
 (0)