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(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(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(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(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] = this.check_shim(abi, CanonAbi::C, link_name, args)?;
65 let result = this.epoll_ctl(epfd, op, fd, event)?;
66 this.write_scalar(result, dest)?;
67 }
68 "epoll_wait" => {
69 let [epfd, events, maxevents, timeout] =
70 this.check_shim(abi, CanonAbi::C, link_name, args)?;
71 this.epoll_wait(epfd, events, maxevents, timeout, dest)?;
72 }
73 "eventfd" => {
74 let [val, flag] = this.check_shim(abi, CanonAbi::C, link_name, args)?;
75 let result = this.eventfd(val, flag)?;
76 this.write_scalar(result, dest)?;
77 }
78
79 "pthread_setname_np" => {
81 let [thread, name] = this.check_shim(abi, CanonAbi::C, link_name, args)?;
82 let res = match this.pthread_setname_np(
83 this.read_scalar(thread)?,
84 this.read_scalar(name)?,
85 TASK_COMM_LEN,
86 false,
87 )? {
88 ThreadNameResult::Ok => Scalar::from_u32(0),
89 ThreadNameResult::NameTooLong => this.eval_libc("ERANGE"),
90 ThreadNameResult::ThreadNotFound => this.eval_libc("ENOENT"),
92 };
93 this.write_scalar(res, dest)?;
94 }
95 "pthread_getname_np" => {
96 let [thread, name, len] = this.check_shim(abi, CanonAbi::C, link_name, args)?;
97 let len = this.read_scalar(len)?;
101 let res = if len.to_target_usize(this)? >= TASK_COMM_LEN {
102 match this.pthread_getname_np(
103 this.read_scalar(thread)?,
104 this.read_scalar(name)?,
105 len,
106 false,
107 )? {
108 ThreadNameResult::Ok => Scalar::from_u32(0),
109 ThreadNameResult::NameTooLong => unreachable!(),
110 ThreadNameResult::ThreadNotFound => this.eval_libc("ENOENT"),
112 }
113 } else {
114 this.eval_libc("ERANGE")
115 };
116 this.write_scalar(res, dest)?;
117 }
118 "gettid" => {
119 let [] = this.check_shim(abi, CanonAbi::C, link_name, args)?;
120 let result = this.unix_gettid(link_name.as_str())?;
121 this.write_scalar(result, dest)?;
122 }
123
124 "syscall" => {
126 syscall(this, link_name, abi, args, dest)?;
127 }
128
129 "mmap64" => {
131 let [addr, length, prot, flags, fd, offset] =
132 this.check_shim(abi, CanonAbi::C, link_name, args)?;
133 let offset = this.read_scalar(offset)?.to_i64()?;
134 let ptr = this.mmap(addr, length, prot, flags, fd, offset.into())?;
135 this.write_scalar(ptr, dest)?;
136 }
137 "mremap" => {
138 let ([old_address, old_size, new_size, flags], _) =
139 this.check_shim_variadic(abi, CanonAbi::C, link_name, args)?;
140 let ptr = this.mremap(old_address, old_size, new_size, flags)?;
141 this.write_scalar(ptr, dest)?;
142 }
143 "__xpg_strerror_r" => {
144 let [errnum, buf, buflen] = this.check_shim(abi, CanonAbi::C, link_name, args)?;
145 let result = this.strerror_r(errnum, buf, buflen)?;
146 this.write_scalar(result, dest)?;
147 }
148 "__errno_location" => {
149 let [] = this.check_shim(abi, CanonAbi::C, link_name, args)?;
150 let errno_place = this.last_error_place()?;
151 this.write_scalar(errno_place.to_ref(this).to_scalar(), dest)?;
152 }
153 "__libc_current_sigrtmin" => {
154 let [] = this.check_shim(abi, CanonAbi::C, link_name, args)?;
155
156 this.write_int(SIGRTMIN, dest)?;
157 }
158 "__libc_current_sigrtmax" => {
159 let [] = this.check_shim(abi, CanonAbi::C, link_name, args)?;
160
161 this.write_int(SIGRTMAX, dest)?;
162 }
163
164 "pthread_getattr_np" if this.frame_in_std() => {
167 let [_thread, _attr] = this.check_shim(abi, CanonAbi::C, link_name, args)?;
168 this.write_null(dest)?;
169 }
170
171 _ => return interp_ok(EmulateItemResult::NotSupported),
172 };
173
174 interp_ok(EmulateItemResult::NeedsReturn)
175 }
176}