]> git.lizzy.rs Git - rust.git/blob - src/shims/foreign_items/posix/linux.rs
reorganize comments for shim's classification
[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             "open64" => {
23                 let result = this.open(args[0], args[1])?;
24                 this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
25             }
26
27             "close" => {
28                 let result = this.close(args[0])?;
29                 this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
30             }
31
32             "lseek64" => {
33                 let result = this.lseek64(args[0], args[1], args[2])?;
34                 this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
35             }
36
37             // Time related shims
38             "clock_gettime" => {
39                 let result = this.clock_gettime(args[0], args[1])?;
40                 this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
41             }
42
43             // Other shims
44             "pthread_getattr_np" => {
45                 this.write_null(dest)?;
46             }
47
48             "syscall" => {
49                 let sys_getrandom = this
50                     .eval_path_scalar(&["libc", "SYS_getrandom"])?
51                     .expect("Failed to get libc::SYS_getrandom")
52                     .to_machine_usize(this)?;
53
54                 let sys_statx = this
55                     .eval_path_scalar(&["libc", "SYS_statx"])?
56                     .expect("Failed to get libc::SYS_statx")
57                     .to_machine_usize(this)?;
58
59                 match this.read_scalar(args[0])?.to_machine_usize(this)? {
60                     // `libc::syscall(NR_GETRANDOM, buf.as_mut_ptr(), buf.len(), GRND_NONBLOCK)`
61                     // is called if a `HashMap` is created the regular way (e.g. HashMap<K, V>).
62                     id if id == sys_getrandom => {
63                         // The first argument is the syscall id,
64                         // so skip over it.
65                         super::getrandom(this, &args[1..], dest)?;
66                     }
67                     id if id == sys_statx => {
68                         // The first argument is the syscall id,
69                         // so skip over it.
70                         let result = this.statx(args[1], args[2], args[3], args[4], args[5])?;
71                         this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
72                     }
73                     id => throw_unsup_format!("miri does not support syscall ID {}", id),
74                 }
75             }
76
77             "getrandom" => {
78                 super::getrandom(this, args, dest)?;
79             }
80
81             "sched_getaffinity" => {
82                 // Return an error; `num_cpus` then falls back to `sysconf`.
83                 this.write_scalar(Scalar::from_int(-1, dest.layout.size), dest)?;
84             }
85
86             _ => throw_unsup_format!("can't call foreign function: {}", link_name),
87         };
88
89         Ok(true)
90     }
91 }