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_deprecated(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 };
39 this.write_scalar(res, dest)?;
40 }
41 "pthread_getname_np" => {
42 let [thread, name, len] =
43 this.check_shim_sig_deprecated(abi, CanonAbi::C, link_name, args)?;
44 let res = match this.pthread_getname_np(
48 this.read_scalar(thread)?,
49 this.read_scalar(name)?,
50 this.read_scalar(len)?,
51 true,
52 )? {
53 ThreadNameResult::Ok => Scalar::from_u32(0),
54 ThreadNameResult::NameTooLong => Scalar::from_u32(0),
56 };
57 this.write_scalar(res, dest)?;
58 }
59 "pthread_getthreadid_np" => {
60 let [] = this.check_shim_sig_deprecated(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_sig_deprecated(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 if this.machine.thread_cpu_affinity.is_none() {
77 throw_unsup_format!(
78 "`cpuset_getaffinity` is not supported on #![no_core] programs"
79 )
80 }
81
82 let _level_root = this.eval_libc_i32("CPU_LEVEL_ROOT");
83 let _level_cpuset = this.eval_libc_i32("CPU_LEVEL_CPUSET");
84 let level_which = this.eval_libc_i32("CPU_LEVEL_WHICH");
85
86 let _which_tid = this.eval_libc_i32("CPU_WHICH_TID");
87 let which_pid = this.eval_libc_i32("CPU_WHICH_PID");
88 let _which_jail = this.eval_libc_i32("CPU_WHICH_JAIL");
89 let _which_cpuset = this.eval_libc_i32("CPU_WHICH_CPUSET");
90 let _which_irq = this.eval_libc_i32("CPU_WHICH_IRQ");
91
92 let id = match id {
95 -1 => this.active_thread(),
96 _ =>
97 throw_unsup_format!(
98 "`cpuset_getaffinity` is only supported with a pid of -1 (indicating the current thread)"
99 ),
100 };
101
102 if this.ptr_is_null(mask)? {
103 this.set_errno_and_return_neg1(LibcError("EFAULT"), dest)?;
104 }
105 else if level != level_which || which != which_pid {
108 throw_unsup_format!(
109 "`cpuset_getaffinity` is only supported with `level` set to CPU_LEVEL_WHICH and `which` set to CPU_WHICH_PID."
110 );
111 } else if let Some(cpuset) =
112 this.machine.thread_cpu_affinity.as_ref().unwrap().get(&id)
113 {
114 if set_size < u64::from(this.machine.num_cpus).div_ceil(8) {
120 this.set_errno_and_return_neg1(LibcError("ERANGE"), dest)?;
121 } else {
122 let cpuset = cpuset.clone();
123 let byte_count =
124 Ord::min(cpuset.as_slice().len(), set_size.try_into().unwrap());
125 this.write_bytes_ptr(
126 mask,
127 cpuset.as_slice()[..byte_count].iter().copied(),
128 )?;
129 this.write_null(dest)?;
130 }
131 } else {
132 unreachable!();
134 }
135 }
136
137 "_umtx_op" => {
139 let [obj, op, val, uaddr, uaddr2] =
140 this.check_shim_sig_deprecated(abi, CanonAbi::C, link_name, args)?;
141 this._umtx_op(obj, op, val, uaddr, uaddr2, dest)?;
142 }
143
144 "stat@FBSD_1.0" => {
146 let [path, buf] =
147 this.check_shim_sig_deprecated(abi, CanonAbi::C, link_name, args)?;
148 let result = this.stat(path, buf)?;
149 this.write_scalar(result, dest)?;
150 }
151 "lstat@FBSD_1.0" => {
152 let [path, buf] =
153 this.check_shim_sig_deprecated(abi, CanonAbi::C, link_name, args)?;
154 let result = this.lstat(path, buf)?;
155 this.write_scalar(result, dest)?;
156 }
157 "fstat@FBSD_1.0" => {
158 let [fd, buf] =
159 this.check_shim_sig_deprecated(abi, CanonAbi::C, link_name, args)?;
160 let result = this.fstat(fd, buf)?;
161 this.write_scalar(result, dest)?;
162 }
163 "readdir@FBSD_1.0" => {
164 let [dirp] = this.check_shim_sig_deprecated(abi, CanonAbi::C, link_name, args)?;
165 this.readdir(dirp, dest)?;
166 }
167 "__error" => {
169 let [] = this.check_shim_sig_deprecated(abi, CanonAbi::C, link_name, args)?;
170 let errno_place = this.last_error_place()?;
171 this.write_scalar(errno_place.to_ref(this).to_scalar(), dest)?;
172 }
173 "__xuname" => {
174 let [size, uname] = this.check_shim_sig(
178 shim_sig!(extern "C" fn(i32, *_) -> i32),
179 (link_name, abi, args),
180 )?;
181 let result = this.uname(uname, Some(size))?;
182 this.write_scalar(result, dest)?;
183 }
184
185 "pthread_attr_get_np" if this.frame_in_std() => {
188 let [_thread, _attr] =
189 this.check_shim_sig_deprecated(abi, CanonAbi::C, link_name, args)?;
190 this.write_null(dest)?;
191 }
192
193 _ => return interp_ok(EmulateItemResult::NotSupported),
194 }
195 interp_ok(EmulateItemResult::NeedsReturn)
196 }
197}