Skip to main content

miri/shims/unix/
fd.rs

1//! General management of file descriptors, and support for
2//! standard file descriptors (stdin/stdout/stderr).
3
4use std::io;
5use std::io::ErrorKind;
6
7use rand::Rng;
8use rustc_abi::Size;
9use rustc_target::spec::Os;
10
11use crate::shims::files::FileDescription;
12use crate::shims::sig::check_min_vararg_count;
13use crate::shims::unix::linux_like::epoll::EpollEvents;
14use crate::shims::unix::*;
15use crate::*;
16
17#[derive(Debug, Clone, Copy, Eq, PartialEq)]
18pub enum FlockOp {
19    SharedLock { nonblocking: bool },
20    ExclusiveLock { nonblocking: bool },
21    Unlock,
22}
23
24/// Represents unix-specific file descriptions.
25pub trait UnixFileDescription: FileDescription {
26    /// Reads as much as possible into the given buffer `ptr` from a given offset.
27    /// `len` indicates how many bytes we should try to read.
28    /// `dest` is where the return value should be stored: number of bytes read, or `-1` in case of error.
29    fn pread<'tcx>(
30        &self,
31        _communicate_allowed: bool,
32        _offset: u64,
33        _ptr: Pointer,
34        _len: usize,
35        _ecx: &mut MiriInterpCx<'tcx>,
36        _finish: DynMachineCallback<'tcx, Result<usize, IoError>>,
37    ) -> InterpResult<'tcx> {
38        throw_unsup_format!("cannot pread from {}", self.name());
39    }
40
41    /// Writes as much as possible from the given buffer `ptr` starting at a given offset.
42    /// `ptr` is the pointer to the user supplied read buffer.
43    /// `len` indicates how many bytes we should try to write.
44    /// `dest` is where the return value should be stored: number of bytes written, or `-1` in case of error.
45    fn pwrite<'tcx>(
46        &self,
47        _communicate_allowed: bool,
48        _ptr: Pointer,
49        _len: usize,
50        _offset: u64,
51        _ecx: &mut MiriInterpCx<'tcx>,
52        _finish: DynMachineCallback<'tcx, Result<usize, IoError>>,
53    ) -> InterpResult<'tcx> {
54        throw_unsup_format!("cannot pwrite to {}", self.name());
55    }
56
57    fn flock<'tcx>(
58        &self,
59        _communicate_allowed: bool,
60        _op: FlockOp,
61    ) -> InterpResult<'tcx, io::Result<()>> {
62        throw_unsup_format!("cannot flock {}", self.name());
63    }
64
65    /// Modifies device parameters.
66    /// `op` is the device-dependent operation code. It's either a `c_long` or `c_int`, depending on
67    /// the target and whether it uses glibc or musl.
68    /// `arg` is the optional third argument which exists depending on the operation code. It's either
69    /// an integer or a pointer.
70    fn ioctl<'tcx>(
71        &self,
72        _op: Scalar,
73        _arg: Option<&OpTy<'tcx>>,
74        _ecx: &mut MiriInterpCx<'tcx>,
75    ) -> InterpResult<'tcx, i32> {
76        throw_unsup_format!("cannot use ioctl on {}", self.name());
77    }
78
79    /// Return which epoll events are currently active.
80    fn epoll_active_events<'tcx>(&self) -> InterpResult<'tcx, EpollEvents> {
81        throw_unsup_format!("{}: epoll does not support this file description", self.name());
82    }
83}
84
85impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
86pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
87    fn dup(&mut self, old_fd_num: i32) -> InterpResult<'tcx, Scalar> {
88        let this = self.eval_context_mut();
89
90        let Some(fd) = this.machine.fds.get(old_fd_num) else {
91            return this.set_last_error_and_return_i32(LibcError("EBADF"));
92        };
93        interp_ok(Scalar::from_i32(this.machine.fds.insert(fd)))
94    }
95
96    fn dup2(&mut self, old_fd_num: i32, new_fd_num: i32) -> InterpResult<'tcx, Scalar> {
97        let this = self.eval_context_mut();
98
99        let Some(fd) = this.machine.fds.get(old_fd_num) else {
100            return this.set_last_error_and_return_i32(LibcError("EBADF"));
101        };
102        if new_fd_num != old_fd_num {
103            // Close new_fd if it is previously opened.
104            // If old_fd and new_fd point to the same description, then `dup_fd` ensures we keep the underlying file description alive.
105            if let Some(old_new_fd) = this.machine.fds.fds.insert(new_fd_num, fd) {
106                // Ignore close error (not interpreter's) according to dup2() doc.
107                old_new_fd.close_ref(this.machine.communicate(), this)?.ok();
108            }
109        }
110        interp_ok(Scalar::from_i32(new_fd_num))
111    }
112
113    fn flock(&mut self, fd_num: i32, op: i32) -> InterpResult<'tcx, Scalar> {
114        let this = self.eval_context_mut();
115        let Some(fd) = this.machine.fds.get(fd_num) else {
116            return this.set_last_error_and_return_i32(LibcError("EBADF"));
117        };
118
119        // We need to check that there aren't unsupported options in `op`.
120        let lock_sh = this.eval_libc_i32("LOCK_SH");
121        let lock_ex = this.eval_libc_i32("LOCK_EX");
122        let lock_nb = this.eval_libc_i32("LOCK_NB");
123        let lock_un = this.eval_libc_i32("LOCK_UN");
124
125        use FlockOp::*;
126        let parsed_op = if op == lock_sh {
127            SharedLock { nonblocking: false }
128        } else if op == lock_sh | lock_nb {
129            SharedLock { nonblocking: true }
130        } else if op == lock_ex {
131            ExclusiveLock { nonblocking: false }
132        } else if op == lock_ex | lock_nb {
133            ExclusiveLock { nonblocking: true }
134        } else if op == lock_un {
135            Unlock
136        } else {
137            throw_unsup_format!("unsupported flags {:#x}", op);
138        };
139
140        let result = fd.as_unix(this).flock(this.machine.communicate(), parsed_op)?;
141        // return `0` if flock is successful
142        let result = result.map(|()| 0i32);
143        interp_ok(Scalar::from_i32(this.try_unwrap_io_result(result)?))
144    }
145
146    fn ioctl(
147        &mut self,
148        fd: &OpTy<'tcx>,
149        op: &OpTy<'tcx>,
150        varargs: &[OpTy<'tcx>],
151    ) -> InterpResult<'tcx, Scalar> {
152        let this = self.eval_context_mut();
153
154        let fd = this.read_scalar(fd)?.to_i32()?;
155        let op = this.read_scalar(op)?;
156        // There is at most one relevant variadic argument.
157        // It exists depending on the device and the opcode and thus we can't
158        // use `check_min_vararg_count` here.
159        let arg = varargs.first();
160
161        let Some(fd) = this.machine.fds.get(fd) else {
162            return this.set_last_error_and_return_i32(LibcError("EBADF"));
163        };
164
165        // Handle common opcodes.
166        let fioclex = this.eval_libc("FIOCLEX");
167        let fionclex = this.eval_libc("FIONCLEX");
168        if op == fioclex || op == fionclex {
169            // Since we don't support `exec`, those are NOPs.
170            return interp_ok(Scalar::from_i32(0));
171        }
172
173        // Since some ioctl operations use the return value as an output parameter, we cannot strictly use the convention of
174        // zero indicating success and -1 indicating an error.
175        let return_value = fd.as_unix(this).ioctl(op, arg, this)?;
176        interp_ok(Scalar::from_i32(return_value))
177    }
178
179    fn fcntl(
180        &mut self,
181        fd_num: &OpTy<'tcx>,
182        cmd: &OpTy<'tcx>,
183        varargs: &[OpTy<'tcx>],
184    ) -> InterpResult<'tcx, Scalar> {
185        let this = self.eval_context_mut();
186
187        let fd_num = this.read_scalar(fd_num)?.to_i32()?;
188        let cmd = this.read_scalar(cmd)?.to_i32()?;
189
190        let f_getfd = this.eval_libc_i32("F_GETFD");
191        let f_dupfd = this.eval_libc_i32("F_DUPFD");
192        let f_dupfd_cloexec = this.eval_libc_i32("F_DUPFD_CLOEXEC");
193        let f_getfl = this.eval_libc_i32("F_GETFL");
194        let f_setfl = this.eval_libc_i32("F_SETFL");
195
196        // We only support getting the flags for a descriptor.
197        match cmd {
198            cmd if cmd == f_getfd => {
199                // Currently this is the only flag that `F_GETFD` returns. It is OK to just return the
200                // `FD_CLOEXEC` value without checking if the flag is set for the file because `std`
201                // always sets this flag when opening a file. However we still need to check that the
202                // file itself is open.
203                if !this.machine.fds.is_fd_num(fd_num) {
204                    this.set_last_error_and_return_i32(LibcError("EBADF"))
205                } else {
206                    interp_ok(this.eval_libc("FD_CLOEXEC"))
207                }
208            }
209            cmd if cmd == f_dupfd || cmd == f_dupfd_cloexec => {
210                // Note that we always assume the FD_CLOEXEC flag is set for every open file, in part
211                // because exec() isn't supported. The F_DUPFD and F_DUPFD_CLOEXEC commands only
212                // differ in whether the FD_CLOEXEC flag is pre-set on the new file descriptor,
213                // thus they can share the same implementation here.
214                let cmd_name = if cmd == f_dupfd {
215                    "fcntl(fd, F_DUPFD, ...)"
216                } else {
217                    "fcntl(fd, F_DUPFD_CLOEXEC, ...)"
218                };
219
220                let [start] = check_min_vararg_count(cmd_name, varargs)?;
221                let start = this.read_scalar(start)?.to_i32()?;
222
223                if let Some(fd) = this.machine.fds.get(fd_num) {
224                    interp_ok(Scalar::from_i32(this.machine.fds.insert_with_min_num(fd, start)))
225                } else {
226                    this.set_last_error_and_return_i32(LibcError("EBADF"))
227                }
228            }
229            cmd if cmd == f_getfl => {
230                // Check if this is a valid open file descriptor.
231                let Some(fd) = this.machine.fds.get(fd_num) else {
232                    return this.set_last_error_and_return_i32(LibcError("EBADF"));
233                };
234
235                fd.get_flags(this)
236            }
237            cmd if cmd == f_setfl => {
238                // Check if this is a valid open file descriptor.
239                let Some(fd) = this.machine.fds.get(fd_num) else {
240                    return this.set_last_error_and_return_i32(LibcError("EBADF"));
241                };
242
243                let [flag] = check_min_vararg_count("fcntl(fd, F_SETFL, ...)", varargs)?;
244                let flag = this.read_scalar(flag)?.to_i32()?;
245
246                // Ignore flags that never get stored by SETFL.
247                // "File access mode (O_RDONLY, O_WRONLY, O_RDWR) and file
248                // creation flags (i.e., O_CREAT, O_EXCL, O_NOCTTY, O_TRUNC)
249                // in arg are ignored."
250                let ignored_flags = this.eval_libc_i32("O_RDONLY")
251                    | this.eval_libc_i32("O_WRONLY")
252                    | this.eval_libc_i32("O_RDWR")
253                    | this.eval_libc_i32("O_CREAT")
254                    | this.eval_libc_i32("O_EXCL")
255                    | this.eval_libc_i32("O_NOCTTY")
256                    | this.eval_libc_i32("O_TRUNC");
257
258                fd.set_flags(flag & !ignored_flags, this)
259            }
260            cmd if this.tcx.sess.target.os == Os::MacOs
261                && cmd == this.eval_libc_i32("F_FULLFSYNC") =>
262            {
263                // Reject if isolation is enabled.
264                if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
265                    this.reject_in_isolation("`fcntl`", reject_with)?;
266                    return this.set_last_error_and_return_i32(ErrorKind::PermissionDenied);
267                }
268
269                this.ffullsync_fd(fd_num)
270            }
271            cmd => {
272                throw_unsup_format!("fcntl: unsupported command {cmd:#x}");
273            }
274        }
275    }
276
277    fn close(&mut self, fd_op: &OpTy<'tcx>) -> InterpResult<'tcx, Scalar> {
278        let this = self.eval_context_mut();
279
280        let fd_num = this.read_scalar(fd_op)?.to_i32()?;
281
282        let Some(fd) = this.machine.fds.remove(fd_num) else {
283            return this.set_last_error_and_return_i32(LibcError("EBADF"));
284        };
285        let result = fd.close_ref(this.machine.communicate(), this)?;
286        // return `0` if close is successful
287        let result = result.map(|()| 0i32);
288        interp_ok(Scalar::from_i32(this.try_unwrap_io_result(result)?))
289    }
290
291    /// Read data from `fd` into buffer specified by `buf` and `count`.
292    ///
293    /// If `offset` is `None`, reads data from current cursor position associated with `fd`
294    /// and updates cursor position on completion. Otherwise, reads from the specified offset
295    /// and keeps the cursor unchanged.
296    fn read(
297        &mut self,
298        fd_num: i32,
299        buf: Pointer,
300        count: u64,
301        offset: Option<i128>,
302        dest: &MPlaceTy<'tcx>,
303    ) -> InterpResult<'tcx> {
304        let this = self.eval_context_mut();
305
306        // Isolation check is done via `FileDescription` trait.
307
308        trace!("Reading from FD {}, size {}", fd_num, count);
309
310        // Check that the *entire* buffer is actually valid memory.
311        this.check_ptr_access(buf, Size::from_bytes(count), CheckInAllocMsg::MemoryAccess)?;
312
313        // We cap the number of read bytes to the largest value that we are able to fit in both the
314        // host's and target's `isize`. This saves us from having to handle overflows later.
315        let count = count
316            .min(u64::try_from(this.target_isize_max()).unwrap())
317            .min(u64::try_from(isize::MAX).unwrap());
318        let count = usize::try_from(count).unwrap(); // now it fits in a `usize`
319        let communicate = this.machine.communicate();
320
321        // Get the FD.
322        let Some(fd) = this.machine.fds.get(fd_num) else {
323            trace!("read: FD not found");
324            return this.set_last_error_and_return(LibcError("EBADF"), dest);
325        };
326
327        // Handle the zero-sized case. The man page says:
328        // > If count is zero, read() may detect the errors described below.  In the absence of any
329        // > errors, or if read() does not check for errors, a read() with a count of 0 returns zero
330        // > and has no other effects.
331        if count == 0 {
332            this.write_null(dest)?;
333            return interp_ok(());
334        }
335        // Non-deterministically decide to further reduce the count, simulating a partial read (but
336        // never to 0, that would indicate EOF).
337        let count = if this.machine.short_fd_operations
338            && fd.short_fd_operations()
339            && count >= 2
340            && this.machine.rng.get_mut().random()
341        {
342            count / 2 // since `count` is at least 2, the result is still at least 1
343        } else {
344            count
345        };
346
347        trace!("read: FD mapped to {fd:?}");
348        // We want to read at most `count` bytes. We are sure that `count` is not negative
349        // because it was a target's `usize`. Also we are sure that it's smaller than
350        // `usize::MAX` because it is bounded by the host's `isize`.
351
352        let finish = {
353            let dest = dest.clone();
354            callback!(
355                @capture<'tcx> {
356                    count: usize,
357                    dest: MPlaceTy<'tcx>,
358                }
359                |this, result: Result<usize, IoError>| {
360                    match result {
361                        Ok(read_size) => {
362                            assert!(read_size <= count);
363                            // This must fit since `count` fits.
364                            this.write_int(u64::try_from(read_size).unwrap(), &dest)
365                        }
366                        Err(e) => {
367                            this.set_last_error_and_return(e, &dest)
368                        }
369                }}
370            )
371        };
372        match offset {
373            None => fd.read(communicate, buf, count, this, finish)?,
374            Some(offset) => {
375                let Ok(offset) = u64::try_from(offset) else {
376                    return this.set_last_error_and_return(LibcError("EINVAL"), dest);
377                };
378                fd.as_unix(this).pread(communicate, offset, buf, count, this, finish)?
379            }
380        };
381        interp_ok(())
382    }
383
384    fn write(
385        &mut self,
386        fd_num: i32,
387        buf: Pointer,
388        count: u64,
389        offset: Option<i128>,
390        dest: &MPlaceTy<'tcx>,
391    ) -> InterpResult<'tcx> {
392        let this = self.eval_context_mut();
393
394        // Isolation check is done via `FileDescription` trait.
395
396        // Check that the *entire* buffer is actually valid memory.
397        this.check_ptr_access(buf, Size::from_bytes(count), CheckInAllocMsg::MemoryAccess)?;
398
399        // We cap the number of written bytes to the largest value that we are able to fit in both the
400        // host's and target's `isize`. This saves us from having to handle overflows later.
401        let count = count
402            .min(u64::try_from(this.target_isize_max()).unwrap())
403            .min(u64::try_from(isize::MAX).unwrap());
404        let count = usize::try_from(count).unwrap(); // now it fits in a `usize`
405        let communicate = this.machine.communicate();
406
407        // We temporarily dup the FD to be able to retain mutable access to `this`.
408        let Some(fd) = this.machine.fds.get(fd_num) else {
409            return this.set_last_error_and_return(LibcError("EBADF"), dest);
410        };
411
412        // Handle the zero-sized case. The man page says:
413        // > If count is zero and fd refers to a regular file, then write() may return a failure
414        // > status if one of the errors below is detected.  If no errors are detected, or error
415        // > detection is not performed, 0 is returned without causing any other effect.   If  count
416        // > is  zero  and  fd refers to a file other than a regular file, the results are not
417        // > specified.
418        if count == 0 {
419            // For now let's not open the can of worms of what exactly "not specified" could mean...
420            this.write_null(dest)?;
421            return interp_ok(());
422        }
423        // Non-deterministically decide to further reduce the count, simulating a partial write.
424        // We avoid reducing the write size to 0: the docs seem to be entirely fine with that,
425        // but the standard library is not (https://github.com/rust-lang/rust/issues/145959).
426        let count = if this.machine.short_fd_operations
427            && fd.short_fd_operations()
428            && count >= 2
429            && this.machine.rng.get_mut().random()
430        {
431            count / 2
432        } else {
433            count
434        };
435
436        let finish = {
437            let dest = dest.clone();
438            callback!(
439                @capture<'tcx> {
440                    count: usize,
441                    dest: MPlaceTy<'tcx>,
442                }
443                |this, result: Result<usize, IoError>| {
444                    match result {
445                        Ok(write_size) => {
446                            assert!(write_size <= count);
447                            // This must fit since `count` fits.
448                            this.write_int(u64::try_from(write_size).unwrap(), &dest)
449                        }
450                        Err(e) => {
451                            this.set_last_error_and_return(e, &dest)
452                        }
453                }}
454            )
455        };
456        match offset {
457            None => fd.write(communicate, buf, count, this, finish)?,
458            Some(offset) => {
459                let Ok(offset) = u64::try_from(offset) else {
460                    return this.set_last_error_and_return(LibcError("EINVAL"), dest);
461                };
462                fd.as_unix(this).pwrite(communicate, buf, count, offset, this, finish)?
463            }
464        };
465        interp_ok(())
466    }
467}