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