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::env::EvalContextExt as _;
7use crate::shims::unix::linux_like::epoll::EvalContextExt as _;
8use crate::shims::unix::linux_like::eventfd::EvalContextExt as _;
9use crate::shims::unix::linux_like::syscall::syscall;
10use crate::shims::unix::linux_like::thread::prctl;
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            "pread64" => {
31                // FIXME: This does not have a direct test (#3179).
32                let [fd, buf, count, offset] = this.check_shim_sig(
33                    shim_sig!(extern "C" fn(i32, *_, usize, libc::off64_t) -> isize),
34                    (link_name, abi, args),
35                )?;
36                let fd = this.read_scalar(fd)?.to_i32()?;
37                let buf = this.read_pointer(buf)?;
38                let count = this.read_target_usize(count)?;
39                let offset = this.read_scalar(offset)?.to_int(offset.layout.size)?;
40                this.read(fd, buf, count, Some(offset), dest)?;
41            }
42            "pwrite64" => {
43                // FIXME: This does not have a direct test (#3179).
44                let [fd, buf, n, offset] = this.check_shim_sig(
45                    shim_sig!(extern "C" fn(i32, *_, usize, libc::off64_t) -> isize),
46                    (link_name, abi, args),
47                )?;
48                let fd = this.read_scalar(fd)?.to_i32()?;
49                let buf = this.read_pointer(buf)?;
50                let count = this.read_target_usize(n)?;
51                let offset = this.read_scalar(offset)?.to_int(offset.layout.size)?;
52                trace!("Called pwrite64({:?}, {:?}, {:?}, {:?})", fd, buf, count, offset);
53                this.write(fd, buf, count, Some(offset), dest)?;
54            }
55            "lseek64" => {
56                // FIXME: This does not have a direct test (#3179).
57                let [fd, offset, whence] = this.check_shim_sig(
58                    shim_sig!(extern "C" fn(i32, libc::off64_t, i32) -> libc::off64_t),
59                    (link_name, abi, args),
60                )?;
61                let fd = this.read_scalar(fd)?.to_i32()?;
62                let offset = this.read_scalar(offset)?.to_int(offset.layout.size)?;
63                let whence = this.read_scalar(whence)?.to_i32()?;
64                this.lseek(fd, offset, whence, dest)?;
65            }
66            "ftruncate64" => {
67                let [fd, length] = this.check_shim_sig(
68                    shim_sig!(extern "C" fn(i32, libc::off64_t) -> i32),
69                    (link_name, abi, args),
70                )?;
71                let fd = this.read_scalar(fd)?.to_i32()?;
72                let length = this.read_scalar(length)?.to_int(length.layout.size)?;
73                let result = this.ftruncate64(fd, length)?;
74                this.write_scalar(result, dest)?;
75            }
76
77            // epoll, eventfd
78            "epoll_create1" => {
79                let [flag] = this.check_shim_sig_deprecated(abi, CanonAbi::C, link_name, args)?;
80                let result = this.epoll_create1(flag)?;
81                this.write_scalar(result, dest)?;
82            }
83            "epoll_ctl" => {
84                let [epfd, op, fd, event] =
85                    this.check_shim_sig_deprecated(abi, CanonAbi::C, link_name, args)?;
86                let result = this.epoll_ctl(epfd, op, fd, event)?;
87                this.write_scalar(result, dest)?;
88            }
89            "epoll_wait" => {
90                let [epfd, events, maxevents, timeout] =
91                    this.check_shim_sig_deprecated(abi, CanonAbi::C, link_name, args)?;
92                this.epoll_wait(epfd, events, maxevents, timeout, dest)?;
93            }
94            "eventfd" => {
95                let [val, flag] =
96                    this.check_shim_sig_deprecated(abi, CanonAbi::C, link_name, args)?;
97                let result = this.eventfd(val, flag)?;
98                this.write_scalar(result, dest)?;
99            }
100
101            // Miscellaneous
102            "__errno" => {
103                let [] = this.check_shim_sig_deprecated(abi, CanonAbi::C, link_name, args)?;
104                let errno_place = this.last_error_place()?;
105                this.write_scalar(errno_place.to_ref(this).to_scalar(), dest)?;
106            }
107
108            "gettid" => {
109                let [] = this.check_shim_sig_deprecated(abi, CanonAbi::C, link_name, args)?;
110                let result = this.unix_gettid(link_name.as_str())?;
111                this.write_scalar(result, dest)?;
112            }
113
114            // Dynamically invoked syscalls
115            "syscall" => syscall(this, link_name, abi, args, dest)?,
116
117            // Threading
118            "prctl" => prctl(this, link_name, abi, args, dest)?,
119
120            _ => return interp_ok(EmulateItemResult::NotSupported),
121        }
122        interp_ok(EmulateItemResult::NeedsReturn)
123    }
124}