2 use rustc_span::Symbol;
3 use rustc_target::spec::abi::Abi;
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 _;
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 [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)?;
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)?;
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)?;
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)?;
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)?;
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)?;
76 // This is a POSIX function but it has only been tested on linux.
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)?;
85 // prctl is variadic. (It is not documented like that in the manpage, but defined like that in the libc crate.)
86 this.check_abi_and_shim_symbol_clash(abi, Abi::C { unwind: false }, link_name)?;
87 let result = this.prctl(args)?;
88 this.write_scalar(Scalar::from_i32(result), dest)?;
90 "pthread_condattr_setclock" => {
91 let [attr, clock_id] =
92 this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
93 let result = this.pthread_condattr_setclock(attr, clock_id)?;
94 this.write_scalar(Scalar::from_i32(result), dest)?;
96 "pthread_condattr_getclock" => {
97 let [attr, clock_id] =
98 this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
99 let result = this.pthread_condattr_getclock(attr, clock_id)?;
100 this.write_scalar(Scalar::from_i32(result), dest)?;
103 // Dynamically invoked syscalls
105 // We do not use `check_shim` here because `syscall` is variadic. The argument
106 // count is checked bellow.
107 this.check_abi_and_shim_symbol_clash(abi, Abi::C { unwind: false }, link_name)?;
108 // The syscall variadic function is legal to call with more arguments than needed,
109 // extra arguments are simply ignored. The important check is that when we use an
110 // argument, we have to also check all arguments *before* it to ensure that they
111 // have the right type.
113 let sys_getrandom = this.eval_libc("SYS_getrandom")?.to_machine_usize(this)?;
115 let sys_statx = this.eval_libc("SYS_statx")?.to_machine_usize(this)?;
117 let sys_futex = this.eval_libc("SYS_futex")?.to_machine_usize(this)?;
121 "incorrect number of arguments for syscall: got 0, expected at least 1"
124 match this.read_scalar(&args[0])?.to_machine_usize(this)? {
125 // `libc::syscall(NR_GETRANDOM, buf.as_mut_ptr(), buf.len(), GRND_NONBLOCK)`
126 // is called if a `HashMap` is created the regular way (e.g. HashMap<K, V>).
127 id if id == sys_getrandom => {
128 // The first argument is the syscall id, so skip over it.
131 "incorrect number of arguments for `getrandom` syscall: got {}, expected at least 4",
135 getrandom(this, &args[1], &args[2], &args[3], dest)?;
137 // `statx` is used by `libstd` to retrieve metadata information on `linux`
138 // instead of using `stat`,`lstat` or `fstat` as on `macos`.
139 id if id == sys_statx => {
140 // The first argument is the syscall id, so skip over it.
143 "incorrect number of arguments for `statx` syscall: got {}, expected at least 6",
148 this.linux_statx(&args[1], &args[2], &args[3], &args[4], &args[5])?;
149 this.write_scalar(Scalar::from_machine_isize(result.into(), this), dest)?;
151 // `futex` is used by some synchonization primitives.
152 id if id == sys_futex => {
153 futex(this, &args[1..], dest)?;
156 this.handle_unsupported(format!("can't execute syscall with ID {}", id))?;
157 return Ok(EmulateByNameResult::AlreadyJumped);
164 let [ptr, len, flags] =
165 this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
166 getrandom(this, ptr, len, flags, dest)?;
168 "sched_getaffinity" => {
169 let [pid, cpusetsize, mask] =
170 this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
171 this.read_scalar(pid)?.to_i32()?;
172 this.read_scalar(cpusetsize)?.to_machine_usize(this)?;
173 this.deref_operand(mask)?;
174 // FIXME: we just return an error; `num_cpus` then falls back to `sysconf`.
175 let einval = this.eval_libc("EINVAL")?;
176 this.set_last_error(einval)?;
177 this.write_scalar(Scalar::from_i32(-1), dest)?;
180 // Incomplete shims that we "stub out" just to get pre-main initialization code to work.
181 // These shims are enabled only when the caller is in the standard library.
182 "pthread_getattr_np" if this.frame_in_std() => {
183 let [_thread, _attr] =
184 this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
185 this.write_null(dest)?;
188 _ => return Ok(EmulateByNameResult::NotSupported),
191 Ok(EmulateByNameResult::NeedsJumping)
195 // Shims the linux `getrandom` syscall.
197 this: &mut MiriEvalContext<'_, 'tcx>,
198 ptr: &OpTy<'tcx, Tag>,
199 len: &OpTy<'tcx, Tag>,
200 flags: &OpTy<'tcx, Tag>,
201 dest: &PlaceTy<'tcx, Tag>,
202 ) -> InterpResult<'tcx> {
203 let ptr = this.read_pointer(ptr)?;
204 let len = this.read_scalar(len)?.to_machine_usize(this)?;
206 // The only supported flags are GRND_RANDOM and GRND_NONBLOCK,
207 // neither of which have any effect on our current PRNG.
208 // See <https://github.com/rust-lang/rust/pull/79196> for a discussion of argument sizes.
209 let _flags = this.read_scalar(flags)?.to_i32();
211 this.gen_random(ptr, len)?;
212 this.write_scalar(Scalar::from_machine_usize(len, this), dest)?;