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::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
15// The documentation of glibc complains that the kernel never exposes
16// TASK_COMM_LEN through the headers, so it's assumed to always be 16 bytes
17// long including a null terminator.
18const 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        // See `fn emulate_foreign_item_inner` in `shims/foreign_items.rs` for the general pattern.
36
37        match link_name.as_str() {
38            // File related shims
39            "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, eventfd
58            "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            // Threading
81            "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                    /* truncate */ false,
89                )? {
90                    ThreadNameResult::Ok => Scalar::from_u32(0),
91                    ThreadNameResult::NameTooLong => this.eval_libc("ERANGE"),
92                    // Act like we faild to open `/proc/self/task/$tid/comm`.
93                    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                // The function's behavior isn't portable between platforms.
101                // In case of glibc, the length of the output buffer must
102                // be not shorter than TASK_COMM_LEN.
103                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                        /* truncate*/ false,
110                    )? {
111                        ThreadNameResult::Ok => Scalar::from_u32(0),
112                        ThreadNameResult::NameTooLong => unreachable!(),
113                        // Act like we faild to open `/proc/self/task/$tid/comm`.
114                        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            // Dynamically invoked syscalls
128            "syscall" => {
129                syscall(this, link_name, abi, args, dest)?;
130            }
131
132            // Miscellaneous
133            "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            // Incomplete shims that we "stub out" just to get pre-main initialization code to work.
169            // These shims are enabled only when the caller is in the standard library.
170            "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}