miri/shims/unix/freebsd/
foreign_items.rs

1use rustc_middle::ty::Ty;
2use rustc_span::Symbol;
3use rustc_target::callconv::{Conv, FnAbi};
4
5use super::sync::EvalContextExt as _;
6use crate::shims::unix::*;
7use crate::*;
8
9pub fn is_dyn_sym(_name: &str) -> bool {
10    false
11}
12
13impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
14pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
15    fn emulate_foreign_item_inner(
16        &mut self,
17        link_name: Symbol,
18        abi: &FnAbi<'tcx, Ty<'tcx>>,
19        args: &[OpTy<'tcx>],
20        dest: &MPlaceTy<'tcx>,
21    ) -> InterpResult<'tcx, EmulateItemResult> {
22        let this = self.eval_context_mut();
23        match link_name.as_str() {
24            // Threading
25            "pthread_setname_np" => {
26                let [thread, name] = this.check_shim(abi, Conv::C, link_name, args)?;
27                let max_len = usize::MAX; // FreeBSD does not seem to have a limit.
28                let res = match this.pthread_setname_np(
29                    this.read_scalar(thread)?,
30                    this.read_scalar(name)?,
31                    max_len,
32                    /* truncate */ false,
33                )? {
34                    ThreadNameResult::Ok => Scalar::from_u32(0),
35                    ThreadNameResult::NameTooLong => unreachable!(),
36                    ThreadNameResult::ThreadNotFound => this.eval_libc("ESRCH"),
37                };
38                this.write_scalar(res, dest)?;
39            }
40            "pthread_getname_np" => {
41                let [thread, name, len] = this.check_shim(abi, Conv::C, link_name, args)?;
42                // FreeBSD's pthread_getname_np uses strlcpy, which truncates the resulting value,
43                // but always adds a null terminator (except for zero-sized buffers).
44                // https://github.com/freebsd/freebsd-src/blob/c2d93a803acef634bd0eede6673aeea59e90c277/lib/libthr/thread/thr_info.c#L119-L144
45                let res = match this.pthread_getname_np(
46                    this.read_scalar(thread)?,
47                    this.read_scalar(name)?,
48                    this.read_scalar(len)?,
49                    /* truncate */ true,
50                )? {
51                    ThreadNameResult::Ok => Scalar::from_u32(0),
52                    // `NameTooLong` is possible when the buffer is zero sized,
53                    ThreadNameResult::NameTooLong => Scalar::from_u32(0),
54                    ThreadNameResult::ThreadNotFound => this.eval_libc("ESRCH"),
55                };
56                this.write_scalar(res, dest)?;
57            }
58
59            // Synchronization primitives
60            "_umtx_op" => {
61                let [obj, op, val, uaddr, uaddr2] =
62                    this.check_shim(abi, Conv::C, link_name, args)?;
63                this._umtx_op(obj, op, val, uaddr, uaddr2, dest)?;
64            }
65
66            // File related shims
67            // For those, we both intercept `func` and `call@FBSD_1.0` symbols cases
68            // since freebsd 12 the former form can be expected.
69            "stat" | "stat@FBSD_1.0" => {
70                let [path, buf] = this.check_shim(abi, Conv::C, link_name, args)?;
71                let result = this.macos_fbsd_solarish_stat(path, buf)?;
72                this.write_scalar(result, dest)?;
73            }
74            "lstat" | "lstat@FBSD_1.0" => {
75                let [path, buf] = this.check_shim(abi, Conv::C, link_name, args)?;
76                let result = this.macos_fbsd_solarish_lstat(path, buf)?;
77                this.write_scalar(result, dest)?;
78            }
79            "fstat" | "fstat@FBSD_1.0" => {
80                let [fd, buf] = this.check_shim(abi, Conv::C, link_name, args)?;
81                let result = this.macos_fbsd_solarish_fstat(fd, buf)?;
82                this.write_scalar(result, dest)?;
83            }
84            "readdir_r" | "readdir_r@FBSD_1.0" => {
85                let [dirp, entry, result] = this.check_shim(abi, Conv::C, link_name, args)?;
86                let result = this.macos_fbsd_readdir_r(dirp, entry, result)?;
87                this.write_scalar(result, dest)?;
88            }
89
90            // Miscellaneous
91            "__error" => {
92                let [] = this.check_shim(abi, Conv::C, link_name, args)?;
93                let errno_place = this.last_error_place()?;
94                this.write_scalar(errno_place.to_ref(this).to_scalar(), dest)?;
95            }
96
97            // Incomplete shims that we "stub out" just to get pre-main initialization code to work.
98            // These shims are enabled only when the caller is in the standard library.
99            "pthread_attr_get_np" if this.frame_in_std() => {
100                let [_thread, _attr] = this.check_shim(abi, Conv::C, link_name, args)?;
101                this.write_null(dest)?;
102            }
103
104            _ => return interp_ok(EmulateItemResult::NotSupported),
105        }
106        interp_ok(EmulateItemResult::NeedsReturn)
107    }
108}