]> git.lizzy.rs Git - rust.git/blob - src/shims/foreign_items/posix/linux.rs
7267cc1af8e5ca21bfdbfce89c261d531ceabb0c
[rust.git] / src / shims / foreign_items / posix / linux.rs
1 use crate::*;
2
3 impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
4 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
5     fn emulate_foreign_item_by_name(
6         &mut self,
7         link_name: &str,
8         args: &[OpTy<'tcx, Tag>],
9         dest: PlaceTy<'tcx, Tag>,
10     ) -> InterpResult<'tcx> {
11         let this = self.eval_context_mut();
12
13         match link_name {
14             "__errno_location" => {
15                 let errno_place = this.machine.last_error.unwrap();
16                 this.write_scalar(errno_place.to_ref().to_scalar()?, dest)?;
17             }
18
19             // File related shims
20             "open64" => {
21                 let result = this.open(args[0], args[1])?;
22                 this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
23             }
24
25             "close" => {
26                 let result = this.close(args[0])?;
27                 this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
28             }
29
30             "lseek64" => {
31                 let result = this.lseek64(args[0], args[1], args[2])?;
32                 this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
33             }
34
35             // Time related shims
36             "clock_gettime" => {
37                 let result = this.clock_gettime(args[0], args[1])?;
38                 this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
39             }
40
41             "pthread_getattr_np" => {
42                 this.write_null(dest)?;
43             }
44
45             "syscall" => {
46                 let sys_getrandom = this
47                     .eval_path_scalar(&["libc", "SYS_getrandom"])?
48                     .expect("Failed to get libc::SYS_getrandom")
49                     .to_machine_usize(this)?;
50
51                 let sys_statx = this
52                     .eval_path_scalar(&["libc", "SYS_statx"])?
53                     .expect("Failed to get libc::SYS_statx")
54                     .to_machine_usize(this)?;
55
56                 match this.read_scalar(args[0])?.to_machine_usize(this)? {
57                     // `libc::syscall(NR_GETRANDOM, buf.as_mut_ptr(), buf.len(), GRND_NONBLOCK)`
58                     // is called if a `HashMap` is created the regular way (e.g. HashMap<K, V>).
59                     id if id == sys_getrandom => {
60                         // The first argument is the syscall id,
61                         // so skip over it.
62                         super::getrandom(this, &args[1..], dest)?;
63                     }
64                     id if id == sys_statx => {
65                         // The first argument is the syscall id,
66                         // so skip over it.
67                         let result = this.statx(args[1], args[2], args[3], args[4], args[5])?;
68                         this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
69                     }
70                     id => throw_unsup_format!("miri does not support syscall ID {}", id),
71                 }
72             }
73
74             "getrandom" => {
75                 super::getrandom(this, args, dest)?;
76             }
77
78             "sched_getaffinity" => {
79                 // Return an error; `num_cpus` then falls back to `sysconf`.
80                 this.write_scalar(Scalar::from_int(-1, dest.layout.size), dest)?;
81             }
82
83             _ => throw_unsup_format!("can't call foreign function: {}", link_name),
84         };
85
86         Ok(())
87     }
88 }