1use rustc_abi::CanonAbi;
2use rustc_middle::ty::Ty;
3use rustc_span::Symbol;
4use rustc_target::callconv::FnAbi;
5
6use self::shims::unix::linux::mem::EvalContextExt as _;
7use self::shims::unix::linux_like::eventfd::EvalContextExt as _;
8use self::shims::unix::linux_like::syscall::syscall;
9use crate::machine::{SIGRTMAX, SIGRTMIN};
10use crate::shims::unix::foreign_items::EvalContextExt as _;
11use crate::shims::unix::linux_like::epoll::EvalContextExt as _;
12use crate::shims::unix::linux_like::thread::prctl;
13use crate::shims::unix::*;
14use crate::*;
15
16const TASK_COMM_LEN: u64 = 16;
20
21pub fn is_dyn_sym(name: &str) -> bool {
22 matches!(name, "gettid" | "statx")
23}
24
25impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
26pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
27 fn emulate_foreign_item_inner(
28 &mut self,
29 link_name: Symbol,
30 abi: &FnAbi<'tcx, Ty<'tcx>>,
31 args: &[OpTy<'tcx>],
32 dest: &MPlaceTy<'tcx>,
33 ) -> InterpResult<'tcx, EmulateItemResult> {
34 let this = self.eval_context_mut();
35
36 match link_name.as_str() {
39 "open64" => {
41 let ([path_raw, flag], varargs) =
44 this.check_shim_sig_variadic_lenient(abi, CanonAbi::C, link_name, args)?;
45 let result = this.open(path_raw, flag, varargs)?;
46 this.write_scalar(result, dest)?;
47 }
48 "pread64" => {
49 let [fd, buf, count, offset] = this.check_shim_sig(
51 shim_sig!(extern "C" fn(i32, *mut _, usize, libc::off64_t) -> isize),
52 link_name,
53 abi,
54 args,
55 )?;
56 let fd = this.read_scalar(fd)?.to_i32()?;
57 let buf = this.read_pointer(buf)?;
58 let count = this.read_target_usize(count)?;
59 let offset = this.read_scalar(offset)?.to_int(offset.layout.size)?;
60 this.read(fd, buf, count, Some(offset), dest)?;
61 }
62 "pwrite64" => {
63 let [fd, buf, n, offset] = this.check_shim_sig(
65 shim_sig!(extern "C" fn(i32, *const _, usize, libc::off64_t) -> isize),
66 link_name,
67 abi,
68 args,
69 )?;
70 let fd = this.read_scalar(fd)?.to_i32()?;
71 let buf = this.read_pointer(buf)?;
72 let count = this.read_target_usize(n)?;
73 let offset = this.read_scalar(offset)?.to_int(offset.layout.size)?;
74 trace!("Called pwrite64({:?}, {:?}, {:?}, {:?})", fd, buf, count, offset);
75 this.write(fd, buf, count, Some(offset), dest)?;
76 }
77 "lseek64" => {
78 let [fd, offset, whence] = this.check_shim_sig(
80 shim_sig!(extern "C" fn(i32, libc::off64_t, i32) -> libc::off64_t),
81 link_name,
82 abi,
83 args,
84 )?;
85 let fd = this.read_scalar(fd)?.to_i32()?;
86 let offset = this.read_scalar(offset)?.to_int(offset.layout.size)?;
87 let whence = this.read_scalar(whence)?.to_i32()?;
88 this.lseek(fd, offset, whence, dest)?;
89 }
90 "ftruncate64" => {
91 let [fd, length] = this.check_shim_sig(
92 shim_sig!(extern "C" fn(i32, libc::off64_t) -> i32),
93 link_name,
94 abi,
95 args,
96 )?;
97 let fd = this.read_scalar(fd)?.to_i32()?;
98 let length = this.read_scalar(length)?.to_int(length.layout.size)?;
99 let result = this.ftruncate64(fd, length)?;
100 this.write_scalar(result, dest)?;
101 }
102 "posix_fallocate64" => {
103 let [fd, offset, len] = this.check_shim_sig(
104 shim_sig!(extern "C" fn(i32, libc::off64_t, libc::off64_t) -> i32),
105 link_name,
106 abi,
107 args,
108 )?;
109
110 let fd = this.read_scalar(fd)?.to_i32()?;
111 let offset = this.read_scalar(offset)?.to_i64()?;
112 let len = this.read_scalar(len)?.to_i64()?;
113
114 let result = this.posix_fallocate(fd, offset, len)?;
115 this.write_scalar(result, dest)?;
116 }
117 "readdir64" => {
118 let [dirp] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
119 this.readdir(dirp, dest)?;
120 }
121 "sync_file_range" => {
122 let [fd, offset, nbytes, flags] =
123 this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
124 let result = this.sync_file_range(fd, offset, nbytes, flags)?;
125 this.write_scalar(result, dest)?;
126 }
127 "statx" => {
128 let [dirfd, pathname, flags, mask, statxbuf] =
129 this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
130 let result = this.linux_statx(dirfd, pathname, flags, mask, statxbuf)?;
131 this.write_scalar(result, dest)?;
132 }
133 "epoll_create1" => {
135 let [flag] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
136 let result = this.epoll_create1(flag)?;
137 this.write_scalar(result, dest)?;
138 }
139 "epoll_ctl" => {
140 let [epfd, op, fd, event] =
141 this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
142 let result = this.epoll_ctl(epfd, op, fd, event)?;
143 this.write_scalar(result, dest)?;
144 }
145 "epoll_wait" => {
146 let [epfd, events, maxevents, timeout] =
147 this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
148 this.epoll_wait(epfd, events, maxevents, timeout, dest)?;
149 }
150 "eventfd" => {
151 let [val, flag] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
152 let result = this.eventfd(val, flag)?;
153 this.write_scalar(result, dest)?;
154 }
155
156 "pthread_setname_np" => {
158 let [thread, name] =
159 this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
160 let res = match this.pthread_setname_np(
161 this.read_scalar(thread)?,
162 this.read_scalar(name)?,
163 TASK_COMM_LEN,
164 false,
165 )? {
166 ThreadNameResult::Ok => Scalar::from_u32(0),
167 ThreadNameResult::NameTooLong => this.eval_libc("ERANGE"),
168 ThreadNameResult::ThreadNotFound => this.eval_libc("ENOENT"),
170 };
171 this.write_scalar(res, dest)?;
172 }
173 "pthread_getname_np" => {
174 let [thread, name, len] =
175 this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
176 let len = this.read_scalar(len)?;
180 let res = if len.to_target_usize(this)? >= TASK_COMM_LEN {
181 match this.pthread_getname_np(
182 this.read_scalar(thread)?,
183 this.read_scalar(name)?,
184 len,
185 false,
186 )? {
187 ThreadNameResult::Ok => Scalar::from_u32(0),
188 ThreadNameResult::NameTooLong => unreachable!(),
189 ThreadNameResult::ThreadNotFound => this.eval_libc("ENOENT"),
191 }
192 } else {
193 this.eval_libc("ERANGE")
194 };
195 this.write_scalar(res, dest)?;
196 }
197 "gettid" => {
198 let [] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
199 let result = this.unix_gettid(link_name.as_str())?;
200 this.write_scalar(result, dest)?;
201 }
202 "prctl" => prctl(this, link_name, abi, args, dest)?,
203
204 "syscall" => {
206 syscall(this, link_name, abi, args, dest)?;
207 }
208
209 "mmap64" => {
211 let [addr, length, prot, flags, fd, offset] =
212 this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
213 let offset = this.read_scalar(offset)?.to_i64()?;
214 let ptr = this.mmap(addr, length, prot, flags, fd, offset.into())?;
215 this.write_scalar(ptr, dest)?;
216 }
217 "mremap" => {
218 let ([old_address, old_size, new_size, flags], _) =
219 this.check_shim_sig_variadic_lenient(abi, CanonAbi::C, link_name, args)?;
220 let ptr = this.mremap(old_address, old_size, new_size, flags)?;
221 this.write_scalar(ptr, dest)?;
222 }
223 "__xpg_strerror_r" => {
224 let [errnum, buf, buflen] =
225 this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
226 let result = this.strerror_r(errnum, buf, buflen)?;
227 this.write_scalar(result, dest)?;
228 }
229 "__errno_location" => {
230 let [] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
231 let errno_place = this.last_error_place()?;
232 this.write_scalar(errno_place.to_ref(this).to_scalar(), dest)?;
233 }
234 "__libc_current_sigrtmin" => {
235 let [] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
236
237 this.write_int(SIGRTMIN, dest)?;
238 }
239 "__libc_current_sigrtmax" => {
240 let [] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
241
242 this.write_int(SIGRTMAX, dest)?;
243 }
244
245 "pthread_getattr_np" if this.frame_in_std() => {
248 let [_thread, _attr] =
249 this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
250 this.write_null(dest)?;
251 }
252 "gnu_get_libc_version"
253 if this.frame_in_std()
254 && this.tcx.sess.target.env == rustc_target::spec::Env::Gnu =>
255 {
256 let [] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
257 let version = "2.26\0";
260 let version = this.allocate_str_dedup(version)?;
261 this.write_pointer(version.ptr(), dest)?;
262 }
263
264 _ => return interp_ok(EmulateItemResult::NotSupported),
265 };
266
267 interp_ok(EmulateItemResult::NeedsReturn)
268 }
269}