Skip to main content

miri/shims/unix/android/
foreign_items.rs

1use rustc_abi::CanonAbi;
2use rustc_middle::ty::Ty;
3use rustc_span::Symbol;
4use rustc_target::callconv::FnAbi;
5
6use crate::shims::unix::android::thread::prctl;
7use crate::shims::unix::env::EvalContextExt as _;
8use crate::shims::unix::linux_like::epoll::EvalContextExt as _;
9use crate::shims::unix::linux_like::eventfd::EvalContextExt as _;
10use crate::shims::unix::linux_like::syscall::syscall;
11use crate::shims::unix::*;
12use crate::*;
13
14pub fn is_dyn_sym(name: &str) -> bool {
15    matches!(name, "gettid")
16}
17
18impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
19pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
20    fn emulate_foreign_item_inner(
21        &mut self,
22        link_name: Symbol,
23        abi: &FnAbi<'tcx, Ty<'tcx>>,
24        args: &[OpTy<'tcx>],
25        dest: &MPlaceTy<'tcx>,
26    ) -> InterpResult<'tcx, EmulateItemResult> {
27        let this = self.eval_context_mut();
28        match link_name.as_str() {
29            // File related shims
30            "stat" => {
31                // FIXME: This does not have a direct test (#3179).
32                let [path, buf] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
33                let result = this.stat(path, buf)?;
34                this.write_scalar(result, dest)?;
35            }
36            "lstat" => {
37                // FIXME: This does not have a direct test (#3179).
38                let [path, buf] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
39                let result = this.lstat(path, buf)?;
40                this.write_scalar(result, dest)?;
41            }
42            "pread64" => {
43                // FIXME: This does not have a direct test (#3179).
44                let [fd, buf, count, offset] = this.check_shim_sig(
45                    shim_sig!(extern "C" fn(i32, *mut _, usize, libc::off64_t) -> isize),
46                    link_name,
47                    abi,
48                    args,
49                )?;
50                let fd = this.read_scalar(fd)?.to_i32()?;
51                let buf = this.read_pointer(buf)?;
52                let count = this.read_target_usize(count)?;
53                let offset = this.read_scalar(offset)?.to_int(offset.layout.size)?;
54                this.read(fd, buf, count, Some(offset), dest)?;
55            }
56            "pwrite64" => {
57                // FIXME: This does not have a direct test (#3179).
58                let [fd, buf, n, offset] = this.check_shim_sig(
59                    shim_sig!(extern "C" fn(i32, *const _, usize, libc::off64_t) -> isize),
60                    link_name,
61                    abi,
62                    args,
63                )?;
64                let fd = this.read_scalar(fd)?.to_i32()?;
65                let buf = this.read_pointer(buf)?;
66                let count = this.read_target_usize(n)?;
67                let offset = this.read_scalar(offset)?.to_int(offset.layout.size)?;
68                trace!("Called pwrite64({:?}, {:?}, {:?}, {:?})", fd, buf, count, offset);
69                this.write(fd, buf, count, Some(offset), dest)?;
70            }
71            "lseek64" => {
72                // FIXME: This does not have a direct test (#3179).
73                let [fd, offset, whence] = this.check_shim_sig(
74                    shim_sig!(extern "C" fn(i32, libc::off64_t, i32) -> libc::off64_t),
75                    link_name,
76                    abi,
77                    args,
78                )?;
79                let fd = this.read_scalar(fd)?.to_i32()?;
80                let offset = this.read_scalar(offset)?.to_int(offset.layout.size)?;
81                let whence = this.read_scalar(whence)?.to_i32()?;
82                this.lseek(fd, offset, whence, dest)?;
83            }
84            "ftruncate64" => {
85                let [fd, length] = this.check_shim_sig(
86                    shim_sig!(extern "C" fn(i32, libc::off64_t) -> i32),
87                    link_name,
88                    abi,
89                    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
97            // epoll, eventfd
98            "epoll_create1" => {
99                let [flag] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
100                let result = this.epoll_create1(flag)?;
101                this.write_scalar(result, dest)?;
102            }
103            "epoll_ctl" => {
104                let [epfd, op, fd, event] =
105                    this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
106                let result = this.epoll_ctl(epfd, op, fd, event)?;
107                this.write_scalar(result, dest)?;
108            }
109            "epoll_wait" => {
110                let [epfd, events, maxevents, timeout] =
111                    this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
112                this.epoll_wait(epfd, events, maxevents, timeout, dest)?;
113            }
114            "eventfd" => {
115                let [val, flag] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
116                let result = this.eventfd(val, flag)?;
117                this.write_scalar(result, dest)?;
118            }
119
120            // Miscellaneous
121            "__errno" => {
122                let [] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
123                let errno_place = this.last_error_place()?;
124                this.write_scalar(errno_place.to_ref(this).to_scalar(), dest)?;
125            }
126
127            "gettid" => {
128                let [] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
129                let result = this.unix_gettid(link_name.as_str())?;
130                this.write_scalar(result, dest)?;
131            }
132
133            // Dynamically invoked syscalls
134            "syscall" => syscall(this, link_name, abi, args, dest)?,
135
136            // Threading
137            "prctl" => prctl(this, link_name, abi, args, dest)?,
138
139            _ => return interp_ok(EmulateItemResult::NotSupported),
140        }
141        interp_ok(EmulateItemResult::NeedsReturn)
142    }
143}