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 "stat" => {
31 let [path, buf] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
32 let result = this.stat(path, buf)?;
33 this.write_scalar(result, dest)?;
34 }
35 "lstat" => {
36 let [path, buf] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
37 let result = this.lstat(path, buf)?;
38 this.write_scalar(result, dest)?;
39 }
40 "readdir" => {
41 let [dirp] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
42 let result = this.readdir64("dirent", dirp)?;
43 this.write_scalar(result, dest)?;
44 }
45 "pread64" => {
46 let [fd, buf, count, offset] = this.check_shim_sig(
47 shim_sig!(extern "C" fn(i32, *mut _, 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(count)?;
55 let offset = this.read_scalar(offset)?.to_int(offset.layout.size)?;
56 this.read(fd, buf, count, Some(offset), dest)?;
57 }
58 "pwrite64" => {
59 let [fd, buf, n, offset] = this.check_shim_sig(
60 shim_sig!(extern "C" fn(i32, *const _, usize, libc::off64_t) -> isize),
61 link_name,
62 abi,
63 args,
64 )?;
65 let fd = this.read_scalar(fd)?.to_i32()?;
66 let buf = this.read_pointer(buf)?;
67 let count = this.read_target_usize(n)?;
68 let offset = this.read_scalar(offset)?.to_int(offset.layout.size)?;
69 trace!("Called pwrite64({:?}, {:?}, {:?}, {:?})", fd, buf, count, offset);
70 this.write(fd, buf, count, Some(offset), dest)?;
71 }
72 "lseek64" => {
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.lseek64(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_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 "__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 "syscall" => syscall(this, link_name, abi, args, dest)?,
135
136 "prctl" => prctl(this, link_name, abi, args, dest)?,
138
139 _ => return interp_ok(EmulateItemResult::NotSupported),
140 }
141 interp_ok(EmulateItemResult::NeedsReturn)
142 }
143}