]> git.lizzy.rs Git - rust.git/blob - src/shims/unix/linux/foreign_items.rs
some clippy-induced cleanup
[rust.git] / src / shims / unix / linux / foreign_items.rs
1 use rustc_middle::mir;
2 use rustc_span::Symbol;
3 use rustc_target::spec::abi::Abi;
4
5 use crate::*;
6 use shims::foreign_items::EmulateByNameResult;
7 use shims::unix::fs::EvalContextExt as _;
8 use shims::unix::linux::sync::futex;
9 use shims::unix::sync::EvalContextExt as _;
10 use shims::unix::thread::EvalContextExt as _;
11
12 impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
13 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
14     fn emulate_foreign_item_by_name(
15         &mut self,
16         link_name: Symbol,
17         abi: Abi,
18         args: &[OpTy<'tcx, Tag>],
19         dest: &PlaceTy<'tcx, Tag>,
20         _ret: mir::BasicBlock,
21     ) -> InterpResult<'tcx, EmulateByNameResult<'mir, 'tcx>> {
22         let this = self.eval_context_mut();
23
24         match link_name.as_str() {
25             // errno
26             "__errno_location" => {
27                 let [] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
28                 let errno_place = this.last_error_place()?;
29                 this.write_scalar(errno_place.to_ref(this).to_scalar()?, dest)?;
30             }
31
32             // File related shims (but also see "syscall" below for statx)
33             // These symbols have different names on Linux and macOS, which is the only reason they are not
34             // in the `posix` module.
35             "close" => {
36                 let [fd] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
37                 let result = this.close(fd)?;
38                 this.write_scalar(Scalar::from_i32(result), dest)?;
39             }
40             "opendir" => {
41                 let [name] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
42                 let result = this.opendir(name)?;
43                 this.write_scalar(result, dest)?;
44             }
45             "readdir64" => {
46                 let [dirp] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
47                 let result = this.linux_readdir64(dirp)?;
48                 this.write_scalar(result, dest)?;
49             }
50             "ftruncate64" => {
51                 let [fd, length] =
52                     this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
53                 let result = this.ftruncate64(fd, length)?;
54                 this.write_scalar(Scalar::from_i32(result), dest)?;
55             }
56             // Linux-only
57             "posix_fadvise" => {
58                 let [fd, offset, len, advice] =
59                     this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
60                 this.read_scalar(fd)?.to_i32()?;
61                 this.read_scalar(offset)?.to_machine_isize(this)?;
62                 this.read_scalar(len)?.to_machine_isize(this)?;
63                 this.read_scalar(advice)?.to_i32()?;
64                 // fadvise is only informational, we can ignore it.
65                 this.write_null(dest)?;
66             }
67             "sync_file_range" => {
68                 let [fd, offset, nbytes, flags] =
69                     this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
70                 let result = this.sync_file_range(fd, offset, nbytes, flags)?;
71                 this.write_scalar(Scalar::from_i32(result), dest)?;
72             }
73
74             // Time related shims
75             "clock_gettime" => {
76                 // This is a POSIX function but it has only been tested on linux.
77                 let [clk_id, tp] =
78                     this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
79                 let result = this.clock_gettime(clk_id, tp)?;
80                 this.write_scalar(Scalar::from_i32(result), dest)?;
81             }
82
83             // Querying system information
84             "pthread_attr_getstack" => {
85                 // We don't support "pthread_attr_setstack", so we just pretend all stacks have the same values here.
86                 let [attr_place, addr_place, size_place] =
87                     this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
88                 this.deref_operand(attr_place)?;
89                 let addr_place = this.deref_operand(addr_place)?;
90                 let size_place = this.deref_operand(size_place)?;
91
92                 this.write_scalar(
93                     Scalar::from_uint(STACK_ADDR, this.pointer_size()),
94                     &addr_place.into(),
95                 )?;
96                 this.write_scalar(
97                     Scalar::from_uint(STACK_SIZE, this.pointer_size()),
98                     &size_place.into(),
99                 )?;
100
101                 // Return success (`0`).
102                 this.write_null(dest)?;
103             }
104
105             // Threading
106             "prctl" => {
107                 // prctl is variadic. (It is not documented like that in the manpage, but defined like that in the libc crate.)
108                 this.check_abi_and_shim_symbol_clash(abi, Abi::C { unwind: false }, link_name)?;
109                 let result = this.prctl(args)?;
110                 this.write_scalar(Scalar::from_i32(result), dest)?;
111             }
112             "pthread_condattr_setclock" => {
113                 let [attr, clock_id] =
114                     this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
115                 let result = this.pthread_condattr_setclock(attr, clock_id)?;
116                 this.write_scalar(Scalar::from_i32(result), dest)?;
117             }
118             "pthread_condattr_getclock" => {
119                 let [attr, clock_id] =
120                     this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
121                 let result = this.pthread_condattr_getclock(attr, clock_id)?;
122                 this.write_scalar(Scalar::from_i32(result), dest)?;
123             }
124
125             // Dynamically invoked syscalls
126             "syscall" => {
127                 // We do not use `check_shim` here because `syscall` is variadic. The argument
128                 // count is checked bellow.
129                 this.check_abi_and_shim_symbol_clash(abi, Abi::C { unwind: false }, link_name)?;
130                 // The syscall variadic function is legal to call with more arguments than needed,
131                 // extra arguments are simply ignored. The important check is that when we use an
132                 // argument, we have to also check all arguments *before* it to ensure that they
133                 // have the right type.
134
135                 let sys_getrandom = this.eval_libc("SYS_getrandom")?.to_machine_usize(this)?;
136
137                 let sys_statx = this.eval_libc("SYS_statx")?.to_machine_usize(this)?;
138
139                 let sys_futex = this.eval_libc("SYS_futex")?.to_machine_usize(this)?;
140
141                 if args.is_empty() {
142                     throw_ub_format!(
143                         "incorrect number of arguments for syscall: got 0, expected at least 1"
144                     );
145                 }
146                 match this.read_scalar(&args[0])?.to_machine_usize(this)? {
147                     // `libc::syscall(NR_GETRANDOM, buf.as_mut_ptr(), buf.len(), GRND_NONBLOCK)`
148                     // is called if a `HashMap` is created the regular way (e.g. HashMap<K, V>).
149                     id if id == sys_getrandom => {
150                         // The first argument is the syscall id, so skip over it.
151                         if args.len() < 4 {
152                             throw_ub_format!(
153                                 "incorrect number of arguments for `getrandom` syscall: got {}, expected at least 4",
154                                 args.len()
155                             );
156                         }
157                         getrandom(this, &args[1], &args[2], &args[3], dest)?;
158                     }
159                     // `statx` is used by `libstd` to retrieve metadata information on `linux`
160                     // instead of using `stat`,`lstat` or `fstat` as on `macos`.
161                     id if id == sys_statx => {
162                         // The first argument is the syscall id, so skip over it.
163                         if args.len() < 6 {
164                             throw_ub_format!(
165                                 "incorrect number of arguments for `statx` syscall: got {}, expected at least 6",
166                                 args.len()
167                             );
168                         }
169                         let result =
170                             this.linux_statx(&args[1], &args[2], &args[3], &args[4], &args[5])?;
171                         this.write_scalar(Scalar::from_machine_isize(result.into(), this), dest)?;
172                     }
173                     // `futex` is used by some synchonization primitives.
174                     id if id == sys_futex => {
175                         futex(this, &args[1..], dest)?;
176                     }
177                     id => {
178                         this.handle_unsupported(format!("can't execute syscall with ID {}", id))?;
179                         return Ok(EmulateByNameResult::AlreadyJumped);
180                     }
181                 }
182             }
183
184             // Miscelanneous
185             "getrandom" => {
186                 let [ptr, len, flags] =
187                     this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
188                 getrandom(this, ptr, len, flags, dest)?;
189             }
190             "sched_getaffinity" => {
191                 let [pid, cpusetsize, mask] =
192                     this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
193                 this.read_scalar(pid)?.to_i32()?;
194                 this.read_scalar(cpusetsize)?.to_machine_usize(this)?;
195                 this.deref_operand(mask)?;
196                 // FIXME: we just return an error; `num_cpus` then falls back to `sysconf`.
197                 let einval = this.eval_libc("EINVAL")?;
198                 this.set_last_error(einval)?;
199                 this.write_scalar(Scalar::from_i32(-1), dest)?;
200             }
201
202             // Incomplete shims that we "stub out" just to get pre-main initialization code to work.
203             // These shims are enabled only when the caller is in the standard library.
204             "pthread_getattr_np" if this.frame_in_std() => {
205                 let [_thread, _attr] =
206                     this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
207                 this.write_null(dest)?;
208             }
209
210             _ => return Ok(EmulateByNameResult::NotSupported),
211         };
212
213         Ok(EmulateByNameResult::NeedsJumping)
214     }
215 }
216
217 // Shims the linux `getrandom` syscall.
218 fn getrandom<'tcx>(
219     this: &mut MiriEvalContext<'_, 'tcx>,
220     ptr: &OpTy<'tcx, Tag>,
221     len: &OpTy<'tcx, Tag>,
222     flags: &OpTy<'tcx, Tag>,
223     dest: &PlaceTy<'tcx, Tag>,
224 ) -> InterpResult<'tcx> {
225     let ptr = this.read_pointer(ptr)?;
226     let len = this.read_scalar(len)?.to_machine_usize(this)?;
227
228     // The only supported flags are GRND_RANDOM and GRND_NONBLOCK,
229     // neither of which have any effect on our current PRNG.
230     // See <https://github.com/rust-lang/rust/pull/79196> for a discussion of argument sizes.
231     let _flags = this.read_scalar(flags)?.to_i32();
232
233     this.gen_random(ptr, len)?;
234     this.write_scalar(Scalar::from_machine_usize(len, this), dest)?;
235     Ok(())
236 }