Skip to content

Commit c4d5754

Browse files
committed
Move some macos functions to writing to dest directly
1 parent ec4bef5 commit c4d5754

File tree

3 files changed

+37
-44
lines changed

3 files changed

+37
-44
lines changed

src/shims/unix/freebsd/foreign_items.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,21 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
4848
"stat" | "stat@FBSD_1.0" => {
4949
let [path, buf] =
5050
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
51-
let result = this.macos_fbsd_stat(path, buf)?;
52-
this.write_scalar(result, dest)?;
51+
this.macos_fbsd_stat(path, buf, dest)?;
5352
}
5453
"lstat" | "lstat@FBSD_1.0" => {
5554
let [path, buf] =
5655
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
57-
let result = this.macos_fbsd_lstat(path, buf)?;
58-
this.write_scalar(result, dest)?;
56+
this.macos_fbsd_lstat(path, buf, dest)?;
5957
}
6058
"fstat" | "fstat@FBSD_1.0" => {
6159
let [fd, buf] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
62-
let result = this.macos_fbsd_fstat(fd, buf)?;
63-
this.write_scalar(result, dest)?;
60+
this.macos_fbsd_fstat(fd, buf, dest)?;
6461
}
6562
"readdir_r" | "readdir_r@FBSD_1.0" => {
6663
let [dirp, entry, result] =
6764
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
68-
let result = this.macos_fbsd_readdir_r(dirp, entry, result)?;
69-
this.write_scalar(result, dest)?;
65+
this.macos_fbsd_readdir_r(dirp, entry, result, dest)?;
7066
}
7167

7268
// Miscellaneous

src/shims/unix/fs.rs

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,8 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
657657
&mut self,
658658
path_op: &OpTy<'tcx>,
659659
buf_op: &OpTy<'tcx>,
660-
) -> InterpResult<'tcx, Scalar> {
660+
dest: &MPlaceTy<'tcx>,
661+
) -> InterpResult<'tcx> {
661662
let this = self.eval_context_mut();
662663

663664
if !matches!(&*this.tcx.sess.target.os, "macos" | "freebsd") {
@@ -670,26 +671,25 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
670671
// Reject if isolation is enabled.
671672
if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
672673
this.reject_in_isolation("`stat`", reject_with)?;
673-
let eacc = this.eval_libc("EACCES");
674-
this.set_last_error(eacc)?;
675-
return Ok(Scalar::from_i32(-1));
674+
return this.set_libc_err_and_return_neg1("EACCES", dest);
676675
}
677676

678677
// `stat` always follows symlinks.
679678
let metadata = match FileMetadata::from_path(this, &path, true)? {
680679
Some(metadata) => metadata,
681-
None => return Ok(Scalar::from_i32(-1)), // `FileMetadata` has set errno
680+
None => return this.write_int(-1, dest), // `FileMetadata` has set errno
682681
};
683-
684-
Ok(Scalar::from_i32(this.macos_stat_write_buf(metadata, buf_op)?))
682+
let res = this.macos_stat_write_buf(metadata, buf_op)?;
683+
this.write_int(res, dest)
685684
}
686685

687686
// `lstat` is used to get symlink metadata.
688687
fn macos_fbsd_lstat(
689688
&mut self,
690689
path_op: &OpTy<'tcx>,
691690
buf_op: &OpTy<'tcx>,
692-
) -> InterpResult<'tcx, Scalar> {
691+
dest: &MPlaceTy<'tcx>,
692+
) -> InterpResult<'tcx> {
693693
let this = self.eval_context_mut();
694694

695695
if !matches!(&*this.tcx.sess.target.os, "macos" | "freebsd") {
@@ -702,24 +702,23 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
702702
// Reject if isolation is enabled.
703703
if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
704704
this.reject_in_isolation("`lstat`", reject_with)?;
705-
let eacc = this.eval_libc("EACCES");
706-
this.set_last_error(eacc)?;
707-
return Ok(Scalar::from_i32(-1));
705+
return this.set_libc_err_and_return_neg1("EACCES", dest);
708706
}
709707

710708
let metadata = match FileMetadata::from_path(this, &path, false)? {
711709
Some(metadata) => metadata,
712-
None => return Ok(Scalar::from_i32(-1)), // `FileMetadata` has set errno
710+
None => return this.write_int(-1, dest), // `FileMetadata` has set errno
713711
};
714-
715-
Ok(Scalar::from_i32(this.macos_stat_write_buf(metadata, buf_op)?))
712+
let res = this.macos_stat_write_buf(metadata, buf_op)?;
713+
this.write_int(res, dest)
716714
}
717715

718716
fn macos_fbsd_fstat(
719717
&mut self,
720718
fd_op: &OpTy<'tcx>,
721719
buf_op: &OpTy<'tcx>,
722-
) -> InterpResult<'tcx, Scalar> {
720+
dest: &MPlaceTy<'tcx>,
721+
) -> InterpResult<'tcx> {
723722
let this = self.eval_context_mut();
724723

