miri/shims/unix/macos/
foreign_items.rs

1use rustc_abi::CanonAbi;
2use rustc_middle::ty::Ty;
3use rustc_span::Symbol;
4use rustc_target::callconv::FnAbi;
5
6use super::sync::{EvalContextExt as _, MacOsFutexTimeout};
7use crate::shims::unix::*;
8use crate::*;
9
10pub fn is_dyn_sym(name: &str) -> bool {
11    match name {
12        // These only became available with macOS 11.0, so std looks them up dynamically.
13        "os_sync_wait_on_address"
14        | "os_sync_wait_on_address_with_deadline"
15        | "os_sync_wait_on_address_with_timeout"
16        | "os_sync_wake_by_address_any"
17        | "os_sync_wake_by_address_all" => true,
18        _ => false,
19    }
20}
21
22impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
23pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
24    fn emulate_foreign_item_inner(
25        &mut self,
26        link_name: Symbol,
27        abi: &FnAbi<'tcx, Ty<'tcx>>,
28        args: &[OpTy<'tcx>],
29        dest: &MPlaceTy<'tcx>,
30    ) -> InterpResult<'tcx, EmulateItemResult> {
31        let this = self.eval_context_mut();
32
33        // See `fn emulate_foreign_item_inner` in `shims/foreign_items.rs` for the general pattern.
34
35        match link_name.as_str() {
36            // errno
37            "__error" => {
38                let [] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
39                let errno_place = this.last_error_place()?;
40                this.write_scalar(errno_place.to_ref(this).to_scalar(), dest)?;
41            }
42
43            // File related shims
44            "close$NOCANCEL" => {
45                let [result] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
46                let result = this.close(result)?;
47                this.write_scalar(result, dest)?;
48            }
49            "stat" | "stat$INODE64" => {
50                let [path, buf] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
51                let result = this.macos_fbsd_solarish_stat(path, buf)?;
52                this.write_scalar(result, dest)?;
53            }
54            "lstat" | "lstat$INODE64" => {
55                let [path, buf] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
56                let result = this.macos_fbsd_solarish_lstat(path, buf)?;
57                this.write_scalar(result, dest)?;
58            }
59            "fstat$INODE64" => {
60                let [fd, buf] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
61                let result = this.fstat(fd, buf)?;
62                this.write_scalar(result, dest)?;
63            }
64            "opendir$INODE64" => {
65                let [name] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
66                let result = this.opendir(name)?;
67                this.write_scalar(result, dest)?;
68            }
69            "readdir_r" | "readdir_r$INODE64" => {
70                let [dirp, entry, result] =
71                    this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
72                let result = this.macos_readdir_r(dirp, entry, result)?;
73                this.write_scalar(result, dest)?;
74            }
75            "realpath$DARWIN_EXTSN" => {
76                let [path, resolved_path] =
77                    this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
78                let result = this.realpath(path, resolved_path)?;
79                this.write_scalar(result, dest)?;
80            }
81            "ioctl" => {
82                let ([fd_num, cmd], varargs) =
83                    this.check_shim_sig_variadic_lenient(abi, CanonAbi::C, link_name, args)?;
84                let result = this.ioctl(fd_num, cmd, varargs)?;
85                this.write_scalar(result, dest)?;
86            }
87
88            // Environment related shims
89            "_NSGetEnviron" => {
90                let [] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
91                let environ = this.machine.env_vars.unix().environ();
92                this.write_pointer(environ, dest)?;
93            }
94
95            // Random data generation
96            "CCRandomGenerateBytes" => {
97                let [bytes, count] =
98                    this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
99                let bytes = this.read_pointer(bytes)?;
100                let count = this.read_target_usize(count)?;
101                let success = this.eval_libc_i32("kCCSuccess");
102                this.gen_random(bytes, count)?;
103                this.write_int(success, dest)?;
104            }
105
106            // Time related shims
107            "mach_absolute_time" => {
108                let [] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
109                let result = this.mach_absolute_time()?;
110                this.write_scalar(result, dest)?;
111            }
112
113            "mach_timebase_info" => {
114                let [info] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
115                let result = this.mach_timebase_info(info)?;
116                this.write_scalar(result, dest)?;
117            }
118
119            // Access to command-line arguments
120            "_NSGetArgc" => {
121                let [] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
122                this.write_pointer(this.machine.argc.expect("machine must be initialized"), dest)?;
123            }
124            "_NSGetArgv" => {
125                let [] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
126                this.write_pointer(this.machine.argv.expect("machine must be initialized"), dest)?;
127            }
128            "_NSGetExecutablePath" => {
129                let [buf, bufsize] =
130                    this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
131                this.check_no_isolation("`_NSGetExecutablePath`")?;
132
133                let buf_ptr = this.read_pointer(buf)?;
134                let bufsize = this.deref_pointer_as(bufsize, this.machine.layouts.u32)?;
135
136                // Using the host current_exe is a bit off, but consistent with Linux
137                // (where stdlib reads /proc/self/exe).
138                let path = std::env::current_exe().unwrap();
139                let (written, size_needed) = this.write_path_to_c_str(
140                    &path,
141                    buf_ptr,
142                    this.read_scalar(&bufsize)?.to_u32()?.into(),
143                )?;
144
145                if written {
146                    this.write_null(dest)?;
147                } else {
148                    this.write_scalar(Scalar::from_u32(size_needed.try_into().unwrap()), &bufsize)?;
149                    this.write_int(-1, dest)?;
150                }
151            }
152
153            // Thread-local storage
154            "_tlv_atexit" => {
155                let [dtor, data] =
156                    this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
157                let dtor = this.read_pointer(dtor)?;
158                let dtor = this.get_ptr_fn(dtor)?.as_instance()?;
159                let data = this.read_scalar(data)?;
160                let active_thread = this.active_thread();
161                this.machine.tls.add_macos_thread_dtor(
162                    active_thread,
163                    dtor,
164                    data,
165                    this.machine.current_user_relevant_span(),
166                )?;
167            }
168
169            // Querying system information
170            "pthread_get_stackaddr_np" => {
171                let [thread] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
172                this.read_target_usize(thread)?;
173                let stack_addr = Scalar::from_uint(this.machine.stack_addr, this.pointer_size());
174                this.write_scalar(stack_addr, dest)?;
175            }
176            "pthread_get_stacksize_np" => {
177                let [thread] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
178                this.read_target_usize(thread)?;
179                let stack_size = Scalar::from_uint(this.machine.stack_size, this.pointer_size());
180                this.write_scalar(stack_size, dest)?;
181            }
182
183            // Threading
184            "pthread_setname_np" => {
185                let [name] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
186
187                // The real implementation has logic in two places:
188                // * in userland at https://github.com/apple-oss-distributions/libpthread/blob/c032e0b076700a0a47db75528a282b8d3a06531a/src/pthread.c#L1178-L1200,
189                // * in kernel at https://github.com/apple-oss-distributions/xnu/blob/8d741a5de7ff4191bf97d57b9f54c2f6d4a15585/bsd/kern/proc_info.c#L3218-L3227.
190                //
191                // The function in libc calls the kernel to validate
192                // the security policies and the input. If all of the requirements
193                // are met, then the name is set and 0 is returned. Otherwise, if
194                // the specified name is lomnger than MAXTHREADNAMESIZE, then
195                // ENAMETOOLONG is returned.
196                let thread = this.pthread_self()?;
197                let res = match this.pthread_setname_np(
198                    thread,
199                    this.read_scalar(name)?,
200                    this.eval_libc("MAXTHREADNAMESIZE").to_target_usize(this)?,
201                    /* truncate */ false,
202                )? {
203                    ThreadNameResult::Ok => Scalar::from_u32(0),
204                    ThreadNameResult::NameTooLong => this.eval_libc("ENAMETOOLONG"),
205                    ThreadNameResult::ThreadNotFound => unreachable!(),
206                };
207                // Contrary to the manpage, `pthread_setname_np` on macOS still
208                // returns an integer indicating success.
209                this.write_scalar(res, dest)?;
210            }
211            "pthread_getname_np" => {
212                let [thread, name, len] =
213                    this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
214
215                // The function's behavior isn't portable between platforms.
216                // In case of macOS, a truncated name (due to a too small buffer)
217                // does not lead to an error.
218                //
219                // For details, see the implementation at
220                // https://github.com/apple-oss-distributions/libpthread/blob/c032e0b076700a0a47db75528a282b8d3a06531a/src/pthread.c#L1160-L1175.
221                // The key part is the strlcpy, which truncates the resulting value,
222                // but always null terminates (except for zero sized buffers).
223                let res = match this.pthread_getname_np(
224                    this.read_scalar(thread)?,
225                    this.read_scalar(name)?,
226                    this.read_scalar(len)?,
227                    /* truncate */ true,
228                )? {
229                    ThreadNameResult::Ok => Scalar::from_u32(0),
230                    // `NameTooLong` is possible when the buffer is zero sized,
231                    ThreadNameResult::NameTooLong => Scalar::from_u32(0),
232                    ThreadNameResult::ThreadNotFound => this.eval_libc("ESRCH"),
233                };
234                this.write_scalar(res, dest)?;
235            }
236            "pthread_threadid_np" => {
237                let [thread, tid_ptr] =
238                    this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
239                let res = this.apple_pthread_threadip_np(thread, tid_ptr)?;
240                this.write_scalar(res, dest)?;
241            }
242
243            // Synchronization primitives
244            "os_sync_wait_on_address" => {
245                let [addr_op, value_op, size_op, flags_op] =
246                    this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
247                this.os_sync_wait_on_address(
248                    addr_op,
249                    value_op,
250                    size_op,
251                    flags_op,
252                    MacOsFutexTimeout::None,
253                    dest,
254                )?;
255            }
256            "os_sync_wait_on_address_with_deadline" => {
257                let [addr_op, value_op, size_op, flags_op, clock_op, timeout_op] =
258                    this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
259                this.os_sync_wait_on_address(
260                    addr_op,
261                    value_op,
262                    size_op,
263                    flags_op,
264                    MacOsFutexTimeout::Absolute { clock_op, timeout_op },
265                    dest,
266                )?;
267            }
268            "os_sync_wait_on_address_with_timeout" => {
269                let [addr_op, value_op, size_op, flags_op, clock_op, timeout_op] =
270                    this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
271                this.os_sync_wait_on_address(
272                    addr_op,
273                    value_op,
274                    size_op,
275                    flags_op,
276                    MacOsFutexTimeout::Relative { clock_op, timeout_op },
277                    dest,
278                )?;
279            }
280            "os_sync_wake_by_address_any" => {
281                let [addr_op, size_op, flags_op] =
282                    this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
283                this.os_sync_wake_by_address(
284                    addr_op, size_op, flags_op, /* all */ false, dest,
285                )?;
286            }
287            "os_sync_wake_by_address_all" => {
288                let [addr_op, size_op, flags_op] =
289                    this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
290                this.os_sync_wake_by_address(
291                    addr_op, size_op, flags_op, /* all */ true, dest,
292                )?;
293            }
294            "os_unfair_lock_lock" => {
295                let [lock_op] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
296                this.os_unfair_lock_lock(lock_op)?;
297            }
298            "os_unfair_lock_trylock" => {
299                let [lock_op] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
300                this.os_unfair_lock_trylock(lock_op, dest)?;
301            }
302            "os_unfair_lock_unlock" => {
303                let [lock_op] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
304                this.os_unfair_lock_unlock(lock_op)?;
305            }
306            "os_unfair_lock_assert_owner" => {
307                let [lock_op] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
308                this.os_unfair_lock_assert_owner(lock_op)?;
309            }
310            "os_unfair_lock_assert_not_owner" => {
311                let [lock_op] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
312                this.os_unfair_lock_assert_not_owner(lock_op)?;
313            }
314
315            "pthread_cond_timedwait_relative_np" => {
316                let [cond, mutex, reltime] =
317                    this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
318                this.pthread_cond_timedwait(
319                    cond, mutex, reltime, dest, /* macos_relative_np */ true,
320                )?;
321            }
322
323            _ => return interp_ok(EmulateItemResult::NotSupported),
324        };
325
326        interp_ok(EmulateItemResult::NeedsReturn)
327    }
328
329    fn ioctl(
330        &mut self,
331        fd_num: &OpTy<'tcx>,
332        cmd: &OpTy<'tcx>,
333        _varargs: &[OpTy<'tcx>],
334    ) -> InterpResult<'tcx, Scalar> {
335        let this = self.eval_context_mut();
336
337        let fioclex = this.eval_libc_u64("FIOCLEX");
338
339        let fd_num = this.read_scalar(fd_num)?.to_i32()?;
340        let cmd = this.read_scalar(cmd)?.to_u64()?;
341
342        if cmd == fioclex {
343            // Since we don't support `exec`, this is a NOP. However, we want to
344            // return EBADF if the FD is invalid.
345            if this.machine.fds.is_fd_num(fd_num) {
346                interp_ok(Scalar::from_i32(0))
347            } else {
348                this.set_last_error_and_return_i32(LibcError("EBADF"))
349            }
350        } else {
351            throw_unsup_format!("ioctl: unsupported command {cmd:#x}");
352        }
353    }
354}