]> git.lizzy.rs Git - rust.git/blob - src/shims/foreign_items/posix/linux.rs
improve docs
[rust.git] / src / shims / foreign_items / posix / linux.rs
1 use crate::*;
2 use rustc::mir;
3
4 impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
5 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
6     fn emulate_foreign_item_by_name(
7         &mut self,
8         link_name: &str,
9         args: &[OpTy<'tcx, Tag>],
10         dest: PlaceTy<'tcx, Tag>,
11         _ret: mir::BasicBlock,
12     ) -> InterpResult<'tcx, bool> {
13         let this = self.eval_context_mut();
14
15         match link_name {
16             "__errno_location" => {
17                 let errno_place = this.machine.last_error.unwrap();
18                 this.write_scalar(errno_place.to_ref().to_scalar()?, dest)?;
19             }
20
21             // File related shims
22
23             // The only reason this is not in the `posix` module is because the `macos` item has a
24             // different name.
25             "close" => {
26                 let result = this.close(args[0])?;
27                 this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
28             }
29
30             // Time related shims
31
32             // This is a POSIX function but it has only been tested on linux.
33             "clock_gettime" => {
34                 let result = this.clock_gettime(args[0], args[1])?;
35                 this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
36             }
37
38             // Other shims
39             "pthread_getattr_np" => {
40                 this.write_null(dest)?;
41             }
42
43             "syscall" => {
44                 let sys_getrandom = this
45                     .eval_path_scalar(&["libc", "SYS_getrandom"])?
46                     .expect("Failed to get libc::SYS_getrandom")
47                     .to_machine_usize(this)?;
48
49                 let sys_statx = this
50                     .eval_path_scalar(&["libc", "SYS_statx"])?
51                     .expect("Failed to get libc::SYS_statx")
52                     .to_machine_usize(this)?;
53
54                 match this.read_scalar(args[0])?.to_machine_usize(this)? {
55                     // `libc::syscall(NR_GETRANDOM, buf.as_mut_ptr(), buf.len(), GRND_NONBLOCK)`
56                     // is called if a `HashMap` is created the regular way (e.g. HashMap<K, V>).
57                     id if id == sys_getrandom => {
58                         // The first argument is the syscall id,
59                         // so skip over it.
60                         getrandom(this, &args[1..], dest)?;
61                     }
62                     // `statx` is used by `libstd` to retrieve metadata information in `linux`
63                     // instead of using `stat`,`lstat` or `fstat` as in the `macos` platform.
64                     id if id == sys_statx => {
65                         // The first argument is the syscall id,
66                         // so skip over it.
67                         let result = this.linux_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                 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(true)
87     }
88 }
89
90 // Shims the linux 'getrandom()' syscall.
91 fn getrandom<'tcx>(
92     this: &mut MiriEvalContext<'_, 'tcx>,
93     args: &[OpTy<'tcx, Tag>],
94     dest: PlaceTy<'tcx, Tag>,
95 ) -> InterpResult<'tcx> {
96     let ptr = this.read_scalar(args[0])?.not_undef()?;
97     let len = this.read_scalar(args[1])?.to_machine_usize(this)?;
98
99     // The only supported flags are GRND_RANDOM and GRND_NONBLOCK,
100     // neither of which have any effect on our current PRNG.
101     let _flags = this.read_scalar(args[2])?.to_i32()?;
102
103     this.gen_random(ptr, len as usize)?;
104     this.write_scalar(Scalar::from_uint(len, dest.layout.size), dest)?;
105     Ok(())
106 }