Skip to content

Commit

Permalink
solarish stat following-up, supports for readdir/readdir64.
Browse files Browse the repository at this point in the history
  • Loading branch information
devnexen committed Dec 2, 2024
1 parent b27f1d6 commit a851fbf
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 18 deletions.
35 changes: 24 additions & 11 deletions src/shims/unix/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1048,10 +1048,12 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
}
}

fn linux_readdir64(&mut self, dirp_op: &OpTy<'tcx>) -> InterpResult<'tcx, Scalar> {
fn linux_solarish_readdir64(&mut self, dirp_op: &OpTy<'tcx>) -> InterpResult<'tcx, Scalar> {
let this = self.eval_context_mut();

this.assert_target_os("linux", "readdir64");
if !matches!(&*this.tcx.sess.target.os, "linux" | "solaris" | "illumos") {
panic!("`linux_solaris_readdir64` should not be called on {}", this.tcx.sess.target.os);
}

let dirp = this.read_target_usize(dirp_op)?;

Expand Down Expand Up @@ -1086,8 +1088,18 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
let name_bytes = name.as_encoded_bytes();
let name_len = u64::try_from(name_bytes.len()).unwrap();

let dirent64_layout = this.libc_ty_layout("dirent64");
let d_name_offset = dirent64_layout.fields.offset(4 /* d_name */).bytes();
let is_linux = matches!(&*this.tcx.sess.target.os, "linux");

let dirent64_layout = if is_linux {
this.libc_ty_layout("dirent64")
} else {
this.libc_ty_layout("dirent")
};
let d_name_offset = if is_linux {
dirent64_layout.fields.offset(4 /* d_name */).bytes()
} else {
dirent64_layout.fields.offset(3 /* d_name */).bytes()
};
let size = d_name_offset.strict_add(name_len);

let entry = this.allocate_ptr(
Expand All @@ -1105,17 +1117,18 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
let ino = 0u64;

let file_type = this.file_type_to_d_type(dir_entry.file_type())?;

this.write_int_fields_named(
&[
("d_ino", ino.into()),
("d_off", 0),
("d_reclen", size.into()),
("d_type", file_type.into()),
],
&[("d_ino", ino.into()), ("d_off", 0), ("d_reclen", size.into())],
&this.ptr_to_mplace(entry, dirent64_layout),
)?;

if is_linux {
this.write_int_fields_named(
&[("d_type", file_type.into())],
&this.ptr_to_mplace(entry, dirent64_layout),
)?;
}

let name_ptr = entry.wrapping_offset(Size::from_bytes(d_name_offset), this);
this.write_bytes_ptr(name_ptr, name_bytes.iter().copied())?;

Expand Down
2 changes: 1 addition & 1 deletion src/shims/unix/linux/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
"readdir64" => {
let [dirp] =
this.check_shim(abi, ExternAbi::C { unwind: false }, link_name, args)?;
let result = this.linux_readdir64(dirp)?;
let result = this.linux_solarish_readdir64(dirp)?;
this.write_scalar(result, dest)?;
}
"sync_file_range" => {
Expand Down
6 changes: 6 additions & 0 deletions src/shims/unix/solarish/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
let result = this.macos_fbsd_solaris_fstat(fd, buf)?;
this.write_scalar(result, dest)?;
}
"readdir" | "readdir64" => {
let [dirp] =
this.check_shim(abi, ExternAbi::C { unwind: false }, link_name, args)?;
let result = this.linux_solarish_readdir64(dirp)?;
this.write_scalar(result, dest)?;
}

// Miscellaneous
"___errno" => {
Expand Down
17 changes: 11 additions & 6 deletions tests/pass/shims/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,8 @@ fn main() {
test_file_sync();
test_errors();
test_rename();
// solarish needs to support readdir/readdir64 for these tests.
if cfg!(not(any(target_os = "solaris", target_os = "illumos"))) {
test_directory();
test_canonicalize();
}
test_directory();
test_canonicalize();
test_from_raw_os_error();
#[cfg(unix)]
test_pread_pwrite();
Expand Down Expand Up @@ -279,7 +276,15 @@ fn test_directory() {
.collect::<BTreeMap<_, _>>()
);
// Deleting the directory should fail, since it is not empty.
assert_eq!(ErrorKind::DirectoryNotEmpty, remove_dir(&dir_path).unwrap_err().kind());
let errno = if cfg!(any(target_os = "solaris", target_os = "illumos")) {
// Solaris/Illumos `rmdir` call set errno to EEXIST if directory contains
// other entries than `.` and `..`.
// https://docs.oracle.com/cd/E86824_01/html/E54765/rmdir-2.html
ErrorKind::AlreadyExists
} else {
ErrorKind::DirectoryNotEmpty
};
assert_eq!(errno, remove_dir(&dir_path).unwrap_err().kind());
// Clean up the files in the directory
remove_file(&path_1).unwrap();
remove_file(&path_2).unwrap();
Expand Down

0 comments on commit a851fbf

Please sign in to comment.