Skip to content

Commit d2ded9c

Browse files
committed
feat: Support fstat in linux
refactor: rename `macos_fbsd_solarish_write_stat_buf` to `write_stat_buf` refactor: rename `macos_fbsd_solarish_fstat` to `fstat` feat: support `fstat` in linux test: testing support of `fstat` in linux fix: missed add `Os::Linux` for supported OSs in `fstat` feat: add nanosecond fields to file metadata in `EvalContextExtPrivate` add `fstat` to foreign items in unix enhance test of `fstat` fix the test
1 parent 5fbfc88 commit d2ded9c

File tree

7 files changed

+61
-20
lines changed

7 files changed

+61
-20
lines changed

src/shims/unix/foreign_items.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,11 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
345345
let result = this.symlink(target, linkpath)?;
346346
this.write_scalar(result, dest)?;
347347
}
348+
"fstat" => {
349+
let [fd, buf] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
350+
let result = this.fstat(fd, buf)?;
351+
this.write_scalar(result, dest)?;
352+
}
348353
"rename" => {
349354
let [oldpath, newpath] = this.check_shim_sig(
350355
shim_sig!(extern "C" fn(*const _, *const _) -> i32),

src/shims/unix/freebsd/foreign_items.rs

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

src/shims/unix/fs.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl UnixFileDescription for FileHandle {
118118

119119
impl<'tcx> EvalContextExtPrivate<'tcx> for crate::MiriInterpCx<'tcx> {}
120120
trait EvalContextExtPrivate<'tcx>: crate::MiriInterpCxExt<'tcx> {
121-
fn macos_fbsd_solarish_write_stat_buf(
121+
fn write_stat_buf(
122122
&mut self,
123123
metadata: FileMetadata,
124124
buf_op: &OpTy<'tcx>,
@@ -141,8 +141,11 @@ trait EvalContextExtPrivate<'tcx>: crate::MiriInterpCxExt<'tcx> {
141141
("st_gid", metadata.gid.into()),
142142
("st_rdev", 0),
143143
("st_atime", access_sec.into()),
144+
("st_atime_nsec", access_nsec.into()),
144145
("st_mtime", modified_sec.into()),
146+
("st_mtime_nsec", modified_nsec.into()),
145147
("st_ctime", 0),
148+
("st_ctime_nsec", 0),
146149
("st_size", metadata.size.into()),
147150
("st_blocks", 0),
148151
("st_blksize", 0),
@@ -550,7 +553,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
550553
Err(err) => return this.set_last_error_and_return_i32(err),
551554
};
552555

553-
interp_ok(Scalar::from_i32(this.macos_fbsd_solarish_write_stat_buf(metadata, buf_op)?))
556+
interp_ok(Scalar::from_i32(this.write_stat_buf(metadata, buf_op)?))
554557
}
555558

556559
// `lstat` is used to get symlink metadata.
@@ -583,22 +586,17 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
583586
Err(err) => return this.set_last_error_and_return_i32(err),
584587
};
585588

586-
interp_ok(Scalar::from_i32(this.macos_fbsd_solarish_write_stat_buf(metadata, buf_op)?))
589+
interp_ok(Scalar::from_i32(this.write_stat_buf(metadata, buf_op)?))
587590
}
588591

589-
fn macos_fbsd_solarish_fstat(
590-
&mut self,
591-
fd_op: &OpTy<'tcx>,
592-
buf_op: &OpTy<'tcx>,
593-
) -> InterpResult<'tcx, Scalar> {
592+
fn fstat(&mut self, fd_op: &OpTy<'tcx>, buf_op: &OpTy<'tcx>) -> InterpResult<'tcx, Scalar> {
594593
let this = self.eval_context_mut();
595594

596-
if !matches!(&this.tcx.sess.target.os, Os::MacOs | Os::FreeBsd | Os::Solaris | Os::Illumos)
597-
{
598-
panic!(
599-
"`macos_fbsd_solaris_fstat` should not be called on {}",
600-
this.tcx.sess.target.os
601-
);
595+
if !matches!(
596+
&this.tcx.sess.target.os,
597+
Os::MacOs | Os::FreeBsd | Os::Solaris | Os::Illumos | Os::Linux
598+
) {
599+
panic!("`fstat` should not be called on {}", this.tcx.sess.target.os);
602600
}
603601

604602
let fd = this.read_scalar(fd_op)?.to_i32()?;
@@ -614,7 +612,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
614612
Ok(metadata) => metadata,
615613
Err(err) => return this.set_last_error_and_return_i32(err),
616614
};
617-
interp_ok(Scalar::from_i32(this.macos_fbsd_solarish_write_stat_buf(metadata, buf_op)?))
615+
interp_ok(Scalar::from_i32(this.write_stat_buf(metadata, buf_op)?))
618616
}
619617

620618
fn linux_statx(

src/shims/unix/linux/foreign_items.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ 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-
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+
}
5761
// epoll, eventfd
5862
"epoll_create1" => {
5963
let [flag] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;

src/shims/unix/macos/foreign_items.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
5656
let result = this.macos_fbsd_solarish_lstat(path, buf)?;
5757
this.write_scalar(result, dest)?;
5858
}
59-
"fstat" | "fstat64" | "fstat$INODE64" => {
59+
"fstat$INODE64" => {
6060
let [fd, buf] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
61-
let result = this.macos_fbsd_solarish_fstat(fd, buf)?;
61+
let result = this.fstat(fd, buf)?;
6262
this.write_scalar(result, dest)?;
6363
}
6464
"opendir$INODE64" => {

src/shims/unix/solarish/foreign_items.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
102102
}
103103
"fstat" | "fstat64" => {
104104
let [fd, buf] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
105-
let result = this.macos_fbsd_solarish_fstat(fd, buf)?;
105+
let result = this.fstat(fd, buf)?;
106106
this.write_scalar(result, dest)?;
107107
}
108108
"readdir" => {

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ fn main() {
3838
test_posix_fadvise();
3939
#[cfg(target_os = "linux")]
4040
test_sync_file_range();
41+
test_fstat();
4142
test_isatty();
4243
test_read_and_uninit();
4344
test_nofollow_not_symlink();
@@ -380,6 +381,39 @@ fn test_sync_file_range() {
380381
assert_eq!(result_2, 0);
381382
}
382383

384+
fn test_fstat() {
385+
use std::mem::MaybeUninit;
386+
use std::os::unix::io::AsRawFd;
387+
388+
let path = utils::prepare_with_content("miri_test_libc_fstat.txt", b"hello");
389+
let file = File::open(&path).unwrap();
390+
let fd = file.as_raw_fd();
391+
392+
let mut stat: libc::stat = unsafe { MaybeUninit::zeroed().assume_init() };
393+
let res = unsafe { libc::fstat(fd, &mut stat) };
394+
assert_eq!(res, 0);
395+
396+
assert_eq!(stat.st_size, 5);
397+
assert_eq!(stat.st_mode & libc::S_IFMT, libc::S_IFREG);
398+
399+
let _st_nlink = stat.st_nlink;
400+
let _st_blksize = stat.st_blksize;
401+
let _st_blocks = stat.st_blocks;
402+
let _st_ino = stat.st_ino;
403+
let _st_dev = stat.st_dev;
404+
let _st_uid = stat.st_uid;
405+
let _st_gid = stat.st_gid;
406+
let _st_rdev = stat.st_rdev;
407+
let _st_atime = stat.st_atime;
408+
let _st_mtime = stat.st_mtime;
409+
let _st_ctime = stat.st_ctime;
410+
let _st_atime_nsec = stat.st_atime_nsec;
411+
let _st_mtime_nsec = stat.st_mtime_nsec;
412+
let _st_ctime_nsec = stat.st_ctime_nsec;
413+
414+
remove_file(&path).unwrap();
415+
}
416+
383417
fn test_isatty() {
384418
// Testing whether our isatty shim returns the right value would require controlling whether
385419
// these streams are actually TTYs, which is hard.

0 commit comments

Comments
 (0)