2020-02-09 14:43:45 -06:00
|
|
|
use std::collections::BTreeMap;
|
2020-01-25 12:57:15 -06:00
|
|
|
use std::collections::HashMap;
|
2019-12-23 05:56:23 -06:00
|
|
|
use std::convert::{TryFrom, TryInto};
|
2020-01-25 17:36:51 -06:00
|
|
|
use std::fs::{read_dir, remove_dir, remove_file, rename, DirBuilder, File, FileType, OpenOptions, ReadDir};
|
2020-01-26 12:36:36 -06:00
|
|
|
use std::io::{Read, Seek, SeekFrom, Write};
|
2019-11-30 14:09:52 -06:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::time::SystemTime;
|
2019-09-24 17:28:00 -05:00
|
|
|
|
2019-12-23 05:56:23 -06:00
|
|
|
use rustc::ty::layout::{Align, LayoutOf, Size};
|
2019-09-30 14:21:45 -05:00
|
|
|
|
2019-09-24 17:28:00 -05:00
|
|
|
use crate::stacked_borrows::Tag;
|
|
|
|
use crate::*;
|
2019-11-30 14:09:52 -06:00
|
|
|
use helpers::immty_from_uint_checked;
|
|
|
|
use shims::time::system_time_to_duration;
|
2019-09-24 17:28:00 -05:00
|
|
|
|
2019-10-20 05:20:48 -05:00
|
|
|
#[derive(Debug)]
|
2020-02-16 16:59:54 -06:00
|
|
|
pub struct FileHandle {
|
|
|
|
file: File,
|
|
|
|
writable: bool,
|
2019-09-30 11:51:09 -05:00
|
|
|
}
|
|
|
|
|
2020-02-16 16:59:54 -06:00
|
|
|
#[derive(Debug, Default)]
|
2019-09-24 17:28:00 -05:00
|
|
|
pub struct FileHandler {
|
2020-02-09 14:43:45 -06:00
|
|
|
handles: BTreeMap<i32, FileHandle>,
|
2019-09-24 17:28:00 -05:00
|
|
|
}
|
|
|
|
|
2020-02-18 18:06:33 -06:00
|
|
|
// fd numbers 0, 1, and 2 are reserved for stdin, stdout, and stderr
|
|
|
|
const MIN_NORMAL_FILE_FD: i32 = 3;
|
|
|
|
|
2020-01-30 18:38:30 -06:00
|
|
|
impl FileHandler {
|
2020-02-09 14:43:45 -06:00
|
|
|
fn insert_fd(&mut self, file_handle: FileHandle) -> i32 {
|
2020-02-18 18:06:33 -06:00
|
|
|
self.insert_fd_with_min_fd(file_handle, 0)
|
2020-01-30 18:38:30 -06:00
|
|
|
}
|
|
|
|
|
2020-02-09 14:43:45 -06:00
|
|
|
fn insert_fd_with_min_fd(&mut self, file_handle: FileHandle, min_fd: i32) -> i32 {
|
2020-02-18 18:06:33 -06:00
|
|
|
let min_fd = std::cmp::max(min_fd, MIN_NORMAL_FILE_FD);
|
2020-02-16 16:51:02 -06:00
|
|
|
|
|
|
|
// Find the lowest unused FD, starting from min_fd. If the first such unused FD is in
|
|
|
|
// between used FDs, the find_map combinator will return it. If the first such unused FD
|
|
|
|
// is after all other used FDs, the find_map combinator will return None, and we will use
|
|
|
|
// the FD following the greatest FD thus far.
|
2020-02-09 14:43:45 -06:00
|
|
|
let candidate_new_fd = self
|
|
|
|
.handles
|
|
|
|
.range(min_fd..)
|
|
|
|
.zip(min_fd..)
|
|
|
|
.find_map(|((fd, _fh), counter)| {
|
|
|
|
if *fd != counter {
|
|
|
|
// There was a gap in the fds stored, return the first unused one
|
|
|
|
// (note that this relies on BTreeMap iterating in key order)
|
|
|
|
Some(counter)
|
|
|
|
} else {
|
|
|
|
// This fd is used, keep going
|
|
|
|
None
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let new_fd = candidate_new_fd.unwrap_or_else(|| {
|
|
|
|
// find_map ran out of BTreeMap entries before finding a free fd, use one plus the
|
|
|
|
// maximum fd in the map
|
2020-02-16 16:51:02 -06:00
|
|
|
self.handles.last_entry().map(|entry| entry.key() + 1).unwrap_or(min_fd)
|
2020-02-09 14:43:45 -06:00
|
|
|
});
|
2020-02-16 16:51:02 -06:00
|
|
|
|
2020-02-09 14:43:45 -06:00
|
|
|
self.handles.insert(new_fd, file_handle).unwrap_none();
|
|
|
|
new_fd
|
2020-01-30 18:38:30 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-23 11:48:38 -06:00
|
|
|
impl<'mir, 'tcx> EvalContextExtPrivate<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
|
|
|
|
trait EvalContextExtPrivate<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
|
|
|
|
/// Emulate `stat` or `lstat` on the `macos` platform. This function is not intended to be
|
|
|
|
/// called directly from `emulate_foreign_item_by_name`, so it does not check if isolation is
|
|
|
|
/// disabled or if the target platform is the correct one. Please use `macos_stat` or
|
|
|
|
/// `macos_lstat` instead.
|
|
|
|
fn macos_stat_or_lstat(
|
|
|
|
&mut self,
|
|
|
|
follow_symlink: bool,
|
|
|
|
path_op: OpTy<'tcx, Tag>,
|
|
|
|
buf_op: OpTy<'tcx, Tag>,
|
|
|
|
) -> InterpResult<'tcx, i32> {
|
|
|
|
let this = self.eval_context_mut();
|
|
|
|
|
|
|
|
let path_scalar = this.read_scalar(path_op)?.not_undef()?;
|
|
|
|
let path: PathBuf = this.read_os_str_from_c_str(path_scalar)?.into();
|
|
|
|
|
|
|
|
let metadata = match FileMetadata::from_path(this, path, follow_symlink)? {
|
|
|
|
Some(metadata) => metadata,
|
|
|
|
None => return Ok(-1),
|
|
|
|
};
|
|
|
|
this.macos_stat_write_buf(metadata, buf_op)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn macos_stat_write_buf(
|
|
|
|
&mut self,
|
|
|
|
metadata: FileMetadata,
|
|
|
|
buf_op: OpTy<'tcx, Tag>,
|
|
|
|
) -> InterpResult<'tcx, i32> {
|
|
|
|
let this = self.eval_context_mut();
|
|
|
|
|
|
|
|
let mode: u16 = metadata.mode.to_u16()?;
|
|
|
|
|
|
|
|
let (access_sec, access_nsec) = metadata.accessed.unwrap_or((0, 0));
|
|
|
|
let (created_sec, created_nsec) = metadata.created.unwrap_or((0, 0));
|
|
|
|
let (modified_sec, modified_nsec) = metadata.modified.unwrap_or((0, 0));
|
|
|
|
|
|
|
|
let dev_t_layout = this.libc_ty_layout("dev_t")?;
|
|
|
|
let mode_t_layout = this.libc_ty_layout("mode_t")?;
|
|
|
|
let nlink_t_layout = this.libc_ty_layout("nlink_t")?;
|
|
|
|
let ino_t_layout = this.libc_ty_layout("ino_t")?;
|
|
|
|
let uid_t_layout = this.libc_ty_layout("uid_t")?;
|
|
|
|
let gid_t_layout = this.libc_ty_layout("gid_t")?;
|
|
|
|
let time_t_layout = this.libc_ty_layout("time_t")?;
|
|
|
|
let long_layout = this.libc_ty_layout("c_long")?;
|
|
|
|
let off_t_layout = this.libc_ty_layout("off_t")?;
|
|
|
|
let blkcnt_t_layout = this.libc_ty_layout("blkcnt_t")?;
|
|
|
|
let blksize_t_layout = this.libc_ty_layout("blksize_t")?;
|
|
|
|
let uint32_t_layout = this.libc_ty_layout("uint32_t")?;
|
|
|
|
|
|
|
|
// We need to add 32 bits of padding after `st_rdev` if we are on a 64-bit platform.
|
|
|
|
let pad_layout = if this.tcx.sess.target.ptr_width == 64 {
|
|
|
|
uint32_t_layout
|
|
|
|
} else {
|
|
|
|
this.layout_of(this.tcx.mk_unit())?
|
|
|
|
};
|
|
|
|
|
|
|
|
let imms = [
|
|
|
|
immty_from_uint_checked(0u128, dev_t_layout)?, // st_dev
|
|
|
|
immty_from_uint_checked(mode, mode_t_layout)?, // st_mode
|
|
|
|
immty_from_uint_checked(0u128, nlink_t_layout)?, // st_nlink
|
|
|
|
immty_from_uint_checked(0u128, ino_t_layout)?, // st_ino
|
|
|
|
immty_from_uint_checked(0u128, uid_t_layout)?, // st_uid
|
|
|
|
immty_from_uint_checked(0u128, gid_t_layout)?, // st_gid
|
|
|
|
immty_from_uint_checked(0u128, dev_t_layout)?, // st_rdev
|
|
|
|
immty_from_uint_checked(0u128, pad_layout)?, // padding for 64-bit targets
|
|
|
|
immty_from_uint_checked(access_sec, time_t_layout)?, // st_atime
|
|
|
|
immty_from_uint_checked(access_nsec, long_layout)?, // st_atime_nsec
|
|
|
|
immty_from_uint_checked(modified_sec, time_t_layout)?, // st_mtime
|
|
|
|
immty_from_uint_checked(modified_nsec, long_layout)?, // st_mtime_nsec
|
|
|
|
immty_from_uint_checked(0u128, time_t_layout)?, // st_ctime
|
|
|
|
immty_from_uint_checked(0u128, long_layout)?, // st_ctime_nsec
|
|
|
|
immty_from_uint_checked(created_sec, time_t_layout)?, // st_birthtime
|
|
|
|
immty_from_uint_checked(created_nsec, long_layout)?, // st_birthtime_nsec
|
|
|
|
immty_from_uint_checked(metadata.size, off_t_layout)?, // st_size
|
|
|
|
immty_from_uint_checked(0u128, blkcnt_t_layout)?, // st_blocks
|
|
|
|
immty_from_uint_checked(0u128, blksize_t_layout)?, // st_blksize
|
|
|
|
immty_from_uint_checked(0u128, uint32_t_layout)?, // st_flags
|
|
|
|
immty_from_uint_checked(0u128, uint32_t_layout)?, // st_gen
|
|
|
|
];
|
|
|
|
|
|
|
|
let buf = this.deref_operand(buf_op)?;
|
|
|
|
this.write_packed_immediates(buf, &imms)?;
|
|
|
|
|
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Function used when a handle is not found inside `FileHandler`. It returns `Ok(-1)`and sets
|
|
|
|
/// the last OS error to `libc::EBADF` (invalid file descriptor). This function uses
|
|
|
|
/// `T: From<i32>` instead of `i32` directly because some fs functions return different integer
|
|
|
|
/// types (like `read`, that returns an `i64`).
|
|
|
|
fn handle_not_found<T: From<i32>>(&mut self) -> InterpResult<'tcx, T> {
|
|
|
|
let this = self.eval_context_mut();
|
|
|
|
let ebadf = this.eval_libc("EBADF")?;
|
|
|
|
this.set_last_error(ebadf)?;
|
|
|
|
Ok((-1).into())
|
|
|
|
}
|
2020-01-25 17:36:51 -06:00
|
|
|
|
|
|
|
fn file_type_to_d_type(&mut self, file_type: std::io::Result<FileType>) -> InterpResult<'tcx, i32> {
|
|
|
|
let this = self.eval_context_mut();
|
|
|
|
match file_type {
|
|
|
|
Ok(file_type) => {
|
|
|
|
if file_type.is_dir() {
|
|
|
|
Ok(this.eval_libc("DT_DIR")?.to_u8()? as i32)
|
|
|
|
} else if file_type.is_file() {
|
|
|
|
Ok(this.eval_libc("DT_REG")?.to_u8()? as i32)
|
|
|
|
} else if file_type.is_symlink() {
|
|
|
|
Ok(this.eval_libc("DT_LNK")?.to_u8()? as i32)
|
|
|
|
} else {
|
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
|
|
|
use std::os::unix::fs::FileTypeExt;
|
|
|
|
if file_type.is_block_device() {
|
|
|
|
Ok(this.eval_libc("DT_BLK")?.to_u8()? as i32)
|
|
|
|
} else if file_type.is_char_device() {
|
|
|
|
Ok(this.eval_libc("DT_CHR")?.to_u8()? as i32)
|
|
|
|
} else if file_type.is_fifo() {
|
|
|
|
Ok(this.eval_libc("DT_FIFO")?.to_u8()? as i32)
|
|
|
|
} else if file_type.is_socket() {
|
|
|
|
Ok(this.eval_libc("DT_SOCK")?.to_u8()? as i32)
|
|
|
|
} else {
|
|
|
|
Ok(this.eval_libc("DT_UNKNOWN")?.to_u8()? as i32)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
Ok(this.eval_libc("DT_UNKNOWN")?.to_u8()? as i32)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => return match e.raw_os_error() {
|
|
|
|
Some(error) => Ok(error),
|
|
|
|
None => throw_unsup_format!("The error {} couldn't be converted to a return value", e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-23 11:48:38 -06:00
|
|
|
}
|
|
|
|
|
2020-01-25 12:57:15 -06:00
|
|
|
#[derive(Debug, Default)]
|
|
|
|
pub struct DirHandler {
|
|
|
|
streams: HashMap<Pointer<Tag>, ReadDir>,
|
|
|
|
}
|
|
|
|
|
2019-09-24 17:28:00 -05:00
|
|
|
impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
|
|
|
|
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
|
|
|
|
fn open(
|
|
|
|
&mut self,
|
|
|
|
path_op: OpTy<'tcx, Tag>,
|
|
|
|
flag_op: OpTy<'tcx, Tag>,
|
|
|
|
) -> InterpResult<'tcx, i32> {
|
|
|
|
let this = self.eval_context_mut();
|
|
|
|
|
2019-10-14 15:36:15 -05:00
|
|
|
this.check_no_isolation("open")?;
|
2019-09-24 17:28:00 -05:00
|
|
|
|
|
|
|
let flag = this.read_scalar(flag_op)?.to_i32()?;
|
|
|
|
|
2019-09-30 11:46:07 -05:00
|
|
|
let mut options = OpenOptions::new();
|
|
|
|
|
2019-10-14 16:42:29 -05:00
|
|
|
let o_rdonly = this.eval_libc_i32("O_RDONLY")?;
|
|
|
|
let o_wronly = this.eval_libc_i32("O_WRONLY")?;
|
|
|
|
let o_rdwr = this.eval_libc_i32("O_RDWR")?;
|
|
|
|
// The first two bits of the flag correspond to the access mode in linux, macOS and
|
|
|
|
// windows. We need to check that in fact the access mode flags for the current platform
|
|
|
|
// only use these two bits, otherwise we are in an unsupported platform and should error.
|
|
|
|
if (o_rdonly | o_wronly | o_rdwr) & !0b11 != 0 {
|
|
|
|
throw_unsup_format!("Access mode flags on this platform are unsupported");
|
|
|
|
}
|
2019-12-30 16:26:17 -06:00
|
|
|
let mut writable = true;
|
2019-12-27 07:34:35 -06:00
|
|
|
|
2019-10-14 16:42:29 -05:00
|
|
|
// Now we check the access mode
|
2019-09-30 11:46:07 -05:00
|
|
|
let access_mode = flag & 0b11;
|
|
|
|
|
2019-10-14 16:42:29 -05:00
|
|
|
if access_mode == o_rdonly {
|
2019-12-30 16:26:17 -06:00
|
|
|
writable = false;
|
2019-09-30 11:46:07 -05:00
|
|
|
options.read(true);
|
2019-10-14 16:42:29 -05:00
|
|
|
} else if access_mode == o_wronly {
|
2019-09-30 11:46:07 -05:00
|
|
|
options.write(true);
|
2019-10-14 16:42:29 -05:00
|
|
|
} else if access_mode == o_rdwr {
|
2019-09-30 11:46:07 -05:00
|
|
|
options.read(true).write(true);
|
|
|
|
} else {
|
|
|
|
throw_unsup_format!("Unsupported access mode {:#x}", access_mode);
|
|
|
|
}
|
2019-10-11 05:30:50 -05:00
|
|
|
// We need to check that there aren't unsupported options in `flag`. For this we try to
|
|
|
|
// reproduce the content of `flag` in the `mirror` variable using only the supported
|
|
|
|
// options.
|
|
|
|
let mut mirror = access_mode;
|
2019-09-30 11:46:07 -05:00
|
|
|
|
2019-10-11 05:30:50 -05:00
|
|
|
let o_append = this.eval_libc_i32("O_APPEND")?;
|
|
|
|
if flag & o_append != 0 {
|
2019-09-30 11:46:07 -05:00
|
|
|
options.append(true);
|
2019-10-11 05:30:50 -05:00
|
|
|
mirror |= o_append;
|
2019-09-30 11:46:07 -05:00
|
|
|
}
|
2019-10-11 05:30:50 -05:00
|
|
|
let o_trunc = this.eval_libc_i32("O_TRUNC")?;
|
|
|
|
if flag & o_trunc != 0 {
|
2019-09-30 11:46:07 -05:00
|
|
|
options.truncate(true);
|
2019-10-11 05:30:50 -05:00
|
|
|
mirror |= o_trunc;
|
2019-09-30 11:46:07 -05:00
|
|
|
}
|
2019-10-11 05:30:50 -05:00
|
|
|
let o_creat = this.eval_libc_i32("O_CREAT")?;
|
|
|
|
if flag & o_creat != 0 {
|
2019-09-30 11:46:07 -05:00
|
|
|
options.create(true);
|
2019-10-11 05:30:50 -05:00
|
|
|
mirror |= o_creat;
|
|
|
|
}
|
|
|
|
let o_cloexec = this.eval_libc_i32("O_CLOEXEC")?;
|
|
|
|
if flag & o_cloexec != 0 {
|
2019-10-12 20:12:26 -05:00
|
|
|
// We do not need to do anything for this flag because `std` already sets it.
|
|
|
|
// (Technically we do not support *not* setting this flag, but we ignore that.)
|
2019-10-11 05:30:50 -05:00
|
|
|
mirror |= o_cloexec;
|
|
|
|
}
|
|
|
|
// If `flag` is not equal to `mirror`, there is an unsupported option enabled in `flag`,
|
|
|
|
// then we throw an error.
|
|
|
|
if flag != mirror {
|
2019-10-12 20:12:26 -05:00
|
|
|
throw_unsup_format!("unsupported flags {:#x}", flag & !mirror);
|
2019-09-24 17:28:00 -05:00
|
|
|
}
|
|
|
|
|
2019-12-04 03:43:36 -06:00
|
|
|
let path = this.read_os_str_from_c_str(this.read_scalar(path_op)?.not_undef()?)?;
|
2019-09-30 11:46:07 -05:00
|
|
|
|
2019-11-30 14:09:52 -06:00
|
|
|
let fd = options.open(&path).map(|file| {
|
2020-01-30 18:38:30 -06:00
|
|
|
let fh = &mut this.machine.file_handler;
|
2020-02-16 16:59:54 -06:00
|
|
|
fh.insert_fd(FileHandle { file, writable })
|
2019-09-30 14:07:08 -05:00
|
|
|
});
|
|
|
|
|
2019-10-18 14:33:25 -05:00
|
|
|
this.try_unwrap_io_result(fd)
|
2019-09-24 17:28:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn fcntl(
|
|
|
|
&mut self,
|
|
|
|
fd_op: OpTy<'tcx, Tag>,
|
|
|
|
cmd_op: OpTy<'tcx, Tag>,
|
2020-02-16 16:51:02 -06:00
|
|
|
start_op: Option<OpTy<'tcx, Tag>>,
|
2019-09-24 17:28:00 -05:00
|
|
|
) -> InterpResult<'tcx, i32> {
|
|
|
|
let this = self.eval_context_mut();
|
|
|
|
|
2019-10-14 15:36:15 -05:00
|
|
|
this.check_no_isolation("fcntl")?;
|
2019-09-24 17:28:00 -05:00
|
|
|
|
|
|
|
let fd = this.read_scalar(fd_op)?.to_i32()?;
|
|
|
|
let cmd = this.read_scalar(cmd_op)?.to_i32()?;
|
2019-10-21 06:24:56 -05:00
|
|
|
// We only support getting the flags for a descriptor.
|
2019-10-11 12:17:54 -05:00
|
|
|
if cmd == this.eval_libc_i32("F_GETFD")? {
|
2019-10-12 20:12:26 -05:00
|
|
|
// Currently this is the only flag that `F_GETFD` returns. It is OK to just return the
|
|
|
|
// `FD_CLOEXEC` value without checking if the flag is set for the file because `std`
|
|
|
|
// always sets this flag when opening a file. However we still need to check that the
|
|
|
|
// file itself is open.
|
2019-10-24 08:44:13 -05:00
|
|
|
if this.machine.file_handler.handles.contains_key(&fd) {
|
|
|
|
Ok(this.eval_libc_i32("FD_CLOEXEC")?)
|
|
|
|
} else {
|
|
|
|
this.handle_not_found()
|
|
|
|
}
|
2020-02-09 14:43:45 -06:00
|
|
|
} else if cmd == this.eval_libc_i32("F_DUPFD")?
|
|
|
|
|| cmd == this.eval_libc_i32("F_DUPFD_CLOEXEC")?
|
|
|
|
{
|
2020-01-28 23:18:09 -06:00
|
|
|
// Note that we always assume the FD_CLOEXEC flag is set for every open file, in part
|
|
|
|
// because exec() isn't supported. The F_DUPFD and F_DUPFD_CLOEXEC commands only
|
2020-01-30 18:31:34 -06:00
|
|
|
// differ in whether the FD_CLOEXEC flag is pre-set on the new file descriptor,
|
2020-01-28 23:18:09 -06:00
|
|
|
// thus they can share the same implementation here.
|
2020-02-18 18:06:33 -06:00
|
|
|
if fd < MIN_NORMAL_FILE_FD {
|
2020-02-16 16:59:54 -06:00
|
|
|
throw_unsup_format!("Duplicating file descriptors for stdin, stdout, or stderr is not supported")
|
|
|
|
}
|
2020-02-16 16:51:02 -06:00
|
|
|
let start_op = start_op.ok_or_else(|| {
|
2020-02-09 14:43:45 -06:00
|
|
|
err_unsup_format!(
|
|
|
|
"fcntl with command F_DUPFD or F_DUPFD_CLOEXEC requires a third argument"
|
|
|
|
)
|
|
|
|
})?;
|
2020-02-16 16:51:02 -06:00
|
|
|
let start = this.read_scalar(start_op)?.to_i32()?;
|
2020-01-26 11:19:06 -06:00
|
|
|
let fh = &mut this.machine.file_handler;
|
|
|
|
let (file_result, writable) = match fh.handles.get(&fd) {
|
2020-02-16 16:59:54 -06:00
|
|
|
Some(FileHandle { file, writable }) => (file.try_clone(), *writable),
|
2020-01-26 11:19:06 -06:00
|
|
|
None => return this.handle_not_found(),
|
|
|
|
};
|
|
|
|
let fd_result = file_result.map(|duplicated| {
|
2020-02-16 16:59:54 -06:00
|
|
|
fh.insert_fd_with_min_fd(FileHandle { file: duplicated, writable }, start)
|
2020-01-26 11:19:06 -06:00
|
|
|
});
|
|
|
|
this.try_unwrap_io_result(fd_result)
|
2019-09-24 17:28:00 -05:00
|
|
|
} else {
|
2019-10-11 12:17:54 -05:00
|
|
|
throw_unsup_format!("The {:#x} command is not supported for `fcntl`)", cmd);
|
2019-09-24 17:28:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn close(&mut self, fd_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
|
|
|
|
let this = self.eval_context_mut();
|
|
|
|
|
2019-10-14 15:36:15 -05:00
|
|
|
this.check_no_isolation("close")?;
|
2019-09-24 17:28:00 -05:00
|
|
|
|
|
|
|
let fd = this.read_scalar(fd_op)?.to_i32()?;
|
|
|
|
|
2020-02-16 16:59:54 -06:00
|
|
|
if let Some(FileHandle { file, writable }) = this.machine.file_handler.handles.remove(&fd) {
|
2019-12-27 08:11:21 -06:00
|
|
|
// We sync the file if it was opened in a mode different than read-only.
|
2020-02-09 14:43:45 -06:00
|
|
|
if writable {
|
2019-12-27 08:11:21 -06:00
|
|
|
// `File::sync_all` does the checks that are done when closing a file. We do this to
|
|
|
|
// to handle possible errors correctly.
|
2020-02-09 14:43:45 -06:00
|
|
|
let result = this.try_unwrap_io_result(file.sync_all().map(|_| 0i32));
|
2019-12-27 08:11:21 -06:00
|
|
|
// Now we actually close the file.
|
2020-02-09 14:43:45 -06:00
|
|
|
drop(file);
|
2019-12-27 08:11:21 -06:00
|
|
|
// And return the result.
|
|
|
|
result
|
|
|
|
} else {
|
|
|
|
// We drop the file, this closes it but ignores any errors produced when closing
|
2020-02-09 14:43:45 -06:00
|
|
|
// it. This is done because `File::sync_all` cannot be done over files like
|
2019-12-30 16:26:17 -06:00
|
|
|
// `/dev/urandom` which are read-only. Check
|
2019-12-27 08:11:21 -06:00
|
|
|
// https://github.com/rust-lang/miri/issues/999#issuecomment-568920439 for a deeper
|
|
|
|
// discussion.
|
2020-02-09 14:43:45 -06:00
|
|
|
drop(file);
|
2019-12-27 08:11:21 -06:00
|
|
|
Ok(0)
|
2019-12-27 07:34:35 -06:00
|
|
|
}
|
2019-10-24 08:44:13 -05:00
|
|
|
} else {
|
|
|
|
this.handle_not_found()
|
|
|
|
}
|
2019-09-24 17:28:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn read(
|
|
|
|
&mut self,
|
|
|
|
fd_op: OpTy<'tcx, Tag>,
|
|
|
|
buf_op: OpTy<'tcx, Tag>,
|
|
|
|
count_op: OpTy<'tcx, Tag>,
|
|
|
|
) -> InterpResult<'tcx, i64> {
|
|
|
|
let this = self.eval_context_mut();
|
|
|
|
|
2019-10-14 15:36:15 -05:00
|
|
|
this.check_no_isolation("read")?;
|
2019-09-24 17:28:00 -05:00
|
|
|
|
2019-11-17 02:30:32 -06:00
|
|
|
let fd = this.read_scalar(fd_op)?.to_i32()?;
|
|
|
|
let buf = this.read_scalar(buf_op)?.not_undef()?;
|
2019-12-23 05:56:23 -06:00
|
|
|
let count = this.read_scalar(count_op)?.to_machine_usize(&*this.tcx)?;
|
2019-11-14 03:29:43 -06:00
|
|
|
|
2019-11-17 02:30:32 -06:00
|
|
|
// Check that the *entire* buffer is actually valid memory.
|
2019-12-23 05:56:23 -06:00
|
|
|
this.memory.check_ptr_access(
|
|
|
|
buf,
|
|
|
|
Size::from_bytes(count),
|
|
|
|
Align::from_bytes(1).unwrap(),
|
|
|
|
)?;
|
2019-11-17 02:30:32 -06:00
|
|
|
|
|
|
|
// We cap the number of read bytes to the largest value that we are able to fit in both the
|
|
|
|
// host's and target's `isize`. This saves us from having to handle overflows later.
|
2019-12-23 05:56:23 -06:00
|
|
|
let count = count.min(this.isize_max() as u64).min(isize::max_value() as u64);
|
2019-10-24 08:44:13 -05:00
|
|
|
|
2020-02-16 16:59:54 -06:00
|
|
|
if let Some(FileHandle { file, writable: _ }) = this.machine.file_handler.handles.get_mut(&fd) {
|
2019-11-13 10:57:20 -06:00
|
|
|
// This can never fail because `count` was capped to be smaller than
|
|
|
|
// `isize::max_value()`.
|
2019-11-09 08:15:52 -06:00
|
|
|
let count = isize::try_from(count).unwrap();
|
2019-11-07 13:50:16 -06:00
|
|
|
// We want to read at most `count` bytes. We are sure that `count` is not negative
|
|
|
|
// because it was a target's `usize`. Also we are sure that its smaller than
|
|
|
|
// `usize::max_value()` because it is a host's `isize`.
|
|
|
|
let mut bytes = vec![0; count as usize];
|
2020-02-09 14:43:45 -06:00
|
|
|
let result = file
|
2019-11-09 08:15:52 -06:00
|
|
|
.read(&mut bytes)
|
2019-11-13 13:45:00 -06:00
|
|
|
// `File::read` never returns a value larger than `count`, so this cannot fail.
|
2019-11-09 08:15:52 -06:00
|
|
|
.map(|c| i64::try_from(c).unwrap());
|
2019-10-28 16:44:18 -05:00
|
|
|
|
2019-11-04 08:38:21 -06:00
|
|
|
match result {
|
2019-11-09 08:15:52 -06:00
|
|
|
Ok(read_bytes) => {
|
2019-11-05 15:47:24 -06:00
|
|
|
// If reading to `bytes` did not fail, we write those bytes to the buffer.
|
|
|
|
this.memory.write_bytes(buf, bytes)?;
|
|
|
|
Ok(read_bytes)
|
2019-11-09 08:15:52 -06:00
|
|
|
}
|
2019-11-04 08:38:21 -06:00
|
|
|
Err(e) => {
|
|
|
|
this.set_last_error_from_io_error(e)?;
|
|
|
|
Ok(-1)
|
2019-10-28 16:44:18 -05:00
|
|
|
}
|
2019-10-24 08:44:13 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.handle_not_found()
|
|
|
|
}
|
2019-09-30 15:18:23 -05:00
|
|
|
}
|
|
|
|
|
2019-09-30 11:46:07 -05:00
|
|
|
fn write(
|
|
|
|
&mut self,
|
|
|
|
fd_op: OpTy<'tcx, Tag>,
|
|
|
|
buf_op: OpTy<'tcx, Tag>,
|
|
|
|
count_op: OpTy<'tcx, Tag>,
|
|
|
|
) -> InterpResult<'tcx, i64> {
|
|
|
|
let this = self.eval_context_mut();
|
|
|
|
|
2019-10-14 15:36:15 -05:00
|
|
|
this.check_no_isolation("write")?;
|
2019-09-30 11:46:07 -05:00
|
|
|
|
2019-11-17 02:30:32 -06:00
|
|
|
let fd = this.read_scalar(fd_op)?.to_i32()?;
|
|
|
|
let buf = this.read_scalar(buf_op)?.not_undef()?;
|
2019-12-23 05:56:23 -06:00
|
|
|
let count = this.read_scalar(count_op)?.to_machine_usize(&*this.tcx)?;
|
2019-11-14 03:29:43 -06:00
|
|
|
|
2019-11-17 02:30:32 -06:00
|
|
|
// Check that the *entire* buffer is actually valid memory.
|
2019-12-23 05:56:23 -06:00
|
|
|
this.memory.check_ptr_access(
|
|
|
|
buf,
|
|
|
|
Size::from_bytes(count),
|
|
|
|
Align::from_bytes(1).unwrap(),
|
|
|
|
)?;
|
2019-11-17 02:30:32 -06:00
|
|
|
|
|
|
|
// We cap the number of written bytes to the largest value that we are able to fit in both the
|
|
|
|
// host's and target's `isize`. This saves us from having to handle overflows later.
|
2019-12-23 05:56:23 -06:00
|
|
|
let count = count.min(this.isize_max() as u64).min(isize::max_value() as u64);
|
2019-10-24 08:44:13 -05:00
|
|
|
|
2020-02-16 16:59:54 -06:00
|
|
|
if let Some(FileHandle { file, writable: _ }) = this.machine.file_handler.handles.get_mut(&fd) {
|
2019-10-24 08:44:13 -05:00
|
|
|
let bytes = this.memory.read_bytes(buf, Size::from_bytes(count))?;
|
2020-02-09 14:43:45 -06:00
|
|
|
let result = file.write(&bytes).map(|c| i64::try_from(c).unwrap());
|
2019-11-09 08:15:52 -06:00
|
|
|
this.try_unwrap_io_result(result)
|
2019-10-24 08:44:13 -05:00
|
|
|
} else {
|
|
|
|
this.handle_not_found()
|
|
|
|
}
|
2019-09-30 11:46:07 -05:00
|
|
|
}
|
|
|
|
|
2020-01-26 12:36:36 -06:00
|
|
|
fn lseek64(
|
|
|
|
&mut self,
|
|
|
|
fd_op: OpTy<'tcx, Tag>,
|
|
|
|
offset_op: OpTy<'tcx, Tag>,
|
|
|
|
whence_op: OpTy<'tcx, Tag>,
|
|
|
|
) -> InterpResult<'tcx, i64> {
|
|
|
|
let this = self.eval_context_mut();
|
|
|
|
|
|
|
|
this.check_no_isolation("lseek64")?;
|
|
|
|
|
|
|
|
let fd = this.read_scalar(fd_op)?.to_i32()?;
|
|
|
|
let offset = this.read_scalar(offset_op)?.to_i64()?;
|
|
|
|
let whence = this.read_scalar(whence_op)?.to_i32()?;
|
|
|
|
|
|
|
|
let seek_from = if whence == this.eval_libc_i32("SEEK_SET")? {
|
|
|
|
SeekFrom::Start(offset as u64)
|
|
|
|
} else if whence == this.eval_libc_i32("SEEK_CUR")? {
|
|
|
|
SeekFrom::Current(offset)
|
|
|
|
} else if whence == this.eval_libc_i32("SEEK_END")? {
|
|
|
|
SeekFrom::End(offset)
|
|
|
|
} else {
|
2020-01-29 19:04:18 -06:00
|
|
|
let einval = this.eval_libc("EINVAL")?;
|
|
|
|
this.set_last_error(einval)?;
|
|
|
|
return Ok(-1);
|
2020-01-26 12:36:36 -06:00
|
|
|
};
|
|
|
|
|
2020-02-16 16:59:54 -06:00
|
|
|
if let Some(FileHandle { file, writable: _ }) = this.machine.file_handler.handles.get_mut(&fd) {
|
2020-02-09 14:43:45 -06:00
|
|
|
let result = file.seek(seek_from).map(|offset| offset as i64);
|
2020-01-26 12:36:36 -06:00
|
|
|
this.try_unwrap_io_result(result)
|
|
|
|
} else {
|
|
|
|
this.handle_not_found()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-24 08:44:13 -05:00
|
|
|
fn unlink(&mut self, path_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
|
2019-10-03 09:33:36 -05:00
|
|
|
let this = self.eval_context_mut();
|
|
|
|
|
2019-10-14 15:36:15 -05:00
|
|
|
this.check_no_isolation("unlink")?;
|
2019-10-03 09:33:36 -05:00
|
|
|
|
2019-12-04 03:43:36 -06:00
|
|
|
let path = this.read_os_str_from_c_str(this.read_scalar(path_op)?.not_undef()?)?;
|
2019-10-03 09:33:36 -05:00
|
|
|
|
|
|
|
let result = remove_file(path).map(|_| 0);
|
|
|
|
|
2019-10-18 14:33:25 -05:00
|
|
|
this.try_unwrap_io_result(result)
|
2019-10-03 09:33:36 -05:00
|
|
|
}
|
|
|
|
|
2020-01-06 15:30:17 -06:00
|
|
|
fn symlink(
|
|
|
|
&mut self,
|
|
|
|
target_op: OpTy<'tcx, Tag>,
|
|
|
|
linkpath_op: OpTy<'tcx, Tag>
|
|
|
|
) -> InterpResult<'tcx, i32> {
|
|
|
|
#[cfg(target_family = "unix")]
|
|
|
|
fn create_link(src: PathBuf, dst: PathBuf) -> std::io::Result<()> {
|
|
|
|
std::os::unix::fs::symlink(src, dst)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_family = "windows")]
|
|
|
|
fn create_link(src: PathBuf, dst: PathBuf) -> std::io::Result<()> {
|
|
|
|
use std::os::windows::fs;
|
|
|
|
if src.is_dir() {
|
|
|
|
fs::symlink_dir(src, dst)
|
|
|
|
} else {
|
2020-01-10 11:18:24 -06:00
|
|
|
fs::symlink_file(src, dst)
|
2020-01-06 15:30:17 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let this = self.eval_context_mut();
|
|
|
|
|
|
|
|
this.check_no_isolation("symlink")?;
|
|
|
|
|
|
|
|
let target = this.read_os_str_from_c_str(this.read_scalar(target_op)?.not_undef()?)?.into();
|
|
|
|
let linkpath = this.read_os_str_from_c_str(this.read_scalar(linkpath_op)?.not_undef()?)?.into();
|
|
|
|
|
|
|
|
this.try_unwrap_io_result(create_link(target, linkpath).map(|_| 0))
|
|
|
|
}
|
|
|
|
|
2020-02-19 16:53:33 -06:00
|
|
|
fn macos_stat(
|
2019-12-24 10:53:03 -06:00
|
|
|
&mut self,
|
|
|
|
path_op: OpTy<'tcx, Tag>,
|
2019-12-24 10:01:01 -06:00
|
|
|
buf_op: OpTy<'tcx, Tag>,
|
|
|
|
) -> InterpResult<'tcx, i32> {
|
|
|
|
let this = self.eval_context_mut();
|
2020-01-07 10:29:25 -06:00
|
|
|
this.check_no_isolation("stat")?;
|
2020-02-22 08:02:25 -06:00
|
|
|
this.assert_platform("macos", "stat");
|
2020-01-07 10:29:25 -06:00
|
|
|
// `stat` always follows symlinks.
|
2020-02-19 16:53:33 -06:00
|
|
|
this.macos_stat_or_lstat(true, path_op, buf_op)
|
2020-01-07 10:29:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// `lstat` is used to get symlink metadata.
|
2020-02-19 16:53:33 -06:00
|
|
|
fn macos_lstat(
|
2020-01-07 10:29:25 -06:00
|
|
|
&mut self,
|
|
|
|
path_op: OpTy<'tcx, Tag>,
|
|
|
|
buf_op: OpTy<'tcx, Tag>,
|
|
|
|
) -> InterpResult<'tcx, i32> {
|
|
|
|
let this = self.eval_context_mut();
|
|
|
|
this.check_no_isolation("lstat")?;
|
2020-02-22 08:02:25 -06:00
|
|
|
this.assert_platform("macos", "lstat");
|
2020-02-19 16:53:33 -06:00
|
|
|
this.macos_stat_or_lstat(false, path_op, buf_op)
|
2020-01-07 10:29:25 -06:00
|
|
|
}
|
|
|
|
|
2020-02-19 16:53:33 -06:00
|
|
|
fn macos_fstat(
|
2020-01-26 11:58:08 -06:00
|
|
|
&mut self,
|
|
|
|
fd_op: OpTy<'tcx, Tag>,
|
|
|
|
buf_op: OpTy<'tcx, Tag>,
|
|
|
|
) -> InterpResult<'tcx, i32> {
|
|
|
|
let this = self.eval_context_mut();
|
|
|
|
|
|
|
|
this.check_no_isolation("fstat")?;
|
2020-02-22 08:02:25 -06:00
|
|
|
this.assert_platform("macos", "fstat");
|
2020-01-26 11:58:08 -06:00
|
|
|
|
|
|
|
let fd = this.read_scalar(fd_op)?.to_i32()?;
|
|
|
|
|
|
|
|
let metadata = match FileMetadata::from_fd(this, fd)? {
|
|
|
|
Some(metadata) => metadata,
|
|
|
|
None => return Ok(-1),
|
|
|
|
};
|
2020-02-23 11:48:38 -06:00
|
|
|
this.macos_stat_write_buf(metadata, buf_op)
|
2019-12-24 10:01:01 -06:00
|
|
|
}
|
|
|
|
|
2020-02-19 16:53:33 -06:00
|
|
|
fn linux_statx(
|
2019-11-30 14:09:52 -06:00
|
|
|
&mut self,
|
2019-12-23 05:56:23 -06:00
|
|
|
dirfd_op: OpTy<'tcx, Tag>, // Should be an `int`
|
2019-11-30 14:09:52 -06:00
|
|
|
pathname_op: OpTy<'tcx, Tag>, // Should be a `const char *`
|
2019-12-23 05:56:23 -06:00
|
|
|
flags_op: OpTy<'tcx, Tag>, // Should be an `int`
|
|
|
|
_mask_op: OpTy<'tcx, Tag>, // Should be an `unsigned int`
|
|
|
|
statxbuf_op: OpTy<'tcx, Tag>, // Should be a `struct statx *`
|
2019-11-30 14:09:52 -06:00
|
|
|
) -> InterpResult<'tcx, i32> {
|
|
|
|
let this = self.eval_context_mut();
|
|
|
|
|
|
|
|
this.check_no_isolation("statx")?;
|
2020-02-22 08:02:25 -06:00
|
|
|
this.assert_platform("linux", "statx");
|
2019-12-25 15:09:54 -06:00
|
|
|
|
2019-11-30 14:09:52 -06:00
|
|
|
let statxbuf_scalar = this.read_scalar(statxbuf_op)?.not_undef()?;
|
|
|
|
let pathname_scalar = this.read_scalar(pathname_op)?.not_undef()?;
|
|
|
|
|
|
|
|
// If the statxbuf or pathname pointers are null, the function fails with `EFAULT`.
|
|
|
|
if this.is_null(statxbuf_scalar)? || this.is_null(pathname_scalar)? {
|
|
|
|
let efault = this.eval_libc("EFAULT")?;
|
|
|
|
this.set_last_error(efault)?;
|
|
|
|
return Ok(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Under normal circumstances, we would use `deref_operand(statxbuf_op)` to produce a
|
|
|
|
// proper `MemPlace` and then write the results of this function to it. However, the
|
|
|
|
// `syscall` function is untyped. This means that all the `statx` parameters are provided
|
|
|
|
// as `isize`s instead of having the proper types. Thus, we have to recover the layout of
|
|
|
|
// `statxbuf_op` by using the `libc::statx` struct type.
|
|
|
|
let statxbuf_place = {
|
|
|
|
// FIXME: This long path is required because `libc::statx` is an struct and also a
|
|
|
|
// function and `resolve_path` is returning the latter.
|
|
|
|
let statx_ty = this
|
|
|
|
.resolve_path(&["libc", "unix", "linux_like", "linux", "gnu", "statx"])?
|
2020-01-06 13:53:41 -06:00
|
|
|
.monomorphic_ty(*this.tcx);
|
2019-11-30 14:09:52 -06:00
|
|
|
let statxbuf_ty = this.tcx.mk_mut_ptr(statx_ty);
|
|
|
|
let statxbuf_layout = this.layout_of(statxbuf_ty)?;
|
|
|
|
let statxbuf_imm = ImmTy::from_scalar(statxbuf_scalar, statxbuf_layout);
|
|
|
|
this.ref_to_mplace(statxbuf_imm)?
|
|
|
|
};
|
|
|
|
|
|
|
|
let path: PathBuf = this.read_os_str_from_c_str(pathname_scalar)?.into();
|
|
|
|
// `flags` should be a `c_int` but the `syscall` function provides an `isize`.
|
2019-12-23 05:56:23 -06:00
|
|
|
let flags: i32 =
|
|
|
|
this.read_scalar(flags_op)?.to_machine_isize(&*this.tcx)?.try_into().map_err(|e| {
|
|
|
|
err_unsup_format!("Failed to convert pointer sized operand to integer: {}", e)
|
|
|
|
})?;
|
2020-01-25 22:21:33 -06:00
|
|
|
let empty_path_flag = flags & this.eval_libc("AT_EMPTY_PATH")?.to_i32()? != 0;
|
2019-11-30 14:09:52 -06:00
|
|
|
// `dirfd` should be a `c_int` but the `syscall` function provides an `isize`.
|
2019-12-23 05:56:23 -06:00
|
|
|
let dirfd: i32 =
|
|
|
|
this.read_scalar(dirfd_op)?.to_machine_isize(&*this.tcx)?.try_into().map_err(|e| {
|
|
|
|
err_unsup_format!("Failed to convert pointer sized operand to integer: {}", e)
|
|
|
|
})?;
|
2020-01-28 19:43:55 -06:00
|
|
|
// We only support:
|
|
|
|
// * interpreting `path` as an absolute directory,
|
|
|
|
// * interpreting `path` as a path relative to `dirfd` when the latter is `AT_FDCWD`, or
|
|
|
|
// * interpreting `dirfd` as any file descriptor when `path` is empty and AT_EMPTY_PATH is
|
|
|
|
// set.
|
2020-01-28 22:59:28 -06:00
|
|
|
// Other behaviors cannot be tested from `libstd` and thus are not implemented. If you
|
|
|
|
// found this error, please open an issue reporting it.
|
2020-01-25 22:21:33 -06:00
|
|
|
if !(
|
|
|
|
path.is_absolute() ||
|
|
|
|
dirfd == this.eval_libc_i32("AT_FDCWD")? ||
|
|
|
|
(path.as_os_str().is_empty() && empty_path_flag)
|
|
|
|
) {
|
2019-11-30 14:09:52 -06:00
|
|
|
throw_unsup_format!(
|
2020-01-28 19:43:55 -06:00
|
|
|
"Using statx is only supported with absolute paths, relative paths with the file \
|
|
|
|
descriptor `AT_FDCWD`, and empty paths with the `AT_EMPTY_PATH` flag set and any \
|
|
|
|
file descriptor"
|
2019-11-30 14:09:52 -06:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// the `_mask_op` paramter specifies the file information that the caller requested.
|
|
|
|
// However `statx` is allowed to return information that was not requested or to not
|
|
|
|
// return information that was requested. This `mask` represents the information we can
|
|
|
|
// actually provide in any host platform.
|
|
|
|
let mut mask =
|
|
|
|
this.eval_libc("STATX_TYPE")?.to_u32()? | this.eval_libc("STATX_SIZE")?.to_u32()?;
|
|
|
|
|
|
|
|
// If the `AT_SYMLINK_NOFOLLOW` flag is set, we query the file's metadata without following
|
|
|
|
// symbolic links.
|
2019-12-26 11:12:19 -06:00
|
|
|
let follow_symlink = flags & this.eval_libc("AT_SYMLINK_NOFOLLOW")?.to_i32()? == 0;
|
2019-11-30 14:09:52 -06:00
|
|
|
|
2020-01-25 22:21:33 -06:00
|
|
|
// If the path is empty, and the AT_EMPTY_PATH flag is set, we query the open file
|
|
|
|
// represented by dirfd, whether it's a directory or otherwise.
|
|
|
|
let metadata = if path.as_os_str().is_empty() && empty_path_flag {
|
|
|
|
FileMetadata::from_fd(this, dirfd)?
|
|
|
|
} else {
|
|
|
|
FileMetadata::from_path(this, path, follow_symlink)?
|
|
|
|
};
|
|
|
|
let metadata = match metadata {
|
2019-12-26 12:30:04 -06:00
|
|
|
Some(metadata) => metadata,
|
2019-12-25 15:09:54 -06:00
|
|
|
None => return Ok(-1),
|
2019-11-30 14:09:52 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
// The `mode` field specifies the type of the file and the permissions over the file for
|
|
|
|
// the owner, its group and other users. Given that we can only provide the file type
|
|
|
|
// without using platform specific methods, we only set the bits corresponding to the file
|
|
|
|
// type. This should be an `__u16` but `libc` provides its values as `u32`.
|
2019-12-26 12:30:04 -06:00
|
|
|
let mode: u16 = metadata
|
2019-12-25 15:09:54 -06:00
|
|
|
.mode
|
2019-12-23 05:56:23 -06:00
|
|
|
.to_u32()?
|
|
|
|
.try_into()
|
2019-12-25 15:09:54 -06:00
|
|
|
.unwrap_or_else(|_| bug!("libc contains bad value for constant"));
|
2019-11-30 14:09:52 -06:00
|
|
|
|
2019-12-26 11:12:19 -06:00
|
|
|
// We need to set the corresponding bits of `mask` if the access, creation and modification
|
|
|
|
// times were available. Otherwise we let them be zero.
|
2019-12-26 12:30:04 -06:00
|
|
|
let (access_sec, access_nsec) = metadata.accessed.map(|tup| {
|
2019-12-25 15:09:54 -06:00
|
|
|
mask |= this.eval_libc("STATX_ATIME")?.to_u32()?;
|
2019-12-25 21:22:25 -06:00
|
|
|
InterpResult::Ok(tup)
|
|
|
|
}).unwrap_or(Ok((0, 0)))?;
|
2019-11-30 14:09:52 -06:00
|
|
|
|
2019-12-26 12:30:04 -06:00
|
|
|
let (created_sec, created_nsec) = metadata.created.map(|tup| {
|
2019-12-25 15:09:54 -06:00
|
|
|
mask |= this.eval_libc("STATX_BTIME")?.to_u32()?;
|
2019-12-25 21:22:25 -06:00
|
|
|
InterpResult::Ok(tup)
|
|
|
|
}).unwrap_or(Ok((0, 0)))?;
|
2019-11-30 14:09:52 -06:00
|
|
|
|
2019-12-26 12:30:04 -06:00
|
|
|
let (modified_sec, modified_nsec) = metadata.modified.map(|tup| {
|
2019-12-25 15:09:54 -06:00
|
|
|
mask |= this.eval_libc("STATX_MTIME")?.to_u32()?;
|
2019-12-25 21:22:25 -06:00
|
|
|
InterpResult::Ok(tup)
|
|
|
|
}).unwrap_or(Ok((0, 0)))?;
|
2019-11-30 14:09:52 -06:00
|
|
|
|
|
|
|
let __u32_layout = this.libc_ty_layout("__u32")?;
|
|
|
|
let __u64_layout = this.libc_ty_layout("__u64")?;
|
|
|
|
let __u16_layout = this.libc_ty_layout("__u16")?;
|
|
|
|
|
|
|
|
// Now we transform all this fields into `ImmTy`s and write them to `statxbuf`. We write a
|
|
|
|
// zero for the unavailable fields.
|
|
|
|
let imms = [
|
2019-12-23 05:56:23 -06:00
|
|
|
immty_from_uint_checked(mask, __u32_layout)?, // stx_mask
|
2019-11-30 14:09:52 -06:00
|
|
|
immty_from_uint_checked(0u128, __u32_layout)?, // stx_blksize
|
|
|
|
immty_from_uint_checked(0u128, __u64_layout)?, // stx_attributes
|
|
|
|
immty_from_uint_checked(0u128, __u32_layout)?, // stx_nlink
|
|
|
|
immty_from_uint_checked(0u128, __u32_layout)?, // stx_uid
|
|
|
|
immty_from_uint_checked(0u128, __u32_layout)?, // stx_gid
|
2019-12-23 05:56:23 -06:00
|
|
|
immty_from_uint_checked(mode, __u16_layout)?, // stx_mode
|
2019-11-30 14:09:52 -06:00
|
|
|
immty_from_uint_checked(0u128, __u16_layout)?, // statx padding
|
|
|
|
immty_from_uint_checked(0u128, __u64_layout)?, // stx_ino
|
2019-12-26 12:30:04 -06:00
|
|
|
immty_from_uint_checked(metadata.size, __u64_layout)?, // stx_size
|
2019-11-30 14:09:52 -06:00
|
|
|
immty_from_uint_checked(0u128, __u64_layout)?, // stx_blocks
|
|
|
|
immty_from_uint_checked(0u128, __u64_layout)?, // stx_attributes
|
|
|
|
immty_from_uint_checked(access_sec, __u64_layout)?, // stx_atime.tv_sec
|
|
|
|
immty_from_uint_checked(access_nsec, __u32_layout)?, // stx_atime.tv_nsec
|
|
|
|
immty_from_uint_checked(0u128, __u32_layout)?, // statx_timestamp padding
|
|
|
|
immty_from_uint_checked(created_sec, __u64_layout)?, // stx_btime.tv_sec
|
|
|
|
immty_from_uint_checked(created_nsec, __u32_layout)?, // stx_btime.tv_nsec
|
|
|
|
immty_from_uint_checked(0u128, __u32_layout)?, // statx_timestamp padding
|
|
|
|
immty_from_uint_checked(0u128, __u64_layout)?, // stx_ctime.tv_sec
|
|
|
|
immty_from_uint_checked(0u128, __u32_layout)?, // stx_ctime.tv_nsec
|
|
|
|
immty_from_uint_checked(0u128, __u32_layout)?, // statx_timestamp padding
|
|
|
|
immty_from_uint_checked(modified_sec, __u64_layout)?, // stx_mtime.tv_sec
|
|
|
|
immty_from_uint_checked(modified_nsec, __u32_layout)?, // stx_mtime.tv_nsec
|
|
|
|
immty_from_uint_checked(0u128, __u32_layout)?, // statx_timestamp padding
|
|
|
|
immty_from_uint_checked(0u128, __u64_layout)?, // stx_rdev_major
|
|
|
|
immty_from_uint_checked(0u128, __u64_layout)?, // stx_rdev_minor
|
|
|
|
immty_from_uint_checked(0u128, __u64_layout)?, // stx_dev_major
|
|
|
|
immty_from_uint_checked(0u128, __u64_layout)?, // stx_dev_minor
|
|
|
|
];
|
|
|
|
|
2020-01-30 06:38:49 -06:00
|
|
|
this.write_packed_immediates(statxbuf_place, &imms)?;
|
2019-11-30 14:09:52 -06:00
|
|
|
|
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
|
2020-01-26 16:45:05 -06:00
|
|
|
fn rename(
|
|
|
|
&mut self,
|
|
|
|
oldpath_op: OpTy<'tcx, Tag>,
|
|
|
|
newpath_op: OpTy<'tcx, Tag>,
|
|
|
|
) -> InterpResult<'tcx, i32> {
|
|
|
|
let this = self.eval_context_mut();
|
|
|
|
|
|
|
|
this.check_no_isolation("rename")?;
|
|
|
|
|
|
|
|
let oldpath_scalar = this.read_scalar(oldpath_op)?.not_undef()?;
|
|
|
|
let newpath_scalar = this.read_scalar(newpath_op)?.not_undef()?;
|
|
|
|
|
|
|
|
if this.is_null(oldpath_scalar)? || this.is_null(newpath_scalar)? {
|
|
|
|
let efault = this.eval_libc("EFAULT")?;
|
|
|
|
this.set_last_error(efault)?;
|
|
|
|
return Ok(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
let oldpath = this.read_os_str_from_c_str(oldpath_scalar)?;
|
|
|
|
let newpath = this.read_os_str_from_c_str(newpath_scalar)?;
|
|
|
|
|
|
|
|
let result = rename(oldpath, newpath).map(|_| 0);
|
|
|
|
|
|
|
|
this.try_unwrap_io_result(result)
|
|
|
|
}
|
2020-01-24 18:56:23 -06:00
|
|
|
|
|
|
|
fn mkdir(
|
|
|
|
&mut self,
|
|
|
|
path_op: OpTy<'tcx, Tag>,
|
|
|
|
mode_op: OpTy<'tcx, Tag>,
|
|
|
|
) -> InterpResult<'tcx, i32> {
|
|
|
|
let this = self.eval_context_mut();
|
|
|
|
|
|
|
|
this.check_no_isolation("mkdir")?;
|
|
|
|
|
2020-01-26 14:13:22 -06:00
|
|
|
#[cfg(target_os = "linux")]
|
2020-01-24 18:56:23 -06:00
|
|
|
let mode = this.read_scalar(mode_op)?.to_u32()?;
|
2020-01-26 14:13:22 -06:00
|
|
|
#[cfg(not(target_os = "linux"))]
|
|
|
|
let mode = this.read_scalar(mode_op)?.not_undef()?.to_u16()?;
|
2020-01-24 18:56:23 -06:00
|
|
|
|
|
|
|
let path = this.read_os_str_from_c_str(this.read_scalar(path_op)?.not_undef()?)?;
|
|
|
|
|
|
|
|
let mut builder = DirBuilder::new();
|
|
|
|
#[cfg(target_family = "unix")]
|
|
|
|
{
|
|
|
|
use std::os::unix::fs::DirBuilderExt;
|
2020-01-26 14:13:22 -06:00
|
|
|
builder.mode(mode.into());
|
2020-01-24 18:56:23 -06:00
|
|
|
}
|
2020-01-26 14:13:22 -06:00
|
|
|
#[cfg(not(target_family = "unix"))]
|
|
|
|
let _mode = mode;
|
2020-01-24 18:56:23 -06:00
|
|
|
let result = builder.create(path).map(|_| 0i32);
|
|
|
|
|
|
|
|
this.try_unwrap_io_result(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rmdir(
|
|
|
|
&mut self,
|
|
|
|
path_op: OpTy<'tcx, Tag>,
|
|
|
|
) -> InterpResult<'tcx, i32> {
|
|
|
|
let this = self.eval_context_mut();
|
|
|
|
|
|
|
|
this.check_no_isolation("rmdir")?;
|
|
|
|
|
|
|
|
let path = this.read_os_str_from_c_str(this.read_scalar(path_op)?.not_undef()?)?;
|
|
|
|
|
|
|
|
let result = remove_dir(path).map(|_| 0i32);
|
|
|
|
|
|
|
|
this.try_unwrap_io_result(result)
|
|
|
|
}
|
2020-01-25 12:57:15 -06:00
|
|
|
|
|
|
|
fn opendir(&mut self, name_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, Scalar<Tag>> {
|
|
|
|
let this = self.eval_context_mut();
|
|
|
|
|
|
|
|
this.check_no_isolation("opendir")?;
|
|
|
|
|
|
|
|
let name = this.read_os_str_from_c_str(this.read_scalar(name_op)?.not_undef()?)?;
|
|
|
|
|
|
|
|
let result = read_dir(name);
|
|
|
|
|
|
|
|
match result {
|
|
|
|
Ok(dir_iter) => {
|
|
|
|
let size = 1;
|
|
|
|
let kind = MiriMemoryKind::Env;
|
|
|
|
let align = this.min_align(size, kind);
|
|
|
|
let dir_ptr = this.memory.allocate(Size::from_bytes(size), align, kind.into());
|
|
|
|
let prev = this
|
|
|
|
.machine
|
|
|
|
.dir_handler
|
|
|
|
.streams
|
|
|
|
.insert(dir_ptr, dir_iter);
|
|
|
|
if let Some(_) = prev {
|
|
|
|
throw_unsup_format!("The pointer allocated for opendir was already registered by a previous call to opendir")
|
|
|
|
} else {
|
|
|
|
Ok(Scalar::Ptr(dir_ptr))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
this.set_last_error_from_io_error(e)?;
|
|
|
|
Ok(Scalar::from_int(0, this.memory.pointer_size()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-25 17:36:51 -06:00
|
|
|
fn readdir64_r(
|
|
|
|
&mut self,
|
|
|
|
dirp_op: OpTy<'tcx, Tag>,
|
|
|
|
entry_op: OpTy<'tcx, Tag>,
|
|
|
|
result_op: OpTy<'tcx, Tag>,
|
|
|
|
) -> InterpResult<'tcx, i32> {
|
|
|
|
let this = self.eval_context_mut();
|
|
|
|
|
|
|
|
this.check_no_isolation("readdir64_r")?;
|
|
|
|
|
|
|
|
let dirp = this.force_ptr(this.read_scalar(dirp_op)?.not_undef()?)?;
|
|
|
|
|
|
|
|
let entry_ptr = this.force_ptr(this.read_scalar(entry_op)?.not_undef()?)?;
|
|
|
|
let dirent64_layout = this.libc_ty_layout("dirent64")?;
|
|
|
|
this.memory.check_ptr_access(
|
|
|
|
Scalar::Ptr(entry_ptr),
|
|
|
|
dirent64_layout.size,
|
|
|
|
dirent64_layout.align.abi,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
if let Some(dir_iter) = this.machine.dir_handler.streams.get_mut(&dirp) {
|
|
|
|
match dir_iter.next() {
|
|
|
|
Some(Ok(dir_entry)) => {
|
|
|
|
// write into entry, write pointer to result, return 0 on success
|
|
|
|
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")?;
|
|
|
|
let c_uchar_layout = this.libc_ty_layout("c_uchar")?;
|
|
|
|
|
|
|
|
let name_offset = dirent64_layout.details.fields.offset(4);
|
|
|
|
let name_ptr = entry_ptr.offset(name_offset, this)?;
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
let ino = std::os::unix::fs::DirEntryExt::ino(&dir_entry);
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
let ino = 0;
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
let file_name = dir_entry.file_name();
|
|
|
|
#[cfg(unix)]
|
|
|
|
let file_name = std::os::unix::ffi::OsStrExt::as_bytes(file_name.as_os_str());
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
let file_name = b"";
|
|
|
|
|
|
|
|
let file_type = this.file_type_to_d_type(dir_entry.file_type())? as u128;
|
|
|
|
|
|
|
|
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(file_type, c_uchar_layout)?, // d_type
|
|
|
|
];
|
|
|
|
this.write_packed_immediates(entry_place, &imms)?;
|
|
|
|
this.memory.write_bytes(Scalar::Ptr(name_ptr), file_name.iter().copied())?;
|
|
|
|
|
|
|
|
let result_place = this.deref_operand(result_op)?;
|
|
|
|
this.write_scalar(this.read_scalar(entry_op)?, result_place.into())?;
|
|
|
|
|
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
// end of stream: return 0, assign *result=NULL
|
|
|
|
this.write_null(this.deref_operand(result_op)?.into())?;
|
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw_unsup_format!("The DIR pointer passed to readdir64_r did not come from opendir")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-26 14:47:36 -06:00
|
|
|
fn readdir_r(
|
|
|
|
&mut self,
|
|
|
|
dirp_op: OpTy<'tcx, Tag>,
|
|
|
|
entry_op: OpTy<'tcx, Tag>,
|
|
|
|
result_op: OpTy<'tcx, Tag>,
|
|
|
|
) -> InterpResult<'tcx, i32> {
|
|
|
|
let this = self.eval_context_mut();
|
|
|
|
|
|
|
|
this.check_no_isolation("readdir_r")?;
|
|
|
|
|
|
|
|
let dirp = this.force_ptr(this.read_scalar(dirp_op)?.not_undef()?)?;
|
|
|
|
|
|
|
|
let entry_ptr = this.force_ptr(this.read_scalar(entry_op)?.not_undef()?)?;
|
|
|
|
let dirent_layout = this.libc_ty_layout("dirent")?;
|
|
|
|
this.memory.check_ptr_access(
|
|
|
|
Scalar::Ptr(entry_ptr),
|
|
|
|
dirent_layout.size,
|
|
|
|
dirent_layout.align.abi,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
if let Some(dir_iter) = this.machine.dir_handler.streams.get_mut(&dirp) {
|
|
|
|
match dir_iter.next() {
|
|
|
|
Some(Ok(dir_entry)) => {
|
|
|
|
// write into entry, write pointer to result, return 0 on success
|
|
|
|
let entry_place = this.deref_operand(entry_op)?;
|
|
|
|
let ino_t_layout = this.libc_ty_layout("ino_t")?;
|
|
|
|
let off_t_layout = this.libc_ty_layout("off_t")?;
|
|
|
|
let c_ushort_layout = this.libc_ty_layout("c_ushort")?;
|
|
|
|
let c_uchar_layout = this.libc_ty_layout("c_uchar")?;
|
|
|
|
|
2020-01-30 18:57:01 -06:00
|
|
|
let name_offset = dirent_layout.details.fields.offset(5);
|
2020-01-26 14:47:36 -06:00
|
|
|
let name_ptr = entry_ptr.offset(name_offset, this)?;
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
let ino = std::os::unix::fs::DirEntryExt::ino(&dir_entry);
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
let ino = 0;
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
let file_name = dir_entry.file_name();
|
|
|
|
#[cfg(unix)]
|
|
|
|
let file_name = std::os::unix::ffi::OsStrExt::as_bytes(file_name.as_os_str());
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
let file_name = b"";
|
|
|
|
|
|
|
|
let file_type = this.file_type_to_d_type(dir_entry.file_type())? as u128;
|
|
|
|
|
|
|
|
let imms = [
|
|
|
|
immty_from_uint_checked(ino, ino_t_layout)?, // d_ino
|
2020-01-30 18:57:01 -06:00
|
|
|
immty_from_uint_checked(0u128, off_t_layout)?, // d_seekoff
|
2020-01-26 14:47:36 -06:00
|
|
|
immty_from_uint_checked(0u128, c_ushort_layout)?, // d_reclen
|
2020-01-30 18:57:01 -06:00
|
|
|
immty_from_uint_checked(file_name.len() as u128, c_ushort_layout)?, // d_namlen
|
2020-01-26 14:47:36 -06:00
|
|
|
immty_from_uint_checked(file_type, c_uchar_layout)?, // d_type
|
|
|
|
];
|
|
|
|
this.write_packed_immediates(entry_place, &imms)?;
|
|
|
|
this.memory.write_bytes(Scalar::Ptr(name_ptr), file_name.iter().copied())?;
|
|
|
|
|
|
|
|
let result_place = this.deref_operand(result_op)?;
|
|
|
|
this.write_scalar(this.read_scalar(entry_op)?, result_place.into())?;
|
|
|
|
|
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
// end of stream: return 0, assign *result=NULL
|
|
|
|
this.write_null(this.deref_operand(result_op)?.into())?;
|
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw_unsup_format!("The DIR pointer passed to readdir_r did not come from opendir")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-25 12:57:15 -06:00
|
|
|
fn closedir(&mut self, dirp_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
|
|
|
|
let this = self.eval_context_mut();
|
|
|
|
|
|
|
|
this.check_no_isolation("closedir")?;
|
|
|
|
|
|
|
|
let dirp = this.force_ptr(this.read_scalar(dirp_op)?.not_undef()?)?;
|
|
|
|
|
|
|
|
if let Some(dir_iter) = this.machine.dir_handler.streams.remove(&dirp) {
|
|
|
|
drop(dir_iter);
|
|
|
|
this.memory.deallocate(dirp, None, MiriMemoryKind::Env.into())?;
|
|
|
|
Ok(0)
|
|
|
|
} else {
|
|
|
|
this.handle_not_found()
|
|
|
|
}
|
|
|
|
}
|
2019-09-24 17:28:00 -05:00
|
|
|
}
|
2019-11-30 14:09:52 -06:00
|
|
|
|
2019-12-26 11:12:19 -06:00
|
|
|
/// Extracts the number of seconds and nanoseconds elapsed between `time` and the unix epoch when
|
|
|
|
/// `time` is Ok. Returns `None` if `time` is an error. Fails if `time` happens before the unix
|
|
|
|
/// epoch.
|
|
|
|
fn extract_sec_and_nsec<'tcx>(
|
|
|
|
time: std::io::Result<SystemTime>
|
|
|
|
) -> InterpResult<'tcx, Option<(u64, u32)>> {
|
2019-12-25 15:09:54 -06:00
|
|
|
time.ok().map(|time| {
|
2019-11-30 14:09:52 -06:00
|
|
|
let duration = system_time_to_duration(&time)?;
|
|
|
|
Ok((duration.as_secs(), duration.subsec_nanos()))
|
2019-12-25 15:09:54 -06:00
|
|
|
}).transpose()
|
|
|
|
}
|
|
|
|
|
2019-12-26 12:30:04 -06:00
|
|
|
/// Stores a file's metadata in order to avoid code duplication in the different metadata related
|
|
|
|
/// shims.
|
|
|
|
struct FileMetadata {
|
2019-12-25 15:09:54 -06:00
|
|
|
mode: Scalar<Tag>,
|
|
|
|
size: u64,
|
|
|
|
created: Option<(u64, u32)>,
|
|
|
|
accessed: Option<(u64, u32)>,
|
|
|
|
modified: Option<(u64, u32)>,
|
|
|
|
}
|
|
|
|
|
2019-12-26 12:30:04 -06:00
|
|
|
impl FileMetadata {
|
2020-01-25 22:21:33 -06:00
|
|
|
fn from_path<'tcx, 'mir>(
|
2019-12-25 21:22:25 -06:00
|
|
|
ecx: &mut MiriEvalContext<'mir, 'tcx>,
|
|
|
|
path: PathBuf,
|
2019-12-26 11:12:19 -06:00
|
|
|
follow_symlink: bool
|
2019-12-26 12:30:04 -06:00
|
|
|
) -> InterpResult<'tcx, Option<FileMetadata>> {
|
2019-12-26 11:12:19 -06:00
|
|
|
let metadata = if follow_symlink {
|
2019-12-26 12:30:04 -06:00
|
|
|
std::fs::metadata(path)
|
|
|
|
} else {
|
2019-12-25 15:09:54 -06:00
|
|
|
std::fs::symlink_metadata(path)
|
|
|
|
};
|
|
|
|
|
2020-01-25 22:21:33 -06:00
|
|
|
FileMetadata::from_meta(ecx, metadata)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_fd<'tcx, 'mir>(
|
|
|
|
ecx: &mut MiriEvalContext<'mir, 'tcx>,
|
|
|
|
fd: i32,
|
|
|
|
) -> InterpResult<'tcx, Option<FileMetadata>> {
|
|
|
|
let option = ecx.machine.file_handler.handles.get(&fd);
|
2020-02-09 14:43:45 -06:00
|
|
|
let file = match option {
|
2020-02-16 16:59:54 -06:00
|
|
|
Some(FileHandle { file, writable: _ }) => file,
|
|
|
|
None => return ecx.handle_not_found().map(|_: i32| None),
|
2020-01-25 22:21:33 -06:00
|
|
|
};
|
2020-02-09 14:43:45 -06:00
|
|
|
let metadata = file.metadata();
|
2020-01-25 22:21:33 -06:00
|
|
|
|
|
|
|
FileMetadata::from_meta(ecx, metadata)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_meta<'tcx, 'mir>(
|
|
|
|
ecx: &mut MiriEvalContext<'mir, 'tcx>,
|
|
|
|
metadata: Result<std::fs::Metadata, std::io::Error>,
|
|
|
|
) -> InterpResult<'tcx, Option<FileMetadata>> {
|
2019-12-25 15:09:54 -06:00
|
|
|
let metadata = match metadata {
|
|
|
|
Ok(metadata) => metadata,
|
|
|
|
Err(e) => {
|
|
|
|
ecx.set_last_error_from_io_error(e)?;
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let file_type = metadata.file_type();
|
|
|
|
|
|
|
|
let mode_name = if file_type.is_file() {
|
|
|
|
"S_IFREG"
|
|
|
|
} else if file_type.is_dir() {
|
|
|
|
"S_IFDIR"
|
|
|
|
} else {
|
|
|
|
"S_IFLNK"
|
|
|
|
};
|
|
|
|
|
|
|
|
let mode = ecx.eval_libc(mode_name)?;
|
|
|
|
|
|
|
|
let size = metadata.len();
|
|
|
|
|
|
|
|
let created = extract_sec_and_nsec(metadata.created())?;
|
|
|
|
let accessed = extract_sec_and_nsec(metadata.accessed())?;
|
|
|
|
let modified = extract_sec_and_nsec(metadata.modified())?;
|
|
|
|
|
|
|
|
// FIXME: Provide more fields using platform specific methods.
|
2019-12-26 12:30:04 -06:00
|
|
|
Ok(Some(FileMetadata { mode, size, created, accessed, modified }))
|
2019-11-30 14:09:52 -06:00
|
|
|
}
|
|
|
|
}
|