725724
if !matches!(&*this.tcx.sess.target.os, "macos" | "freebsd") {
@@ -731,15 +730,15 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
731730
// Reject if isolation is enabled.
732731
if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
733732
this.reject_in_isolation("`fstat`", reject_with)?;
734-
// Set error code as "EBADF" (bad fd)
735-
return Ok(Scalar::from_i32(this.fd_not_found()?));
733+
return this.set_libc_err_and_return_neg1("EBADF", dest);
736734
}
737735

738736
let metadata = match FileMetadata::from_fd_num(this, fd)? {
739737
Some(metadata) => metadata,
740-
None => return Ok(Scalar::from_i32(-1)),
738+
None => return this.write_int(-1, dest), // `FileMetadata` has set errno
741739
};
742-
Ok(Scalar::from_i32(this.macos_stat_write_buf(metadata, buf_op)?))
740+
let res = this.macos_stat_write_buf(metadata, buf_op)?;
741+
this.write_int(res, dest)
743742
}
744743

745744
fn linux_statx(
@@ -1138,7 +1137,8 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
11381137
dirp_op: &OpTy<'tcx>,
11391138
entry_op: &OpTy<'tcx>,
11401139
result_op: &OpTy<'tcx>,
1141-
) -> InterpResult<'tcx, Scalar> {
1140+
dest: &MPlaceTy<'tcx>,
1141+
) -> InterpResult<'tcx> {
11421142
let this = self.eval_context_mut();
11431143

11441144
if !matches!(&*this.tcx.sess.target.os, "macos" | "freebsd") {
@@ -1150,14 +1150,14 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
11501150
// Reject if isolation is enabled.
11511151
if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
11521152
this.reject_in_isolation("`readdir_r`", reject_with)?;
1153-
// Set error code as "EBADF" (bad fd)
1154-
return Ok(Scalar::from_i32(this.fd_not_found()?));
1153+
// return positive error number on error
1154+
return this.write_scalar(this.eval_libc("EBADF"), dest);
11551155
}
11561156

11571157
let open_dir = this.machine.dirs.streams.get_mut(&dirp).ok_or_else(|| {
11581158
err_unsup_format!("the DIR pointer passed to readdir_r did not come from opendir")
11591159
})?;
1160-
Ok(Scalar::from_i32(match open_dir.read_dir.next() {
1160+
match open_dir.read_dir.next() {
11611161
Some(Ok(dir_entry)) => {
11621162
// Write into entry, write pointer to result, return 0 on success.
11631163
// The name is written with write_os_str_to_c_str, while the rest of the
@@ -1234,26 +1234,27 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
12341234

12351235
let result_place = this.deref_pointer(result_op)?;
12361236
this.write_scalar(this.read_scalar(entry_op)?, &result_place)?;
1237-
1238-
0
1237+
this.write_null(dest)?;
12391238
}
12401239
None => {
12411240
// end of stream: return 0, assign *result=NULL
12421241
this.write_null(&this.deref_pointer(result_op)?)?;
1243-
0
1242+
this.write_null(dest)?;
12441243
}
12451244
Some(Err(e)) =>
12461245
match e.raw_os_error() {
12471246
// return positive error number on error
1248-
Some(error) => error,
1247+
Some(error) => this.write_int(error, dest)?,
12491248
None => {
12501249
throw_unsup_format!(
12511250
"the error {} couldn't be converted to a return value",
12521251
e
12531252
)
12541253
}
12551254
},
1256-
}))
1255+
}
1256+
1257+
Ok(())
12571258
}
12581259

12591260
fn closedir(&mut self, dirp_op: &OpTy<'tcx>) -> InterpResult<'tcx, Scalar> {

src/shims/unix/macos/foreign_items.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,16 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
3939
"stat" | "stat64" | "stat$INODE64" => {
4040
let [path, buf] =
4141
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
42-
let result = this.macos_fbsd_stat(path, buf)?;
43-
this.write_scalar(result, dest)?;
42+
this.macos_fbsd_stat(path, buf, dest)?;
4443
}
4544
"lstat" | "lstat64" | "lstat$INODE64" => {
4645
let [path, buf] =
4746
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
48-
let result = this.macos_fbsd_lstat(path, buf)?;
49-
this.write_scalar(result, dest)?;
47+
this.macos_fbsd_lstat(path, buf, dest)?;
5048
}
5149
"fstat" | "fstat64" | "fstat$INODE64" => {
5250
let [fd, buf] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
53-
let result = this.macos_fbsd_fstat(fd, buf)?;
54-
this.write_scalar(result, dest)?;
51+
this.macos_fbsd_fstat(fd, buf, dest)?;
5552
}
5653
"opendir$INODE64" => {
5754
let [name] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
@@ -61,8 +58,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
6158
"readdir_r" | "readdir_r$INODE64" => {
6259
let [dirp, entry, result] =
6360
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
64-
let result = this.macos_fbsd_readdir_r(dirp, entry, result)?;
65-
this.write_scalar(result, dest)?;
61+
this.macos_fbsd_readdir_r(dirp, entry, result, dest)?;
6662
}
6763
"realpath$DARWIN_EXTSN" => {
6864
let [path, resolved_path] =

0 commit comments

Comments
 (0)