Skip to main content

miri/shims/unix/linux/
foreign_items.rs

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
16// The documentation of glibc complains that the kernel never exposes
17// TASK_COMM_LEN through the headers, so it's assumed to always be 16 bytes
18// long including a null terminator.
19const 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        // See `fn emulate_foreign_item_inner` in `shims/foreign_items.rs` for the general pattern.
37
38        match link_name.as_str() {
39            // File related shims
40            "open64" => {
41                // `open64` is variadic, the third argument is only present when the second argument
42                // has O_CREAT (or on linux O_TMPFILE, but miri doesn't support that) set
43                let ([path_raw, flag], varargs) = this.check_shim_sig_variadic(
44                    shim_sig!(extern "C" fn(*_, i32, ...) -> i32),
45                    (link_name, abi, args),
46                )?;
47                let result = this.open(path_raw, flag, varargs)?;
48                this.write_scalar(result, dest)?;
49            }
50            "pread64" => {
51                // FIXME: This does not have a direct test (#3179).
52                let [fd, buf, count, offset] = this.check_shim_sig(
53                    shim_sig!(extern "C" fn(i32, *_, usize, libc::off64_t) -> isize),
54                    (link_name, abi, 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                // FIXME: This does not have a direct test (#3179).
64                let [fd, buf, n, offset] = this.check_shim_sig(
65                    shim_sig!(extern "C" fn(i32, *_, usize, libc::off64_t) -> isize),
66                    (link_name, abi, args),
67                )?;
68                let fd = this.read_scalar(fd)?.to_i32()?;
69                let buf = this.read_pointer(buf)?;
70                let count = this.read_target_usize(n)?;
71                let offset = this.read_scalar(offset)?.to_int(offset.layout.size)?;
72                trace!("Called pwrite64({:?}, {:?}, {:?}, {:?})", fd, buf, count, offset);
73                this.write(fd, buf, count, Some(offset), dest)?;
74            }
75            "lseek64" => {
76                // FIXME: This does not have a direct test (#3179).
77                let [fd, offset, whence] = this.check_shim_sig(
78                    shim_sig!(extern "C" fn(i32, libc::off64_t, i32) -> libc::off64_t),
79                    (link_name, abi, args),
80                )?;
81                let fd = this.read_scalar(fd)?.to_i32()?;
82                let offset = this.read_scalar(offset)?.to_int(offset.layout.size)?;
83                let whence = this.read_scalar(whence)?.to_i32()?;
84                this.lseek(fd, offset, whence, dest)?;
85            }
86            "ftruncate64" => {
87                let [fd, length] = this.check_shim_sig(
88                    shim_sig!(extern "C" fn(i32, libc::off64_t) -> i32),
89                    (link_name, abi, args),
90                )?;
91                let fd = this.read_scalar(fd)?.to_i32()?;
92                let length = this.read_scalar(length)?.to_int(length.layout.size)?;
93                let result = this.ftruncate64(fd, length)?;
94                this.write_scalar(result, dest)?;
95            }
96            "posix_fallocate64" => {
97                let [fd, offset, len] = this.check_shim_sig(
98                    shim_sig!(extern "C" fn(i32, libc::off64_t, libc::off64_t) -> i32),
99                    (link_name, abi, args),
100                )?;
101
102                let fd = this.read_scalar(fd)?.to_i32()?;
103                let offset = this.read_scalar(offset)?.to_i64()?;
104                let len = this.read_scalar(len)?.to_i64()?;
105
106                let result = this.posix_fallocate(fd, offset, len)?;
107                this.write_scalar(result, dest)?;
108            }
109
110            "fallocate" => {
111                let [fd, mode, offset, len] = this.check_shim_sig(
112                    shim_sig!(extern "C" fn(i32, i32, libc::off_t, libc::off_t) -> i32),
113                    (link_name, abi, args),
114                )?;
115
116                let fd = this.read_scalar(fd)?.to_i32()?;
117                let mode = this.read_scalar(mode)?.to_i32()?;
118                // We don't support platforms which have libc::off_t bigger than 64 bits.
119                let offset =
120                    i64::try_from(this.read_scalar(offset)?.to_int(offset.layout.size)?).unwrap();
121                let len = i64::try_from(this.read_scalar(len)?.to_int(len.layout.size)?).unwrap();
122
123                let result = this.linux_fallocate(fd, mode, offset, len)?;
124                this.write_scalar(result, dest)?;
125            }
126
127            "fallocate64" => {
128                let [fd, mode, offset, len] = this.check_shim_sig(
129                    shim_sig!(extern "C" fn(i32, i32, libc::off64_t, libc::off64_t) -> i32),
130                    (link_name, abi, args),
131                )?;
132
133                let fd = this.read_scalar(fd)?.to_i32()?;
134                let mode = this.read_scalar(mode)?.to_i32()?;
135                let offset = this.read_scalar(offset)?.to_i64()?;
136                let len = this.read_scalar(len)?.to_i64()?;
137
138                let result = this.linux_fallocate(fd, mode, offset, len)?;
139                this.write_scalar(result, dest)?;
140            }
141
142            "readdir64" => {
143                let [dirp] = this.check_shim_sig_deprecated(abi, CanonAbi::C, link_name, args)?;
144                this.readdir(dirp, dest)?;
145            }
146            "sync_file_range" => {
147                let [fd, offset, nbytes, flags] =
148                    this.check_shim_sig_deprecated(abi, CanonAbi::C, link_name, args)?;
149                let result = this.sync_file_range(fd, offset, nbytes, flags)?;
150                this.write_scalar(result, dest)?;
151            }
152            "statx" => {
153                let [dirfd, pathname, flags, mask, statxbuf] =
154                    this.check_shim_sig_deprecated(abi, CanonAbi::C, link_name, args)?;
155                let result = this.linux_statx(dirfd, pathname, flags, mask, statxbuf)?;
156                this.write_scalar(result, dest)?;
157            }
158            // epoll, eventfd
159            "epoll_create1" => {
160                let [flag] = this.check_shim_sig_deprecated(abi, CanonAbi::C, link_name, args)?;
161                let result = this.epoll_create1(flag)?;
162                this.write_scalar(result, dest)?;
163            }
164            "epoll_ctl" => {
165                let [epfd, op, fd, event] =
166                    this.check_shim_sig_deprecated(abi, CanonAbi::C, link_name, args)?;
167                let result = this.epoll_ctl(epfd, op, fd, event)?;
168                this.write_scalar(result, dest)?;
169            }
170            "epoll_wait" => {
171                let [epfd, events, maxevents, timeout] =
172                    this.check_shim_sig_deprecated(abi, CanonAbi::C, link_name, args)?;
173                this.epoll_wait(epfd, events, maxevents, timeout, dest)?;
174            }
175            "eventfd" => {
176                let [val, flag] =
177                    this.check_shim_sig_deprecated(abi, CanonAbi::C, link_name, args)?;
178                let result = this.eventfd(val, flag)?;
179                this.write_scalar(result, dest)?;
180            }
181
182            // Threading
183            "pthread_setname_np" => {
184                let [thread, name] =
185                    this.check_shim_sig_deprecated(abi, CanonAbi::C, link_name, args)?;
186                let res = match this.pthread_setname_np(
187                    this.read_scalar(thread)?,
188                    this.read_scalar(name)?,
189                    TASK_COMM_LEN,
190                    /* truncate */ false,
191                )? {
192                    ThreadNameResult::Ok => Scalar::from_u32(0),
193                    ThreadNameResult::NameTooLong => this.eval_libc("ERANGE"),
194                };
195                this.write_scalar(res, dest)?;
196            }
197            "pthread_getname_np" => {
198                let [thread, name, len] =
199                    this.check_shim_sig_deprecated(abi, CanonAbi::C, link_name, args)?;
200                // The function's behavior isn't portable between platforms.
201                // In case of glibc, the length of the output buffer must
202                // be not shorter than TASK_COMM_LEN.
203                let len = this.read_scalar(len)?;
204                let res = if len.to_target_usize(this)? >= TASK_COMM_LEN {
205                    match this.pthread_getname_np(
206                        this.read_scalar(thread)?,
207                        this.read_scalar(name)?,
208                        len,
209                        /* truncate*/ false,
210                    )? {
211                        ThreadNameResult::Ok => Scalar::from_u32(0),
212                        ThreadNameResult::NameTooLong => unreachable!(),
213                    }
214                } else {
215                    this.eval_libc("ERANGE")
216                };
217                this.write_scalar(res, dest)?;
218            }
219            "gettid" => {
220                let [] = this.check_shim_sig_deprecated(abi, CanonAbi::C, link_name, args)?;
221                let result = this.unix_gettid(link_name.as_str())?;
222                this.write_scalar(result, dest)?;
223            }
224            "prctl" => prctl(this, link_name, abi, args, dest)?,
225
226            // Dynamically invoked syscalls
227            "syscall" => {
228                syscall(this, link_name, abi, args, dest)?;
229            }
230
231            // Miscellaneous
232            "mmap64" => {
233                let [addr, length, prot, flags, fd, offset] =
234                    this.check_shim_sig_deprecated(abi, CanonAbi::C, link_name, args)?;
235                let offset = this.read_scalar(offset)?.to_i64()?;
236                let ptr = this.mmap(addr, length, prot, flags, fd, offset.into())?;
237                this.write_scalar(ptr, dest)?;
238            }
239            "mremap" => {
240                let ([old_address, old_size, new_size, flags], _) = this.check_shim_sig_variadic(
241                    shim_sig!(extern "C" fn(*_, usize, usize, i32, ...) -> *_),
242                    (link_name, abi, args),
243                )?;
244                let ptr = this.mremap(old_address, old_size, new_size, flags)?;
245                this.write_scalar(ptr, dest)?;
246            }
247            "__xpg_strerror_r" => {
248                let [errnum, buf, buflen] =
249                    this.check_shim_sig_deprecated(abi, CanonAbi::C, link_name, args)?;
250                let result = this.strerror_r(errnum, buf, buflen)?;
251                this.write_scalar(result, dest)?;
252            }
253            "__errno_location" => {
254                let [] = this.check_shim_sig_deprecated(abi, CanonAbi::C, link_name, args)?;
255                let errno_place = this.last_error_place()?;
256                this.write_scalar(errno_place.to_ref(this).to_scalar(), dest)?;
257            }
258            "__libc_current_sigrtmin" => {
259                let [] = this.check_shim_sig_deprecated(abi, CanonAbi::C, link_name, args)?;
260
261                this.write_int(SIGRTMIN, dest)?;
262            }
263            "__libc_current_sigrtmax" => {
264                let [] = this.check_shim_sig_deprecated(abi, CanonAbi::C, link_name, args)?;
265
266                this.write_int(SIGRTMAX, dest)?;
267            }
268
269            // Incomplete shims that we "stub out" just to get pre-main initialization code to work.
270            // These shims are enabled only when the caller is in the standard library.
271            "pthread_getattr_np" if this.frame_in_std() => {
272                let [_thread, _attr] =
273                    this.check_shim_sig_deprecated(abi, CanonAbi::C, link_name, args)?;
274                this.write_null(dest)?;
275            }
276            "gnu_get_libc_version"
277                if this.frame_in_std()
278                    && this.tcx.sess.target.env == rustc_target::spec::Env::Gnu =>
279            {
280                let [] = this.check_shim_sig_deprecated(abi, CanonAbi::C, link_name, args)?;
281                // We have to be at least version 2.26 so that std does not call `res_init`.
282                // This returns a C string, so we have to add a null terminator.
283                let version = "2.26\0";
284                let version = this.allocate_str_dedup(version)?;
285                this.write_pointer(version.ptr(), dest)?;
286            }
287
288            _ => return interp_ok(EmulateItemResult::NotSupported),
289        };
290
291        interp_ok(EmulateItemResult::NeedsReturn)
292    }
293}