diff --git a/src/shims/posix/fs.rs b/src/shims/posix/fs.rs index 36c0760377..3ca1924bd2 100644 --- a/src/shims/posix/fs.rs +++ b/src/shims/posix/fs.rs @@ -16,6 +16,7 @@ use rustc_target::abi::{Align, Size}; use crate::*; use helpers::{check_arg_count, immty_from_int_checked, immty_from_uint_checked}; +use shims::os_str::os_str_to_bytes; use shims::time::system_time_to_duration; #[derive(Debug)] @@ -421,6 +422,24 @@ trait EvalContextExtPrivate<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, ' } } +/// An open directory, tracked by DirHandler. +#[derive(Debug)] +pub struct OpenDir { + /// The directory reader on the host. + read_dir: ReadDir, + /// The most recent entry returned by readdir() + entry: Pointer>, +} + +impl OpenDir { + fn new(read_dir: ReadDir) -> Self { + Self { + read_dir, + entry: Pointer::null(), + } + } +} + #[derive(Debug)] pub struct DirHandler { /// Directory iterators used to emulate libc "directory streams", as used in opendir, readdir, @@ -432,7 +451,7 @@ pub struct DirHandler { /// the corresponding ReadDir iterator from this map, and information from the next /// directory entry is returned. When closedir is called, the ReadDir iterator is removed from /// the map. - streams: FxHashMap, + streams: FxHashMap, /// ID number to be used by the next call to opendir next_id: u64, } @@ -441,7 +460,7 @@ impl DirHandler { fn insert_new(&mut self, read_dir: ReadDir) -> u64 { let id = self.next_id; self.next_id += 1; - self.streams.try_insert(id, read_dir).unwrap(); + self.streams.try_insert(id, OpenDir::new(read_dir)).unwrap(); id } } @@ -1196,6 +1215,85 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx } } + fn linux_readdir64(&mut self, dirp_op: &OpTy<'tcx, Tag>) -> InterpResult<'tcx, Scalar> { + let this = self.eval_context_mut(); + + this.assert_target_os("linux", "readdir64"); + + let dirp = this.read_scalar(dirp_op)?.to_machine_usize(this)?; + + // Reject if isolation is enabled. + if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op { + this.reject_in_isolation("`readdir`", reject_with)?; + let eacc = this.eval_libc("EACCES")?; + this.set_last_error(eacc)?; + return Ok(Scalar::null_ptr(this)); + } + + let open_dir = this.machine.dir_handler.streams.get_mut(&dirp).ok_or_else(|| { + err_unsup_format!("the DIR pointer passed to readdir64 did not come from opendir") + })?; + + let entry = match open_dir.read_dir.next() { + Some(Ok(dir_entry)) => { + let ino64_t_layout = this.libc_ty_layout("ino64_t")?; + let off64_t_layout = this.libc_ty_layout("off64_t")?; + let c_ushort_layout = this.libc_ty_layout("c_ushort")?; + let c_uchar_layout = this.libc_ty_layout("c_uchar")?; + + // If the host is a Unix system, fill in the inode number with its real value. + // If not, use 0 as a fallback value. + #[cfg(unix)] + let ino = std::os::unix::fs::DirEntryExt::ino(&dir_entry); + #[cfg(not(unix))] + let ino = 0u64; + + let file_type = this.file_type_to_d_type(dir_entry.file_type())?; + + let imms = [ + immty_from_uint_checked(ino, ino64_t_layout)?, // d_ino + immty_from_uint_checked(0u128, off64_t_layout)?, // d_off + immty_from_uint_checked(0u128, c_ushort_layout)?, // d_reclen + immty_from_int_checked(file_type, c_uchar_layout)?, // d_type + ]; + let name = dir_entry.file_name(); + let name_bytes = os_str_to_bytes(&name)?; + let name_offset = imms.iter() + .map(|imm| imm.layout.size.bytes()) + .sum::(); + let size = name_offset + .checked_add(u64::try_from(name_bytes.len()).unwrap()) + .unwrap() + .checked_add(1) + .unwrap(); + + let entry = this.malloc(size, /*zero_init:*/ false, MiriMemoryKind::C)?; + let entry_layout = this.layout_of(this.tcx.mk_array(this.tcx.types.u8, size))?; + let entry_place = MPlaceTy::from_aligned_ptr(entry, entry_layout); + this.write_packed_immediates(&entry_place, &imms)?; + + let name_ptr = entry.offset(Size::from_bytes(name_offset), this)?; + this.memory.write_bytes(name_ptr, name_bytes.iter().copied().chain(std::iter::once(0)))?; + + entry + }, + None => { + // end of stream: return NULL + Pointer::null() + } + Some(Err(e)) => { + this.set_last_error_from_io_error(e.kind())?; + Pointer::null() + } + }; + + let open_dir = this.machine.dir_handler.streams.get_mut(&dirp).unwrap(); + let old_entry = std::mem::replace(&mut open_dir.entry, entry); + this.free(old_entry, MiriMemoryKind::C)?; + + Ok(Scalar::from_maybe_pointer(entry, this)) + } + fn linux_readdir64_r( &mut self, dirp_op: &OpTy<'tcx, Tag>, @@ -1215,10 +1313,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx return this.handle_not_found(); } - let dir_iter = this.machine.dir_handler.streams.get_mut(&dirp).ok_or_else(|| { + let open_dir = this.machine.dir_handler.streams.get_mut(&dirp).ok_or_else(|| { err_unsup_format!("the DIR pointer passed to readdir64_r did not come from opendir") })?; - match dir_iter.next() { + match open_dir.read_dir.next() { Some(Ok(dir_entry)) => { // Write into entry, write pointer to result, return 0 on success. // The name is written with write_os_str_to_c_str, while the rest of the @@ -1314,10 +1412,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx return this.handle_not_found(); } - let dir_iter = this.machine.dir_handler.streams.get_mut(&dirp).ok_or_else(|| { + let open_dir = this.machine.dir_handler.streams.get_mut(&dirp).ok_or_else(|| { err_unsup_format!("the DIR pointer passed to readdir_r did not come from opendir") })?; - match dir_iter.next() { + match open_dir.read_dir.next() { Some(Ok(dir_entry)) => { // Write into entry, write pointer to result, return 0 on success. // The name is written with write_os_str_to_c_str, while the rest of the @@ -1408,8 +1506,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx return this.handle_not_found(); } - if let Some(dir_iter) = this.machine.dir_handler.streams.remove(&dirp) { - drop(dir_iter); + if let Some(open_dir) = this.machine.dir_handler.streams.remove(&dirp) { + this.free(open_dir.entry, MiriMemoryKind::C)?; + drop(open_dir); Ok(0) } else { this.handle_not_found() diff --git a/src/shims/posix/linux/foreign_items.rs b/src/shims/posix/linux/foreign_items.rs index 8d0f8487f5..39163c9514 100644 --- a/src/shims/posix/linux/foreign_items.rs +++ b/src/shims/posix/linux/foreign_items.rs @@ -43,6 +43,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx let result = this.opendir(name)?; this.write_scalar(result, dest)?; } + "readdir64" => { + let &[ref dirp] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?; + let result = this.linux_readdir64(dirp)?; + this.write_scalar(result, dest)?; + } "readdir64_r" => { let &[ref dirp, ref entry, ref result] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?; diff --git a/tests/run-pass/fs.rs b/tests/run-pass/fs.rs index 568b7ab4e3..3e44b8715f 100644 --- a/tests/run-pass/fs.rs +++ b/tests/run-pass/fs.rs @@ -331,19 +331,17 @@ fn test_directory() { let path_2 = dir_path.join("test_file_2"); drop(File::create(&path_2).unwrap()); // Test that the files are present inside the directory - /* FIXME(1966) disabled due to missing readdir support let dir_iter = read_dir(&dir_path).unwrap(); let mut file_names = dir_iter.map(|e| e.unwrap().file_name()).collect::>(); file_names.sort_unstable(); - assert_eq!(file_names, vec!["test_file_1", "test_file_2"]); */ + assert_eq!(file_names, vec!["test_file_1", "test_file_2"]); // Clean up the files in the directory remove_file(&path_1).unwrap(); remove_file(&path_2).unwrap(); // Now there should be nothing left in the directory. - /* FIXME(1966) disabled due to missing readdir support dir_iter = read_dir(&dir_path).unwrap(); let file_names = dir_iter.map(|e| e.unwrap().file_name()).collect::>(); - assert!(file_names.is_empty());*/ + assert!(file_names.is_empty()); // Deleting the directory should succeed. remove_dir(&dir_path).unwrap();