Skip to content

Commit 1ca8391

Browse files
committed
freebsd add *stat calls interception support
1 parent d251208 commit 1ca8391

File tree

4 files changed

+37
-10
lines changed

4 files changed

+37
-10
lines changed

src/shims/unix/freebsd/foreign_items.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use rustc_target::spec::abi::Abi;
33

44
use crate::*;
55
use shims::foreign_items::EmulateForeignItemResult;
6+
use shims::unix::fs::EvalContextExt as _;
67
use shims::unix::thread::EvalContextExt as _;
78

89
pub fn is_dyn_sym(_name: &str) -> bool {
@@ -63,6 +64,25 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
6364
this.write_scalar(Scalar::from_target_usize(len, this), dest)?;
6465
}
6566

67+
// File related shims
68+
"stat" => {
69+
let [path, buf] =
70+
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
71+
let result = this.macos_fbsd_stat(path, buf)?;
72+
this.write_scalar(result, dest)?;
73+
}
74+
"lstat" => {
75+
let [path, buf] =
76+
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
77+
let result = this.macos_fbsd_lstat(path, buf)?;
78+
this.write_scalar(result, dest)?;
79+
}
80+
"fstat" => {
81+
let [fd, buf] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
82+
let result = this.macos_fbsd_fstat(fd, buf)?;
83+
this.write_scalar(result, dest)?;
84+
}
85+
6686
// errno
6787
"__error" => {
6888
let [] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;

src/shims/unix/fs.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -911,13 +911,16 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
911911
this.try_unwrap_io_result(result)
912912
}
913913

914-
fn macos_stat(
914+
fn macos_fbsd_stat(
915915
&mut self,
916916
path_op: &OpTy<'tcx, Provenance>,
917917
buf_op: &OpTy<'tcx, Provenance>,
918918
) -> InterpResult<'tcx, Scalar<Provenance>> {
919919
let this = self.eval_context_mut();
920-
this.assert_target_os("macos", "stat");
920+
921+
if !matches!(&*this.tcx.sess.target.os, "macos" | "freebsd") {
922+
throw_unsup_format!("macos_fbsd_stat not meant for os {}", this.tcx.sess.target.os);
923+
}
921924

922925
let path_scalar = this.read_pointer(path_op)?;
923926
let path = this.read_path_from_c_str(path_scalar)?.into_owned();
@@ -940,13 +943,16 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
940943
}
941944

942945
// `lstat` is used to get symlink metadata.
943-
fn macos_lstat(
946+
fn macos_fbsd_lstat(
944947
&mut self,
945948
path_op: &OpTy<'tcx, Provenance>,
946949
buf_op: &OpTy<'tcx, Provenance>,
947950
) -> InterpResult<'tcx, Scalar<Provenance>> {
948951
let this = self.eval_context_mut();
949-
this.assert_target_os("macos", "lstat");
952+
953+
if !matches!(&*this.tcx.sess.target.os, "macos" | "freebsd") {
954+
throw_unsup_format!("macos_fbsd_lstat not meant for os {}", this.tcx.sess.target.os);
955+
}
950956

951957
let path_scalar = this.read_pointer(path_op)?;
952958
let path = this.read_path_from_c_str(path_scalar)?.into_owned();
@@ -967,14 +973,16 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
967973
Ok(Scalar::from_i32(this.macos_stat_write_buf(metadata, buf_op)?))
968974
}
969975

970-
fn macos_fstat(
976+
fn macos_fbsd_fstat(
971977
&mut self,
972978
fd_op: &OpTy<'tcx, Provenance>,
973979
buf_op: &OpTy<'tcx, Provenance>,
974980
) -> InterpResult<'tcx, Scalar<Provenance>> {
975981
let this = self.eval_context_mut();
976982

977-
this.assert_target_os("macos", "fstat");
983+
if !matches!(&*this.tcx.sess.target.os, "macos" | "freebsd") {
984+
throw_unsup_format!("macos_fbsd_fstat not meant for os {}", this.tcx.sess.target.os);
985+
}
978986

979987
let fd = this.read_scalar(fd_op)?.to_i32()?;
980988

src/shims/unix/macos/foreign_items.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,18 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
4040
"stat" | "stat64" | "stat$INODE64" => {
4141
let [path, buf] =
4242
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
43-
let result = this.macos_stat(path, buf)?;
43+
let result = this.macos_fbsd_stat(path, buf)?;
4444
this.write_scalar(result, dest)?;
4545
}
4646
"lstat" | "lstat64" | "lstat$INODE64" => {
4747
let [path, buf] =
4848
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
49-
let result = this.macos_lstat(path, buf)?;
49+
let result = this.macos_fbsd_lstat(path, buf)?;
5050
this.write_scalar(result, dest)?;
5151
}
5252
"fstat" | "fstat64" | "fstat$INODE64" => {
5353
let [fd, buf] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
54-
let result = this.macos_fstat(fd, buf)?;
54+
let result = this.macos_fbsd_fstat(fd, buf)?;
5555
this.write_scalar(result, dest)?;
5656
}
5757
"opendir$INODE64" => {

tests/pass-dep/shims/libc-fs-with-isolation.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//@ignore-target-windows: no libc on Windows
2-
//@ignore-target-freebsd: FIXME needs foreign function `stat@FBSD_1.0`
32
//@compile-flags: -Zmiri-isolation-error=warn-nobacktrace
43
//@normalize-stderr-test: "(stat(x)?)" -> "$$STAT"
54

0 commit comments

Comments
 (0)