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::epoll::EvalContextExt as _;
8use self::shims::unix::linux_like::eventfd::EvalContextExt as _;
9use self::shims::unix::linux_like::syscall::syscall;
10use crate::machine::{SIGRTMAX, SIGRTMIN};
11use crate::shims::unix::foreign_items::EvalContextExt as _;
12use crate::shims::unix::*;
13use crate::*;
14
15const TASK_COMM_LEN: u64 = 16;
19
20pub fn is_dyn_sym(name: &str) -> bool {
21 matches!(name, "gettid" | "statx")
22}
23
24impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
25pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
26 fn emulate_foreign_item_inner(
27 &mut self,
28 link_name: Symbol,
29 abi: &FnAbi<'tcx, Ty<'tcx>>,
30 args: &[OpTy<'tcx>],
31 dest: &MPlaceTy<'tcx>,
32 ) -> InterpResult<'tcx, EmulateItemResult> {
33 let this = self.eval_context_mut();
34
35 match link_name.as_str() {
38 "readdir64" => {
40 let [dirp] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
41 let result = this.linux_solarish_readdir64("dirent64", dirp)?;
42 this.write_scalar(result, dest)?;
43 }
44 "sync_file_range" => {
45 let [fd, offset, nbytes, flags] =
46 this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
47 let result = this.sync_file_range(fd, offset, nbytes, flags)?;
48 this.write_scalar(result, dest)?;
49 }
50 "statx" => {
51 let [dirfd, pathname, flags, mask, statxbuf] =
52 this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
53 let result = this.linux_statx(dirfd, pathname, flags, mask, statxbuf)?;
54 this.write_scalar(result, dest)?;
55 }
56
57 "epoll_create1" => {
59 let [flag] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
60 let result = this.epoll_create1(flag)?;
61 this.write_scalar(result, dest)?;
62 }
63 "epoll_ctl" => {
64 let [epfd, op, fd, event] =
65 this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
66 let result = this.epoll_ctl(epfd, op, fd, event)?;
67 this.write_scalar(result, dest)?;
68 }
69 "epoll_wait" => {
70 let [epfd, events, maxevents, timeout] =
71 this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
72 this.epoll_wait(epfd, events, maxevents, timeout, dest)?;
73 }
74 "eventfd" => {
75 let [val, flag] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
76 let result = this.eventfd(val, flag)?;
77 this.write_scalar(result, dest)?;
78 }
79
80 "pthread_setname_np" => {
82 let [thread, name] =
83 this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
84 let res = match this.pthread_setname_np(
85 this.read_scalar(thread)?,
86 this.read_scalar(name)?,
87 TASK_COMM_LEN,
88 false,
89 )? {
90 ThreadNameResult::Ok => Scalar::from_u32(0),
91 ThreadNameResult::NameTooLong => this.eval_libc("ERANGE"),
92 ThreadNameResult::ThreadNotFound => this.eval_libc("ENOENT"),
94 };
95 this.write_scalar(res, dest)?;
96 }
97 "pthread_getname_np" => {
98 let [thread, name, len] =
99 this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
100 let len = this.read_scalar(len)?;
104 let res = if len.to_target_usize(this)? >= TASK_COMM_LEN {
105 match this.pthread_getname_np(
106 this.read_scalar(thread)?,
107 this.read_scalar(name)?,
108 len,
109 false,
110 )? {
111 ThreadNameResult::Ok => Scalar::from_u32(0),
112 ThreadNameResult::NameTooLong => unreachable!(),
113 ThreadNameResult::ThreadNotFound => this.eval_libc("ENOENT"),
115 }
116 } else {
117 this.eval_libc("ERANGE")
118 };
119 this.write_scalar(res, dest)?;
120 }
121 "gettid" => {
122 let [] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
123 let result = this.unix_gettid(link_name.as_str())?;
124 this.write_scalar(result, dest)?;
125 }
126
127 "syscall" => {
129 syscall(this, link_name, abi, args, dest)?;
130 }
131
132 "mmap64" => {
134 let [addr, length, prot, flags, fd, offset] =
135 this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
136 let offset = this.read_scalar(offset)?.to_i64()?;
137 let ptr = this.mmap(addr, length, prot, flags, fd, offset.into())?;
138 this.write_scalar(ptr, dest)?;
139 }
140 "mremap" => {
141 let ([old_address, old_size, new_size, flags], _) =
142 this.check_shim_sig_variadic_lenient(abi, CanonAbi::C, link_name, args)?;
143 let ptr = this.mremap(old_address, old_size, new_size, flags)?;
144 this.write_scalar(ptr, dest)?;
145 }
146 "__xpg_strerror_r" => {
147 let [errnum, buf, buflen] =
148 this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
149 let result = this.strerror_r(errnum, buf, buflen)?;
150 this.write_scalar(result, dest)?;
151 }
152 "__errno_location" => {
153 let [] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
154 let errno_place = this.last_error_place()?;
155 this.write_scalar(errno_place.to_ref(this).to_scalar(), dest)?;
156 }
157 "__libc_current_sigrtmin" => {
158 let [] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
159
160 this.write_int(SIGRTMIN, dest)?;
161 }
162 "__libc_current_sigrtmax" => {
163 let [] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
164
165 this.write_int(SIGRTMAX, dest)?;
166 }
167
168 "pthread_getattr_np" if this.frame_in_std() => {
171 let [_thread, _attr] =
172 this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
173 this.write_null(dest)?;
174 }
175
176 _ => return interp_ok(EmulateItemResult::NotSupported),
177 };
178
179 interp_ok(EmulateItemResult::NeedsReturn)
180 }
181}