]> git.lizzy.rs Git - rust.git/blob - src/tools/miri/src/helpers.rs
Rollup merge of #103084 - inquisitivecrystal:control-flow, r=scottmcm
[rust.git] / src / tools / miri / src / helpers.rs
1 pub mod convert;
2
3 use std::cmp;
4 use std::iter;
5 use std::mem;
6 use std::num::NonZeroUsize;
7 use std::time::Duration;
8
9 use log::trace;
10
11 use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX};
12 use rustc_middle::mir;
13 use rustc_middle::ty::{
14     self,
15     layout::{LayoutOf, TyAndLayout},
16     List, TyCtxt,
17 };
18 use rustc_span::{def_id::CrateNum, sym, Span, Symbol};
19 use rustc_target::abi::{Align, FieldsShape, Size, Variants};
20 use rustc_target::spec::abi::Abi;
21
22 use rand::RngCore;
23
24 use crate::*;
25
26 impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriInterpCx<'mir, 'tcx> {}
27
28 // This mapping should match `decode_error_kind` in
29 // <https://github.com/rust-lang/rust/blob/master/library/std/src/sys/unix/mod.rs>.
30 const UNIX_IO_ERROR_TABLE: &[(&str, std::io::ErrorKind)] = {
31     use std::io::ErrorKind::*;
32     &[
33         ("E2BIG", ArgumentListTooLong),
34         ("EADDRINUSE", AddrInUse),
35         ("EADDRNOTAVAIL", AddrNotAvailable),
36         ("EBUSY", ResourceBusy),
37         ("ECONNABORTED", ConnectionAborted),
38         ("ECONNREFUSED", ConnectionRefused),
39         ("ECONNRESET", ConnectionReset),
40         ("EDEADLK", Deadlock),
41         ("EDQUOT", FilesystemQuotaExceeded),
42         ("EEXIST", AlreadyExists),
43         ("EFBIG", FileTooLarge),
44         ("EHOSTUNREACH", HostUnreachable),
45         ("EINTR", Interrupted),
46         ("EINVAL", InvalidInput),
47         ("EISDIR", IsADirectory),
48         ("ELOOP", FilesystemLoop),
49         ("ENOENT", NotFound),
50         ("ENOMEM", OutOfMemory),
51         ("ENOSPC", StorageFull),
52         ("ENOSYS", Unsupported),
53         ("EMLINK", TooManyLinks),
54         ("ENAMETOOLONG", InvalidFilename),
55         ("ENETDOWN", NetworkDown),
56         ("ENETUNREACH", NetworkUnreachable),
57         ("ENOTCONN", NotConnected),
58         ("ENOTDIR", NotADirectory),
59         ("ENOTEMPTY", DirectoryNotEmpty),
60         ("EPIPE", BrokenPipe),
61         ("EROFS", ReadOnlyFilesystem),
62         ("ESPIPE", NotSeekable),
63         ("ESTALE", StaleNetworkFileHandle),
64         ("ETIMEDOUT", TimedOut),
65         ("ETXTBSY", ExecutableFileBusy),
66         ("EXDEV", CrossesDevices),
67         // The following have two valid options. We have both for the forwards mapping; only the
68         // first one will be used for the backwards mapping.
69         ("EPERM", PermissionDenied),
70         ("EACCES", PermissionDenied),
71         ("EWOULDBLOCK", WouldBlock),
72         ("EAGAIN", WouldBlock),
73     ]
74 };
75
76 /// Gets an instance for a path.
77 fn try_resolve_did<'tcx>(tcx: TyCtxt<'tcx>, path: &[&str]) -> Option<DefId> {
78     tcx.crates(()).iter().find(|&&krate| tcx.crate_name(krate).as_str() == path[0]).and_then(
79         |krate| {
80             let krate = DefId { krate: *krate, index: CRATE_DEF_INDEX };
81             let mut items = tcx.module_children(krate);
82             let mut path_it = path.iter().skip(1).peekable();
83
84             while let Some(segment) = path_it.next() {
85                 for item in mem::take(&mut items).iter() {
86                     if item.ident.name.as_str() == *segment {
87                         if path_it.peek().is_none() {
88                             return Some(item.res.def_id());
89                         }
90
91                         items = tcx.module_children(item.res.def_id());
92                         break;
93                     }
94                 }
95             }
96             None
97         },
98     )
99 }
100
101 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
102     /// Gets an instance for a path; fails gracefully if the path does not exist.
103     fn try_resolve_path(&self, path: &[&str]) -> Option<ty::Instance<'tcx>> {
104         let did = try_resolve_did(self.eval_context_ref().tcx.tcx, path)?;
105         Some(ty::Instance::mono(self.eval_context_ref().tcx.tcx, did))
106     }
107
108     /// Gets an instance for a path.
109     fn resolve_path(&self, path: &[&str]) -> ty::Instance<'tcx> {
110         self.try_resolve_path(path)
111             .unwrap_or_else(|| panic!("failed to find required Rust item: {path:?}"))
112     }
113
114     /// Evaluates the scalar at the specified path. Returns Some(val)
115     /// if the path could be resolved, and None otherwise
116     fn eval_path_scalar(&self, path: &[&str]) -> InterpResult<'tcx, Scalar<Provenance>> {
117         let this = self.eval_context_ref();
118         let instance = this.resolve_path(path);
119         let cid = GlobalId { instance, promoted: None };
120         let const_val = this.eval_to_allocation(cid)?;
121         this.read_scalar(&const_val.into())
122     }
123
124     /// Helper function to get a `libc` constant as a `Scalar`.
125     fn eval_libc(&self, name: &str) -> InterpResult<'tcx, Scalar<Provenance>> {
126         self.eval_path_scalar(&["libc", name])
127     }
128
129     /// Helper function to get a `libc` constant as an `i32`.
130     fn eval_libc_i32(&self, name: &str) -> InterpResult<'tcx, i32> {
131         // TODO: Cache the result.
132         self.eval_libc(name)?.to_i32()
133     }
134
135     /// Helper function to get a `windows` constant as a `Scalar`.
136     fn eval_windows(&self, module: &str, name: &str) -> InterpResult<'tcx, Scalar<Provenance>> {
137         self.eval_context_ref().eval_path_scalar(&["std", "sys", "windows", module, name])
138     }
139
140     /// Helper function to get a `windows` constant as a `u64`.
141     fn eval_windows_u64(&self, module: &str, name: &str) -> InterpResult<'tcx, u64> {
142         // TODO: Cache the result.
143         self.eval_windows(module, name)?.to_u64()
144     }
145
146     /// Helper function to get the `TyAndLayout` of a `libc` type
147     fn libc_ty_layout(&self, name: &str) -> InterpResult<'tcx, TyAndLayout<'tcx>> {
148         let this = self.eval_context_ref();
149         let ty = this.resolve_path(&["libc", name]).ty(*this.tcx, ty::ParamEnv::reveal_all());
150         this.layout_of(ty)
151     }
152
153     /// Helper function to get the `TyAndLayout` of a `windows` type
154     fn windows_ty_layout(&self, name: &str) -> InterpResult<'tcx, TyAndLayout<'tcx>> {
155         let this = self.eval_context_ref();
156         let ty = this
157             .resolve_path(&["std", "sys", "windows", "c", name])
158             .ty(*this.tcx, ty::ParamEnv::reveal_all());
159         this.layout_of(ty)
160     }
161
162     /// Project to the given *named* field of the mplace (which must be a struct or union type).
163     fn mplace_field_named(
164         &self,
165         mplace: &MPlaceTy<'tcx, Provenance>,
166         name: &str,
167     ) -> InterpResult<'tcx, MPlaceTy<'tcx, Provenance>> {
168         let this = self.eval_context_ref();
169         let adt = mplace.layout.ty.ty_adt_def().unwrap();
170         for (idx, field) in adt.non_enum_variant().fields.iter().enumerate() {
171             if field.name.as_str() == name {
172                 return this.mplace_field(mplace, idx);
173             }
174         }
175         bug!("No field named {} in type {}", name, mplace.layout.ty);
176     }
177
178     /// Write an int of the appropriate size to `dest`. The target type may be signed or unsigned,
179     /// we try to do the right thing anyway. `i128` can fit all integer types except for `u128` so
180     /// this method is fine for almost all integer types.
181     fn write_int(
182         &mut self,
183         i: impl Into<i128>,
184         dest: &PlaceTy<'tcx, Provenance>,
185     ) -> InterpResult<'tcx> {
186         assert!(dest.layout.abi.is_scalar(), "write_int on non-scalar type {}", dest.layout.ty);
187         let val = if dest.layout.abi.is_signed() {
188             Scalar::from_int(i, dest.layout.size)
189         } else {
190             Scalar::from_uint(u64::try_from(i.into()).unwrap(), dest.layout.size)
191         };
192         self.eval_context_mut().write_scalar(val, dest)
193     }
194
195     /// Write the first N fields of the given place.
196     fn write_int_fields(
197         &mut self,
198         values: &[i128],
199         dest: &MPlaceTy<'tcx, Provenance>,
200     ) -> InterpResult<'tcx> {
201         let this = self.eval_context_mut();
202         for (idx, &val) in values.iter().enumerate() {
203             let field = this.mplace_field(dest, idx)?;
204             this.write_int(val, &field.into())?;
205         }
206         Ok(())
207     }
208
209     /// Write the given fields of the given place.
210     fn write_int_fields_named(
211         &mut self,
212         values: &[(&str, i128)],
213         dest: &MPlaceTy<'tcx, Provenance>,
214     ) -> InterpResult<'tcx> {
215         let this = self.eval_context_mut();
216         for &(name, val) in values.iter() {
217             let field = this.mplace_field_named(dest, name)?;
218             this.write_int(val, &field.into())?;
219         }
220         Ok(())
221     }
222
223     /// Write a 0 of the appropriate size to `dest`.
224     fn write_null(&mut self, dest: &PlaceTy<'tcx, Provenance>) -> InterpResult<'tcx> {
225         self.write_int(0, dest)
226     }
227
228     /// Test if this pointer equals 0.
229     fn ptr_is_null(&self, ptr: Pointer<Option<Provenance>>) -> InterpResult<'tcx, bool> {
230         Ok(ptr.addr().bytes() == 0)
231     }
232
233     /// Get the `Place` for a local
234     fn local_place(&mut self, local: mir::Local) -> InterpResult<'tcx, PlaceTy<'tcx, Provenance>> {
235         let this = self.eval_context_mut();
236         let place = mir::Place { local, projection: List::empty() };
237         this.eval_place(place)
238     }
239
240     /// Generate some random bytes, and write them to `dest`.
241     fn gen_random(&mut self, ptr: Pointer<Option<Provenance>>, len: u64) -> InterpResult<'tcx> {
242         // Some programs pass in a null pointer and a length of 0
243         // to their platform's random-generation function (e.g. getrandom())
244         // on Linux. For compatibility with these programs, we don't perform
245         // any additional checks - it's okay if the pointer is invalid,
246         // since we wouldn't actually be writing to it.
247         if len == 0 {
248             return Ok(());
249         }
250         let this = self.eval_context_mut();
251
252         let mut data = vec![0; usize::try_from(len).unwrap()];
253
254         if this.machine.communicate() {
255             // Fill the buffer using the host's rng.
256             getrandom::getrandom(&mut data)
257                 .map_err(|err| err_unsup_format!("host getrandom failed: {}", err))?;
258         } else {
259             let rng = this.machine.rng.get_mut();
260             rng.fill_bytes(&mut data);
261         }
262
263         this.write_bytes_ptr(ptr, data.iter().copied())
264     }
265
266     /// Call a function: Push the stack frame and pass the arguments.
267     /// For now, arguments must be scalars (so that the caller does not have to know the layout).
268     ///
269     /// If you do not provie a return place, a dangling zero-sized place will be created
270     /// for your convenience.
271     fn call_function(
272         &mut self,
273         f: ty::Instance<'tcx>,
274         caller_abi: Abi,
275         args: &[Immediate<Provenance>],
276         dest: Option<&PlaceTy<'tcx, Provenance>>,
277         stack_pop: StackPopCleanup,
278     ) -> InterpResult<'tcx> {
279         let this = self.eval_context_mut();
280         let param_env = ty::ParamEnv::reveal_all(); // in Miri this is always the param_env we use... and this.param_env is private.
281         let callee_abi = f.ty(*this.tcx, param_env).fn_sig(*this.tcx).abi();
282         if this.machine.enforce_abi && callee_abi != caller_abi {
283             throw_ub_format!(
284                 "calling a function with ABI {} using caller ABI {}",
285                 callee_abi.name(),
286                 caller_abi.name()
287             )
288         }
289
290         // Push frame.
291         let mir = this.load_mir(f.def, None)?;
292         let dest = match dest {
293             Some(dest) => dest.clone(),
294             None => MPlaceTy::fake_alloc_zst(this.layout_of(mir.return_ty())?).into(),
295         };
296         this.push_stack_frame(f, mir, &dest, stack_pop)?;
297
298         // Initialize arguments.
299         let mut callee_args = this.frame().body.args_iter();
300         for arg in args {
301             let callee_arg = this.local_place(
302                 callee_args
303                     .next()
304                     .ok_or_else(|| err_ub_format!("callee has fewer arguments than expected"))?,
305             )?;
306             this.write_immediate(*arg, &callee_arg)?;
307         }
308         if callee_args.next().is_some() {
309             throw_ub_format!("callee has more arguments than expected");
310         }
311
312         Ok(())
313     }
314
315     /// Visits the memory covered by `place`, sensitive to freezing: the 2nd parameter
316     /// of `action` will be true if this is frozen, false if this is in an `UnsafeCell`.
317     /// The range is relative to `place`.
318     fn visit_freeze_sensitive(
319         &self,
320         place: &MPlaceTy<'tcx, Provenance>,
321         size: Size,
322         mut action: impl FnMut(AllocRange, bool) -> InterpResult<'tcx>,
323     ) -> InterpResult<'tcx> {
324         let this = self.eval_context_ref();
325         trace!("visit_frozen(place={:?}, size={:?})", *place, size);
326         debug_assert_eq!(
327             size,
328             this.size_and_align_of_mplace(place)?
329                 .map(|(size, _)| size)
330                 .unwrap_or_else(|| place.layout.size)
331         );
332         // Store how far we proceeded into the place so far. Everything to the left of
333         // this offset has already been handled, in the sense that the frozen parts
334         // have had `action` called on them.
335         let start_addr = place.ptr.addr();
336         let mut cur_addr = start_addr;
337         // Called when we detected an `UnsafeCell` at the given offset and size.
338         // Calls `action` and advances `cur_ptr`.
339         let mut unsafe_cell_action = |unsafe_cell_ptr: &Pointer<Option<Provenance>>,
340                                       unsafe_cell_size: Size| {
341             // We assume that we are given the fields in increasing offset order,
342             // and nothing else changes.
343             let unsafe_cell_addr = unsafe_cell_ptr.addr();
344             assert!(unsafe_cell_addr >= cur_addr);
345             let frozen_size = unsafe_cell_addr - cur_addr;
346             // Everything between the cur_ptr and this `UnsafeCell` is frozen.
347             if frozen_size != Size::ZERO {
348                 action(alloc_range(cur_addr - start_addr, frozen_size), /*frozen*/ true)?;
349             }
350             cur_addr += frozen_size;
351             // This `UnsafeCell` is NOT frozen.
352             if unsafe_cell_size != Size::ZERO {
353                 action(
354                     alloc_range(cur_addr - start_addr, unsafe_cell_size),
355                     /*frozen*/ false,
356                 )?;
357             }
358             cur_addr += unsafe_cell_size;
359             // Done
360             Ok(())
361         };
362         // Run a visitor
363         {
364             let mut visitor = UnsafeCellVisitor {
365                 ecx: this,
366                 unsafe_cell_action: |place| {
367                     trace!("unsafe_cell_action on {:?}", place.ptr);
368                     // We need a size to go on.
369                     let unsafe_cell_size = this
370                         .size_and_align_of_mplace(place)?
371                         .map(|(size, _)| size)
372                         // for extern types, just cover what we can
373                         .unwrap_or_else(|| place.layout.size);
374                     // Now handle this `UnsafeCell`, unless it is empty.
375                     if unsafe_cell_size != Size::ZERO {
376                         unsafe_cell_action(&place.ptr, unsafe_cell_size)
377                     } else {
378                         Ok(())
379                     }
380                 },
381             };
382             visitor.visit_value(place)?;
383         }
384         // The part between the end_ptr and the end of the place is also frozen.
385         // So pretend there is a 0-sized `UnsafeCell` at the end.
386         unsafe_cell_action(&place.ptr.offset(size, this)?, Size::ZERO)?;
387         // Done!
388         return Ok(());
389
390         /// Visiting the memory covered by a `MemPlace`, being aware of
391         /// whether we are inside an `UnsafeCell` or not.
392         struct UnsafeCellVisitor<'ecx, 'mir, 'tcx, F>
393         where
394             F: FnMut(&MPlaceTy<'tcx, Provenance>) -> InterpResult<'tcx>,
395         {
396             ecx: &'ecx MiriInterpCx<'mir, 'tcx>,
397             unsafe_cell_action: F,
398         }
399
400         impl<'ecx, 'mir, 'tcx: 'mir, F> ValueVisitor<'mir, 'tcx, MiriMachine<'mir, 'tcx>>
401             for UnsafeCellVisitor<'ecx, 'mir, 'tcx, F>
402         where
403             F: FnMut(&MPlaceTy<'tcx, Provenance>) -> InterpResult<'tcx>,
404         {
405             type V = MPlaceTy<'tcx, Provenance>;
406
407             #[inline(always)]
408             fn ecx(&self) -> &MiriInterpCx<'mir, 'tcx> {
409                 self.ecx
410             }
411
412             // Hook to detect `UnsafeCell`.
413             fn visit_value(&mut self, v: &MPlaceTy<'tcx, Provenance>) -> InterpResult<'tcx> {
414                 trace!("UnsafeCellVisitor: {:?} {:?}", *v, v.layout.ty);
415                 let is_unsafe_cell = match v.layout.ty.kind() {
416                     ty::Adt(adt, _) =>
417                         Some(adt.did()) == self.ecx.tcx.lang_items().unsafe_cell_type(),
418                     _ => false,
419                 };
420                 if is_unsafe_cell {
421                     // We do not have to recurse further, this is an `UnsafeCell`.
422                     (self.unsafe_cell_action)(v)
423                 } else if self.ecx.type_is_freeze(v.layout.ty) {
424                     // This is `Freeze`, there cannot be an `UnsafeCell`
425                     Ok(())
426                 } else if matches!(v.layout.fields, FieldsShape::Union(..)) {
427                     // A (non-frozen) union. We fall back to whatever the type says.
428                     (self.unsafe_cell_action)(v)
429                 } else {
430                     // We want to not actually read from memory for this visit. So, before
431                     // walking this value, we have to make sure it is not a
432                     // `Variants::Multiple`.
433                     match v.layout.variants {
434                         Variants::Multiple { .. } => {
435                             // A multi-variant enum, or generator, or so.
436                             // Treat this like a union: without reading from memory,
437                             // we cannot determine the variant we are in. Reading from
438                             // memory would be subject to Stacked Borrows rules, leading
439                             // to all sorts of "funny" recursion.
440                             // We only end up here if the type is *not* freeze, so we just call the
441                             // `UnsafeCell` action.
442                             (self.unsafe_cell_action)(v)
443                         }
444                         Variants::Single { .. } => {
445                             // Proceed further, try to find where exactly that `UnsafeCell`
446                             // is hiding.
447                             self.walk_value(v)
448                         }
449                     }
450                 }
451             }
452
453             // Make sure we visit aggregrates in increasing offset order.
454             fn visit_aggregate(
455                 &mut self,
456                 place: &MPlaceTy<'tcx, Provenance>,
457                 fields: impl Iterator<Item = InterpResult<'tcx, MPlaceTy<'tcx, Provenance>>>,
458             ) -> InterpResult<'tcx> {
459                 match place.layout.fields {
460                     FieldsShape::Array { .. } => {
461                         // For the array layout, we know the iterator will yield sorted elements so
462                         // we can avoid the allocation.
463                         self.walk_aggregate(place, fields)
464                     }
465                     FieldsShape::Arbitrary { .. } => {
466                         // Gather the subplaces and sort them before visiting.
467                         let mut places = fields
468                             .collect::<InterpResult<'tcx, Vec<MPlaceTy<'tcx, Provenance>>>>()?;
469                         // we just compare offsets, the abs. value never matters
470                         places.sort_by_key(|place| place.ptr.addr());
471                         self.walk_aggregate(place, places.into_iter().map(Ok))
472                     }
473                     FieldsShape::Union { .. } | FieldsShape::Primitive => {
474                         // Uh, what?
475                         bug!("unions/primitives are not aggregates we should ever visit")
476                     }
477                 }
478             }
479
480             fn visit_union(
481                 &mut self,
482                 _v: &MPlaceTy<'tcx, Provenance>,
483                 _fields: NonZeroUsize,
484             ) -> InterpResult<'tcx> {
485                 bug!("we should have already handled unions in `visit_value`")
486             }
487         }
488     }
489
490     /// Helper function used inside the shims of foreign functions to check that isolation is
491     /// disabled. It returns an error using the `name` of the foreign function if this is not the
492     /// case.
493     fn check_no_isolation(&self, name: &str) -> InterpResult<'tcx> {
494         if !self.eval_context_ref().machine.communicate() {
495             self.reject_in_isolation(name, RejectOpWith::Abort)?;
496         }
497         Ok(())
498     }
499
500     /// Helper function used inside the shims of foreign functions which reject the op
501     /// when isolation is enabled. It is used to print a warning/backtrace about the rejection.
502     fn reject_in_isolation(&self, op_name: &str, reject_with: RejectOpWith) -> InterpResult<'tcx> {
503         let this = self.eval_context_ref();
504         match reject_with {
505             RejectOpWith::Abort => isolation_abort_error(op_name),
506             RejectOpWith::WarningWithoutBacktrace => {
507                 this.tcx
508                     .sess
509                     .warn(format!("{op_name} was made to return an error due to isolation"));
510                 Ok(())
511             }
512             RejectOpWith::Warning => {
513                 this.emit_diagnostic(NonHaltingDiagnostic::RejectedIsolatedOp(op_name.to_string()));
514                 Ok(())
515             }
516             RejectOpWith::NoWarning => Ok(()), // no warning
517         }
518     }
519
520     /// Helper function used inside the shims of foreign functions to assert that the target OS
521     /// is `target_os`. It panics showing a message with the `name` of the foreign function
522     /// if this is not the case.
523     fn assert_target_os(&self, target_os: &str, name: &str) {
524         assert_eq!(
525             self.eval_context_ref().tcx.sess.target.os,
526             target_os,
527             "`{}` is only available on the `{}` target OS",
528             name,
529             target_os,
530         )
531     }
532
533     /// Helper function used inside the shims of foreign functions to assert that the target OS
534     /// is part of the UNIX family. It panics showing a message with the `name` of the foreign function
535     /// if this is not the case.
536     fn assert_target_os_is_unix(&self, name: &str) {
537         assert!(
538             target_os_is_unix(self.eval_context_ref().tcx.sess.target.os.as_ref()),
539             "`{}` is only available for supported UNIX family targets",
540             name,
541         );
542     }
543
544     /// Get last error variable as a place, lazily allocating thread-local storage for it if
545     /// necessary.
546     fn last_error_place(&mut self) -> InterpResult<'tcx, MPlaceTy<'tcx, Provenance>> {
547         let this = self.eval_context_mut();
548         if let Some(errno_place) = this.active_thread_ref().last_error {
549             Ok(errno_place)
550         } else {
551             // Allocate new place, set initial value to 0.
552             let errno_layout = this.machine.layouts.u32;
553             let errno_place = this.allocate(errno_layout, MiriMemoryKind::Machine.into())?;
554             this.write_scalar(Scalar::from_u32(0), &errno_place.into())?;
555             this.active_thread_mut().last_error = Some(errno_place);
556             Ok(errno_place)
557         }
558     }
559
560     /// Sets the last error variable.
561     fn set_last_error(&mut self, scalar: Scalar<Provenance>) -> InterpResult<'tcx> {
562         let this = self.eval_context_mut();
563         let errno_place = this.last_error_place()?;
564         this.write_scalar(scalar, &errno_place.into())
565     }
566
567     /// Gets the last error variable.
568     fn get_last_error(&mut self) -> InterpResult<'tcx, Scalar<Provenance>> {
569         let this = self.eval_context_mut();
570         let errno_place = this.last_error_place()?;
571         this.read_scalar(&errno_place.into())
572     }
573
574     /// This function tries to produce the most similar OS error from the `std::io::ErrorKind`
575     /// as a platform-specific errnum.
576     fn io_error_to_errnum(
577         &self,
578         err_kind: std::io::ErrorKind,
579     ) -> InterpResult<'tcx, Scalar<Provenance>> {
580         let this = self.eval_context_ref();
581         let target = &this.tcx.sess.target;
582         if target.families.iter().any(|f| f == "unix") {
583             for &(name, kind) in UNIX_IO_ERROR_TABLE {
584                 if err_kind == kind {
585                     return this.eval_libc(name);
586                 }
587             }
588             throw_unsup_format!("io error {:?} cannot be translated into a raw os error", err_kind)
589         } else if target.families.iter().any(|f| f == "windows") {
590             // FIXME: we have to finish implementing the Windows equivalent of this.
591             use std::io::ErrorKind::*;
592             this.eval_windows(
593                 "c",
594                 match err_kind {
595                     NotFound => "ERROR_FILE_NOT_FOUND",
596                     PermissionDenied => "ERROR_ACCESS_DENIED",
597                     _ =>
598                         throw_unsup_format!(
599                             "io error {:?} cannot be translated into a raw os error",
600                             err_kind
601                         ),
602                 },
603             )
604         } else {
605             throw_unsup_format!(
606                 "converting io::Error into errnum is unsupported for OS {}",
607                 target.os
608             )
609         }
610     }
611
612     /// The inverse of `io_error_to_errnum`.
613     #[allow(clippy::needless_return)]
614     fn try_errnum_to_io_error(
615         &self,
616         errnum: Scalar<Provenance>,
617     ) -> InterpResult<'tcx, Option<std::io::ErrorKind>> {
618         let this = self.eval_context_ref();
619         let target = &this.tcx.sess.target;
620         if target.families.iter().any(|f| f == "unix") {
621             let errnum = errnum.to_i32()?;
622             for &(name, kind) in UNIX_IO_ERROR_TABLE {
623                 if errnum == this.eval_libc_i32(name)? {
624                     return Ok(Some(kind));
625                 }
626             }
627             // Our table is as complete as the mapping in std, so we are okay with saying "that's a
628             // strange one" here.
629             return Ok(None);
630         } else {
631             throw_unsup_format!(
632                 "converting errnum into io::Error is unsupported for OS {}",
633                 target.os
634             )
635         }
636     }
637
638     /// Sets the last OS error using a `std::io::ErrorKind`.
639     fn set_last_error_from_io_error(&mut self, err_kind: std::io::ErrorKind) -> InterpResult<'tcx> {
640         self.set_last_error(self.io_error_to_errnum(err_kind)?)
641     }
642
643     /// Helper function that consumes an `std::io::Result<T>` and returns an
644     /// `InterpResult<'tcx,T>::Ok` instead. In case the result is an error, this function returns
645     /// `Ok(-1)` and sets the last OS error accordingly.
646     ///
647     /// This function uses `T: From<i32>` instead of `i32` directly because some IO related
648     /// functions return different integer types (like `read`, that returns an `i64`).
649     fn try_unwrap_io_result<T: From<i32>>(
650         &mut self,
651         result: std::io::Result<T>,
652     ) -> InterpResult<'tcx, T> {
653         match result {
654             Ok(ok) => Ok(ok),
655             Err(e) => {
656                 self.eval_context_mut().set_last_error_from_io_error(e.kind())?;
657                 Ok((-1).into())
658             }
659         }
660     }
661
662     /// Calculates the MPlaceTy given the offset and layout of an access on an operand
663     fn deref_operand_and_offset(
664         &self,
665         op: &OpTy<'tcx, Provenance>,
666         offset: u64,
667         layout: TyAndLayout<'tcx>,
668     ) -> InterpResult<'tcx, MPlaceTy<'tcx, Provenance>> {
669         let this = self.eval_context_ref();
670         let op_place = this.deref_operand(op)?;
671         let offset = Size::from_bytes(offset);
672
673         // Ensure that the access is within bounds.
674         assert!(op_place.layout.size >= offset + layout.size);
675         let value_place = op_place.offset(offset, layout, this)?;
676         Ok(value_place)
677     }
678
679     fn read_scalar_at_offset(
680         &self,
681         op: &OpTy<'tcx, Provenance>,
682         offset: u64,
683         layout: TyAndLayout<'tcx>,
684     ) -> InterpResult<'tcx, Scalar<Provenance>> {
685         let this = self.eval_context_ref();
686         let value_place = this.deref_operand_and_offset(op, offset, layout)?;
687         this.read_scalar(&value_place.into())
688     }
689
690     fn write_immediate_at_offset(
691         &mut self,
692         op: &OpTy<'tcx, Provenance>,
693         offset: u64,
694         value: &ImmTy<'tcx, Provenance>,
695     ) -> InterpResult<'tcx, ()> {
696         let this = self.eval_context_mut();
697         let value_place = this.deref_operand_and_offset(op, offset, value.layout)?;
698         this.write_immediate(**value, &value_place.into())
699     }
700
701     fn write_scalar_at_offset(
702         &mut self,
703         op: &OpTy<'tcx, Provenance>,
704         offset: u64,
705         value: impl Into<Scalar<Provenance>>,
706         layout: TyAndLayout<'tcx>,
707     ) -> InterpResult<'tcx, ()> {
708         self.write_immediate_at_offset(op, offset, &ImmTy::from_scalar(value.into(), layout))
709     }
710
711     /// Parse a `timespec` struct and return it as a `std::time::Duration`. It returns `None`
712     /// if the value in the `timespec` struct is invalid. Some libc functions will return
713     /// `EINVAL` in this case.
714     fn read_timespec(
715         &mut self,
716         tp: &MPlaceTy<'tcx, Provenance>,
717     ) -> InterpResult<'tcx, Option<Duration>> {
718         let this = self.eval_context_mut();
719         let seconds_place = this.mplace_field(tp, 0)?;
720         let seconds_scalar = this.read_scalar(&seconds_place.into())?;
721         let seconds = seconds_scalar.to_machine_isize(this)?;
722         let nanoseconds_place = this.mplace_field(tp, 1)?;
723         let nanoseconds_scalar = this.read_scalar(&nanoseconds_place.into())?;
724         let nanoseconds = nanoseconds_scalar.to_machine_isize(this)?;
725
726         Ok(try {
727             // tv_sec must be non-negative.
728             let seconds: u64 = seconds.try_into().ok()?;
729             // tv_nsec must be non-negative.
730             let nanoseconds: u32 = nanoseconds.try_into().ok()?;
731             if nanoseconds >= 1_000_000_000 {
732                 // tv_nsec must not be greater than 999,999,999.
733                 None?
734             }
735             Duration::new(seconds, nanoseconds)
736         })
737     }
738
739     /// Read a sequence of bytes until the first null terminator.
740     fn read_c_str<'a>(&'a self, ptr: Pointer<Option<Provenance>>) -> InterpResult<'tcx, &'a [u8]>
741     where
742         'tcx: 'a,
743         'mir: 'a,
744     {
745         let this = self.eval_context_ref();
746         let size1 = Size::from_bytes(1);
747
748         // Step 1: determine the length.
749         let mut len = Size::ZERO;
750         loop {
751             // FIXME: We are re-getting the allocation each time around the loop.
752             // Would be nice if we could somehow "extend" an existing AllocRange.
753             let alloc = this.get_ptr_alloc(ptr.offset(len, this)?, size1, Align::ONE)?.unwrap(); // not a ZST, so we will get a result
754             let byte = alloc.read_integer(alloc_range(Size::ZERO, size1))?.to_u8()?;
755             if byte == 0 {
756                 break;
757             } else {
758                 len += size1;
759             }
760         }
761
762         // Step 2: get the bytes.
763         this.read_bytes_ptr_strip_provenance(ptr, len)
764     }
765
766     /// Helper function to write a sequence of bytes with an added null-terminator, which is what
767     /// the Unix APIs usually handle. This function returns `Ok((false, length))` without trying
768     /// to write if `size` is not large enough to fit the contents of `c_str` plus a null
769     /// terminator. It returns `Ok((true, length))` if the writing process was successful. The
770     /// string length returned does include the null terminator.
771     fn write_c_str(
772         &mut self,
773         c_str: &[u8],
774         ptr: Pointer<Option<Provenance>>,
775         size: u64,
776     ) -> InterpResult<'tcx, (bool, u64)> {
777         // If `size` is smaller or equal than `bytes.len()`, writing `bytes` plus the required null
778         // terminator to memory using the `ptr` pointer would cause an out-of-bounds access.
779         let string_length = u64::try_from(c_str.len()).unwrap();
780         let string_length = string_length.checked_add(1).unwrap();
781         if size < string_length {
782             return Ok((false, string_length));
783         }
784         self.eval_context_mut()
785             .write_bytes_ptr(ptr, c_str.iter().copied().chain(iter::once(0u8)))?;
786         Ok((true, string_length))
787     }
788
789     /// Read a sequence of u16 until the first null terminator.
790     fn read_wide_str(&self, mut ptr: Pointer<Option<Provenance>>) -> InterpResult<'tcx, Vec<u16>> {
791         let this = self.eval_context_ref();
792         let size2 = Size::from_bytes(2);
793         let align2 = Align::from_bytes(2).unwrap();
794
795         let mut wchars = Vec::new();
796         loop {
797             // FIXME: We are re-getting the allocation each time around the loop.
798             // Would be nice if we could somehow "extend" an existing AllocRange.
799             let alloc = this.get_ptr_alloc(ptr, size2, align2)?.unwrap(); // not a ZST, so we will get a result
800             let wchar = alloc.read_integer(alloc_range(Size::ZERO, size2))?.to_u16()?;
801             if wchar == 0 {
802                 break;
803             } else {
804                 wchars.push(wchar);
805                 ptr = ptr.offset(size2, this)?;
806             }
807         }
808
809         Ok(wchars)
810     }
811
812     /// Helper function to write a sequence of u16 with an added 0x0000-terminator, which is what
813     /// the Windows APIs usually handle. This function returns `Ok((false, length))` without trying
814     /// to write if `size` is not large enough to fit the contents of `os_string` plus a null
815     /// terminator. It returns `Ok((true, length))` if the writing process was successful. The
816     /// string length returned does include the null terminator. Length is measured in units of
817     /// `u16.`
818     fn write_wide_str(
819         &mut self,
820         wide_str: &[u16],
821         ptr: Pointer<Option<Provenance>>,
822         size: u64,
823     ) -> InterpResult<'tcx, (bool, u64)> {
824         // If `size` is smaller or equal than `bytes.len()`, writing `bytes` plus the required
825         // 0x0000 terminator to memory would cause an out-of-bounds access.
826         let string_length = u64::try_from(wide_str.len()).unwrap();
827         let string_length = string_length.checked_add(1).unwrap();
828         if size < string_length {
829             return Ok((false, string_length));
830         }
831
832         // Store the UTF-16 string.
833         let size2 = Size::from_bytes(2);
834         let this = self.eval_context_mut();
835         let mut alloc = this
836             .get_ptr_alloc_mut(ptr, size2 * string_length, Align::from_bytes(2).unwrap())?
837             .unwrap(); // not a ZST, so we will get a result
838         for (offset, wchar) in wide_str.iter().copied().chain(iter::once(0x0000)).enumerate() {
839             let offset = u64::try_from(offset).unwrap();
840             alloc.write_scalar(alloc_range(size2 * offset, size2), Scalar::from_u16(wchar))?;
841         }
842         Ok((true, string_length))
843     }
844
845     /// Check that the ABI is what we expect.
846     fn check_abi<'a>(&self, abi: Abi, exp_abi: Abi) -> InterpResult<'a, ()> {
847         if self.eval_context_ref().machine.enforce_abi && abi != exp_abi {
848             throw_ub_format!(
849                 "calling a function with ABI {} using caller ABI {}",
850                 exp_abi.name(),
851                 abi.name()
852             )
853         }
854         Ok(())
855     }
856
857     fn frame_in_std(&self) -> bool {
858         let this = self.eval_context_ref();
859         let Some(start_fn) = this.tcx.lang_items().start_fn() else {
860             // no_std situations
861             return false;
862         };
863         let frame = this.frame();
864         // Make an attempt to get at the instance of the function this is inlined from.
865         let instance: Option<_> = try {
866             let scope = frame.current_source_info()?.scope;
867             let inlined_parent = frame.body.source_scopes[scope].inlined_parent_scope?;
868             let source = &frame.body.source_scopes[inlined_parent];
869             source.inlined.expect("inlined_parent_scope points to scope without inline info").0
870         };
871         // Fall back to the instance of the function itself.
872         let instance = instance.unwrap_or(frame.instance);
873         // Now check if this is in the same crate as start_fn.
874         // As a special exception we also allow unit tests from
875         // <https://github.com/rust-lang/miri-test-libstd/tree/master/std_miri_test> to call these
876         // shims.
877         let frame_crate = this.tcx.def_path(instance.def_id()).krate;
878         frame_crate == this.tcx.def_path(start_fn).krate
879             || this.tcx.crate_name(frame_crate).as_str() == "std_miri_test"
880     }
881
882     /// Handler that should be called when unsupported functionality is encountered.
883     /// This function will either panic within the context of the emulated application
884     /// or return an error in the Miri process context
885     ///
886     /// Return value of `Ok(bool)` indicates whether execution should continue.
887     fn handle_unsupported<S: AsRef<str>>(&mut self, error_msg: S) -> InterpResult<'tcx, ()> {
888         let this = self.eval_context_mut();
889         if this.machine.panic_on_unsupported {
890             // message is slightly different here to make automated analysis easier
891             let error_msg = format!("unsupported Miri functionality: {}", error_msg.as_ref());
892             this.start_panic(error_msg.as_ref(), StackPopUnwind::Skip)?;
893             Ok(())
894         } else {
895             throw_unsup_format!("{}", error_msg.as_ref());
896         }
897     }
898
899     fn check_abi_and_shim_symbol_clash(
900         &mut self,
901         abi: Abi,
902         exp_abi: Abi,
903         link_name: Symbol,
904     ) -> InterpResult<'tcx, ()> {
905         self.check_abi(abi, exp_abi)?;
906         if let Some((body, _)) = self.eval_context_mut().lookup_exported_symbol(link_name)? {
907             throw_machine_stop!(TerminationInfo::SymbolShimClashing {
908                 link_name,
909                 span: body.span.data(),
910             })
911         }
912         Ok(())
913     }
914
915     fn check_shim<'a, const N: usize>(
916         &mut self,
917         abi: Abi,
918         exp_abi: Abi,
919         link_name: Symbol,
920         args: &'a [OpTy<'tcx, Provenance>],
921     ) -> InterpResult<'tcx, &'a [OpTy<'tcx, Provenance>; N]>
922     where
923         &'a [OpTy<'tcx, Provenance>; N]: TryFrom<&'a [OpTy<'tcx, Provenance>]>,
924     {
925         self.check_abi_and_shim_symbol_clash(abi, exp_abi, link_name)?;
926         check_arg_count(args)
927     }
928
929     /// Mark a machine allocation that was just created as immutable.
930     fn mark_immutable(&mut self, mplace: &MemPlace<Provenance>) {
931         let this = self.eval_context_mut();
932         // This got just allocated, so there definitely is a pointer here.
933         let provenance = mplace.ptr.into_pointer_or_addr().unwrap().provenance;
934         this.alloc_mark_immutable(provenance.get_alloc_id().unwrap()).unwrap();
935     }
936
937     fn item_link_name(&self, def_id: DefId) -> Symbol {
938         let tcx = self.eval_context_ref().tcx;
939         match tcx.get_attrs(def_id, sym::link_name).filter_map(|a| a.value_str()).next() {
940             Some(name) => name,
941             None => tcx.item_name(def_id),
942         }
943     }
944 }
945
946 impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {
947     pub fn current_span(&self) -> CurrentSpan<'_, 'mir, 'tcx> {
948         CurrentSpan { current_frame_idx: None, machine: self }
949     }
950 }
951
952 /// A `CurrentSpan` should be created infrequently (ideally once) per interpreter step. It does
953 /// nothing on creation, but when `CurrentSpan::get` is called, searches the current stack for the
954 /// topmost frame which corresponds to a local crate, and returns the current span in that frame.
955 /// The result of that search is cached so that later calls are approximately free.
956 #[derive(Clone)]
957 pub struct CurrentSpan<'a, 'mir, 'tcx> {
958     current_frame_idx: Option<usize>,
959     machine: &'a MiriMachine<'mir, 'tcx>,
960 }
961
962 impl<'a, 'mir: 'a, 'tcx: 'a + 'mir> CurrentSpan<'a, 'mir, 'tcx> {
963     pub fn machine(&self) -> &'a MiriMachine<'mir, 'tcx> {
964         self.machine
965     }
966
967     /// Get the current span, skipping non-local frames.
968     /// This function is backed by a cache, and can be assumed to be very fast.
969     pub fn get(&mut self) -> Span {
970         let idx = self.current_frame_idx();
971         self.stack().get(idx).map(Frame::current_span).unwrap_or(rustc_span::DUMMY_SP)
972     }
973
974     /// Returns the span of the *caller* of the current operation, again
975     /// walking down the stack to find the closest frame in a local crate, if the caller of the
976     /// current operation is not in a local crate.
977     /// This is useful when we are processing something which occurs on function-entry and we want
978     /// to point at the call to the function, not the function definition generally.
979     pub fn get_caller(&mut self) -> Span {
980         // We need to go down at least to the caller (len - 2), or however
981         // far we have to go to find a frame in a local crate.
982         let local_frame_idx = self.current_frame_idx();
983         let stack = self.stack();
984         let idx = cmp::min(local_frame_idx, stack.len().saturating_sub(2));
985         stack.get(idx).map(Frame::current_span).unwrap_or(rustc_span::DUMMY_SP)
986     }
987
988     fn stack(&self) -> &[Frame<'mir, 'tcx, Provenance, machine::FrameData<'tcx>>] {
989         self.machine.threads.active_thread_stack()
990     }
991
992     fn current_frame_idx(&mut self) -> usize {
993         *self
994             .current_frame_idx
995             .get_or_insert_with(|| Self::compute_current_frame_index(self.machine))
996     }
997
998     // Find the position of the inner-most frame which is part of the crate being
999     // compiled/executed, part of the Cargo workspace, and is also not #[track_caller].
1000     #[inline(never)]
1001     fn compute_current_frame_index(machine: &MiriMachine<'_, '_>) -> usize {
1002         machine
1003             .threads
1004             .active_thread_stack()
1005             .iter()
1006             .enumerate()
1007             .rev()
1008             .find_map(|(idx, frame)| {
1009                 let def_id = frame.instance.def_id();
1010                 if (def_id.is_local() || machine.local_crates.contains(&def_id.krate))
1011                     && !frame.instance.def.requires_caller_location(machine.tcx)
1012                 {
1013                     Some(idx)
1014                 } else {
1015                     None
1016                 }
1017             })
1018             .unwrap_or(0)
1019     }
1020 }
1021
1022 /// Check that the number of args is what we expect.
1023 pub fn check_arg_count<'a, 'tcx, const N: usize>(
1024     args: &'a [OpTy<'tcx, Provenance>],
1025 ) -> InterpResult<'tcx, &'a [OpTy<'tcx, Provenance>; N]>
1026 where
1027     &'a [OpTy<'tcx, Provenance>; N]: TryFrom<&'a [OpTy<'tcx, Provenance>]>,
1028 {
1029     if let Ok(ops) = args.try_into() {
1030         return Ok(ops);
1031     }
1032     throw_ub_format!("incorrect number of arguments: got {}, expected {}", args.len(), N)
1033 }
1034
1035 pub fn isolation_abort_error<'tcx>(name: &str) -> InterpResult<'tcx> {
1036     throw_machine_stop!(TerminationInfo::UnsupportedInIsolation(format!(
1037         "{} not available when isolation is enabled",
1038         name,
1039     )))
1040 }
1041
1042 /// Retrieve the list of local crates that should have been passed by cargo-miri in
1043 /// MIRI_LOCAL_CRATES and turn them into `CrateNum`s.
1044 pub fn get_local_crates(tcx: TyCtxt<'_>) -> Vec<CrateNum> {
1045     // Convert the local crate names from the passed-in config into CrateNums so that they can
1046     // be looked up quickly during execution
1047     let local_crate_names = std::env::var("MIRI_LOCAL_CRATES")
1048         .map(|crates| crates.split(',').map(|krate| krate.to_string()).collect::<Vec<_>>())
1049         .unwrap_or_default();
1050     let mut local_crates = Vec::new();
1051     for &crate_num in tcx.crates(()) {
1052         let name = tcx.crate_name(crate_num);
1053         let name = name.as_str();
1054         if local_crate_names.iter().any(|local_name| local_name == name) {
1055             local_crates.push(crate_num);
1056         }
1057     }
1058     local_crates
1059 }
1060
1061 /// Helper function used inside the shims of foreign functions to check that
1062 /// `target_os` is a supported UNIX OS.
1063 pub fn target_os_is_unix(target_os: &str) -> bool {
1064     matches!(target_os, "linux" | "macos" | "freebsd" | "android")
1065 }