Skip to content

Commit

Permalink
Implement a readdir64() shim for Linux
Browse files Browse the repository at this point in the history
Partial fix for #1966.
  • Loading branch information
tavianator committed Mar 7, 2022
1 parent 64e4e2a commit b3165fa
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 64 deletions.
118 changes: 64 additions & 54 deletions src/shims/posix/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -421,6 +422,22 @@ 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<Option<Tag>>,
}

impl OpenDir {
fn new(read_dir: ReadDir) -> Self {
// We rely on `free` being a NOP on null pointers.
Self { read_dir, entry: Pointer::null() }
}
}

#[derive(Debug)]
pub struct DirHandler {
/// Directory iterators used to emulate libc "directory streams", as used in opendir, readdir,
Expand All @@ -432,7 +449,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<u64, ReadDir>,
streams: FxHashMap<u64, OpenDir>,
/// ID number to be used by the next call to opendir
next_id: u64,
}
Expand All @@ -441,7 +458,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
}
}
Expand Down Expand Up @@ -1207,32 +1224,29 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
}
}

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

this.assert_target_os("linux", "readdir64_r");
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("`readdir64_r`", reject_with)?;
// Set error code as "EBADF" (bad fd)
return this.handle_not_found();
this.reject_in_isolation("`readdir`", reject_with)?;
let eacc = this.eval_libc("EBADF")?;
this.set_last_error(eacc)?;
return Ok(Scalar::null_ptr(this));
}

let dir_iter = 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")
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")
})?;
match dir_iter.next() {

let entry = 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
// Write the directory entry into a newly allocated buffer.
// The name is written with write_bytes, while the rest of the
// dirent64 struct is written using write_packed_immediates.

// For reference:
Expand All @@ -1244,22 +1258,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
// pub d_name: [c_char; 256],
// }

let entry_place = this.deref_operand(entry_op)?;
let name_place = this.mplace_field(&entry_place, 4)?;
let mut name = dir_entry.file_name(); // not a Path as there are no separators!
name.push("\0"); // Add a NUL terminator
let name_bytes = os_str_to_bytes(&name)?;
let name_len = u64::try_from(name_bytes.len()).unwrap();

let file_name = dir_entry.file_name(); // not a Path as there are no separators!
let (name_fits, _) = this.write_os_str_to_c_str(
&file_name,
name_place.ptr,
name_place.layout.size.bytes(),
)?;
if !name_fits {
throw_unsup_format!(
"a directory entry had a name too large to fit in libc::dirent64"
);
}
let dirent64_layout = this.libc_ty_layout("dirent64")?;
let d_name_offset = dirent64_layout.fields.offset(4 /* d_name */).bytes();
let size = d_name_offset.checked_add(name_len).unwrap();

let entry = this.malloc(size, /*zero_init:*/ false, MiriMemoryKind::Runtime)?;

let entry_place = this.deref_operand(entry_op)?;
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")?;
Expand All @@ -1277,33 +1286,33 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
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_uint_checked(size, c_ushort_layout)?, // d_reclen
immty_from_int_checked(file_type, c_uchar_layout)?, // d_type
];
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 result_place = this.deref_operand(result_op)?;
this.write_scalar(this.read_scalar(entry_op)?, &result_place.into())?;
let name_ptr = entry.offset(Size::from_bytes(d_name_offset), this)?;
this.memory.write_bytes(name_ptr, name_bytes.iter().copied())?;

Ok(0)
entry
}
None => {
// end of stream: return 0, assign *result=NULL
this.write_null(&this.deref_operand(result_op)?.into())?;
Ok(0)
// end of stream: return NULL
Pointer::null()
}
Some(Err(e)) =>
match e.raw_os_error() {
// return positive error number on error
Some(error) => Ok(error),
None => {
throw_unsup_format!(
"the error {} couldn't be converted to a return value",
e
)
}
},
}
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::Runtime)?;

Ok(Scalar::from_maybe_pointer(entry, this))
}

fn macos_readdir_r(
Expand All @@ -1325,10 +1334,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
Expand Down Expand Up @@ -1419,8 +1428,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::Runtime)?;
drop(open_dir);
Ok(0)
} else {
this.handle_not_found()
Expand Down
8 changes: 4 additions & 4 deletions src/shims/posix/linux/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
let result = this.opendir(name)?;
this.write_scalar(result, dest)?;
}
"readdir64_r" => {
let &[ref dirp, ref entry, ref result] =
"readdir64" => {
let &[ref dirp] =
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
let result = this.linux_readdir64_r(dirp, entry, result)?;
this.write_scalar(Scalar::from_i32(result), dest)?;
let result = this.linux_readdir64(dirp)?;
this.write_scalar(result, dest)?;
}
"ftruncate64" => {
let &[ref fd, ref length] =
Expand Down
12 changes: 6 additions & 6 deletions tests/run-pass/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
extern crate libc;

use std::ffi::CString;
use std::fs::{create_dir, remove_dir, remove_dir_all, remove_file, rename, File, OpenOptions};
use std::fs::{
create_dir, read_dir, remove_dir, remove_dir_all, remove_file, rename, File, OpenOptions,
};
use std::io::{Error, ErrorKind, Read, Result, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};

Expand Down Expand Up @@ -374,19 +376,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::<Vec<_>>();
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 dir_iter = read_dir(&dir_path).unwrap();
let file_names = dir_iter.map(|e| e.unwrap().file_name()).collect::<Vec<_>>();
assert!(file_names.is_empty());*/
assert!(file_names.is_empty());

// Deleting the directory should succeed.
remove_dir(&dir_path).unwrap();
Expand Down

0 comments on commit b3165fa

Please sign in to comment.