1 use rustc_span::Symbol;
2 use rustc_target::spec::abi::Abi;
4 use crate::machine::SIGRTMAX;
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 _;
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(
19 args: &[OpTy<'tcx, Provenance>],
20 dest: &PlaceTy<'tcx, Provenance>,
21 ) -> InterpResult<'tcx, EmulateByNameResult<'mir, 'tcx>> {
22 let this = self.eval_context_mut();
24 // See `fn emulate_foreign_item_by_name` in `shims/foreign_items.rs` for the general pattern.
26 match link_name.as_str() {
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)?;
34 // File related shims (but also see "syscall" below for statx)
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)?;
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)?;
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)?;
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)?;
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)?;
65 let [domain, type_, protocol, sv] =
66 this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
68 let result = this.socketpair(domain, type_, protocol, sv)?;
69 this.write_scalar(result, dest)?;
71 "__libc_current_sigrtmax" => {
72 let [] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
74 this.write_scalar(Scalar::from_i32(SIGRTMAX), dest)?;
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)?;
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)?;
90 "pthread_setname_np" => {
92 this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
94 let res = this.pthread_setname_np(
95 this.read_scalar(thread)?,
96 this.read_scalar(name)?,
99 this.write_scalar(res, dest)?;
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)?,
109 this.write_scalar(res, dest)?;
112 // Dynamically invoked syscalls
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.
122 let sys_getrandom = this.eval_libc("SYS_getrandom").to_machine_usize(this)?;
124 let sys_statx = this.eval_libc("SYS_statx").to_machine_usize(this)?;
126 let sys_futex = this.eval_libc("SYS_futex").to_machine_usize(this)?;
130 "incorrect number of arguments for syscall: got 0, expected at least 1"
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.
140 "incorrect number of arguments for `getrandom` syscall: got {}, expected at least 4",
144 getrandom(this, &args[1], &args[2], &args[3], dest)?;
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.
152 "incorrect number of arguments for `statx` syscall: got {}, expected at least 6",
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)?;
160 // `futex` is used by some synchonization primitives.
161 id if id == sys_futex => {
162 futex(this, &args[1..], dest)?;
165 this.handle_unsupported(format!("can't execute syscall with ID {id}"))?;
166 return Ok(EmulateByNameResult::AlreadyJumped);
173 let [ptr, len, flags] =
174 this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
175 getrandom(this, ptr, len, flags, dest)?;
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)?;
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)?;
197 _ => return Ok(EmulateByNameResult::NotSupported),
200 Ok(EmulateByNameResult::NeedsJumping)
204 // Shims the linux `getrandom` syscall.
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)?;
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();
220 this.gen_random(ptr, len)?;
221 this.write_scalar(Scalar::from_machine_usize(len, this), dest)?;