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] = this.check_shim(abi, CanonAbi::C, link_name, args)?;
28 let max_len = u64::MAX; let res = match this.pthread_setname_np(
30 this.read_scalar(thread)?,
31 this.read_scalar(name)?,
32 max_len,
33 false,
34 )? {
35 ThreadNameResult::Ok => Scalar::from_u32(0),
36 ThreadNameResult::NameTooLong => unreachable!(),
37 ThreadNameResult::ThreadNotFound => this.eval_libc("ESRCH"),
38 };
39 this.write_scalar(res, dest)?;
40 }
41 "pthread_getname_np" => {
42 let [thread, name, len] = this.check_shim(abi, CanonAbi::C, link_name, args)?;
43 let res = match this.pthread_getname_np(
47 this.read_scalar(thread)?,
48 this.read_scalar(name)?,
49 this.read_scalar(len)?,
50 true,
51 )? {
52 ThreadNameResult::Ok => Scalar::from_u32(0),
53 ThreadNameResult::NameTooLong => Scalar::from_u32(0),
55 ThreadNameResult::ThreadNotFound => this.eval_libc("ESRCH"),
56 };
57 this.write_scalar(res, dest)?;
58 }
59 "pthread_getthreadid_np" => {
60 let [] = this.check_shim(abi, CanonAbi::C, link_name, args)?;
61 let result = this.unix_gettid(link_name.as_str())?;
62 this.write_scalar(result, dest)?;
63 }
64
65 "cpuset_getaffinity" => {
66 let [level, which, id, set_size, mask] =
68 this.check_shim(abi, CanonAbi::C, link_name, args)?;
69
70 let level = this.read_scalar(level)?.to_i32()?;
71 let which = this.read_scalar(which)?.to_i32()?;
72 let id = this.read_scalar(id)?.to_i64()?;
73 let set_size = this.read_target_usize(set_size)?; let mask = this.read_pointer(mask)?;
75
76 let _level_root = this.eval_libc_i32("CPU_LEVEL_ROOT");
77 let _level_cpuset = this.eval_libc_i32("CPU_LEVEL_CPUSET");
78 let level_which = this.eval_libc_i32("CPU_LEVEL_WHICH");
79
80 let _which_tid = this.eval_libc_i32("CPU_WHICH_TID");
81 let which_pid = this.eval_libc_i32("CPU_WHICH_PID");
82 let _which_jail = this.eval_libc_i32("CPU_WHICH_JAIL");
83 let _which_cpuset = this.eval_libc_i32("CPU_WHICH_CPUSET");
84 let _which_irq = this.eval_libc_i32("CPU_WHICH_IRQ");
85
86 let id = match id {
89 -1 => this.active_thread(),
90 _ =>
91 throw_unsup_format!(
92 "`cpuset_getaffinity` is only supported with a pid of -1 (indicating the current thread)"
93 ),
94 };
95
96 if this.ptr_is_null(mask)? {
97 this.set_last_error_and_return(LibcError("EFAULT"), dest)?;
98 }
99 else if level != level_which || which != which_pid {
102 throw_unsup_format!(
103 "`cpuset_getaffinity` is only supported with `level` set to CPU_LEVEL_WHICH and `which` set to CPU_WHICH_PID."
104 );
105 } else if let Some(cpuset) = this.machine.thread_cpu_affinity.get(&id) {
106 if set_size < u64::from(this.machine.num_cpus).div_ceil(8) {
112 this.set_last_error_and_return(LibcError("ERANGE"), dest)?;
113 } else {
114 let cpuset = cpuset.clone();
115 let byte_count =
116 Ord::min(cpuset.as_slice().len(), set_size.try_into().unwrap());
117 this.write_bytes_ptr(
118 mask,
119 cpuset.as_slice()[..byte_count].iter().copied(),
120 )?;
121 this.write_null(dest)?;
122 }
123 } else {
124 unreachable!();
126 }
127 }
128
129 "_umtx_op" => {
131 let [obj, op, val, uaddr, uaddr2] =
132 this.check_shim(abi, CanonAbi::C, link_name, args)?;
133 this._umtx_op(obj, op, val, uaddr, uaddr2, dest)?;
134 }
135
136 "stat" | "stat@FBSD_1.0" => {
140 let [path, buf] = this.check_shim(abi, CanonAbi::C, link_name, args)?;
141 let result = this.macos_fbsd_solarish_stat(path, buf)?;
142 this.write_scalar(result, dest)?;
143 }
144 "lstat" | "lstat@FBSD_1.0" => {
145 let [path, buf] = this.check_shim(abi, CanonAbi::C, link_name, args)?;
146 let result = this.macos_fbsd_solarish_lstat(path, buf)?;
147 this.write_scalar(result, dest)?;
148 }
149 "fstat" | "fstat@FBSD_1.0" => {
150 let [fd, buf] = this.check_shim(abi, CanonAbi::C, link_name, args)?;
151 let result = this.macos_fbsd_solarish_fstat(fd, buf)?;
152 this.write_scalar(result, dest)?;
153 }
154 "readdir_r" | "readdir_r@FBSD_1.0" => {
155 let [dirp, entry, result] = this.check_shim(abi, CanonAbi::C, link_name, args)?;
156 let result = this.macos_fbsd_readdir_r(dirp, entry, result)?;
157 this.write_scalar(result, dest)?;
158 }
159
160 "__error" => {
162 let [] = this.check_shim(abi, CanonAbi::C, link_name, args)?;
163 let errno_place = this.last_error_place()?;
164 this.write_scalar(errno_place.to_ref(this).to_scalar(), dest)?;
165 }
166
167 "pthread_attr_get_np" if this.frame_in_std() => {
170 let [_thread, _attr] = this.check_shim(abi, CanonAbi::C, link_name, args)?;
171 this.write_null(dest)?;
172 }
173
174 _ => return interp_ok(EmulateItemResult::NotSupported),
175 }
176 interp_ok(EmulateItemResult::NeedsReturn)
177 }
178}