2 use rustc_span::Symbol;
3 use rustc_target::spec::abi::Abi;
6 use shims::foreign_items::EmulateByNameResult;
7 use shims::posix::fs::EvalContextExt as _;
8 use shims::posix::linux::sync::futex;
9 use shims::posix::sync::EvalContextExt as _;
10 use shims::posix::thread::EvalContextExt as _;
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(
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();
24 match &*link_name.as_str() {
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)?;
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.
36 let &[ref 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)?;
42 this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
43 let result = this.opendir(name)?;
44 this.write_scalar(result, dest)?;
48 this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
49 let result = this.linux_readdir64(dirp)?;
50 this.write_scalar(result, dest)?;
53 let &[ref fd, ref length] =
54 this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
55 let result = this.ftruncate64(fd, length)?;
56 this.write_scalar(Scalar::from_i32(result), dest)?;
60 let &[ref fd, ref offset, ref len, ref advice] =
61 this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
62 this.read_scalar(fd)?.to_i32()?;
63 this.read_scalar(offset)?.to_machine_isize(this)?;
64 this.read_scalar(len)?.to_machine_isize(this)?;
65 this.read_scalar(advice)?.to_i32()?;
66 // fadvise is only informational, we can ignore it.
67 this.write_null(dest)?;
69 "sync_file_range" => {
70 let &[ref fd, ref offset, ref nbytes, ref flags] =
71 this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
72 let result = this.sync_file_range(fd, offset, nbytes, flags)?;
73 this.write_scalar(Scalar::from_i32(result), dest)?;
78 // This is a POSIX function but it has only been tested on linux.
79 let &[ref clk_id, ref tp] =
80 this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
81 let result = this.clock_gettime(clk_id, tp)?;
82 this.write_scalar(Scalar::from_i32(result), dest)?;
85 // Querying system information
86 "pthread_attr_getstack" => {
87 // We don't support "pthread_attr_setstack", so we just pretend all stacks have the same values here.
88 let &[ref attr_place, ref addr_place, ref size_place] =
89 this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
90 this.deref_operand(attr_place)?;
91 let addr_place = this.deref_operand(addr_place)?;
92 let size_place = this.deref_operand(size_place)?;
95 Scalar::from_uint(STACK_ADDR, this.pointer_size()),
99 Scalar::from_uint(STACK_SIZE, this.pointer_size()),
103 // Return success (`0`).
104 this.write_null(dest)?;
109 // prctl is variadic. (It is not documented like that in the manpage, but defined like that in the libc crate.)
110 this.check_abi_and_shim_symbol_clash(abi, Abi::C { unwind: false }, link_name)?;
111 let result = this.prctl(args)?;
112 this.write_scalar(Scalar::from_i32(result), dest)?;
114 "pthread_condattr_setclock" => {
115 let &[ref attr, ref clock_id] =
116 this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
117 let result = this.pthread_condattr_setclock(attr, clock_id)?;
118 this.write_scalar(Scalar::from_i32(result), dest)?;
120 "pthread_condattr_getclock" => {
121 let &[ref attr, ref clock_id] =
122 this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
123 let result = this.pthread_condattr_getclock(attr, clock_id)?;
124 this.write_scalar(Scalar::from_i32(result), dest)?;
127 // Dynamically invoked syscalls
129 // We do not use `check_shim` here because `syscall` is variadic. The argument
130 // count is checked bellow.
131 this.check_abi_and_shim_symbol_clash(abi, Abi::C { unwind: false }, link_name)?;
132 // The syscall variadic function is legal to call with more arguments than needed,
133 // extra arguments are simply ignored. The important check is that when we use an
134 // argument, we have to also check all arguments *before* it to ensure that they
135 // have the right type.
137 let sys_getrandom = this.eval_libc("SYS_getrandom")?.to_machine_usize(this)?;
139 let sys_statx = this.eval_libc("SYS_statx")?.to_machine_usize(this)?;
141 let sys_futex = this.eval_libc("SYS_futex")?.to_machine_usize(this)?;
145 "incorrect number of arguments for syscall: got 0, expected at least 1"
148 match this.read_scalar(&args[0])?.to_machine_usize(this)? {
149 // `libc::syscall(NR_GETRANDOM, buf.as_mut_ptr(), buf.len(), GRND_NONBLOCK)`
150 // is called if a `HashMap` is created the regular way (e.g. HashMap<K, V>).
151 id if id == sys_getrandom => {
152 // The first argument is the syscall id, so skip over it.
155 "incorrect number of arguments for `getrandom` syscall: got {}, expected at least 4",
159 getrandom(this, &args[1], &args[2], &args[3], dest)?;
161 // `statx` is used by `libstd` to retrieve metadata information on `linux`
162 // instead of using `stat`,`lstat` or `fstat` as on `macos`.
163 id if id == sys_statx => {
164 // The first argument is the syscall id, so skip over it.
167 "incorrect number of arguments for `statx` syscall: got {}, expected at least 6",
172 this.linux_statx(&args[1], &args[2], &args[3], &args[4], &args[5])?;
173 this.write_scalar(Scalar::from_machine_isize(result.into(), this), dest)?;
175 // `futex` is used by some synchonization primitives.
176 id if id == sys_futex => {
177 futex(this, &args[1..], dest)?;
180 this.handle_unsupported(format!("can't execute syscall with ID {}", id))?;
181 return Ok(EmulateByNameResult::AlreadyJumped);
188 let &[ref ptr, ref len, ref flags] =
189 this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
190 getrandom(this, ptr, len, flags, dest)?;
192 "sched_getaffinity" => {
193 let &[ref pid, ref cpusetsize, ref mask] =
194 this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
195 this.read_scalar(pid)?.to_i32()?;
196 this.read_scalar(cpusetsize)?.to_machine_usize(this)?;
197 this.deref_operand(mask)?;
198 // FIXME: we just return an error; `num_cpus` then falls back to `sysconf`.
199 let einval = this.eval_libc("EINVAL")?;
200 this.set_last_error(einval)?;
201 this.write_scalar(Scalar::from_i32(-1), dest)?;
204 // Incomplete shims that we "stub out" just to get pre-main initialization code to work.
205 // These shims are enabled only when the caller is in the standard library.
206 "pthread_getattr_np" if this.frame_in_std() => {
207 let &[ref _thread, ref _attr] =
208 this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
209 this.write_null(dest)?;
212 _ => return Ok(EmulateByNameResult::NotSupported),
215 Ok(EmulateByNameResult::NeedsJumping)
219 // Shims the linux `getrandom` syscall.
221 this: &mut MiriEvalContext<'_, 'tcx>,
222 ptr: &OpTy<'tcx, Tag>,
223 len: &OpTy<'tcx, Tag>,
224 flags: &OpTy<'tcx, Tag>,
225 dest: &PlaceTy<'tcx, Tag>,
226 ) -> InterpResult<'tcx> {
227 let ptr = this.read_pointer(ptr)?;
228 let len = this.read_scalar(len)?.to_machine_usize(this)?;
230 // The only supported flags are GRND_RANDOM and GRND_NONBLOCK,
231 // neither of which have any effect on our current PRNG.
232 // See <https://github.com/rust-lang/rust/pull/79196> for a discussion of argument sizes.
233 let _flags = this.read_scalar(flags)?.to_i32();
235 this.gen_random(ptr, len)?;
236 this.write_scalar(Scalar::from_machine_usize(len, this), dest)?;