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