miri/
helpers.rs

1use std::num::NonZero;
2use std::sync::Mutex;
3use std::time::Duration;
4use std::{cmp, iter};
5
6use rand::RngCore;
7use rustc_abi::{Align, ExternAbi, FieldIdx, FieldsShape, Size, Variants};
8use rustc_apfloat::Float;
9use rustc_data_structures::fx::{FxBuildHasher, FxHashSet};
10use rustc_hir::Safety;
11use rustc_hir::def::{DefKind, Namespace};
12use rustc_hir::def_id::{CRATE_DEF_INDEX, CrateNum, DefId, LOCAL_CRATE};
13use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
14use rustc_middle::middle::dependency_format::Linkage;
15use rustc_middle::middle::exported_symbols::ExportedSymbol;
16use rustc_middle::ty::layout::{LayoutOf, MaybeResult, TyAndLayout};
17use rustc_middle::ty::{self, IntTy, Ty, TyCtxt, UintTy};
18use rustc_session::config::CrateType;
19use rustc_span::{Span, Symbol};
20use rustc_symbol_mangling::mangle_internal_symbol;
21use rustc_target::spec::Os;
22
23use crate::*;
24
25/// Gets an instance for a path.
26///
27/// A `None` namespace indicates we are looking for a module.
28fn try_resolve_did(tcx: TyCtxt<'_>, path: &[&str], namespace: Option<Namespace>) -> Option<DefId> {
29    let _trace = enter_trace_span!("try_resolve_did", ?path);
30
31    /// Yield all children of the given item, that have the given name.
32    fn find_children<'tcx: 'a, 'a>(
33        tcx: TyCtxt<'tcx>,
34        item: DefId,
35        name: &'a str,
36    ) -> impl Iterator<Item = DefId> + 'a {
37        let name = Symbol::intern(name);
38        tcx.module_children(item)
39            .iter()
40            .filter(move |item| item.ident.name == name)
41            .map(move |item| item.res.def_id())
42    }
43
44    // Take apart the path: leading crate, a sequence of modules, and potentially a final item.
45    let (&crate_name, path) = path.split_first().expect("paths must have at least one segment");
46    let (modules, item) = if let Some(namespace) = namespace {
47        let (&item_name, modules) =
48            path.split_last().expect("non-module paths must have at least 2 segments");
49        (modules, Some((item_name, namespace)))
50    } else {
51        (path, None)
52    };
53
54    // There may be more than one crate with this name. We try them all.
55    // (This is particularly relevant when running `std` tests as then there are two `std` crates:
56    // the one in the sysroot and the one locally built by `cargo test`.)
57    // FIXME: can we prefer the one from the sysroot?
58    'crates: for krate in
59        tcx.crates(()).iter().filter(|&&krate| tcx.crate_name(krate).as_str() == crate_name)
60    {
61        let mut cur_item = DefId { krate: *krate, index: CRATE_DEF_INDEX };
62        // Go over the modules.
63        for &segment in modules {
64            let Some(next_item) = find_children(tcx, cur_item, segment)
65                .find(|item| tcx.def_kind(item) == DefKind::Mod)
66            else {
67                continue 'crates;
68            };
69            cur_item = next_item;
70        }
71        // Finally, look up the desired item in this module, if any.
72        match item {
73            Some((item_name, namespace)) => {
74                let Some(item) = find_children(tcx, cur_item, item_name)
75                    .find(|item| tcx.def_kind(item).ns() == Some(namespace))
76                else {
77                    continue 'crates;
78                };
79                return Some(item);
80            }
81            None => {
82                // Just return the module.
83                return Some(cur_item);
84            }
85        }
86    }
87    // Item not found in any of the crates with the right name.
88    None
89}
90
91/// Gets an instance for a path; fails gracefully if the path does not exist.
92pub fn try_resolve_path<'tcx>(
93    tcx: TyCtxt<'tcx>,
94    path: &[&str],
95    namespace: Namespace,
96) -> Option<ty::Instance<'tcx>> {
97    let did = try_resolve_did(tcx, path, Some(namespace))?;
98    Some(ty::Instance::mono(tcx, did))
99}
100
101/// Gets an instance for a path.
102#[track_caller]
103pub fn resolve_path<'tcx>(
104    tcx: TyCtxt<'tcx>,
105    path: &[&str],
106    namespace: Namespace,
107) -> ty::Instance<'tcx> {
108    try_resolve_path(tcx, path, namespace)
109        .unwrap_or_else(|| panic!("failed to find required Rust item: {path:?}"))
110}
111
112/// Gets the layout of a type at a path.
113#[track_caller]
114pub fn path_ty_layout<'tcx>(cx: &impl LayoutOf<'tcx>, path: &[&str]) -> TyAndLayout<'tcx> {
115    let ty = resolve_path(cx.tcx(), path, Namespace::TypeNS).ty(cx.tcx(), cx.typing_env());
116    cx.layout_of(ty).to_result().ok().unwrap()
117}
118
119/// Call `f` for each exported symbol.
120pub fn iter_exported_symbols<'tcx>(
121    tcx: TyCtxt<'tcx>,
122    mut f: impl FnMut(CrateNum, DefId) -> InterpResult<'tcx>,
123) -> InterpResult<'tcx> {
124    // First, the symbols in the local crate. We can't use `exported_symbols` here as that
125    // skips `#[used]` statics (since `reachable_set` skips them in binary crates).
126    // So we walk all HIR items ourselves instead.
127    let crate_items = tcx.hir_crate_items(());
128    for def_id in crate_items.definitions() {
129        let exported = tcx.def_kind(def_id).has_codegen_attrs() && {
130            let codegen_attrs = tcx.codegen_fn_attrs(def_id);
131            codegen_attrs.contains_extern_indicator()
132                || codegen_attrs.flags.contains(CodegenFnAttrFlags::USED_COMPILER)
133                || codegen_attrs.flags.contains(CodegenFnAttrFlags::USED_LINKER)
134        };
135        if exported {
136            f(LOCAL_CRATE, def_id.into())?;
137        }
138    }
139
140    // Next, all our dependencies.
141    // `dependency_formats` includes all the transitive informations needed to link a crate,
142    // which is what we need here since we need to dig out `exported_symbols` from all transitive
143    // dependencies.
144    let dependency_formats = tcx.dependency_formats(());
145    // Find the dependencies of the executable we are running.
146    let dependency_format = dependency_formats
147        .get(&CrateType::Executable)
148        .expect("interpreting a non-executable crate");
149    for cnum in dependency_format
150        .iter_enumerated()
151        .filter_map(|(num, &linkage)| (linkage != Linkage::NotLinked).then_some(num))
152    {
153        if cnum == LOCAL_CRATE {
154            continue; // Already handled above
155        }
156
157        // We can ignore `_export_info` here: we are a Rust crate, and everything is exported
158        // from a Rust crate.
159        for &(symbol, _export_info) in tcx.exported_non_generic_symbols(cnum) {
160            if let ExportedSymbol::NonGeneric(def_id) = symbol {
161                f(cnum, def_id)?;
162            }
163        }
164    }
165    interp_ok(())
166}
167
168/// Convert a softfloat type to its corresponding hostfloat type.
169pub trait ToHost {
170    type HostFloat;
171    fn to_host(self) -> Self::HostFloat;
172}
173
174/// Convert a hostfloat type to its corresponding softfloat type.
175pub trait ToSoft {
176    type SoftFloat;
177    fn to_soft(self) -> Self::SoftFloat;
178}
179
180impl ToHost for rustc_apfloat::ieee::Double {
181    type HostFloat = f64;
182
183    fn to_host(self) -> Self::HostFloat {
184        f64::from_bits(self.to_bits().try_into().unwrap())
185    }
186}
187
188impl ToSoft for f64 {
189    type SoftFloat = rustc_apfloat::ieee::Double;
190
191    fn to_soft(self) -> Self::SoftFloat {
192        Float::from_bits(self.to_bits().into())
193    }
194}
195
196impl ToHost for rustc_apfloat::ieee::Single {
197    type HostFloat = f32;
198
199    fn to_host(self) -> Self::HostFloat {
200        f32::from_bits(self.to_bits().try_into().unwrap())
201    }
202}
203
204impl ToSoft for f32 {
205    type SoftFloat = rustc_apfloat::ieee::Single;
206
207    fn to_soft(self) -> Self::SoftFloat {
208        Float::from_bits(self.to_bits().into())
209    }
210}
211
212impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
213pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
214    /// Checks if the given crate/module exists.
215    fn have_module(&self, path: &[&str]) -> bool {
216        try_resolve_did(*self.eval_context_ref().tcx, path, None).is_some()
217    }
218
219    /// Evaluates the scalar at the specified path.
220    fn eval_path(&self, path: &[&str]) -> MPlaceTy<'tcx> {
221        let this = self.eval_context_ref();
222        let instance = resolve_path(*this.tcx, path, Namespace::ValueNS);
223        // We don't give a span -- this isn't actually used directly by the program anyway.
224        this.eval_global(instance).unwrap_or_else(|err| {
225            panic!("failed to evaluate required Rust item: {path:?}\n{err:?}")
226        })
227    }
228    fn eval_path_scalar(&self, path: &[&str]) -> Scalar {
229        let this = self.eval_context_ref();
230        let val = this.eval_path(path);
231        this.read_scalar(&val)
232            .unwrap_or_else(|err| panic!("failed to read required Rust item: {path:?}\n{err:?}"))
233    }
234
235    /// Helper function to get a `libc` constant as a `Scalar`.
236    fn eval_libc(&self, name: &str) -> Scalar {
237        if self.eval_context_ref().tcx.sess.target.os == Os::Windows {
238            panic!(
239                "`libc` crate is not reliably available on Windows targets; Miri should not use it there"
240            );
241        }
242        self.eval_path_scalar(&["libc", name])
243    }
244
245    /// Helper function to get a `libc` constant as an `i32`.
246    fn eval_libc_i32(&self, name: &str) -> i32 {
247        // TODO: Cache the result.
248        self.eval_libc(name).to_i32().unwrap_or_else(|_err| {
249            panic!("required libc item has unexpected type (not `i32`): {name}")
250        })
251    }
252
253    /// Helper function to get a `libc` constant as an `u32`.
254    fn eval_libc_u32(&self, name: &str) -> u32 {
255        // TODO: Cache the result.
256        self.eval_libc(name).to_u32().unwrap_or_else(|_err| {
257            panic!("required libc item has unexpected type (not `u32`): {name}")
258        })
259    }
260
261    /// Helper function to get a `libc` constant as an `u64`.
262    fn eval_libc_u64(&self, name: &str) -> u64 {
263        // TODO: Cache the result.
264        self.eval_libc(name).to_u64().unwrap_or_else(|_err| {
265            panic!("required libc item has unexpected type (not `u64`): {name}")
266        })
267    }
268
269    /// Helper function to get a `windows` constant as a `Scalar`.
270    fn eval_windows(&self, module: &str, name: &str) -> Scalar {
271        self.eval_context_ref().eval_path_scalar(&["std", "sys", "pal", "windows", module, name])
272    }
273
274    /// Helper function to get a `windows` constant as a `u32`.
275    fn eval_windows_u32(&self, module: &str, name: &str) -> u32 {
276        // TODO: Cache the result.
277        self.eval_windows(module, name).to_u32().unwrap_or_else(|_err| {
278            panic!("required Windows item has unexpected type (not `u32`): {module}::{name}")
279        })
280    }
281
282    /// Helper function to get a `windows` constant as a `u64`.
283    fn eval_windows_u64(&self, module: &str, name: &str) -> u64 {
284        // TODO: Cache the result.
285        self.eval_windows(module, name).to_u64().unwrap_or_else(|_err| {
286            panic!("required Windows item has unexpected type (not `u64`): {module}::{name}")
287        })
288    }
289
290    /// Helper function to get the `TyAndLayout` of a `libc` type
291    fn libc_ty_layout(&self, name: &str) -> TyAndLayout<'tcx> {
292        let this = self.eval_context_ref();
293        if this.tcx.sess.target.os == Os::Windows {
294            panic!(
295                "`libc` crate is not reliably available on Windows targets; Miri should not use it there"
296            );
297        }
298        path_ty_layout(this, &["libc", name])
299    }
300
301    /// Helper function to get the `TyAndLayout` of a `windows` type
302    fn windows_ty_layout(&self, name: &str) -> TyAndLayout<'tcx> {
303        let this = self.eval_context_ref();
304        path_ty_layout(this, &["std", "sys", "pal", "windows", "c", name])
305    }
306
307    /// Helper function to get `TyAndLayout` of an array that consists of `libc` type.
308    fn libc_array_ty_layout(&self, name: &str, size: u64) -> TyAndLayout<'tcx> {
309        let this = self.eval_context_ref();
310        let elem_ty_layout = this.libc_ty_layout(name);
311        let array_ty = Ty::new_array(*this.tcx, elem_ty_layout.ty, size);
312        this.layout_of(array_ty).unwrap()
313    }
314
315    /// Project to the given *named* field (which must be a struct or union type).
316    fn try_project_field_named<P: Projectable<'tcx, Provenance>>(
317        &self,
318        base: &P,
319        name: &str,
320    ) -> InterpResult<'tcx, Option<P>> {
321        let this = self.eval_context_ref();
322        let adt = base.layout().ty.ty_adt_def().unwrap();
323        for (idx, field) in adt.non_enum_variant().fields.iter_enumerated() {
324            if field.name.as_str() == name {
325                return interp_ok(Some(this.project_field(base, idx)?));
326            }
327        }
328        interp_ok(None)
329    }
330
331    /// Project to the given *named* field (which must be a struct or union type).
332    fn project_field_named<P: Projectable<'tcx, Provenance>>(
333        &self,
334        base: &P,
335        name: &str,
336    ) -> InterpResult<'tcx, P> {
337        interp_ok(
338            self.try_project_field_named(base, name)?
339                .unwrap_or_else(|| bug!("no field named {} in type {}", name, base.layout().ty)),
340        )
341    }
342
343    /// Write an int of the appropriate size to `dest`. The target type may be signed or unsigned,
344    /// we try to do the right thing anyway. `i128` can fit all integer types except for `u128` so
345    /// this method is fine for almost all integer types.
346    fn write_int(
347        &mut self,
348        i: impl Into<i128>,
349        dest: &impl Writeable<'tcx, Provenance>,
350    ) -> InterpResult<'tcx> {
351        assert!(
352            dest.layout().backend_repr.is_scalar(),
353            "write_int on non-scalar type {}",
354            dest.layout().ty
355        );
356        let val = if dest.layout().backend_repr.is_signed() {
357            Scalar::from_int(i, dest.layout().size)
358        } else {
359            // `unwrap` can only fail here if `i` is negative
360            Scalar::from_uint(u128::try_from(i.into()).unwrap(), dest.layout().size)
361        };
362        self.eval_context_mut().write_scalar(val, dest)
363    }
364
365    /// Write the first N fields of the given place.
366    fn write_int_fields(
367        &mut self,
368        values: &[i128],
369        dest: &impl Writeable<'tcx, Provenance>,
370    ) -> InterpResult<'tcx> {
371        let this = self.eval_context_mut();
372        for (idx, &val) in values.iter().enumerate() {
373            let idx = FieldIdx::from_usize(idx);
374            let field = this.project_field(dest, idx)?;
375            this.write_int(val, &field)?;
376        }
377        interp_ok(())
378    }
379
380    /// Write the given fields of the given place.
381    fn write_int_fields_named(
382        &mut self,
383        values: &[(&str, i128)],
384        dest: &impl Writeable<'tcx, Provenance>,
385    ) -> InterpResult<'tcx> {
386        let this = self.eval_context_mut();
387        for &(name, val) in values.iter() {
388            let field = this.project_field_named(dest, name)?;
389            this.write_int(val, &field)?;
390        }
391        interp_ok(())
392    }
393
394    /// Write a 0 of the appropriate size to `dest`.
395    fn write_null(&mut self, dest: &impl Writeable<'tcx, Provenance>) -> InterpResult<'tcx> {
396        self.write_int(0, dest)
397    }
398
399    /// Test if this pointer equals 0.
400    fn ptr_is_null(&self, ptr: Pointer) -> InterpResult<'tcx, bool> {
401        interp_ok(ptr.addr().bytes() == 0)
402    }
403
404    /// Generate some random bytes, and write them to `dest`.
405    fn gen_random(&mut self, ptr: Pointer, len: u64) -> InterpResult<'tcx> {
406        // Some programs pass in a null pointer and a length of 0
407        // to their platform's random-generation function (e.g. getrandom())
408        // on Linux. For compatibility with these programs, we don't perform
409        // any additional checks - it's okay if the pointer is invalid,
410        // since we wouldn't actually be writing to it.
411        if len == 0 {
412            return interp_ok(());
413        }
414        let this = self.eval_context_mut();
415
416        let mut data = vec![0; usize::try_from(len).unwrap()];
417
418        if this.machine.communicate() {
419            // Fill the buffer using the host's rng.
420            getrandom::fill(&mut data)
421                .map_err(|err| err_unsup_format!("host getrandom failed: {}", err))?;
422        } else {
423            let rng = this.machine.rng.get_mut();
424            rng.fill_bytes(&mut data);
425        }
426
427        this.write_bytes_ptr(ptr, data.iter().copied())
428    }
429
430    /// Call a function: Push the stack frame and pass the arguments.
431    /// For now, arguments must be scalars (so that the caller does not have to know the layout).
432    ///
433    /// If you do not provide a return place, a dangling zero-sized place will be created
434    /// for your convenience. This is only valid if the return type is `()`.
435    fn call_function(
436        &mut self,
437        f: ty::Instance<'tcx>,
438        caller_abi: ExternAbi,
439        args: &[ImmTy<'tcx>],
440        dest: Option<&MPlaceTy<'tcx>>,
441        cont: ReturnContinuation,
442    ) -> InterpResult<'tcx> {
443        let this = self.eval_context_mut();
444
445        // Get MIR.
446        let mir = this.load_mir(f.def, None)?;
447        let dest = match dest {
448            Some(dest) => dest.clone(),
449            None => MPlaceTy::fake_alloc_zst(this.machine.layouts.unit),
450        };
451
452        // Construct a function pointer type representing the caller perspective.
453        let sig = this.tcx.mk_fn_sig(
454            args.iter().map(|a| a.layout.ty),
455            dest.layout.ty,
456            /*c_variadic*/ false,
457            Safety::Safe,
458            caller_abi,
459        );
460        let caller_fn_abi = this.fn_abi_of_fn_ptr(ty::Binder::dummy(sig), ty::List::empty())?;
461
462        // This will also show proper errors if there is any ABI mismatch.
463        this.init_stack_frame(
464            f,
465            mir,
466            caller_fn_abi,
467            &args.iter().map(|a| FnArg::Copy(a.clone().into())).collect::<Vec<_>>(),
468            /*with_caller_location*/ false,
469            &dest.into(),
470            cont,
471        )
472    }
473
474    /// Call a function in an "empty" thread.
475    fn call_thread_root_function(
476        &mut self,
477        f: ty::Instance<'tcx>,
478        caller_abi: ExternAbi,
479        args: &[ImmTy<'tcx>],
480        dest: Option<&MPlaceTy<'tcx>>,
481        span: Span,
482    ) -> InterpResult<'tcx> {
483        let this = self.eval_context_mut();
484        assert!(this.active_thread_stack().is_empty());
485        assert!(this.active_thread_ref().origin_span.is_dummy());
486        this.active_thread_mut().origin_span = span;
487        this.call_function(f, caller_abi, args, dest, ReturnContinuation::Stop { cleanup: true })
488    }
489
490    /// Visits the memory covered by `place`, sensitive to freezing: the 2nd parameter
491    /// of `action` will be true if this is frozen, false if this is in an `UnsafeCell`.
492    /// The range is relative to `place`.
493    fn visit_freeze_sensitive(
494        &self,
495        place: &MPlaceTy<'tcx>,
496        size: Size,
497        mut action: impl FnMut(AllocRange, bool) -> InterpResult<'tcx>,
498    ) -> InterpResult<'tcx> {
499        let this = self.eval_context_ref();
500        trace!("visit_frozen(place={:?}, size={:?})", *place, size);
501        debug_assert_eq!(
502            size,
503            this.size_and_align_of_val(place)?
504                .map(|(size, _)| size)
505                .unwrap_or_else(|| place.layout.size)
506        );
507        // Store how far we proceeded into the place so far. Everything to the left of
508        // this offset has already been handled, in the sense that the frozen parts
509        // have had `action` called on them.
510        let start_addr = place.ptr().addr();
511        let mut cur_addr = start_addr;
512        // Called when we detected an `UnsafeCell` at the given offset and size.
513        // Calls `action` and advances `cur_ptr`.
514        let mut unsafe_cell_action = |unsafe_cell_ptr: &Pointer, unsafe_cell_size: Size| {
515            // We assume that we are given the fields in increasing offset order,
516            // and nothing else changes.
517            let unsafe_cell_addr = unsafe_cell_ptr.addr();
518            assert!(unsafe_cell_addr >= cur_addr);
519            let frozen_size = unsafe_cell_addr - cur_addr;
520            // Everything between the cur_ptr and this `UnsafeCell` is frozen.
521            if frozen_size != Size::ZERO {
522                action(alloc_range(cur_addr - start_addr, frozen_size), /*frozen*/ true)?;
523            }
524            cur_addr += frozen_size;
525            // This `UnsafeCell` is NOT frozen.
526            if unsafe_cell_size != Size::ZERO {
527                action(
528                    alloc_range(cur_addr - start_addr, unsafe_cell_size),
529                    /*frozen*/ false,
530                )?;
531            }
532            cur_addr += unsafe_cell_size;
533            // Done
534            interp_ok(())
535        };
536        // Run a visitor
537        {
538            let mut visitor = UnsafeCellVisitor {
539                ecx: this,
540                unsafe_cell_action: |place| {
541                    trace!("unsafe_cell_action on {:?}", place.ptr());
542                    // We need a size to go on.
543                    let unsafe_cell_size = this
544                        .size_and_align_of_val(place)?
545                        .map(|(size, _)| size)
546                        // for extern types, just cover what we can
547                        .unwrap_or_else(|| place.layout.size);
548                    // Now handle this `UnsafeCell`, unless it is empty.
549                    if unsafe_cell_size != Size::ZERO {
550                        unsafe_cell_action(&place.ptr(), unsafe_cell_size)
551                    } else {
552                        interp_ok(())
553                    }
554                },
555            };
556            visitor.visit_value(place)?;
557        }
558        // The part between the end_ptr and the end of the place is also frozen.
559        // So pretend there is a 0-sized `UnsafeCell` at the end.
560        unsafe_cell_action(&place.ptr().wrapping_offset(size, this), Size::ZERO)?;
561        // Done!
562        return interp_ok(());
563
564        /// Visiting the memory covered by a `MemPlace`, being aware of
565        /// whether we are inside an `UnsafeCell` or not.
566        struct UnsafeCellVisitor<'ecx, 'tcx, F>
567        where
568            F: FnMut(&MPlaceTy<'tcx>) -> InterpResult<'tcx>,
569        {
570            ecx: &'ecx MiriInterpCx<'tcx>,
571            unsafe_cell_action: F,
572        }
573
574        impl<'ecx, 'tcx, F> ValueVisitor<'tcx, MiriMachine<'tcx>> for UnsafeCellVisitor<'ecx, 'tcx, F>
575        where
576            F: FnMut(&MPlaceTy<'tcx>) -> InterpResult<'tcx>,
577        {
578            type V = MPlaceTy<'tcx>;
579
580            #[inline(always)]
581            fn ecx(&self) -> &MiriInterpCx<'tcx> {
582                self.ecx
583            }
584
585            // Hook to detect `UnsafeCell`.
586            fn visit_value(&mut self, v: &MPlaceTy<'tcx>) -> InterpResult<'tcx> {
587                trace!("UnsafeCellVisitor: {:?} {:?}", *v, v.layout.ty);
588                let is_unsafe_cell = match v.layout.ty.kind() {
589                    ty::Adt(adt, _) =>
590                        Some(adt.did()) == self.ecx.tcx.lang_items().unsafe_cell_type(),
591                    _ => false,
592                };
593                if is_unsafe_cell {
594                    // We do not have to recurse further, this is an `UnsafeCell`.
595                    (self.unsafe_cell_action)(v)
596                } else if self.ecx.type_is_freeze(v.layout.ty) {
597                    // This is `Freeze`, there cannot be an `UnsafeCell`
598                    interp_ok(())
599                } else if matches!(v.layout.fields, FieldsShape::Union(..)) {
600                    // A (non-frozen) union. We fall back to whatever the type says.
601                    (self.unsafe_cell_action)(v)
602                } else {
603                    // We want to not actually read from memory for this visit. So, before
604                    // walking this value, we have to make sure it is not a
605                    // `Variants::Multiple`.
606                    // FIXME: the current logic here is layout-dependent, so enums with
607                    // multiple variants where all but 1 are uninhabited will be recursed into.
608                    // Is that truly what we want?
609                    match v.layout.variants {
610                        Variants::Multiple { .. } => {
611                            // A multi-variant enum, or coroutine, or so.
612                            // Treat this like a union: without reading from memory,
613                            // we cannot determine the variant we are in. Reading from
614                            // memory would be subject to Stacked Borrows rules, leading
615                            // to all sorts of "funny" recursion.
616                            // We only end up here if the type is *not* freeze, so we just call the
617                            // `UnsafeCell` action.
618                            (self.unsafe_cell_action)(v)
619                        }
620                        Variants::Single { .. } | Variants::Empty => {
621                            // Proceed further, try to find where exactly that `UnsafeCell`
622                            // is hiding.
623                            self.walk_value(v)
624                        }
625                    }
626                }
627            }
628
629            fn visit_union(
630                &mut self,
631                _v: &MPlaceTy<'tcx>,
632                _fields: NonZero<usize>,
633            ) -> InterpResult<'tcx> {
634                bug!("we should have already handled unions in `visit_value`")
635            }
636        }
637    }
638
639    /// Helper function used inside the shims of foreign functions to check that isolation is
640    /// disabled. It returns an error using the `name` of the foreign function if this is not the
641    /// case.
642    fn check_no_isolation(&self, name: &str) -> InterpResult<'tcx> {
643        if !self.eval_context_ref().machine.communicate() {
644            self.reject_in_isolation(name, RejectOpWith::Abort)?;
645        }
646        interp_ok(())
647    }
648
649    /// Helper function used inside the shims of foreign functions which reject the op
650    /// when isolation is enabled. It is used to print a warning/backtrace about the rejection.
651    fn reject_in_isolation(&self, op_name: &str, reject_with: RejectOpWith) -> InterpResult<'tcx> {
652        let this = self.eval_context_ref();
653        match reject_with {
654            RejectOpWith::Abort => isolation_abort_error(op_name),
655            RejectOpWith::WarningWithoutBacktrace => {
656                // Deduplicate these warnings *by shim* (not by span)
657                static DEDUP: Mutex<FxHashSet<String>> =
658                    Mutex::new(FxHashSet::with_hasher(FxBuildHasher));
659                let mut emitted_warnings = DEDUP.lock().unwrap();
660                if !emitted_warnings.contains(op_name) {
661                    // First time we are seeing this.
662                    emitted_warnings.insert(op_name.to_owned());
663                    this.tcx
664                        .dcx()
665                        .warn(format!("{op_name} was made to return an error due to isolation"));
666                }
667
668                interp_ok(())
669            }
670            RejectOpWith::Warning => {
671                this.emit_diagnostic(NonHaltingDiagnostic::RejectedIsolatedOp(op_name.to_string()));
672                interp_ok(())
673            }
674            RejectOpWith::NoWarning => interp_ok(()), // no warning
675        }
676    }
677
678    /// Helper function used inside the shims of foreign functions to assert that the target OS
679    /// is `target_os`. It panics showing a message with the `name` of the foreign function
680    /// if this is not the case.
681    fn assert_target_os(&self, target_os: Os, name: &str) {
682        assert_eq!(
683            self.eval_context_ref().tcx.sess.target.os,
684            target_os,
685            "`{name}` is only available on the `{target_os}` target OS",
686        )
687    }
688
689    /// Helper function used inside shims of foreign functions to check that the target OS
690    /// is one of `target_oses`. It returns an error containing the `name` of the foreign function
691    /// in a message if this is not the case.
692    fn check_target_os(&self, target_oses: &[Os], name: Symbol) -> InterpResult<'tcx> {
693        let target_os = &self.eval_context_ref().tcx.sess.target.os;
694        if !target_oses.contains(target_os) {
695            throw_unsup_format!("`{name}` is not supported on {target_os}");
696        }
697        interp_ok(())
698    }
699
700    /// Helper function used inside the shims of foreign functions to assert that the target OS
701    /// is part of the UNIX family. It panics showing a message with the `name` of the foreign function
702    /// if this is not the case.
703    fn assert_target_os_is_unix(&self, name: &str) {
704        assert!(self.target_os_is_unix(), "`{name}` is only available for unix targets",);
705    }
706
707    fn target_os_is_unix(&self) -> bool {
708        self.eval_context_ref().tcx.sess.target.families.iter().any(|f| f == "unix")
709    }
710
711    /// Dereference a pointer operand to a place using `layout` instead of the pointer's declared type
712    fn deref_pointer_as(
713        &self,
714        op: &impl Projectable<'tcx, Provenance>,
715        layout: TyAndLayout<'tcx>,
716    ) -> InterpResult<'tcx, MPlaceTy<'tcx>> {
717        let this = self.eval_context_ref();
718        let ptr = this.read_pointer(op)?;
719        interp_ok(this.ptr_to_mplace(ptr, layout))
720    }
721
722    /// Calculates the MPlaceTy given the offset and layout of an access on an operand
723    fn deref_pointer_and_offset(
724        &self,
725        op: &impl Projectable<'tcx, Provenance>,
726        offset: u64,
727        base_layout: TyAndLayout<'tcx>,
728        value_layout: TyAndLayout<'tcx>,
729    ) -> InterpResult<'tcx, MPlaceTy<'tcx>> {
730        let this = self.eval_context_ref();
731        let op_place = this.deref_pointer_as(op, base_layout)?;
732        let offset = Size::from_bytes(offset);
733
734        // Ensure that the access is within bounds.
735        assert!(base_layout.size >= offset + value_layout.size);
736        let value_place = op_place.offset(offset, value_layout, this)?;
737        interp_ok(value_place)
738    }
739
740    fn deref_pointer_and_read(
741        &self,
742        op: &impl Projectable<'tcx, Provenance>,
743        offset: u64,
744        base_layout: TyAndLayout<'tcx>,
745        value_layout: TyAndLayout<'tcx>,
746    ) -> InterpResult<'tcx, Scalar> {
747        let this = self.eval_context_ref();
748        let value_place = this.deref_pointer_and_offset(op, offset, base_layout, value_layout)?;
749        this.read_scalar(&value_place)
750    }
751
752    fn deref_pointer_and_write(
753        &mut self,
754        op: &impl Projectable<'tcx, Provenance>,
755        offset: u64,
756        value: impl Into<Scalar>,
757        base_layout: TyAndLayout<'tcx>,
758        value_layout: TyAndLayout<'tcx>,
759    ) -> InterpResult<'tcx, ()> {
760        let this = self.eval_context_mut();
761        let value_place = this.deref_pointer_and_offset(op, offset, base_layout, value_layout)?;
762        this.write_scalar(value, &value_place)
763    }
764
765    /// Parse a `timespec` struct and return it as a `std::time::Duration`. It returns `None`
766    /// if the value in the `timespec` struct is invalid. Some libc functions will return
767    /// `EINVAL` in this case.
768    fn read_timespec(&mut self, tp: &MPlaceTy<'tcx>) -> InterpResult<'tcx, Option<Duration>> {
769        let this = self.eval_context_mut();
770        let seconds_place = this.project_field(tp, FieldIdx::ZERO)?;
771        let seconds_scalar = this.read_scalar(&seconds_place)?;
772        let seconds = seconds_scalar.to_target_isize(this)?;
773        let nanoseconds_place = this.project_field(tp, FieldIdx::ONE)?;
774        let nanoseconds_scalar = this.read_scalar(&nanoseconds_place)?;
775        let nanoseconds = nanoseconds_scalar.to_target_isize(this)?;
776
777        interp_ok(
778            try {
779                // tv_sec must be non-negative.
780                let seconds: u64 = seconds.try_into().ok()?;
781                // tv_nsec must be non-negative.
782                let nanoseconds: u32 = nanoseconds.try_into().ok()?;
783                if nanoseconds >= 1_000_000_000 {
784                    // tv_nsec must not be greater than 999,999,999.
785                    None?
786                }
787                Duration::new(seconds, nanoseconds)
788            },
789        )
790    }
791
792    /// Read bytes from a byte slice.
793    fn read_byte_slice<'a>(&'a self, slice: &ImmTy<'tcx>) -> InterpResult<'tcx, &'a [u8]>
794    where
795        'tcx: 'a,
796    {
797        let this = self.eval_context_ref();
798        let (ptr, len) = slice.to_scalar_pair();
799        let ptr = ptr.to_pointer(this)?;
800        let len = len.to_target_usize(this)?;
801        let bytes = this.read_bytes_ptr_strip_provenance(ptr, Size::from_bytes(len))?;
802        interp_ok(bytes)
803    }
804
805    /// Read a sequence of bytes until the first null terminator.
806    fn read_c_str<'a>(&'a self, ptr: Pointer) -> InterpResult<'tcx, &'a [u8]>
807    where
808        'tcx: 'a,
809    {
810        let this = self.eval_context_ref();
811        let size1 = Size::from_bytes(1);
812
813        // Step 1: determine the length.
814        let mut len = Size::ZERO;
815        loop {
816            // FIXME: We are re-getting the allocation each time around the loop.
817            // Would be nice if we could somehow "extend" an existing AllocRange.
818            let alloc = this.get_ptr_alloc(ptr.wrapping_offset(len, this), size1)?.unwrap(); // not a ZST, so we will get a result
819            let byte = alloc.read_integer(alloc_range(Size::ZERO, size1))?.to_u8()?;
820            if byte == 0 {
821                break;
822            } else {
823                len += size1;
824            }
825        }
826
827        // Step 2: get the bytes.
828        this.read_bytes_ptr_strip_provenance(ptr, len)
829    }
830
831    /// Helper function to write a sequence of bytes with an added null-terminator, which is what
832    /// the Unix APIs usually handle. This function returns `Ok((false, length))` without trying
833    /// to write if `size` is not large enough to fit the contents of `c_str` plus a null
834    /// terminator. It returns `Ok((true, length))` if the writing process was successful. The
835    /// string length returned does include the null terminator.
836    fn write_c_str(
837        &mut self,
838        c_str: &[u8],
839        ptr: Pointer,
840        size: u64,
841    ) -> InterpResult<'tcx, (bool, u64)> {
842        // If `size` is smaller or equal than `bytes.len()`, writing `bytes` plus the required null
843        // terminator to memory using the `ptr` pointer would cause an out-of-bounds access.
844        let string_length = u64::try_from(c_str.len()).unwrap();
845        let string_length = string_length.strict_add(1);
846        if size < string_length {
847            return interp_ok((false, string_length));
848        }
849        self.eval_context_mut()
850            .write_bytes_ptr(ptr, c_str.iter().copied().chain(iter::once(0u8)))?;
851        interp_ok((true, string_length))
852    }
853
854    /// Helper function to read a sequence of unsigned integers of the given size and alignment
855    /// until the first null terminator.
856    fn read_c_str_with_char_size<T>(
857        &self,
858        mut ptr: Pointer,
859        size: Size,
860        align: Align,
861    ) -> InterpResult<'tcx, Vec<T>>
862    where
863        T: TryFrom<u128>,
864        <T as TryFrom<u128>>::Error: std::fmt::Debug,
865    {
866        assert_ne!(size, Size::ZERO);
867
868        let this = self.eval_context_ref();
869
870        this.check_ptr_align(ptr, align)?;
871
872        let mut wchars = Vec::new();
873        loop {
874            // FIXME: We are re-getting the allocation each time around the loop.
875            // Would be nice if we could somehow "extend" an existing AllocRange.
876            let alloc = this.get_ptr_alloc(ptr, size)?.unwrap(); // not a ZST, so we will get a result
877            let wchar_int = alloc.read_integer(alloc_range(Size::ZERO, size))?.to_bits(size)?;
878            if wchar_int == 0 {
879                break;
880            } else {
881                wchars.push(wchar_int.try_into().unwrap());
882                ptr = ptr.wrapping_offset(size, this);
883            }
884        }
885
886        interp_ok(wchars)
887    }
888
889    /// Read a sequence of u16 until the first null terminator.
890    fn read_wide_str(&self, ptr: Pointer) -> InterpResult<'tcx, Vec<u16>> {
891        self.read_c_str_with_char_size(ptr, Size::from_bytes(2), Align::from_bytes(2).unwrap())
892    }
893
894    /// Helper function to write a sequence of u16 with an added 0x0000-terminator, which is what
895    /// the Windows APIs usually handle. This function returns `Ok((false, length))` without trying
896    /// to write if `size` is not large enough to fit the contents of `os_string` plus a null
897    /// terminator. It returns `Ok((true, length))` if the writing process was successful. The
898    /// string length returned does include the null terminator. Length is measured in units of
899    /// `u16.`
900    fn write_wide_str(
901        &mut self,
902        wide_str: &[u16],
903        ptr: Pointer,
904        size: u64,
905    ) -> InterpResult<'tcx, (bool, u64)> {
906        // If `size` is smaller or equal than `bytes.len()`, writing `bytes` plus the required
907        // 0x0000 terminator to memory would cause an out-of-bounds access.
908        let string_length = u64::try_from(wide_str.len()).unwrap();
909        let string_length = string_length.strict_add(1);
910        if size < string_length {
911            return interp_ok((false, string_length));
912        }
913
914        // Store the UTF-16 string.
915        let size2 = Size::from_bytes(2);
916        let this = self.eval_context_mut();
917        this.check_ptr_align(ptr, Align::from_bytes(2).unwrap())?;
918        let mut alloc = this.get_ptr_alloc_mut(ptr, size2 * string_length)?.unwrap(); // not a ZST, so we will get a result
919        for (offset, wchar) in wide_str.iter().copied().chain(iter::once(0x0000)).enumerate() {
920            let offset = u64::try_from(offset).unwrap();
921            alloc.write_scalar(alloc_range(size2 * offset, size2), Scalar::from_u16(wchar))?;
922        }
923        interp_ok((true, string_length))
924    }
925
926    /// Read a sequence of wchar_t until the first null terminator.
927    /// Always returns a `Vec<u32>` no matter the size of `wchar_t`.
928    fn read_wchar_t_str(&self, ptr: Pointer) -> InterpResult<'tcx, Vec<u32>> {
929        let this = self.eval_context_ref();
930        let wchar_t = if this.tcx.sess.target.os == Os::Windows {
931            // We don't have libc on Windows so we have to hard-code the type ourselves.
932            this.machine.layouts.u16
933        } else {
934            this.libc_ty_layout("wchar_t")
935        };
936        self.read_c_str_with_char_size(ptr, wchar_t.size, wchar_t.align.abi)
937    }
938
939    fn frame_in_std(&self) -> bool {
940        let this = self.eval_context_ref();
941        let frame = this.frame();
942        // Make an attempt to get at the instance of the function this is inlined from.
943        let instance: Option<_> = try {
944            let scope = frame.current_source_info()?.scope;
945            let inlined_parent = frame.body().source_scopes[scope].inlined_parent_scope?;
946            let source = &frame.body().source_scopes[inlined_parent];
947            source.inlined.expect("inlined_parent_scope points to scope without inline info").0
948        };
949        // Fall back to the instance of the function itself.
950        let instance = instance.unwrap_or(frame.instance());
951        // Now check the crate it is in. We could try to be clever here and e.g. check if this is
952        // the same crate as `start_fn`, but that would not work for running std tests in Miri, so
953        // we'd need some more hacks anyway. So we just check the name of the crate. If someone
954        // calls their crate `std` then we'll just let them keep the pieces.
955        let frame_crate = this.tcx.def_path(instance.def_id()).krate;
956        let crate_name = this.tcx.crate_name(frame_crate);
957        let crate_name = crate_name.as_str();
958        // On miri-test-libstd, the name of the crate is different.
959        crate_name == "std" || crate_name == "std_miri_test"
960    }
961
962    /// Mark a machine allocation that was just created as immutable.
963    fn mark_immutable(&mut self, mplace: &MPlaceTy<'tcx>) {
964        let this = self.eval_context_mut();
965        // This got just allocated, so there definitely is a pointer here.
966        let provenance = mplace.ptr().into_pointer_or_addr().unwrap().provenance;
967        this.alloc_mark_immutable(provenance.get_alloc_id().unwrap()).unwrap();
968    }
969
970    /// Returns an integer type that is twice wide as `ty`
971    fn get_twice_wide_int_ty(&self, ty: Ty<'tcx>) -> Ty<'tcx> {
972        let this = self.eval_context_ref();
973        match ty.kind() {
974            // Unsigned
975            ty::Uint(UintTy::U8) => this.tcx.types.u16,
976            ty::Uint(UintTy::U16) => this.tcx.types.u32,
977            ty::Uint(UintTy::U32) => this.tcx.types.u64,
978            ty::Uint(UintTy::U64) => this.tcx.types.u128,
979            // Signed
980            ty::Int(IntTy::I8) => this.tcx.types.i16,
981            ty::Int(IntTy::I16) => this.tcx.types.i32,
982            ty::Int(IntTy::I32) => this.tcx.types.i64,
983            ty::Int(IntTy::I64) => this.tcx.types.i128,
984            _ => span_bug!(this.cur_span(), "unexpected type: {ty:?}"),
985        }
986    }
987
988    /// Checks that target feature `target_feature` is enabled.
989    ///
990    /// If not enabled, emits an UB error that states that the feature is
991    /// required by `intrinsic`.
992    fn expect_target_feature_for_intrinsic(
993        &self,
994        intrinsic: Symbol,
995        target_feature: &str,
996    ) -> InterpResult<'tcx, ()> {
997        let this = self.eval_context_ref();
998        if !this.tcx.sess.unstable_target_features.contains(&Symbol::intern(target_feature)) {
999            throw_ub_format!(
1000                "attempted to call intrinsic `{intrinsic}` that requires missing target feature {target_feature}"
1001            );
1002        }
1003        interp_ok(())
1004    }
1005
1006    /// Lookup an array of immediates from any linker sections matching the provided predicate,
1007    /// with the spans of where they were found.
1008    fn lookup_link_section(
1009        &mut self,
1010        include_name: impl Fn(&str) -> bool,
1011    ) -> InterpResult<'tcx, Vec<(ImmTy<'tcx>, Span)>> {
1012        let this = self.eval_context_mut();
1013        let tcx = this.tcx.tcx;
1014
1015        let mut array = vec![];
1016
1017        iter_exported_symbols(tcx, |_cnum, def_id| {
1018            let attrs = tcx.codegen_fn_attrs(def_id);
1019            let Some(link_section) = attrs.link_section else {
1020                return interp_ok(());
1021            };
1022            if include_name(link_section.as_str()) {
1023                let instance = ty::Instance::mono(tcx, def_id);
1024                let span = tcx.def_span(def_id);
1025                let const_val = this.eval_global(instance).unwrap_or_else(|err| {
1026                    panic!(
1027                        "failed to evaluate static in required link_section: {def_id:?}\n{err:?}"
1028                    )
1029                });
1030                match const_val.layout.ty.kind() {
1031                    ty::FnPtr(..) => {
1032                        array.push((this.read_immediate(&const_val)?, span));
1033                    }
1034                    ty::Array(elem_ty, _) if matches!(elem_ty.kind(), ty::FnPtr(..)) => {
1035                        let mut elems = this.project_array_fields(&const_val)?;
1036                        while let Some((_idx, elem)) = elems.next(this)? {
1037                            array.push((this.read_immediate(&elem)?, span));
1038                        }
1039                    }
1040                    _ =>
1041                        throw_unsup_format!(
1042                            "only function pointers and arrays of function pointers are supported in well-known linker sections"
1043                        ),
1044                }
1045            }
1046            interp_ok(())
1047        })?;
1048
1049        interp_ok(array)
1050    }
1051
1052    fn mangle_internal_symbol<'a>(&'a mut self, name: &'static str) -> &'a str
1053    where
1054        'tcx: 'a,
1055    {
1056        let this = self.eval_context_mut();
1057        let tcx = *this.tcx;
1058        this.machine
1059            .mangle_internal_symbol_cache
1060            .entry(name)
1061            .or_insert_with(|| mangle_internal_symbol(tcx, name))
1062    }
1063}
1064
1065impl<'tcx> MiriMachine<'tcx> {
1066    /// Get the current span in the topmost function which is workspace-local and not
1067    /// `#[track_caller]`.
1068    /// This function is backed by a cache, and can be assumed to be very fast.
1069    /// It will work even when the stack is empty.
1070    pub fn current_user_relevant_span(&self) -> Span {
1071        self.threads.active_thread_ref().current_user_relevant_span()
1072    }
1073
1074    /// Returns the span of the *caller* of the current operation, again
1075    /// walking down the stack to find the closest frame in a local crate, if the caller of the
1076    /// current operation is not in a local crate.
1077    /// This is useful when we are processing something which occurs on function-entry and we want
1078    /// to point at the call to the function, not the function definition generally.
1079    pub fn caller_span(&self) -> Span {
1080        // We need to go down at least to the caller (len - 2), or however
1081        // far we have to go to find a frame in a local crate which is also not #[track_caller].
1082        let frame_idx = self.top_user_relevant_frame().unwrap();
1083        let frame_idx = cmp::min(frame_idx, self.stack().len().saturating_sub(2));
1084        self.stack()[frame_idx].current_span()
1085    }
1086
1087    fn stack(&self) -> &[Frame<'tcx, Provenance, machine::FrameExtra<'tcx>>] {
1088        self.threads.active_thread_stack()
1089    }
1090
1091    fn top_user_relevant_frame(&self) -> Option<usize> {
1092        self.threads.active_thread_ref().top_user_relevant_frame()
1093    }
1094
1095    /// This is the source of truth for the `user_relevance` flag in our `FrameExtra`.
1096    pub fn user_relevance(&self, frame: &Frame<'tcx, Provenance>) -> u8 {
1097        if frame.instance().def.requires_caller_location(self.tcx) {
1098            return 0;
1099        }
1100        if self.is_local(frame.instance()) {
1101            u8::MAX
1102        } else {
1103            // A non-relevant frame, but at least it doesn't require a caller location, so
1104            // better than nothing.
1105            1
1106        }
1107    }
1108}
1109
1110pub fn isolation_abort_error<'tcx>(name: &str) -> InterpResult<'tcx> {
1111    throw_machine_stop!(TerminationInfo::UnsupportedInIsolation(format!(
1112        "{name} not available when isolation is enabled",
1113    )))
1114}
1115
1116pub(crate) fn bool_to_simd_element(b: bool, size: Size) -> Scalar {
1117    // SIMD uses all-1 as pattern for "true". In two's complement,
1118    // -1 has all its bits set to one and `from_int` will truncate or
1119    // sign-extend it to `size` as required.
1120    let val = if b { -1 } else { 0 };
1121    Scalar::from_int(val, size)
1122}
1123
1124/// Check whether an operation that writes to a target buffer was successful.
1125/// Accordingly select return value.
1126/// Local helper function to be used in Windows shims.
1127pub(crate) fn windows_check_buffer_size((success, len): (bool, u64)) -> u32 {
1128    if success {
1129        // If the function succeeds, the return value is the number of characters stored in the target buffer,
1130        // not including the terminating null character.
1131        u32::try_from(len.strict_sub(1)).unwrap()
1132    } else {
1133        // If the target buffer was not large enough to hold the data, the return value is the buffer size, in characters,
1134        // required to hold the string and its terminating null character.
1135        u32::try_from(len).unwrap()
1136    }
1137}
1138
1139/// We don't support 16-bit systems, so let's have ergonomic conversion from `u32` to `usize`.
1140pub trait ToUsize {
1141    fn to_usize(self) -> usize;
1142}
1143
1144impl ToUsize for u32 {
1145    fn to_usize(self) -> usize {
1146        self.try_into().unwrap()
1147    }
1148}
1149
1150/// Similarly, a maximum address size of `u64` is assumed widely here, so let's have ergonomic
1151/// converion from `usize` to `u64`.
1152pub trait ToU64 {
1153    fn to_u64(self) -> u64;
1154}
1155
1156impl ToU64 for usize {
1157    fn to_u64(self) -> u64 {
1158        self.try_into().unwrap()
1159    }
1160}
1161
1162/// Enters a [tracing::info_span] only if the "tracing" feature is enabled, otherwise does nothing.
1163/// This calls [rustc_const_eval::enter_trace_span] with [MiriMachine] as the first argument, which
1164/// will in turn call [MiriMachine::enter_trace_span], which takes care of determining at compile
1165/// time whether to trace or not (and supposedly the call is compiled out if tracing is disabled).
1166/// Look at [rustc_const_eval::enter_trace_span] for complete documentation, examples and tips.
1167#[macro_export]
1168macro_rules! enter_trace_span {
1169    ($($tt:tt)*) => {
1170        rustc_const_eval::enter_trace_span!($crate::MiriMachine<'static>, $($tt)*)
1171    };
1172}