1use std::io;
2use std::io::ErrorKind;
3
4use crate::*;
5
6#[derive(Debug)]
9pub enum IoError {
10 LibcError(&'static str),
11 WindowsError(&'static str),
12 HostError(io::Error),
13 Raw(Scalar),
14}
15pub use self::IoError::*;
16
17impl IoError {
18 pub(crate) fn into_ntstatus(self) -> i32 {
19 let raw = match self {
20 HostError(e) =>
21 match e.kind() {
22 ErrorKind::ReadOnlyFilesystem => 0xC00000A2u32,
24 ErrorKind::InvalidInput => 0xC0000098,
26 ErrorKind::QuotaExceeded => 0xC000007F,
28 ErrorKind::PermissionDenied => 0xC0000022,
30 _ => 0xC0000185,
32 },
33 _ => 0xC0000185,
35 };
36 raw.cast_signed()
37 }
38}
39
40impl From<io::Error> for IoError {
41 fn from(value: io::Error) -> Self {
42 IoError::HostError(value)
43 }
44}
45
46impl From<io::ErrorKind> for IoError {
47 fn from(value: io::ErrorKind) -> Self {
48 IoError::HostError(value.into())
49 }
50}
51
52impl From<Scalar> for IoError {
53 fn from(value: Scalar) -> Self {
54 IoError::Raw(value)
55 }
56}
57
58const UNIX_IO_ERROR_TABLE: &[(&str, std::io::ErrorKind)] = {
61 use std::io::ErrorKind::*;
62 &[
63 ("E2BIG", ArgumentListTooLong),
64 ("EADDRINUSE", AddrInUse),
65 ("EADDRNOTAVAIL", AddrNotAvailable),
66 ("EBUSY", ResourceBusy),
67 ("ECONNABORTED", ConnectionAborted),
68 ("ECONNREFUSED", ConnectionRefused),
69 ("ECONNRESET", ConnectionReset),
70 ("EDEADLK", Deadlock),
71 ("EDQUOT", QuotaExceeded),
72 ("EEXIST", AlreadyExists),
73 ("EFBIG", FileTooLarge),
74 ("EHOSTUNREACH", HostUnreachable),
75 ("EINTR", Interrupted),
76 ("EINVAL", InvalidInput),
77 ("EISDIR", IsADirectory),
78 ("ELOOP", FilesystemLoop),
79 ("ENOENT", NotFound),
80 ("ENOMEM", OutOfMemory),
81 ("ENOSPC", StorageFull),
82 ("ENOSYS", Unsupported),
83 ("EMLINK", TooManyLinks),
84 ("ENAMETOOLONG", InvalidFilename),
85 ("ENETDOWN", NetworkDown),
86 ("ENETUNREACH", NetworkUnreachable),
87 ("ENOTCONN", NotConnected),
88 ("ENOTDIR", NotADirectory),
89 ("ENOTEMPTY", DirectoryNotEmpty),
90 ("EPIPE", BrokenPipe),
91 ("EROFS", ReadOnlyFilesystem),
92 ("ESPIPE", NotSeekable),
93 ("ESTALE", StaleNetworkFileHandle),
94 ("ETIMEDOUT", TimedOut),
95 ("ETXTBSY", ExecutableFileBusy),
96 ("EXDEV", CrossesDevices),
97 ("EPERM", PermissionDenied),
100 ("EACCES", PermissionDenied),
101 ("EWOULDBLOCK", WouldBlock),
102 ("EAGAIN", WouldBlock),
103 ]
104};
105const WINDOWS_IO_ERROR_TABLE: &[(&str, std::io::ErrorKind)] = {
108 use std::io::ErrorKind::*;
109 &[
114 ("WSAEADDRINUSE", AddrInUse),
115 ("WSAEADDRNOTAVAIL", AddrNotAvailable),
116 ("ERROR_ALREADY_EXISTS", AlreadyExists),
117 ("ERROR_FILE_EXISTS", AlreadyExists),
118 ("ERROR_NO_DATA", BrokenPipe),
119 ("WSAECONNABORTED", ConnectionAborted),
120 ("WSAECONNREFUSED", ConnectionRefused),
121 ("WSAECONNRESET", ConnectionReset),
122 ("ERROR_NOT_SAME_DEVICE", CrossesDevices),
123 ("ERROR_POSSIBLE_DEADLOCK", Deadlock),
124 ("ERROR_DIR_NOT_EMPTY", DirectoryNotEmpty),
125 ("ERROR_CANT_RESOLVE_FILENAME", FilesystemLoop),
126 ("ERROR_DISK_QUOTA_EXCEEDED", QuotaExceeded),
127 ("WSAEDQUOT", QuotaExceeded),
128 ("ERROR_FILE_TOO_LARGE", FileTooLarge),
129 ("ERROR_HOST_UNREACHABLE", HostUnreachable),
130 ("WSAEHOSTUNREACH", HostUnreachable),
131 ("ERROR_INVALID_NAME", InvalidFilename),
132 ("ERROR_BAD_PATHNAME", InvalidFilename),
133 ("ERROR_FILENAME_EXCED_RANGE", InvalidFilename),
134 ("ERROR_INVALID_PARAMETER", InvalidInput),
135 ("WSAEINVAL", InvalidInput),
136 ("ERROR_DIRECTORY_NOT_SUPPORTED", IsADirectory),
137 ("WSAENETDOWN", NetworkDown),
138 ("ERROR_NETWORK_UNREACHABLE", NetworkUnreachable),
139 ("WSAENETUNREACH", NetworkUnreachable),
140 ("ERROR_DIRECTORY", NotADirectory),
141 ("WSAENOTCONN", NotConnected),
142 ("ERROR_FILE_NOT_FOUND", NotFound),
143 ("ERROR_PATH_NOT_FOUND", NotFound),
144 ("ERROR_INVALID_DRIVE", NotFound),
145 ("ERROR_BAD_NETPATH", NotFound),
146 ("ERROR_BAD_NET_NAME", NotFound),
147 ("ERROR_SEEK_ON_DEVICE", NotSeekable),
148 ("ERROR_NOT_ENOUGH_MEMORY", OutOfMemory),
149 ("ERROR_OUTOFMEMORY", OutOfMemory),
150 ("ERROR_ACCESS_DENIED", PermissionDenied),
151 ("WSAEACCES", PermissionDenied),
152 ("ERROR_WRITE_PROTECT", ReadOnlyFilesystem),
153 ("ERROR_BUSY", ResourceBusy),
154 ("ERROR_DISK_FULL", StorageFull),
155 ("ERROR_HANDLE_DISK_FULL", StorageFull),
156 ("WAIT_TIMEOUT", TimedOut),
157 ("WSAETIMEDOUT", TimedOut),
158 ("ERROR_DRIVER_CANCEL_TIMEOUT", TimedOut),
159 ("ERROR_OPERATION_ABORTED", TimedOut),
160 ("ERROR_SERVICE_REQUEST_TIMEOUT", TimedOut),
161 ("ERROR_COUNTER_TIMEOUT", TimedOut),
162 ("ERROR_TIMEOUT", TimedOut),
163 ("ERROR_RESOURCE_CALL_TIMED_OUT", TimedOut),
164 ("ERROR_CTX_MODEM_RESPONSE_TIMEOUT", TimedOut),
165 ("ERROR_CTX_CLIENT_QUERY_TIMEOUT", TimedOut),
166 ("FRS_ERR_SYSVOL_POPULATE_TIMEOUT", TimedOut),
167 ("ERROR_DS_TIMELIMIT_EXCEEDED", TimedOut),
168 ("DNS_ERROR_RECORD_TIMED_OUT", TimedOut),
169 ("ERROR_IPSEC_IKE_TIMED_OUT", TimedOut),
170 ("ERROR_RUNLEVEL_SWITCH_TIMEOUT", TimedOut),
171 ("ERROR_RUNLEVEL_SWITCH_AGENT_TIMEOUT", TimedOut),
172 ("ERROR_TOO_MANY_LINKS", TooManyLinks),
173 ("ERROR_CALL_NOT_IMPLEMENTED", Unsupported),
174 ("WSAEWOULDBLOCK", WouldBlock),
175 ]
176};
177
178impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
179pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
180 fn last_error_place(&mut self) -> InterpResult<'tcx, MPlaceTy<'tcx>> {
183 let this = self.eval_context_mut();
184 if let Some(errno_place) = this.active_thread_ref().last_error.as_ref() {
185 interp_ok(errno_place.clone())
186 } else {
187 let errno_layout = this.machine.layouts.u32;
189 let errno_place = this.allocate(errno_layout, MiriMemoryKind::Machine.into())?;
190 this.write_scalar(Scalar::from_u32(0), &errno_place)?;
191 this.active_thread_mut().last_error = Some(errno_place.clone());
192 interp_ok(errno_place)
193 }
194 }
195
196 fn set_last_error(&mut self, err: impl Into<IoError>) -> InterpResult<'tcx> {
198 let this = self.eval_context_mut();
199 let errno = match err.into() {
200 HostError(err) => this.io_error_to_errnum(err)?,
201 LibcError(name) => this.eval_libc(name),
202 WindowsError(name) => this.eval_windows("c", name),
203 Raw(val) => val,
204 };
205 let errno_place = this.last_error_place()?;
206 this.write_scalar(errno, &errno_place)
207 }
208
209 fn set_last_error_and_return(
211 &mut self,
212 err: impl Into<IoError>,
213 dest: &MPlaceTy<'tcx>,
214 ) -> InterpResult<'tcx> {
215 let this = self.eval_context_mut();
216 this.set_last_error(err)?;
217 this.write_int(-1, dest)?;
218 interp_ok(())
219 }
220
221 fn set_last_error_and_return_i32(
223 &mut self,
224 err: impl Into<IoError>,
225 ) -> InterpResult<'tcx, Scalar> {
226 let this = self.eval_context_mut();
227 this.set_last_error(err)?;
228 interp_ok(Scalar::from_i32(-1))
229 }
230
231 fn set_last_error_and_return_i64(
233 &mut self,
234 err: impl Into<IoError>,
235 ) -> InterpResult<'tcx, Scalar> {
236 let this = self.eval_context_mut();
237 this.set_last_error(err)?;
238 interp_ok(Scalar::from_i64(-1))
239 }
240
241 fn get_last_error(&mut self) -> InterpResult<'tcx, Scalar> {
243 let this = self.eval_context_mut();
244 let errno_place = this.last_error_place()?;
245 this.read_scalar(&errno_place)
246 }
247
248 fn io_error_to_errnum(&self, err: std::io::Error) -> InterpResult<'tcx, Scalar> {
251 let this = self.eval_context_ref();
252 let target = &this.tcx.sess.target;
253 if target.families.iter().any(|f| f == "unix") {
254 for &(name, kind) in UNIX_IO_ERROR_TABLE {
255 if err.kind() == kind {
256 return interp_ok(this.eval_libc(name));
257 }
258 }
259 throw_unsup_format!("unsupported io error: {err}")
260 } else if target.families.iter().any(|f| f == "windows") {
261 for &(name, kind) in WINDOWS_IO_ERROR_TABLE {
262 if err.kind() == kind {
263 return interp_ok(this.eval_windows("c", name));
264 }
265 }
266 throw_unsup_format!("unsupported io error: {err}");
267 } else {
268 throw_unsup_format!(
269 "converting io::Error into errnum is unsupported for OS {}",
270 target.os
271 )
272 }
273 }
274
275 #[expect(clippy::needless_return)]
277 fn try_errnum_to_io_error(
278 &self,
279 errnum: Scalar,
280 ) -> InterpResult<'tcx, Option<std::io::ErrorKind>> {
281 let this = self.eval_context_ref();
282 let target = &this.tcx.sess.target;
283 if target.families.iter().any(|f| f == "unix") {
284 let errnum = errnum.to_i32()?;
285 for &(name, kind) in UNIX_IO_ERROR_TABLE {
286 if errnum == this.eval_libc_i32(name) {
287 return interp_ok(Some(kind));
288 }
289 }
290 return interp_ok(None);
291 } else if target.families.iter().any(|f| f == "windows") {
292 let errnum = errnum.to_u32()?;
293 for &(name, kind) in WINDOWS_IO_ERROR_TABLE {
294 if errnum == this.eval_windows("c", name).to_u32()? {
295 return interp_ok(Some(kind));
296 }
297 }
298 return interp_ok(None);
299 } else {
300 throw_unsup_format!(
301 "converting errnum into io::Error is unsupported for OS {}",
302 target.os
303 )
304 }
305 }
306
307 fn try_unwrap_io_result<T: From<i32>>(
314 &mut self,
315 result: std::io::Result<T>,
316 ) -> InterpResult<'tcx, T> {
317 match result {
318 Ok(ok) => interp_ok(ok),
319 Err(e) => {
320 self.eval_context_mut().set_last_error(e)?;
321 interp_ok((-1).into())
322 }
323 }
324 }
325}