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