Skip to content

Commit 8fd353c

Browse files
committed
use correct stat type and fix some redundancy
1 parent 8f3b31c commit 8fd353c

File tree

5 files changed

+10
-17
lines changed

5 files changed

+10
-17
lines changed

src/shims/unix/freebsd/foreign_items.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
148148
let result = this.macos_fbsd_solarish_lstat(path, buf)?;
149149
this.write_scalar(result, dest)?;
150150
}
151-
"fstat" | "fstat@FBSD_1.0" => {
151+
"fstat@FBSD_1.0" => {
152152
let [fd, buf] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
153153
let result = this.fstat(fd, buf)?;
154154
this.write_scalar(result, dest)?;

src/shims/unix/fs.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,11 @@ trait EvalContextExtPrivate<'tcx>: crate::MiriInterpCxExt<'tcx> {
130130
let (modified_sec, modified_nsec) = metadata.modified.unwrap_or((0, 0));
131131
let mode = metadata.mode.to_uint(this.libc_ty_layout("mode_t").size)?;
132132

133-
let buf = this.deref_pointer_as(buf_op, this.libc_ty_layout("stat"))?;
133+
// We do *not* use `deref_pointer_as` here since determining the right pointee type
134+
// is highly non-trivial: it depends on which exact alias of the function was invoked
135+
// (e.g. `fstat` vs `fstat64`), and then on FreeBSD it also depends on the ABI level
136+
// which can be different between the libc used by std and the libc used by everyone else.
137+
let buf = this.deref_pointer(buf_op)?;
134138
this.write_int_fields_named(
135139
&[
136140
("st_dev", metadata.dev.into()),
@@ -156,9 +160,6 @@ trait EvalContextExtPrivate<'tcx>: crate::MiriInterpCxExt<'tcx> {
156160
if matches!(&this.tcx.sess.target.os, Os::MacOs | Os::FreeBsd) {
157161
this.write_int_fields_named(
158162
&[
159-
("st_atime_nsec", access_nsec.into()),
160-
("st_mtime_nsec", modified_nsec.into()),
161-
("st_ctime_nsec", 0),
162163
("st_birthtime", created_sec.into()),
163164
("st_birthtime_nsec", created_nsec.into()),
164165
("st_flags", 0),

src/shims/unix/linux/foreign_items.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,6 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
5353
let result = this.linux_statx(dirfd, pathname, flags, mask, statxbuf)?;
5454
this.write_scalar(result, dest)?;
5555
}
56-
"fstat" => {
57-
let [fd, buf] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
58-
let result = this.fstat(fd, buf)?;
59-
this.write_scalar(result, dest)?;
60-
}
6156
// epoll, eventfd
6257
"epoll_create1" => {
6358
let [flag] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;

src/shims/unix/solarish/foreign_items.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,6 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
100100
let result = this.macos_fbsd_solarish_lstat(path, buf)?;
101101
this.write_scalar(result, dest)?;
102102
}
103-
"fstat" | "fstat64" => {
104-
let [fd, buf] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
105-
let result = this.fstat(fd, buf)?;
106-
this.write_scalar(result, dest)?;
107-
}
108103
"readdir" => {
109104
let [dirp] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
110105
let result = this.readdir64("dirent", dirp)?;

tests/pass-dep/libc/libc-fs.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,13 +461,15 @@ fn test_fstat() {
461461
let file = File::open(&path).unwrap();
462462
let fd = file.as_raw_fd();
463463

464-
let mut stat: libc::stat = unsafe { MaybeUninit::zeroed().assume_init() };
465-
let res = unsafe { libc::fstat(fd, &mut stat) };
464+
let mut stat = MaybeUninit::<libc::stat>::uninit();
465+
let res = unsafe { libc::fstat(fd, stat.as_mut_ptr()) };
466466
assert_eq!(res, 0);
467+
let stat = unsafe { stat.assume_init_ref() };
467468

468469
assert_eq!(stat.st_size, 5);
469470
assert_eq!(stat.st_mode & libc::S_IFMT, libc::S_IFREG);
470471

472+
// Check that all fields are initialized.
471473
let _st_nlink = stat.st_nlink;
472474
let _st_blksize = stat.st_blksize;
473475
let _st_blocks = stat.st_blocks;

0 commit comments

Comments
 (0)