1use std::any::Any;
2use std::collections::BTreeMap;
3use std::fs::{File, Metadata};
4use std::io::{ErrorKind, IsTerminal, Seek, SeekFrom, Write};
5use std::marker::CoercePointee;
6use std::ops::Deref;
7use std::rc::{Rc, Weak};
8use std::{fs, io};
9
10use rustc_abi::Size;
11
12use crate::shims::unix::UnixFileDescription;
13use crate::*;
14
15#[derive(Debug, Copy, Clone, Default, Eq, PartialEq, Ord, PartialOrd)]
20pub struct FdId(usize);
21
22#[derive(Debug, Clone)]
23struct FdIdWith<T: ?Sized> {
24 id: FdId,
25 inner: T,
26}
27
28#[repr(transparent)]
31#[derive(CoercePointee, Debug)]
32pub struct FileDescriptionRef<T: ?Sized>(Rc<FdIdWith<T>>);
33
34impl<T: ?Sized> Clone for FileDescriptionRef<T> {
35 fn clone(&self) -> Self {
36 FileDescriptionRef(self.0.clone())
37 }
38}
39
40impl<T: ?Sized> Deref for FileDescriptionRef<T> {
41 type Target = T;
42 fn deref(&self) -> &T {
43 &self.0.inner
44 }
45}
46
47impl<T: ?Sized> FileDescriptionRef<T> {
48 pub fn id(&self) -> FdId {
49 self.0.id
50 }
51}
52
53#[derive(Debug)]
55pub struct WeakFileDescriptionRef<T: ?Sized>(Weak<FdIdWith<T>>);
56
57impl<T: ?Sized> Clone for WeakFileDescriptionRef<T> {
58 fn clone(&self) -> Self {
59 WeakFileDescriptionRef(self.0.clone())
60 }
61}
62
63impl<T: ?Sized> FileDescriptionRef<T> {
64 pub fn downgrade(this: &Self) -> WeakFileDescriptionRef<T> {
65 WeakFileDescriptionRef(Rc::downgrade(&this.0))
66 }
67}
68
69impl<T: ?Sized> WeakFileDescriptionRef<T> {
70 pub fn upgrade(&self) -> Option<FileDescriptionRef<T>> {
71 self.0.upgrade().map(FileDescriptionRef)
72 }
73}
74
75impl<T> VisitProvenance for WeakFileDescriptionRef<T> {
76 fn visit_provenance(&self, _visit: &mut VisitWith<'_>) {
77 }
81}
82
83pub trait FileDescriptionExt: 'static {
87 fn into_rc_any(self: FileDescriptionRef<Self>) -> Rc<dyn Any>;
88
89 fn close_ref<'tcx>(
92 self: FileDescriptionRef<Self>,
93 communicate_allowed: bool,
94 ecx: &mut MiriInterpCx<'tcx>,
95 ) -> InterpResult<'tcx, io::Result<()>>;
96}
97
98impl<T: FileDescription + 'static> FileDescriptionExt for T {
99 fn into_rc_any(self: FileDescriptionRef<Self>) -> Rc<dyn Any> {
100 self.0
101 }
102
103 fn close_ref<'tcx>(
104 self: FileDescriptionRef<Self>,
105 communicate_allowed: bool,
106 ecx: &mut MiriInterpCx<'tcx>,
107 ) -> InterpResult<'tcx, io::Result<()>> {
108 match Rc::into_inner(self.0) {
109 Some(fd) => {
110 ecx.machine.epoll_interests.remove_epolls(fd.id);
112
113 fd.inner.destroy(fd.id, communicate_allowed, ecx)
114 }
115 None => {
116 interp_ok(Ok(()))
118 }
119 }
120 }
121}
122
123pub type DynFileDescriptionRef = FileDescriptionRef<dyn FileDescription>;
124
125impl FileDescriptionRef<dyn FileDescription> {
126 pub fn downcast<T: FileDescription + 'static>(self) -> Option<FileDescriptionRef<T>> {
127 let inner = self.into_rc_any().downcast::<FdIdWith<T>>().ok()?;
128 Some(FileDescriptionRef(inner))
129 }
130}
131
132pub trait FileDescription: std::fmt::Debug + FileDescriptionExt {
134 fn name(&self) -> &'static str;
135
136 fn read<'tcx>(
143 self: FileDescriptionRef<Self>,
144 _communicate_allowed: bool,
145 _ptr: Pointer,
146 _len: usize,
147 _ecx: &mut MiriInterpCx<'tcx>,
148 _finish: DynMachineCallback<'tcx, Result<usize, IoError>>,
149 ) -> InterpResult<'tcx> {
150 throw_unsup_format!("cannot read from {}", self.name());
151 }
152
153 fn write<'tcx>(
160 self: FileDescriptionRef<Self>,
161 _communicate_allowed: bool,
162 _ptr: Pointer,
163 _len: usize,
164 _ecx: &mut MiriInterpCx<'tcx>,
165 _finish: DynMachineCallback<'tcx, Result<usize, IoError>>,
166 ) -> InterpResult<'tcx> {
167 throw_unsup_format!("cannot write to {}", self.name());
168 }
169
170 fn short_fd_operations(&self) -> bool {
172 false
174 }
175
176 fn seek<'tcx>(
179 &self,
180 _communicate_allowed: bool,
181 _offset: SeekFrom,
182 ) -> InterpResult<'tcx, io::Result<u64>> {
183 throw_unsup_format!("cannot seek on {}", self.name());
184 }
185
186 fn destroy<'tcx>(
190 self,
191 _self_id: FdId,
192 _communicate_allowed: bool,
193 _ecx: &mut MiriInterpCx<'tcx>,
194 ) -> InterpResult<'tcx, io::Result<()>>
195 where
196 Self: Sized,
197 {
198 throw_unsup_format!("cannot close {}", self.name());
199 }
200
201 fn metadata<'tcx>(&self) -> InterpResult<'tcx, io::Result<fs::Metadata>> {
202 throw_unsup_format!("obtaining metadata is only supported on file-backed file descriptors");
203 }
204
205 fn is_tty(&self, _communicate_allowed: bool) -> bool {
206 false
209 }
210
211 fn as_unix<'tcx>(&self, _ecx: &MiriInterpCx<'tcx>) -> &dyn UnixFileDescription {
212 panic!("Not a unix file descriptor: {}", self.name());
213 }
214
215 fn get_flags<'tcx>(&self, _ecx: &mut MiriInterpCx<'tcx>) -> InterpResult<'tcx, Scalar> {
217 throw_unsup_format!("fcntl: {} is not supported for F_GETFL", self.name());
218 }
219
220 fn set_flags<'tcx>(
222 &self,
223 _flag: i32,
224 _ecx: &mut MiriInterpCx<'tcx>,
225 ) -> InterpResult<'tcx, Scalar> {
226 throw_unsup_format!("fcntl: {} is not supported for F_SETFL", self.name());
227 }
228}
229
230impl FileDescription for io::Stdin {
231 fn name(&self) -> &'static str {
232 "stdin"
233 }
234
235 fn read<'tcx>(
236 self: FileDescriptionRef<Self>,
237 communicate_allowed: bool,
238 ptr: Pointer,
239 len: usize,
240 ecx: &mut MiriInterpCx<'tcx>,
241 finish: DynMachineCallback<'tcx, Result<usize, IoError>>,
242 ) -> InterpResult<'tcx> {
243 if !communicate_allowed {
244 helpers::isolation_abort_error("`read` from stdin")?;
246 }
247
248 let result = ecx.read_from_host(&*self, len, ptr)?;
249 finish.call(ecx, result)
250 }
251
252 fn destroy<'tcx>(
253 self,
254 _self_id: FdId,
255 _communicate_allowed: bool,
256 _ecx: &mut MiriInterpCx<'tcx>,
257 ) -> InterpResult<'tcx, io::Result<()>> {
258 interp_ok(Ok(()))
259 }
260
261 fn is_tty(&self, communicate_allowed: bool) -> bool {
262 communicate_allowed && self.is_terminal()
263 }
264}
265
266impl FileDescription for io::Stdout {
267 fn name(&self) -> &'static str {
268 "stdout"
269 }
270
271 fn write<'tcx>(
272 self: FileDescriptionRef<Self>,
273 _communicate_allowed: bool,
274 ptr: Pointer,
275 len: usize,
276 ecx: &mut MiriInterpCx<'tcx>,
277 finish: DynMachineCallback<'tcx, Result<usize, IoError>>,
278 ) -> InterpResult<'tcx> {
279 let result = ecx.write_to_host(&*self, len, ptr)?;
281 io::stdout().flush().unwrap();
287
288 finish.call(ecx, result)
289 }
290
291 fn destroy<'tcx>(
292 self,
293 _self_id: FdId,
294 _communicate_allowed: bool,
295 _ecx: &mut MiriInterpCx<'tcx>,
296 ) -> InterpResult<'tcx, io::Result<()>> {
297 interp_ok(Ok(()))
298 }
299
300 fn is_tty(&self, communicate_allowed: bool) -> bool {
301 communicate_allowed && self.is_terminal()
302 }
303}
304
305impl FileDescription for io::Stderr {
306 fn name(&self) -> &'static str {
307 "stderr"
308 }
309
310 fn destroy<'tcx>(
311 self,
312 _self_id: FdId,
313 _communicate_allowed: bool,
314 _ecx: &mut MiriInterpCx<'tcx>,
315 ) -> InterpResult<'tcx, io::Result<()>> {
316 interp_ok(Ok(()))
317 }
318
319 fn write<'tcx>(
320 self: FileDescriptionRef<Self>,
321 _communicate_allowed: bool,
322 ptr: Pointer,
323 len: usize,
324 ecx: &mut MiriInterpCx<'tcx>,
325 finish: DynMachineCallback<'tcx, Result<usize, IoError>>,
326 ) -> InterpResult<'tcx> {
327 let result = ecx.write_to_host(&*self, len, ptr)?;
329 finish.call(ecx, result)
331 }
332
333 fn is_tty(&self, communicate_allowed: bool) -> bool {
334 communicate_allowed && self.is_terminal()
335 }
336}
337
338#[derive(Debug)]
339pub struct FileHandle {
340 pub(crate) file: File,
341 pub(crate) writable: bool,
342}
343
344impl FileDescription for FileHandle {
345 fn name(&self) -> &'static str {
346 "file"
347 }
348
349 fn read<'tcx>(
350 self: FileDescriptionRef<Self>,
351 communicate_allowed: bool,
352 ptr: Pointer,
353 len: usize,
354 ecx: &mut MiriInterpCx<'tcx>,
355 finish: DynMachineCallback<'tcx, Result<usize, IoError>>,
356 ) -> InterpResult<'tcx> {
357 assert!(communicate_allowed, "isolation should have prevented even opening a file");
358
359 let result = ecx.read_from_host(&self.file, len, ptr)?;
360 finish.call(ecx, result)
361 }
362
363 fn write<'tcx>(
364 self: FileDescriptionRef<Self>,
365 communicate_allowed: bool,
366 ptr: Pointer,
367 len: usize,
368 ecx: &mut MiriInterpCx<'tcx>,
369 finish: DynMachineCallback<'tcx, Result<usize, IoError>>,
370 ) -> InterpResult<'tcx> {
371 assert!(communicate_allowed, "isolation should have prevented even opening a file");
372
373 if !self.writable {
374 return finish.call(ecx, Err(ErrorKind::PermissionDenied.into()));
381 }
382 let result = ecx.write_to_host(&self.file, len, ptr)?;
383 finish.call(ecx, result)
384 }
385
386 fn seek<'tcx>(
387 &self,
388 communicate_allowed: bool,
389 offset: SeekFrom,
390 ) -> InterpResult<'tcx, io::Result<u64>> {
391 assert!(communicate_allowed, "isolation should have prevented even opening a file");
392 interp_ok((&mut &self.file).seek(offset))
393 }
394
395 fn destroy<'tcx>(
396 self,
397 _self_id: FdId,
398 communicate_allowed: bool,
399 _ecx: &mut MiriInterpCx<'tcx>,
400 ) -> InterpResult<'tcx, io::Result<()>> {
401 assert!(communicate_allowed, "isolation should have prevented even opening a file");
402 if self.writable {
404 let result = self.file.sync_all();
407 drop(self.file);
409 interp_ok(result)
410 } else {
411 drop(self.file);
418 interp_ok(Ok(()))
419 }
420 }
421
422 fn metadata<'tcx>(&self) -> InterpResult<'tcx, io::Result<Metadata>> {
423 interp_ok(self.file.metadata())
424 }
425
426 fn is_tty(&self, communicate_allowed: bool) -> bool {
427 communicate_allowed && self.file.is_terminal()
428 }
429
430 fn short_fd_operations(&self) -> bool {
431 true
435 }
436
437 fn as_unix<'tcx>(&self, ecx: &MiriInterpCx<'tcx>) -> &dyn UnixFileDescription {
438 assert!(
439 ecx.target_os_is_unix(),
440 "unix file operations are only available for unix targets"
441 );
442 self
443 }
444}
445
446#[derive(Debug)]
448pub struct NullOutput;
449
450impl FileDescription for NullOutput {
451 fn name(&self) -> &'static str {
452 "stderr and stdout"
453 }
454
455 fn write<'tcx>(
456 self: FileDescriptionRef<Self>,
457 _communicate_allowed: bool,
458 _ptr: Pointer,
459 len: usize,
460 ecx: &mut MiriInterpCx<'tcx>,
461 finish: DynMachineCallback<'tcx, Result<usize, IoError>>,
462 ) -> InterpResult<'tcx> {
463 finish.call(ecx, Ok(len))
465 }
466
467 fn destroy<'tcx>(
468 self,
469 _self_id: FdId,
470 _communicate_allowed: bool,
471 _ecx: &mut MiriInterpCx<'tcx>,
472 ) -> InterpResult<'tcx, io::Result<()>> {
473 interp_ok(Ok(()))
474 }
475}
476
477pub type FdNum = i32;
479
480#[derive(Debug)]
482pub struct FdTable {
483 pub fds: BTreeMap<FdNum, DynFileDescriptionRef>,
484 next_file_description_id: FdId,
486}
487
488impl VisitProvenance for FdTable {
489 fn visit_provenance(&self, _visit: &mut VisitWith<'_>) {
490 }
492}
493
494impl FdTable {
495 fn new() -> Self {
496 FdTable { fds: BTreeMap::new(), next_file_description_id: FdId(0) }
497 }
498 pub(crate) fn init(mute_stdout_stderr: bool) -> FdTable {
499 let mut fds = FdTable::new();
500 fds.insert_new(io::stdin());
501 if mute_stdout_stderr {
502 assert_eq!(fds.insert_new(NullOutput), 1);
503 assert_eq!(fds.insert_new(NullOutput), 2);
504 } else {
505 assert_eq!(fds.insert_new(io::stdout()), 1);
506 assert_eq!(fds.insert_new(io::stderr()), 2);
507 }
508 fds
509 }
510
511 pub fn new_ref<T: FileDescription>(&mut self, fd: T) -> FileDescriptionRef<T> {
512 let file_handle =
513 FileDescriptionRef(Rc::new(FdIdWith { id: self.next_file_description_id, inner: fd }));
514 self.next_file_description_id = FdId(self.next_file_description_id.0.strict_add(1));
515 file_handle
516 }
517
518 pub fn insert_new(&mut self, fd: impl FileDescription) -> FdNum {
520 let fd_ref = self.new_ref(fd);
521 self.insert(fd_ref)
522 }
523
524 pub fn insert(&mut self, fd_ref: DynFileDescriptionRef) -> FdNum {
525 self.insert_with_min_num(fd_ref, 0)
526 }
527
528 pub fn insert_with_min_num(
530 &mut self,
531 file_handle: DynFileDescriptionRef,
532 min_fd_num: FdNum,
533 ) -> FdNum {
534 let candidate_new_fd =
539 self.fds.range(min_fd_num..).zip(min_fd_num..).find_map(|((fd_num, _fd), counter)| {
540 if *fd_num != counter {
541 Some(counter)
544 } else {
545 None
547 }
548 });
549 let new_fd_num = candidate_new_fd.unwrap_or_else(|| {
550 self.fds.last_key_value().map(|(fd_num, _)| fd_num.strict_add(1)).unwrap_or(min_fd_num)
553 });
554
555 self.fds.try_insert(new_fd_num, file_handle).unwrap();
556 new_fd_num
557 }
558
559 pub fn get(&self, fd_num: FdNum) -> Option<DynFileDescriptionRef> {
560 let fd = self.fds.get(&fd_num)?;
561 Some(fd.clone())
562 }
563
564 pub fn remove(&mut self, fd_num: FdNum) -> Option<DynFileDescriptionRef> {
565 self.fds.remove(&fd_num)
566 }
567
568 pub fn is_fd_num(&self, fd_num: FdNum) -> bool {
569 self.fds.contains_key(&fd_num)
570 }
571}
572
573impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
574pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
575 fn read_from_host(
578 &mut self,
579 mut file: impl io::Read,
580 len: usize,
581 ptr: Pointer,
582 ) -> InterpResult<'tcx, Result<usize, IoError>> {
583 let this = self.eval_context_mut();
584
585 let mut bytes = vec![0; len];
586 let result = file.read(&mut bytes);
587 match result {
588 Ok(read_size) => {
589 this.write_bytes_ptr(ptr, bytes[..read_size].iter().copied())?;
593 interp_ok(Ok(read_size))
594 }
595 Err(e) => interp_ok(Err(IoError::HostError(e))),
596 }
597 }
598
599 fn write_to_host(
601 &mut self,
602 mut file: impl io::Write,
603 len: usize,
604 ptr: Pointer,
605 ) -> InterpResult<'tcx, Result<usize, IoError>> {
606 let this = self.eval_context_mut();
607
608 let bytes = this.read_bytes_ptr_strip_provenance(ptr, Size::from_bytes(len))?;
609 let result = file.write(bytes);
610 interp_ok(result.map_err(IoError::HostError))
611 }
612}