2019-09-24 17:28:00 -05:00
|
|
|
use std::collections::HashMap;
|
2019-10-03 09:33:36 -05:00
|
|
|
use std::fs::{File, OpenOptions, remove_file};
|
2019-10-01 13:48:59 -05:00
|
|
|
use std::io::{Read, Write};
|
2019-09-24 17:28:00 -05:00
|
|
|
|
2019-09-30 14:21:45 -05:00
|
|
|
use rustc::ty::layout::Size;
|
|
|
|
|
2019-09-24 17:28:00 -05:00
|
|
|
use crate::stacked_borrows::Tag;
|
|
|
|
use crate::*;
|
|
|
|
|
2019-09-30 15:18:23 -05:00
|
|
|
pub struct FileHandle {
|
2019-09-30 11:51:09 -05:00
|
|
|
file: File,
|
|
|
|
flag: i32,
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2019-09-28 14:22:57 -05:00
|
|
|
impl Default for FileHandler {
|
|
|
|
fn default() -> Self {
|
|
|
|
FileHandler {
|
2019-09-30 11:51:09 -05:00
|
|
|
handles: Default::default(),
|
2019-09-28 14:22:57 -05:00
|
|
|
// 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();
|
|
|
|
|
|
|
|
if !this.machine.communicate {
|
|
|
|
throw_unsup_format!("`open` not available when isolation is enabled")
|
|
|
|
}
|
|
|
|
|
|
|
|
let flag = this.read_scalar(flag_op)?.to_i32()?;
|
|
|
|
|
2019-09-30 11:46:07 -05:00
|
|
|
let mut options = OpenOptions::new();
|
|
|
|
|
2019-10-11 05:30:50 -05:00
|
|
|
// The first two bits of the flag correspond to the access mode of the file in linux. This
|
|
|
|
// is done this way because `O_RDONLY` is zero in several platforms.
|
2019-09-30 11:46:07 -05:00
|
|
|
let access_mode = flag & 0b11;
|
|
|
|
|
|
|
|
if access_mode == this.eval_libc_i32("O_RDONLY")? {
|
|
|
|
options.read(true);
|
|
|
|
} else if access_mode == this.eval_libc_i32("O_WRONLY")? {
|
|
|
|
options.write(true);
|
|
|
|
} else if access_mode == this.eval_libc_i32("O_RDWR")? {
|
|
|
|
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 {
|
|
|
|
// This flag is a noop for now because `std` already sets it.
|
|
|
|
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 {
|
|
|
|
throw_unsup_format!("unsupported flags {:#x}", flag);
|
2019-09-24 17:28:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let path_bytes = this
|
|
|
|
.memory()
|
|
|
|
.read_c_str(this.read_scalar(path_op)?.not_undef()?)?;
|
|
|
|
let path = std::str::from_utf8(path_bytes)
|
2019-10-01 09:18:55 -05:00
|
|
|
.map_err(|_| err_unsup_format!("{:?} is not a valid utf-8 string", path_bytes))?;
|
2019-09-30 11:46:07 -05: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;
|
2019-09-30 14:21:45 -05:00
|
|
|
fh.handles.insert(fh.low, FileHandle { file, flag });
|
2019-09-30 14:07:08 -05:00
|
|
|
fh.low
|
|
|
|
});
|
|
|
|
|
2019-10-01 09:18:55 -05:00
|
|
|
this.consume_result(fd)
|
2019-09-24 17:28:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn fcntl(
|
|
|
|
&mut self,
|
|
|
|
fd_op: OpTy<'tcx, Tag>,
|
|
|
|
cmd_op: OpTy<'tcx, Tag>,
|
|
|
|
arg_op: Option<OpTy<'tcx, Tag>>,
|
|
|
|
) -> InterpResult<'tcx, i32> {
|
|
|
|
let this = self.eval_context_mut();
|
|
|
|
|
|
|
|
if !this.machine.communicate {
|
2019-10-01 10:57:12 -05:00
|
|
|
throw_unsup_format!("`fcntl` not available when isolation is enabled")
|
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-09-25 10:49:12 -05:00
|
|
|
if cmd == this.eval_libc_i32("F_SETFD")? {
|
2019-09-25 11:11:20 -05:00
|
|
|
// This does not affect the file itself. Certain flags might require changing the file
|
|
|
|
// or the way it is accessed somehow.
|
2019-09-24 17:28:00 -05:00
|
|
|
let flag = this.read_scalar(arg_op.unwrap())?.to_i32()?;
|
2019-09-25 11:11:20 -05:00
|
|
|
// The only usage of this in stdlib at the moment is to enable the `FD_CLOEXEC` flag.
|
|
|
|
let fd_cloexec = this.eval_libc_i32("FD_CLOEXEC")?;
|
2019-09-30 14:21:45 -05:00
|
|
|
if let Some(FileHandle { flag: old_flag, .. }) =
|
|
|
|
this.machine.file_handler.handles.get_mut(&fd)
|
|
|
|
{
|
2019-10-11 03:55:18 -05:00
|
|
|
// Check that the only difference between the old flag and the current flag is
|
|
|
|
// exactly the `FD_CLOEXEC` value.
|
2019-09-25 11:11:20 -05:00
|
|
|
if flag ^ *old_flag == fd_cloexec {
|
|
|
|
*old_flag = flag;
|
|
|
|
} else {
|
|
|
|
throw_unsup_format!("Unsupported arg {:#x} for `F_SETFD`", flag);
|
|
|
|
}
|
|
|
|
}
|
2019-09-24 17:28:00 -05:00
|
|
|
Ok(0)
|
2019-09-25 10:49:12 -05:00
|
|
|
} else if cmd == this.eval_libc_i32("F_GETFD")? {
|
2019-10-01 09:18:55 -05:00
|
|
|
this.get_handle_and(fd, |handle| Ok(handle.flag))
|
2019-09-24 17:28:00 -05:00
|
|
|
} else {
|
|
|
|
throw_unsup_format!("Unsupported command {:#x}", cmd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn close(&mut self, fd_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
|
|
|
|
let this = self.eval_context_mut();
|
|
|
|
|
|
|
|
if !this.machine.communicate {
|
2019-10-01 10:57:12 -05:00
|
|
|
throw_unsup_format!("`close` not available when isolation is enabled")
|
2019-09-24 17:28:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let fd = this.read_scalar(fd_op)?.to_i32()?;
|
|
|
|
|
2019-10-01 13:48:59 -05:00
|
|
|
this.remove_handle_and(fd, |handle, this| {
|
|
|
|
this.consume_result(handle.file.sync_all().map(|_| 0i32))
|
|
|
|
})
|
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();
|
|
|
|
|
|
|
|
if !this.machine.communicate {
|
2019-10-01 10:57:12 -05:00
|
|
|
throw_unsup_format!("`read` not available when isolation is enabled")
|
2019-09-24 17:28:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let tcx = &{ this.tcx.tcx };
|
|
|
|
|
2019-10-02 08:50:32 -05:00
|
|
|
let count = this.read_scalar(count_op)?.to_usize(&*this.tcx)?;
|
|
|
|
// Reading zero bytes should not change `buf`
|
|
|
|
if count == 0 {
|
|
|
|
return Ok(0);
|
|
|
|
}
|
2019-09-24 17:28:00 -05:00
|
|
|
let fd = this.read_scalar(fd_op)?.to_i32()?;
|
2019-10-02 08:43:23 -05:00
|
|
|
let buf_scalar = this.read_scalar(buf_op)?.not_undef()?;
|
2019-09-24 17:28:00 -05:00
|
|
|
|
2019-09-30 14:21:45 -05:00
|
|
|
// Remove the file handle to avoid borrowing issues
|
2019-10-01 13:48:59 -05:00
|
|
|
this.remove_handle_and(fd, |mut handle, this| {
|
|
|
|
// Don't use `?` to avoid returning before reinserting the handle
|
2019-10-02 08:50:32 -05:00
|
|
|
let bytes = this.force_ptr(buf_scalar).and_then(|buf| {
|
|
|
|
this.memory_mut()
|
|
|
|
.get_mut(buf.alloc_id)?
|
|
|
|
.get_bytes_mut(tcx, buf, Size::from_bytes(count))
|
|
|
|
.map(|buffer| handle.file.read(buffer))
|
|
|
|
});
|
2019-10-01 13:48:59 -05:00
|
|
|
// Reinsert the file handle
|
|
|
|
this.machine.file_handler.handles.insert(fd, handle);
|
2019-10-02 08:43:23 -05:00
|
|
|
this.consume_result(bytes?.map(|bytes| bytes as i64))
|
2019-10-01 13:48:59 -05:00
|
|
|
})
|
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();
|
|
|
|
|
|
|
|
if !this.machine.communicate {
|
|
|
|
throw_unsup_format!("`write` not available when isolation is enabled")
|
|
|
|
}
|
|
|
|
|
|
|
|
let tcx = &{ this.tcx.tcx };
|
|
|
|
|
2019-10-02 08:50:32 -05:00
|
|
|
let count = this.read_scalar(count_op)?.to_usize(&*this.tcx)?;
|
|
|
|
// Writing zero bytes should not change `buf`
|
|
|
|
if count == 0 {
|
|
|
|
return Ok(0);
|
|
|
|
}
|
2019-09-30 11:46:07 -05:00
|
|
|
let fd = this.read_scalar(fd_op)?.to_i32()?;
|
|
|
|
let buf = this.force_ptr(this.read_scalar(buf_op)?.not_undef()?)?;
|
|
|
|
|
|
|
|
this.remove_handle_and(fd, |mut handle, this| {
|
2019-10-01 13:48:59 -05:00
|
|
|
let bytes = this.memory().get(buf.alloc_id).and_then(|alloc| {
|
|
|
|
alloc
|
|
|
|
.get_bytes(tcx, buf, Size::from_bytes(count))
|
|
|
|
.map(|bytes| handle.file.write(bytes).map(|bytes| bytes as i64))
|
|
|
|
});
|
2019-09-30 11:46:07 -05:00
|
|
|
this.machine.file_handler.handles.insert(fd, handle);
|
2019-10-01 13:48:59 -05:00
|
|
|
this.consume_result(bytes?)
|
2019-09-30 11:46:07 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-03 09:33:36 -05:00
|
|
|
fn unlink( &mut self, path_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
|
|
|
|
let this = self.eval_context_mut();
|
|
|
|
|
|
|
|
if !this.machine.communicate {
|
|
|
|
throw_unsup_format!("`write` not available when isolation is enabled")
|
|
|
|
}
|
|
|
|
|
|
|
|
let path_bytes = this
|
|
|
|
.memory()
|
|
|
|
.read_c_str(this.read_scalar(path_op)?.not_undef()?)?;
|
|
|
|
let path = std::str::from_utf8(path_bytes)
|
|
|
|
.map_err(|_| err_unsup_format!("{:?} is not a valid utf-8 string", path_bytes))?;
|
|
|
|
|
|
|
|
let result = remove_file(path).map(|_| 0);
|
|
|
|
|
|
|
|
this.consume_result(result)
|
|
|
|
}
|
|
|
|
|
2019-10-01 10:31:04 -05:00
|
|
|
/// Helper function that gets a `FileHandle` immutable reference and allows to manipulate it
|
2019-09-30 11:46:07 -05:00
|
|
|
/// using the `f` closure.
|
2019-10-01 10:31:04 -05:00
|
|
|
///
|
2019-10-01 10:57:12 -05:00
|
|
|
/// If the `fd` file descriptor does not correspond to a file, this functions returns `Ok(-1)`
|
2019-10-01 10:31:04 -05:00
|
|
|
/// and sets `Evaluator::last_error` to `libc::EBADF` (invalid file descriptor).
|
|
|
|
///
|
|
|
|
/// This function uses `T: From<i32>` instead of `i32` directly because some IO related
|
|
|
|
/// functions return different integer types (like `read`, that returns an `i64`)
|
2019-10-01 09:18:55 -05:00
|
|
|
fn get_handle_and<F, T: From<i32>>(&mut self, fd: i32, f: F) -> InterpResult<'tcx, T>
|
2019-09-30 15:18:23 -05:00
|
|
|
where
|
|
|
|
F: Fn(&FileHandle) -> InterpResult<'tcx, T>,
|
|
|
|
{
|
|
|
|
let this = self.eval_context_mut();
|
|
|
|
if let Some(handle) = this.machine.file_handler.handles.get(&fd) {
|
|
|
|
f(handle)
|
|
|
|
} else {
|
2019-10-03 10:21:55 -05:00
|
|
|
let ebadf = this.eval_libc("EBADF")?;
|
|
|
|
this.set_last_error(ebadf)?;
|
2019-10-01 09:18:55 -05:00
|
|
|
Ok((-1).into())
|
2019-09-30 15:18:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-01 10:31:04 -05:00
|
|
|
/// Helper function that removes a `FileHandle` and allows to manipulate it using the `f`
|
|
|
|
/// closure. This function is quite useful when you need to modify a `FileHandle` but you need
|
|
|
|
/// to modify `MiriEvalContext` at the same time, so you can modify the handle and reinsert it
|
|
|
|
/// using `f`.
|
|
|
|
///
|
2019-10-01 10:57:12 -05:00
|
|
|
/// If the `fd` file descriptor does not correspond to a file, this functions returns `Ok(-1)`
|
2019-10-01 10:31:04 -05:00
|
|
|
/// and sets `Evaluator::last_error` to `libc::EBADF` (invalid file descriptor).
|
|
|
|
///
|
|
|
|
/// This function uses `T: From<i32>` instead of `i32` directly because some IO related
|
|
|
|
/// functions return different integer types (like `read`, that returns an `i64`)
|
2019-10-01 09:18:55 -05:00
|
|
|
fn remove_handle_and<F, T: From<i32>>(&mut self, fd: i32, mut f: F) -> InterpResult<'tcx, T>
|
2019-09-30 15:18:23 -05:00
|
|
|
where
|
|
|
|
F: FnMut(FileHandle, &mut MiriEvalContext<'mir, 'tcx>) -> InterpResult<'tcx, T>,
|
|
|
|
{
|
|
|
|
let this = self.eval_context_mut();
|
|
|
|
if let Some(handle) = this.machine.file_handler.handles.remove(&fd) {
|
|
|
|
f(handle, this)
|
2019-09-24 17:28:00 -05:00
|
|
|
} else {
|
2019-10-03 10:21:55 -05:00
|
|
|
let ebadf = this.eval_libc("EBADF")?;
|
|
|
|
this.set_last_error(ebadf)?;
|
2019-10-01 09:18:55 -05:00
|
|
|
Ok((-1).into())
|
2019-09-30 14:07:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-01 10:31:04 -05:00
|
|
|
/// Helper function that consumes an `std::io::Result<T>` and returns an
|
|
|
|
/// `InterpResult<'tcx,T>::Ok` instead. It is expected that the result can be converted to an
|
|
|
|
/// OS error using `std::io::Error::raw_os_error`.
|
|
|
|
///
|
|
|
|
/// This function uses `T: From<i32>` instead of `i32` directly because some IO related
|
|
|
|
/// functions return different integer types (like `read`, that returns an `i64`)
|
2019-10-01 13:48:59 -05:00
|
|
|
fn consume_result<T: From<i32>>(
|
|
|
|
&mut self,
|
|
|
|
result: std::io::Result<T>,
|
|
|
|
) -> InterpResult<'tcx, T> {
|
2019-09-30 14:07:08 -05:00
|
|
|
match result {
|
|
|
|
Ok(ok) => Ok(ok),
|
|
|
|
Err(e) => {
|
2019-10-07 08:38:54 -05:00
|
|
|
self.eval_context_mut().consume_io_error(e)?;
|
2019-10-01 09:18:55 -05:00
|
|
|
Ok((-1).into())
|
2019-09-30 14:07:08 -05:00
|
|
|
}
|
2019-09-24 17:28:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|