1 use std::collections::HashMap;
5 use rustc::ty::layout::Size;
7 use crate::stacked_borrows::Tag;
10 pub struct FileHandle {
15 pub struct FileHandler {
16 handles: HashMap<i32, FileHandle>,
20 impl Default for FileHandler {
21 fn default() -> Self {
23 handles: Default::default(),
24 // 0, 1 and 2 are reserved for stdin, stdout and stderr
30 impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
31 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
34 path_op: OpTy<'tcx, Tag>,
35 flag_op: OpTy<'tcx, Tag>,
36 ) -> InterpResult<'tcx, i32> {
37 let this = self.eval_context_mut();
39 if !this.machine.communicate {
40 throw_unsup_format!("`open` not available when isolation is enabled")
43 let flag = this.read_scalar(flag_op)?.to_i32()?;
45 if flag != this.eval_libc_i32("O_RDONLY")? && flag != this.eval_libc_i32("O_CLOEXEC")? {
46 throw_unsup_format!("Unsupported flag {:#x}", flag);
51 .read_c_str(this.read_scalar(path_op)?.not_undef()?)?;
52 let path = std::str::from_utf8(path_bytes)
53 .map_err(|_| err_unsup_format!("{:?} is not a valid utf-8 string", path_bytes))?;
54 let fd = File::open(path).map(|file| {
55 let mut fh = &mut this.machine.file_handler;
57 fh.handles.insert(fh.low, FileHandle { file, flag });
61 this.consume_result(fd)
66 fd_op: OpTy<'tcx, Tag>,
67 cmd_op: OpTy<'tcx, Tag>,
68 arg_op: Option<OpTy<'tcx, Tag>>,
69 ) -> InterpResult<'tcx, i32> {
70 let this = self.eval_context_mut();
72 if !this.machine.communicate {
73 throw_unsup_format!("`open` not available when isolation is enabled")
76 let fd = this.read_scalar(fd_op)?.to_i32()?;
77 let cmd = this.read_scalar(cmd_op)?.to_i32()?;
79 if cmd == this.eval_libc_i32("F_SETFD")? {
80 // This does not affect the file itself. Certain flags might require changing the file
81 // or the way it is accessed somehow.
82 let flag = this.read_scalar(arg_op.unwrap())?.to_i32()?;
83 // The only usage of this in stdlib at the moment is to enable the `FD_CLOEXEC` flag.
84 let fd_cloexec = this.eval_libc_i32("FD_CLOEXEC")?;
85 if let Some(FileHandle { flag: old_flag, .. }) =
86 this.machine.file_handler.handles.get_mut(&fd)
88 if flag ^ *old_flag == fd_cloexec {
91 throw_unsup_format!("Unsupported arg {:#x} for `F_SETFD`", flag);
95 } else if cmd == this.eval_libc_i32("F_GETFD")? {
96 this.get_handle_and(fd, |handle| Ok(handle.flag))
98 throw_unsup_format!("Unsupported command {:#x}", cmd);
102 fn close(&mut self, fd_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
103 let this = self.eval_context_mut();
105 if !this.machine.communicate {
106 throw_unsup_format!("`open` not available when isolation is enabled")
109 let fd = this.read_scalar(fd_op)?.to_i32()?;
111 this.remove_handle_and(
113 |handle, this| this.consume_result(handle.file.sync_all().map(|_| 0i32)),
119 fd_op: OpTy<'tcx, Tag>,
120 buf_op: OpTy<'tcx, Tag>,
121 count_op: OpTy<'tcx, Tag>,
122 ) -> InterpResult<'tcx, i64> {
123 let this = self.eval_context_mut();
125 if !this.machine.communicate {
126 throw_unsup_format!("`open` not available when isolation is enabled")
129 let tcx = &{ this.tcx.tcx };
131 let fd = this.read_scalar(fd_op)?.to_i32()?;
132 let buf = this.force_ptr(this.read_scalar(buf_op)?.not_undef()?)?;
133 let count = this.read_scalar(count_op)?.to_usize(&*this.tcx)?;
135 // Remove the file handle to avoid borrowing issues
136 this.remove_handle_and(
141 .read(this.memory_mut().get_mut(buf.alloc_id)?.get_bytes_mut(
144 Size::from_bytes(count),
146 .map(|bytes| bytes as i64);
147 // Reinsert the file handle
148 this.machine.file_handler.handles.insert(fd, handle);
149 this.consume_result(bytes)
154 fn get_handle_and<F, T: From<i32>>(&mut self, fd: i32, f: F) -> InterpResult<'tcx, T>
156 F: Fn(&FileHandle) -> InterpResult<'tcx, T>,
158 let this = self.eval_context_mut();
159 if let Some(handle) = this.machine.file_handler.handles.get(&fd) {
162 this.machine.last_error = this.eval_libc_i32("EBADF")? as u32;
167 fn remove_handle_and<F, T: From<i32>>(&mut self, fd: i32, mut f: F) -> InterpResult<'tcx, T>
169 F: FnMut(FileHandle, &mut MiriEvalContext<'mir, 'tcx>) -> InterpResult<'tcx, T>,
171 let this = self.eval_context_mut();
172 if let Some(handle) = this.machine.file_handler.handles.remove(&fd) {
175 this.machine.last_error = this.eval_libc_i32("EBADF")? as u32;
180 fn consume_result<T: From<i32>>(&mut self, result: std::io::Result<T>) -> InterpResult<'tcx, T> {
184 self.eval_context_mut().machine.last_error = e.raw_os_error().unwrap() as u32;