rust/src/shims/fs.rs

463 lines
19 KiB
Rust
Raw Normal View History

2019-09-24 17:28:00 -05:00
use std::collections::HashMap;
2019-11-30 14:09:52 -06:00
use std::convert::{TryInto, TryFrom};
2019-10-24 08:44:13 -05:00
use std::fs::{remove_file, File, OpenOptions};
use std::io::{Read, 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-11-30 14:09:52 -06:00
use rustc::ty::layout::{Size, Align, LayoutOf};
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
#[derive(Debug)]
2019-09-30 15:18:23 -05:00
pub struct FileHandle {
2019-09-30 11:51:09 -05:00
file: File,
}
2019-09-24 17:28:00 -05:00
pub struct FileHandler {
2019-09-30 11:51:09 -05:00
handles: HashMap<i32, FileHandle>,
2019-09-24 17:28:00 -05:00
low: i32,
}
impl Default for FileHandler {
fn default() -> Self {
FileHandler {
2019-09-30 11:51:09 -05:00
handles: Default::default(),
// 0, 1 and 2 are reserved for stdin, stdout and stderr.
low: 3,
}
}
}
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();
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();
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");
}
// Now we check the access mode
2019-09-30 11:46:07 -05:00
let access_mode = flag & 0b11;
if access_mode == o_rdonly {
2019-09-30 11:46:07 -05:00
options.read(true);
} else if access_mode == o_wronly {
2019-09-30 11:46:07 -05:00
options.write(true);
} 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);
}
// 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
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);
mirror |= o_append;
2019-09-30 11:46:07 -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);
mirror |= o_trunc;
2019-09-30 11:46:07 -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);
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.)
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| {
2019-09-30 14:07:08 -05:00
let mut fh = &mut this.machine.file_handler;
fh.low += 1;
fh.handles.insert(fh.low, FileHandle { file }).unwrap_none();
2019-09-30 14:07:08 -05:00
fh.low
});
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>,
2019-10-12 20:12:26 -05:00
_arg1_op: Option<OpTy<'tcx, Tag>>,
2019-09-24 17:28:00 -05:00
) -> InterpResult<'tcx, i32> {
let this = self.eval_context_mut();
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()?;
// 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()
}
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();
this.check_no_isolation("close")?;
2019-09-24 17:28:00 -05:00
let fd = this.read_scalar(fd_op)?.to_i32()?;
2019-10-24 08:44:13 -05:00
if let Some(handle) = this.machine.file_handler.handles.remove(&fd) {
// `File::sync_all` does the checks that are done when closing a file. We do this to
// to handle possible errors correctly.
let result = this.try_unwrap_io_result(handle.file.sync_all().map(|_| 0i32));
2019-11-03 10:04:00 -06:00
// Now we actually close the file.
drop(handle);
// And return the result.
result
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();
this.check_no_isolation("read")?;
2019-09-24 17:28:00 -05:00
let fd = this.read_scalar(fd_op)?.to_i32()?;
let buf = this.read_scalar(buf_op)?.not_undef()?;
2019-11-09 08:15:52 -06:00
let count = this
.read_scalar(count_op)?
.to_machine_usize(&*this.tcx)?;
2019-11-14 03:29:43 -06:00
// Check that the *entire* buffer is actually valid memory.
this.memory.check_ptr_access(buf, Size::from_bytes(count), Align::from_bytes(1).unwrap())?;
// 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.
let count = count
.min(this.isize_max() as u64)
.min(isize::max_value() as u64);
2019-10-24 08:44:13 -05:00
if let Some(handle) = 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];
2019-11-09 08:15:52 -06:00
let result = handle
.file
.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();
this.check_no_isolation("write")?;
2019-09-30 11:46:07 -05:00
let fd = this.read_scalar(fd_op)?.to_i32()?;
let buf = this.read_scalar(buf_op)?.not_undef()?;
2019-11-09 08:15:52 -06:00
let count = this
.read_scalar(count_op)?
.to_machine_usize(&*this.tcx)?;
2019-11-14 03:29:43 -06:00
// Check that the *entire* buffer is actually valid memory.
this.memory.check_ptr_access(buf, Size::from_bytes(count), Align::from_bytes(1).unwrap())?;
// 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.
let count = count
.min(this.isize_max() as u64)
.min(isize::max_value() as u64);
2019-10-24 08:44:13 -05:00
if let Some(handle) = this.machine.file_handler.handles.get_mut(&fd) {
let bytes = this.memory.read_bytes(buf, Size::from_bytes(count))?;
2019-11-09 08:15:52 -06:00
let result = handle.file.write(&bytes).map(|c| i64::try_from(c).unwrap());
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
}
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();
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
}
2019-11-30 14:09:52 -06:00
fn statx(
&mut self,
dirfd_op: OpTy<'tcx, Tag>, // Should be an `int`
pathname_op: OpTy<'tcx, Tag>, // Should be a `const char *`
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 *`
) -> InterpResult<'tcx, i32> {
let this = self.eval_context_mut();
this.check_no_isolation("statx")?;
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"])?
.ty(*this.tcx);
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`.
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
))?;
// `dirfd` should be a `c_int` but the `syscall` function provides an `isize`.
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
))?;
// we only support interpreting `path` as an absolute directory or as a directory relative
// to `dirfd` when the latter is `AT_FDCWD`. The behavior of `statx` with a relative path
// and a directory file descriptor other than `AT_FDCWD` is specified but it cannot be
// tested from `libstd`. If you found this error, please open an issue reporting it.
if !(path.is_absolute() || dirfd == this.eval_libc_i32("AT_FDCWD")?)
{
throw_unsup_format!(
"Using statx with a relative path and a file descriptor different from `AT_FDCWD` is not supported"
)
}
// 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.
let metadata = if flags & this.eval_libc("AT_SYMLINK_NOFOLLOW")?.to_i32()? != 0 {
// FIXME: metadata for symlinks need testing.
std::fs::symlink_metadata(path)
} else {
std::fs::metadata(path)
};
let metadata = match metadata {
Ok(metadata) => metadata,
Err(e) => {
this.set_last_error_from_io_error(e)?;
return Ok(-1);
}
};
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"
};
// 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`.
let mode: u16 = this.eval_libc(mode_name)?
.to_u32()?
.try_into()
.unwrap_or_else(|_| bug!("libc contains bad value for `{}` constant", mode_name));
let size = metadata.len();
let (access_sec, access_nsec) = extract_sec_and_nsec(
metadata.accessed(),
&mut mask,
this.eval_libc("STATX_ATIME")?.to_u32()?
)?;
let (created_sec, created_nsec) = extract_sec_and_nsec(
metadata.created(),
&mut mask,
this.eval_libc("STATX_BTIME")?.to_u32()?
)?;
let (modified_sec, modified_nsec) = extract_sec_and_nsec(
metadata.modified(),
&mut mask,
this.eval_libc("STATX_MTIME")?.to_u32()?
)?;
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.
// FIXME: Provide more fields using platform specific methods.
let imms = [
immty_from_uint_checked(mask, __u32_layout)?, // stx_mask
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
immty_from_uint_checked(mode, __u16_layout)?, // stx_mode
immty_from_uint_checked(0u128, __u16_layout)?, // statx padding
immty_from_uint_checked(0u128, __u64_layout)?, // stx_ino
immty_from_uint_checked(size, __u64_layout)?, // stx_size
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
];
this.write_packed_immediates(&statxbuf_place, &imms)?;
Ok(0)
}
2019-10-24 08:44:13 -05:00
/// 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> {
2019-09-30 15:18:23 -05:00
let this = self.eval_context_mut();
2019-10-24 08:44:13 -05:00
let ebadf = this.eval_libc("EBADF")?;
this.set_last_error(ebadf)?;
Ok((-1).into())
2019-09-30 14:07:08 -05:00
}
2019-09-24 17:28:00 -05:00
}
2019-11-30 14:09:52 -06:00
// Extracts the number of seconds and nanoseconds elapsed between `time` and the unix epoch, and
// then sets the `mask` bits determined by `flag` when `time` is Ok. If `time` is an error, it
// returns `(0, 0)` without setting any bits.
fn extract_sec_and_nsec<'tcx>(time: std::io::Result<SystemTime>, mask: &mut u32, flag: u32) -> InterpResult<'tcx, (u64, u32)> {
if let Ok(time) = time {
let duration = system_time_to_duration(&time)?;
*mask |= flag;
Ok((duration.as_secs(), duration.subsec_nanos()))
} else {
Ok((0, 0))
}
}