miri/shims/unix/linux/
foreign_items.rs

1use rustc_middle::ty::Ty;
2use rustc_span::Symbol;
3use rustc_target::callconv::{Conv, FnAbi};
4
5use self::shims::unix::linux::mem::EvalContextExt as _;
6use self::shims::unix::linux_like::epoll::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::*;
12use crate::*;
13
14// The documentation of glibc complains that the kernel never exposes
15// TASK_COMM_LEN through the headers, so it's assumed to always be 16 bytes
16// long including a null terminator.
17const TASK_COMM_LEN: usize = 16;
18
19pub fn is_dyn_sym(name: &str) -> bool {
20    matches!(name, "statx")
21}
22
23impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
24pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
25    fn emulate_foreign_item_inner(
26        &mut self,
27        link_name: Symbol,
28        abi: &FnAbi<'tcx, Ty<'tcx>>,
29        args: &[OpTy<'tcx>],
30        dest: &MPlaceTy<'tcx>,
31    ) -> InterpResult<'tcx, EmulateItemResult> {
32        let this = self.eval_context_mut();
33
34        // See `fn emulate_foreign_item_inner` in `shims/foreign_items.rs` for the general pattern.
35
36        match link_name.as_str() {
37            // File related shims
38            "readdir64" => {
39                let [dirp] = this.check_shim(abi, Conv::C, link_name, args)?;
40                let result = this.linux_solarish_readdir64("dirent64", dirp)?;
41                this.write_scalar(result, dest)?;
42            }
43            "sync_file_range" => {
44                let [fd, offset, nbytes, flags] = this.check_shim(abi, Conv::C, link_name, args)?;
45                let result = this.sync_file_range(fd, offset, nbytes, flags)?;
46                this.write_scalar(result, dest)?;
47            }
48            "statx" => {
49                let [dirfd, pathname, flags, mask, statxbuf] =
50                    this.check_shim(abi, Conv::C, link_name, args)?;
51                let result = this.linux_statx(dirfd, pathname, flags, mask, statxbuf)?;
52                this.write_scalar(result, dest)?;
53            }
54
55            // epoll, eventfd
56            "epoll_create1" => {
57                let [flag] = this.check_shim(abi, Conv::C, link_name, args)?;
58                let result = this.epoll_create1(flag)?;
59                this.write_scalar(result, dest)?;
60            }
61            "epoll_ctl" => {
62                let [epfd, op, fd, event] = this.check_shim(abi, Conv::C, link_name, args)?;
63                let result = this.epoll_ctl(epfd, op, fd, event)?;
64                this.write_scalar(result, dest)?;
65            }
66            "epoll_wait" => {
67                let [epfd, events, maxevents, timeout] =
68                    this.check_shim(abi, Conv::C, link_name, args)?;
69                this.epoll_wait(epfd, events, maxevents, timeout, dest)?;
70            }
71            "eventfd" => {
72                let [val, flag] = this.check_shim(abi, Conv::C, link_name, args)?;
73                let result = this.eventfd(val, flag)?;
74                this.write_scalar(result, dest)?;
75            }
76
77            // Threading
78            "pthread_setname_np" => {
79                let [thread, name] = this.check_shim(abi, Conv::C, link_name, args)?;
80                let res = match this.pthread_setname_np(
81                    this.read_scalar(thread)?,
82                    this.read_scalar(name)?,
83                    TASK_COMM_LEN,
84                    /* truncate */ false,
85                )? {
86                    ThreadNameResult::Ok => Scalar::from_u32(0),
87                    ThreadNameResult::NameTooLong => this.eval_libc("ERANGE"),
88                    // Act like we faild to open `/proc/self/task/$tid/comm`.
89                    ThreadNameResult::ThreadNotFound => this.eval_libc("ENOENT"),
90                };
91                this.write_scalar(res, dest)?;
92            }
93            "pthread_getname_np" => {
94                let [thread, name, len] = this.check_shim(abi, Conv::C, link_name, args)?;
95                // The function's behavior isn't portable between platforms.
96                // In case of glibc, the length of the output buffer must
97                // be not shorter than TASK_COMM_LEN.
98                let len = this.read_scalar(len)?;
99                let res = if len.to_target_usize(this)? >= TASK_COMM_LEN as u64 {
100                    match this.pthread_getname_np(
101                        this.read_scalar(thread)?,
102                        this.read_scalar(name)?,
103                        len,
104                        /* truncate*/ false,
105                    )? {
106                        ThreadNameResult::Ok => Scalar::from_u32(0),
107                        ThreadNameResult::NameTooLong => unreachable!(),
108                        // Act like we faild to open `/proc/self/task/$tid/comm`.
109                        ThreadNameResult::ThreadNotFound => this.eval_libc("ENOENT"),
110                    }
111                } else {
112                    this.eval_libc("ERANGE")
113                };
114                this.write_scalar(res, dest)?;
115            }
116            "gettid" => {
117                let [] = this.check_shim(abi, Conv::C, link_name, args)?;
118                let result = this.linux_gettid()?;
119                this.write_scalar(result, dest)?;
120            }
121
122            // Dynamically invoked syscalls
123            "syscall" => {
124                syscall(this, link_name, abi, args, dest)?;
125            }
126
127            // Miscellaneous
128            "mmap64" => {
129                let [addr, length, prot, flags, fd, offset] =
130                    this.check_shim(abi, Conv::C, link_name, args)?;
131                let offset = this.read_scalar(offset)?.to_i64()?;
132                let ptr = this.mmap(addr, length, prot, flags, fd, offset.into())?;
133                this.write_scalar(ptr, dest)?;
134            }
135            "mremap" => {
136                let [old_address, old_size, new_size, flags] =
137                    this.check_shim(abi, Conv::C, link_name, args)?;
138                let ptr = this.mremap(old_address, old_size, new_size, flags)?;
139                this.write_scalar(ptr, dest)?;
140            }
141            "__xpg_strerror_r" => {
142                let [errnum, buf, buflen] = this.check_shim(abi, Conv::C, link_name, args)?;
143                let result = this.strerror_r(errnum, buf, buflen)?;
144                this.write_scalar(result, dest)?;
145            }
146            "__errno_location" => {
147                let [] = this.check_shim(abi, Conv::C, link_name, args)?;
148                let errno_place = this.last_error_place()?;
149                this.write_scalar(errno_place.to_ref(this).to_scalar(), dest)?;
150            }
151            "__libc_current_sigrtmin" => {
152                let [] = this.check_shim(abi, Conv::C, link_name, args)?;
153
154                this.write_int(SIGRTMIN, dest)?;
155            }
156            "__libc_current_sigrtmax" => {
157                let [] = this.check_shim(abi, Conv::C, link_name, args)?;
158
159                this.write_int(SIGRTMAX, dest)?;
160            }
161
162            // Incomplete shims that we "stub out" just to get pre-main initialization code to work.
163            // These shims are enabled only when the caller is in the standard library.
164            "pthread_getattr_np" if this.frame_in_std() => {
165                let [_thread, _attr] = this.check_shim(abi, Conv::C, link_name, args)?;
166                this.write_null(dest)?;
167            }
168
169            _ => return interp_ok(EmulateItemResult::NotSupported),
170        };
171
172        interp_ok(EmulateItemResult::NeedsReturn)
173    }
174}