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