1use rustc_abi::CanonAbi;
2use rustc_middle::ty::Ty;
3use rustc_span::Symbol;
4use rustc_target::callconv::FnAbi;
5
6use super::sync::EvalContextExt as _;
7use crate::shims::unix::*;
8use crate::*;
9
10pub fn is_dyn_sym(_name: &str) -> bool {
11 false
12}
13
14impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
15pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
16 fn emulate_foreign_item_inner(
17 &mut self,
18 link_name: Symbol,
19 abi: &FnAbi<'tcx, Ty<'tcx>>,
20 args: &[OpTy<'tcx>],
21 dest: &MPlaceTy<'tcx>,
22 ) -> InterpResult<'tcx, EmulateItemResult> {
23 let this = self.eval_context_mut();
24 match link_name.as_str() {
25 "pthread_setname_np" => {
27 let [thread, name] =
28 this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
29 let max_len = u64::MAX; let res = match this.pthread_setname_np(
31 this.read_scalar(thread)?,
32 this.read_scalar(name)?,
33 max_len,
34 false,
35 )? {
36 ThreadNameResult::Ok => Scalar::from_u32(0),
37 ThreadNameResult::NameTooLong => unreachable!(),
38 ThreadNameResult::ThreadNotFound => this.eval_libc("ESRCH"),
39 };
40 this.write_scalar(res, dest)?;
41 }
42 "pthread_getname_np" => {
43 let [thread, name, len] =
44 this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
45 let res = match this.pthread_getname_np(
49 this.read_scalar(thread)?,
50 this.read_scalar(name)?,
51 this.read_scalar(len)?,
52 true,
53 )? {
54 ThreadNameResult::Ok => Scalar::from_u32(0),
55 ThreadNameResult::NameTooLong => Scalar::from_u32(0),
57 ThreadNameResult::ThreadNotFound => this.eval_libc("ESRCH"),
58 };
59 this.write_scalar(res, dest)?;
60 }
61 "pthread_getthreadid_np" => {
62 let [] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
63 let result = this.unix_gettid(link_name.as_str())?;
64 this.write_scalar(result, dest)?;
65 }
66
67 "cpuset_getaffinity" => {
68 let [level, which, id, set_size, mask] =
70 this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
71
72 let level = this.read_scalar(level)?.to_i32()?;
73 let which = this.read_scalar(which)?.to_i32()?;
74 let id = this.read_scalar(id)?.to_i64()?;
75 let set_size = this.read_target_usize(set_size)?; let mask = this.read_pointer(mask)?;
77
78 let _level_root = this.eval_libc_i32("CPU_LEVEL_ROOT");
79 let _level_cpuset = this.eval_libc_i32("CPU_LEVEL_CPUSET");
80 let level_which = this.eval_libc_i32("CPU_LEVEL_WHICH");
81
82 let _which_tid = this.eval_libc_i32("CPU_WHICH_TID");
83 let which_pid = this.eval_libc_i32("CPU_WHICH_PID");
84 let _which_jail = this.eval_libc_i32("CPU_WHICH_JAIL");
85 let _which_cpuset = this.eval_libc_i32("CPU_WHICH_CPUSET");
86 let _which_irq = this.eval_libc_i32("CPU_WHICH_IRQ");
87
88 let id = match id {
91 -1 => this.active_thread(),
92 _ =>
93 throw_unsup_format!(
94 "`cpuset_getaffinity` is only supported with a pid of -1 (indicating the current thread)"
95 ),
96 };
97
98 if this.ptr_is_null(mask)? {
99 this.set_last_error_and_return(LibcError("EFAULT"), dest)?;
100 }
101 else if level != level_which || which != which_pid {
104 throw_unsup_format!(
105 "`cpuset_getaffinity` is only supported with `level` set to CPU_LEVEL_WHICH and `which` set to CPU_WHICH_PID."
106 );
107 } else if let Some(cpuset) = this.machine.thread_cpu_affinity.get(&id) {
108 if set_size < u64::from(this.machine.num_cpus).div_ceil(8) {
114 this.set_last_error_and_return(LibcError("ERANGE"), dest)?;
115 } else {
116 let cpuset = cpuset.clone();
117 let byte_count =
118 Ord::min(cpuset.as_slice().len(), set_size.try_into().unwrap());
119 this.write_bytes_ptr(
120 mask,
121 cpuset.as_slice()[..byte_count].iter().copied(),
122 )?;
123 this.write_null(dest)?;
124 }
125 } else {
126 unreachable!();
128 }
129 }
130
131 "_umtx_op" => {
133 let [obj, op, val, uaddr, uaddr2] =
134 this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
135 this._umtx_op(obj, op, val, uaddr, uaddr2, dest)?;
136 }
137
138 "stat" | "stat@FBSD_1.0" => {
142 let [path, buf] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
143 let result = this.macos_fbsd_solarish_stat(path, buf)?;
144 this.write_scalar(result, dest)?;
145 }
146 "lstat" | "lstat@FBSD_1.0" => {
147 let [path, buf] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
148 let result = this.macos_fbsd_solarish_lstat(path, buf)?;
149 this.write_scalar(result, dest)?;
150 }
151 "fstat" | "fstat@FBSD_1.0" => {
152 let [fd, buf] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
153 let result = this.macos_fbsd_solarish_fstat(fd, buf)?;
154 this.write_scalar(result, dest)?;
155 }
156 "readdir_r" | "readdir_r@FBSD_1.0" => {
157 let [dirp, entry, result] =
158 this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
159 let result = this.macos_fbsd_readdir_r(dirp, entry, result)?;
160 this.write_scalar(result, dest)?;
161 }
162
163 "__error" => {
165 let [] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
166 let errno_place = this.last_error_place()?;
167 this.write_scalar(errno_place.to_ref(this).to_scalar(), dest)?;
168 }
169
170 "pthread_attr_get_np" if this.frame_in_std() => {
173 let [_thread, _attr] =
174 this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
175 this.write_null(dest)?;
176 }
177
178 _ => return interp_ok(EmulateItemResult::NotSupported),
179 }
180 interp_ok(EmulateItemResult::NeedsReturn)
181 }
182}