1use std::borrow::Cow;
5use std::cell::{Cell, RefCell};
6use std::collections::BTreeMap;
7use std::path::Path;
8use std::rc::Rc;
9use std::{fmt, process};
10
11use rand::rngs::StdRng;
12use rand::{RngExt, SeedableRng};
13use rustc_abi::{Align, ExternAbi, Size};
14use rustc_apfloat::{Float, FloatConvert};
15use rustc_ast::expand::allocator::{self, SpecialAllocatorMethod};
16use rustc_data_structures::either::Either;
17use rustc_data_structures::fx::{FxHashMap, FxHashSet};
18#[allow(unused)]
19use rustc_data_structures::static_assert_size;
20use rustc_hir::attrs::{InlineAttr, Linkage};
21use rustc_log::tracing;
22use rustc_middle::middle::codegen_fn_attrs::TargetFeatureKind;
23use rustc_middle::mir;
24use rustc_middle::query::TyCtxtAt;
25use rustc_middle::ty::layout::{
26 HasTyCtxt, HasTypingEnv, LayoutCx, LayoutError, LayoutOf, TyAndLayout,
27};
28use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
29use rustc_session::config::InliningThreshold;
30use rustc_span::def_id::{CrateNum, DefId};
31use rustc_span::{Span, SpanData, Symbol};
32use rustc_symbol_mangling::mangle_internal_symbol;
33use rustc_target::callconv::FnAbi;
34use rustc_target::spec::{Arch, Os};
35
36use crate::alloc_addresses::EvalContextExt;
37use crate::concurrency::cpu_affinity::{self, CpuAffinityMask};
38use crate::concurrency::data_race::{self, NaReadType, NaWriteType};
39use crate::concurrency::sync::SyncObj;
40use crate::concurrency::{
41 AllocDataRaceHandler, GenmcCtx, GenmcEvalContextExt as _, GlobalDataRaceHandler, weak_memory,
42};
43use crate::helpers::is_no_core;
44use crate::*;
45
46pub const SIGRTMIN: i32 = 34;
50
51pub const SIGRTMAX: i32 = 42;
55
56const ADDRS_PER_ANON_GLOBAL: usize = 32;
60
61#[derive(Copy, Clone, Debug, PartialEq)]
62pub enum AlignmentCheck {
63 None,
65 Symbolic,
67 Int,
69}
70
71#[derive(Copy, Clone, Debug, PartialEq)]
72pub enum RejectOpWith {
73 Abort,
75
76 NoWarning,
80
81 Warning,
83
84 WarningWithoutBacktrace,
86}
87
88#[derive(Copy, Clone, Debug, PartialEq)]
89pub enum IsolatedOp {
90 Reject(RejectOpWith),
95
96 Allow,
98}
99
100#[derive(Debug, Copy, Clone, PartialEq, Eq)]
101pub enum BacktraceStyle {
102 Short,
104 Full,
106 Off,
108}
109
110#[derive(Debug, Copy, Clone, PartialEq, Eq)]
111pub enum ValidationMode {
112 No,
114 Shallow,
116 Deep,
118}
119
120#[derive(Debug, Copy, Clone, PartialEq, Eq)]
121pub enum FloatRoundingErrorMode {
122 Random,
124 None,
126 Max,
128}
129
130pub struct FrameExtra<'tcx> {
132 pub borrow_tracker: Option<borrow_tracker::FrameState>,
134
135 pub catch_unwind: Option<CatchUnwindData<'tcx>>,
139
140 pub timing: Option<measureme::DetachedTiming>,
144
145 pub user_relevance: u8,
149
150 pub data_race: Option<data_race::FrameState>,
152}
153
154impl<'tcx> std::fmt::Debug for FrameExtra<'tcx> {
155 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
156 let FrameExtra { borrow_tracker, catch_unwind, timing: _, user_relevance, data_race } =
158 self;
159 f.debug_struct("FrameData")
160 .field("borrow_tracker", borrow_tracker)
161 .field("catch_unwind", catch_unwind)
162 .field("user_relevance", user_relevance)
163 .field("data_race", data_race)
164 .finish()
165 }
166}
167
168impl VisitProvenance for FrameExtra<'_> {
169 fn visit_provenance(&self, visit: &mut VisitWith<'_>) {
170 let FrameExtra { catch_unwind, borrow_tracker, timing: _, user_relevance: _, data_race: _ } =
171 self;
172
173 catch_unwind.visit_provenance(visit);
174 borrow_tracker.visit_provenance(visit);
175 }
176}
177
178#[derive(Debug, Copy, Clone, PartialEq, Eq)]
180pub enum MiriMemoryKind {
181 Rust,
183 Miri,
185 C,
187 WinHeap,
189 WinLocal,
191 Machine,
194 Runtime,
197 Global,
200 ExternStatic,
203 Tls,
206 Mmap,
208 SocketAddress,
210}
211
212impl From<MiriMemoryKind> for MemoryKind {
213 #[inline(always)]
214 fn from(kind: MiriMemoryKind) -> MemoryKind {
215 MemoryKind::Machine(kind)
216 }
217}
218
219impl MayLeak for MiriMemoryKind {
220 #[inline(always)]
221 fn may_leak(self) -> bool {
222 use self::MiriMemoryKind::*;
223 match self {
224 Rust | Miri | C | WinHeap | WinLocal | Runtime => false,
225 Machine | Global | ExternStatic | Tls | Mmap | SocketAddress => true,
226 }
227 }
228}
229
230impl MiriMemoryKind {
231 fn should_save_allocation_span(self) -> bool {
233 use self::MiriMemoryKind::*;
234 match self {
235 Rust | Miri | C | WinHeap | WinLocal | Mmap => true,
237 Machine | Global | ExternStatic | Tls | Runtime | SocketAddress => false,
239 }
240 }
241}
242
243impl fmt::Display for MiriMemoryKind {
244 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
245 use self::MiriMemoryKind::*;
246 match self {
247 Rust => write!(f, "Rust heap"),
248 Miri => write!(f, "Miri bare-metal heap"),
249 C => write!(f, "C heap"),
250 WinHeap => write!(f, "Windows heap"),
251 WinLocal => write!(f, "Windows local memory"),
252 Machine => write!(f, "machine-managed memory"),
253 Runtime => write!(f, "language runtime memory"),
254 Global => write!(f, "global (static or const)"),
255 ExternStatic => write!(f, "extern static"),
256 Tls => write!(f, "thread-local static"),
257 Mmap => write!(f, "mmap"),
258 SocketAddress => write!(f, "socket address"),
259 }
260 }
261}
262
263pub type MemoryKind = interpret::MemoryKind<MiriMemoryKind>;
264
265#[derive(Clone, Copy, PartialEq, Eq, Hash)]
271pub enum Provenance {
272 Concrete {
275 alloc_id: AllocId,
276 tag: BorTag,
278 },
279 Wildcard,
296}
297
298#[derive(Copy, Clone, PartialEq)]
300pub enum ProvenanceExtra {
301 Concrete(BorTag),
302 Wildcard,
303}
304
305#[cfg(target_pointer_width = "64")]
306static_assert_size!(StrictPointer, 24);
307#[cfg(target_pointer_width = "64")]
312static_assert_size!(Scalar, 32);
313
314impl fmt::Debug for Provenance {
315 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
316 match self {
317 Provenance::Concrete { alloc_id, tag } => {
318 if f.alternate() {
320 write!(f, "[{alloc_id:#?}]")?;
321 } else {
322 write!(f, "[{alloc_id:?}]")?;
323 }
324 write!(f, "{tag:?}")?;
326 }
327 Provenance::Wildcard => {
328 write!(f, "[wildcard]")?;
329 }
330 }
331 Ok(())
332 }
333}
334
335impl interpret::Provenance for Provenance {
336 const OFFSET_IS_ADDR: bool = true;
338
339 const WILDCARD: Option<Self> = Some(Provenance::Wildcard);
341
342 fn get_alloc_id(self) -> Option<AllocId> {
343 match self {
344 Provenance::Concrete { alloc_id, .. } => Some(alloc_id),
345 Provenance::Wildcard => None,
346 }
347 }
348
349 fn fmt(ptr: &interpret::Pointer<Self>, f: &mut fmt::Formatter<'_>) -> fmt::Result {
350 let (prov, addr) = ptr.into_raw_parts(); write!(f, "{:#x}", addr.bytes())?;
352 if f.alternate() {
353 write!(f, "{prov:#?}")?;
354 } else {
355 write!(f, "{prov:?}")?;
356 }
357 Ok(())
358 }
359}
360
361impl fmt::Debug for ProvenanceExtra {
362 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
363 match self {
364 ProvenanceExtra::Concrete(pid) => write!(f, "{pid:?}"),
365 ProvenanceExtra::Wildcard => write!(f, "<wildcard>"),
366 }
367 }
368}
369
370impl ProvenanceExtra {
371 pub fn and_then<T>(self, f: impl FnOnce(BorTag) -> Option<T>) -> Option<T> {
372 match self {
373 ProvenanceExtra::Concrete(pid) => f(pid),
374 ProvenanceExtra::Wildcard => None,
375 }
376 }
377}
378
379#[derive(Debug)]
381pub struct AllocExtra<'tcx> {
382 pub borrow_tracker: Option<borrow_tracker::AllocState>,
384 pub data_race: AllocDataRaceHandler,
388 pub backtrace: Option<Vec<FrameInfo<'tcx>>>,
393 pub sync_objs: BTreeMap<Size, Box<dyn SyncObj>>,
398}
399
400impl<'tcx> Clone for AllocExtra<'tcx> {
403 fn clone(&self) -> Self {
404 panic!("our allocations should never be cloned");
405 }
406}
407
408impl VisitProvenance for AllocExtra<'_> {
409 fn visit_provenance(&self, visit: &mut VisitWith<'_>) {
410 let AllocExtra { borrow_tracker, data_race, backtrace: _, sync_objs: _ } = self;
411
412 borrow_tracker.visit_provenance(visit);
413 data_race.visit_provenance(visit);
414 }
415}
416
417pub struct PrimitiveLayouts<'tcx> {
419 pub unit: TyAndLayout<'tcx>,
420 pub i8: TyAndLayout<'tcx>,
421 pub i16: TyAndLayout<'tcx>,
422 pub i32: TyAndLayout<'tcx>,
423 pub i64: TyAndLayout<'tcx>,
424 pub i128: TyAndLayout<'tcx>,
425 pub isize: TyAndLayout<'tcx>,
426 pub u8: TyAndLayout<'tcx>,
427 pub u16: TyAndLayout<'tcx>,
428 pub u32: TyAndLayout<'tcx>,
429 pub u64: TyAndLayout<'tcx>,
430 pub u128: TyAndLayout<'tcx>,
431 pub usize: TyAndLayout<'tcx>,
432 pub bool: TyAndLayout<'tcx>,
433 pub mut_raw_ptr: TyAndLayout<'tcx>, pub const_raw_ptr: TyAndLayout<'tcx>, }
436
437impl<'tcx> PrimitiveLayouts<'tcx> {
438 fn new(layout_cx: LayoutCx<'tcx>) -> Result<Self, &'tcx LayoutError<'tcx>> {
439 let tcx = layout_cx.tcx();
440 let mut_raw_ptr = Ty::new_mut_ptr(tcx, tcx.types.unit);
441 let const_raw_ptr = Ty::new_imm_ptr(tcx, tcx.types.unit);
442 Ok(Self {
443 unit: layout_cx.layout_of(tcx.types.unit)?,
444 i8: layout_cx.layout_of(tcx.types.i8)?,
445 i16: layout_cx.layout_of(tcx.types.i16)?,
446 i32: layout_cx.layout_of(tcx.types.i32)?,
447 i64: layout_cx.layout_of(tcx.types.i64)?,
448 i128: layout_cx.layout_of(tcx.types.i128)?,
449 isize: layout_cx.layout_of(tcx.types.isize)?,
450 u8: layout_cx.layout_of(tcx.types.u8)?,
451 u16: layout_cx.layout_of(tcx.types.u16)?,
452 u32: layout_cx.layout_of(tcx.types.u32)?,
453 u64: layout_cx.layout_of(tcx.types.u64)?,
454 u128: layout_cx.layout_of(tcx.types.u128)?,
455 usize: layout_cx.layout_of(tcx.types.usize)?,
456 bool: layout_cx.layout_of(tcx.types.bool)?,
457 mut_raw_ptr: layout_cx.layout_of(mut_raw_ptr)?,
458 const_raw_ptr: layout_cx.layout_of(const_raw_ptr)?,
459 })
460 }
461
462 pub fn uint(&self, size: Size) -> Option<TyAndLayout<'tcx>> {
463 match size.bits() {
464 8 => Some(self.u8),
465 16 => Some(self.u16),
466 32 => Some(self.u32),
467 64 => Some(self.u64),
468 128 => Some(self.u128),
469 _ => None,
470 }
471 }
472
473 pub fn int(&self, size: Size) -> Option<TyAndLayout<'tcx>> {
474 match size.bits() {
475 8 => Some(self.i8),
476 16 => Some(self.i16),
477 32 => Some(self.i32),
478 64 => Some(self.i64),
479 128 => Some(self.i128),
480 _ => None,
481 }
482 }
483}
484
485pub struct MiriMachine<'tcx> {
490 pub tcx: TyCtxt<'tcx>,
492
493 pub borrow_tracker: Option<borrow_tracker::GlobalState>,
495
496 pub data_race: GlobalDataRaceHandler,
502
503 pub alloc_addresses: alloc_addresses::GlobalState,
505
506 pub(crate) env_vars: EnvVars<'tcx>,
508
509 pub(crate) main_fn_ret_place: Option<MPlaceTy<'tcx>>,
511
512 pub(crate) argc: Option<Pointer>,
516 pub(crate) argv: Option<Pointer>,
517 pub(crate) cmd_line: Option<Pointer>,
518
519 pub(crate) tls: TlsData<'tcx>,
521
522 pub(crate) isolated_op: IsolatedOp,
526
527 pub(crate) validation: ValidationMode,
529
530 pub(crate) fds: shims::FdTable,
532 pub(crate) dirs: shims::DirTable,
534
535 pub(crate) epoll_interests: shims::EpollInterestTable,
537
538 pub(crate) monotonic_clock: MonotonicClock,
540
541 pub(crate) threads: ThreadManager<'tcx>,
543
544 pub(crate) blocking_io: BlockingIoManager,
546
547 pub(crate) thread_cpu_affinity: Option<FxHashMap<ThreadId, CpuAffinityMask>>,
552
553 pub(crate) layouts: PrimitiveLayouts<'tcx>,
555
556 pub(crate) static_roots: Vec<AllocId>,
558
559 profiler: Option<measureme::Profiler>,
562 string_cache: FxHashMap<String, measureme::StringId>,
565
566 pub(crate) exported_symbols_cache: FxHashMap<Symbol, Option<Instance<'tcx>>>,
569
570 pub(crate) backtrace_style: BacktraceStyle,
572
573 pub(crate) user_relevant_crates: Vec<CrateNum>,
575
576 pub(crate) extern_statics: FxHashMap<Symbol, StrictPointer>,
578 pub(crate) missing_weak_symbol: Option<StrictPointer>,
580
581 pub(crate) rng: RefCell<StdRng>,
584
585 pub(crate) allocator: Option<Rc<RefCell<crate::alloc::isolated_alloc::IsolatedAlloc>>>,
587
588 pub(crate) tracked_alloc_ids: FxHashSet<AllocId>,
591 track_alloc_accesses: bool,
593
594 pub(crate) check_alignment: AlignmentCheck,
596
597 pub(crate) cmpxchg_weak_failure_rate: f64,
599
600 pub(crate) preemption_rate: f64,
602
603 pub(crate) report_progress: Option<u32>,
605 pub(crate) basic_block_count: u64,
607
608 #[cfg(all(feature = "native-lib", unix))]
610 pub native_lib: Vec<(libloading::Library, std::path::PathBuf)>,
611 #[cfg(not(all(feature = "native-lib", unix)))]
612 pub native_lib: Vec<!>,
613 #[cfg(all(feature = "native-lib", unix))]
615 pub native_lib_ecx_interchange: &'static Cell<usize>,
616
617 pub(crate) gc_interval: u32,
619 pub(crate) since_gc: u32,
621
622 pub(crate) num_cpus: u32,
624
625 pub(crate) page_size: u64,
627 pub(crate) stack_addr: u64,
628 pub(crate) stack_size: u64,
629
630 pub(crate) collect_leak_backtraces: bool,
632
633 pub(crate) allocation_spans: RefCell<FxHashMap<AllocId, (Span, Option<Span>)>>,
636
637 pub(crate) symbolic_alignment: RefCell<FxHashMap<AllocId, (Size, Align)>>,
644
645 union_data_ranges: FxHashMap<Ty<'tcx>, RangeSet>,
647
648 pub(crate) pthread_mutex_sanity: Cell<bool>,
650 pub(crate) pthread_rwlock_sanity: Cell<bool>,
651 pub(crate) pthread_condvar_sanity: Cell<bool>,
652
653 pub(crate) allocator_shim_symbols: FxHashMap<Symbol, Either<Symbol, SpecialAllocatorMethod>>,
657 pub(crate) mangle_internal_symbol_cache: FxHashMap<&'static str, String>,
659
660 pub force_intrinsic_fallback: bool,
662
663 pub float_nondet: bool,
665 pub float_rounding_error: FloatRoundingErrorMode,
667
668 pub short_fd_operations: bool,
670}
671
672impl<'tcx> MiriMachine<'tcx> {
673 pub(crate) fn new(
677 config: &MiriConfig,
678 layout_cx: LayoutCx<'tcx>,
679 genmc_ctx: Option<Rc<GenmcCtx>>,
680 ) -> Self {
681 let tcx = layout_cx.tcx();
682 let user_relevant_crates = Self::get_user_relevant_crates(tcx, config);
683 let layouts =
684 PrimitiveLayouts::new(layout_cx).expect("Couldn't get layouts of primitive types");
685 let profiler = config.measureme_out.as_ref().map(|out| {
686 let crate_name =
687 tcx.sess.opts.crate_name.clone().unwrap_or_else(|| "unknown-crate".to_string());
688 let pid = process::id();
689 let filename = format!("{crate_name}-{pid:07}");
694 let path = Path::new(out).join(filename);
695 measureme::Profiler::new(path).expect("Couldn't create `measureme` profiler")
696 });
697 let rng = StdRng::seed_from_u64(config.seed.unwrap_or(0));
698 let borrow_tracker = config.borrow_tracker.map(|bt| bt.instantiate_global_state(config));
699 let data_race = if config.genmc_config.is_some() {
700 GlobalDataRaceHandler::Genmc(genmc_ctx.unwrap())
702 } else if config.data_race_detector {
703 GlobalDataRaceHandler::Vclocks(Box::new(data_race::GlobalState::new(config)))
704 } else {
705 GlobalDataRaceHandler::None
706 };
707 let page_size = if let Some(page_size) = config.page_size {
711 page_size
712 } else {
713 let target = &tcx.sess.target;
714 match target.arch {
715 Arch::Wasm32 | Arch::Wasm64 => 64 * 1024, Arch::AArch64 => {
717 if target.is_like_darwin {
718 16 * 1024
722 } else {
723 4 * 1024
724 }
725 }
726 _ => 4 * 1024,
727 }
728 };
729 let stack_addr = if tcx.pointer_size().bits() < 32 { page_size } else { page_size * 32 };
731 let stack_size =
732 if tcx.pointer_size().bits() < 32 { page_size * 4 } else { page_size * 16 };
733 assert!(
734 usize::try_from(config.num_cpus).unwrap() <= cpu_affinity::MAX_CPUS,
735 "miri only supports up to {} CPUs, but {} were configured",
736 cpu_affinity::MAX_CPUS,
737 config.num_cpus
738 );
739 let threads = ThreadManager::new(config);
740 let thread_cpu_affinity =
741 if matches!(&tcx.sess.target.os, Os::Linux | Os::FreeBsd | Os::Android)
742 && !is_no_core(tcx)
743 {
744 let mut affinity = FxHashMap::default();
745 affinity.insert(
746 threads.active_thread(),
747 CpuAffinityMask::new(&layout_cx, config.num_cpus),
748 );
749 Some(affinity)
750 } else {
751 None
752 };
753 let blocking_io = BlockingIoManager::new(config.isolated_op == IsolatedOp::Allow)
754 .expect("Couldn't create poll instance");
755 let alloc_addresses =
756 RefCell::new(alloc_addresses::GlobalStateInner::new(config, stack_addr, tcx));
757
758 MiriMachine {
759 tcx,
760 borrow_tracker,
761 data_race,
762 alloc_addresses,
763 env_vars: EnvVars::default(),
765 main_fn_ret_place: None,
766 argc: None,
767 argv: None,
768 cmd_line: None,
769 tls: TlsData::default(),
770 isolated_op: config.isolated_op,
771 validation: config.validation,
772 fds: shims::FdTable::init(config.mute_stdout_stderr),
773 epoll_interests: shims::EpollInterestTable::new(),
774 dirs: Default::default(),
775 layouts,
776 threads,
777 thread_cpu_affinity,
778 blocking_io,
779 static_roots: Vec::new(),
780 profiler,
781 string_cache: Default::default(),
782 exported_symbols_cache: FxHashMap::default(),
783 backtrace_style: config.backtrace_style,
784 user_relevant_crates,
785 extern_statics: FxHashMap::default(),
786 missing_weak_symbol: None,
787 rng: RefCell::new(rng),
788 allocator: (!config.native_lib.is_empty())
789 .then(|| Rc::new(RefCell::new(crate::alloc::isolated_alloc::IsolatedAlloc::new()))),
790 tracked_alloc_ids: config.tracked_alloc_ids.clone(),
791 track_alloc_accesses: config.track_alloc_accesses,
792 check_alignment: config.check_alignment,
793 cmpxchg_weak_failure_rate: config.cmpxchg_weak_failure_rate,
794 preemption_rate: config.preemption_rate,
795 report_progress: config.report_progress,
796 basic_block_count: 0,
797 monotonic_clock: MonotonicClock::new(config.isolated_op == IsolatedOp::Allow),
798 #[cfg(all(feature = "native-lib", unix))]
799 native_lib: config.native_lib.iter().map(|lib_file_path| {
800 let host_triple = rustc_session::config::host_tuple();
801 let target_triple = tcx.sess.opts.target_triple.tuple();
802 if host_triple != target_triple {
804 panic!(
805 "calling native C functions in linked .so file requires host and target to be the same: \
806 host={host_triple}, target={target_triple}",
807 );
808 }
809 (
813 unsafe {
814 libloading::Library::new(lib_file_path)
815 .expect("failed to read specified extern shared object file")
816 },
817 lib_file_path.clone(),
818 )
819 }).collect(),
820 #[cfg(all(feature = "native-lib", unix))]
821 native_lib_ecx_interchange: Box::leak(Box::new(Cell::new(0))),
822 #[cfg(not(all(feature = "native-lib", unix)))]
823 native_lib: config.native_lib.iter().map(|_| {
824 panic!("calling functions from native libraries via FFI is not supported in this build of Miri")
825 }).collect(),
826 gc_interval: config.gc_interval,
827 since_gc: 0,
828 num_cpus: config.num_cpus,
829 page_size,
830 stack_addr,
831 stack_size,
832 collect_leak_backtraces: config.collect_leak_backtraces,
833 allocation_spans: RefCell::new(FxHashMap::default()),
834 symbolic_alignment: RefCell::new(FxHashMap::default()),
835 union_data_ranges: FxHashMap::default(),
836 pthread_mutex_sanity: Cell::new(false),
837 pthread_rwlock_sanity: Cell::new(false),
838 pthread_condvar_sanity: Cell::new(false),
839 allocator_shim_symbols: Self::allocator_shim_symbols(tcx),
840 mangle_internal_symbol_cache: Default::default(),
841 force_intrinsic_fallback: config.force_intrinsic_fallback,
842 float_nondet: config.float_nondet,
843 float_rounding_error: config.float_rounding_error,
844 short_fd_operations: config.short_fd_operations,
845 }
846 }
847
848 fn allocator_shim_symbols(
849 tcx: TyCtxt<'tcx>,
850 ) -> FxHashMap<Symbol, Either<Symbol, SpecialAllocatorMethod>> {
851 use rustc_codegen_ssa::base::allocator_shim_contents;
852
853 let Some(kind) = tcx.allocator_kind(()) else {
856 return Default::default();
857 };
858 let methods = allocator_shim_contents(tcx, kind);
859 let mut symbols = FxHashMap::default();
860 for method in methods {
861 let from_name = Symbol::intern(&mangle_internal_symbol(
862 tcx,
863 &allocator::global_fn_name(method.name),
864 ));
865 let to = match method.special {
866 Some(special) => Either::Right(special),
867 None =>
868 Either::Left(Symbol::intern(&mangle_internal_symbol(
869 tcx,
870 &allocator::default_fn_name(method.name),
871 ))),
872 };
873 symbols.try_insert(from_name, to).unwrap();
874 }
875 symbols
876 }
877
878 fn get_user_relevant_crates(tcx: TyCtxt<'_>, config: &MiriConfig) -> Vec<CrateNum> {
881 let local_crate_names = std::env::var("MIRI_LOCAL_CRATES")
884 .map(|crates| crates.split(',').map(|krate| krate.to_string()).collect::<Vec<_>>())
885 .unwrap_or_default();
886 let mut local_crates = Vec::new();
887 for &crate_num in tcx.crates(()) {
888 let name = tcx.crate_name(crate_num);
889 let name = name.as_str();
890 if local_crate_names
891 .iter()
892 .chain(&config.user_relevant_crates)
893 .any(|local_name| local_name == name)
894 {
895 local_crates.push(crate_num);
896 }
897 }
898 local_crates
899 }
900
901 pub(crate) fn late_init(
902 ecx: &mut MiriInterpCx<'tcx>,
903 config: &MiriConfig,
904 on_main_stack_empty: StackEmptyCallback<'tcx>,
905 ) -> InterpResult<'tcx> {
906 EnvVars::init(ecx, config)?;
907 MiriMachine::init_extern_statics(ecx)?;
908 ThreadManager::init(ecx, on_main_stack_empty);
909 interp_ok(())
910 }
911
912 pub(crate) fn add_extern_static(ecx: &mut MiriInterpCx<'tcx>, name: &str, ptr: Pointer) {
913 let ptr = ptr.into_pointer_or_addr().unwrap();
915 ecx.machine.extern_statics.try_insert(Symbol::intern(name), ptr).unwrap();
916 }
917
918 pub(crate) fn communicate(&self) -> bool {
919 self.isolated_op == IsolatedOp::Allow
920 }
921
922 pub(crate) fn is_local(&self, instance: ty::Instance<'tcx>) -> bool {
924 let def_id = instance.def_id();
925 def_id.is_local() || self.user_relevant_crates.contains(&def_id.krate)
926 }
927
928 pub(crate) fn handle_abnormal_termination(&mut self) {
930 drop(self.profiler.take());
935 }
936
937 pub(crate) fn page_align(&self) -> Align {
938 Align::from_bytes(self.page_size).unwrap()
939 }
940
941 pub(crate) fn allocated_span(&self, alloc_id: AllocId) -> Option<SpanData> {
942 self.allocation_spans
943 .borrow()
944 .get(&alloc_id)
945 .map(|(allocated, _deallocated)| allocated.data())
946 }
947
948 pub(crate) fn deallocated_span(&self, alloc_id: AllocId) -> Option<SpanData> {
949 self.allocation_spans
950 .borrow()
951 .get(&alloc_id)
952 .and_then(|(_allocated, deallocated)| *deallocated)
953 .map(Span::data)
954 }
955
956 fn init_allocation(
957 ecx: &MiriInterpCx<'tcx>,
958 id: AllocId,
959 kind: MemoryKind,
960 size: Size,
961 align: Align,
962 ) -> InterpResult<'tcx, AllocExtra<'tcx>> {
963 if ecx.machine.tracked_alloc_ids.contains(&id) {
964 ecx.emit_diagnostic(NonHaltingDiagnostic::TrackingAlloc(id, size, align));
965 }
966
967 let borrow_tracker = ecx
968 .machine
969 .borrow_tracker
970 .as_ref()
971 .map(|bt| bt.borrow_mut().new_allocation(id, size, kind, &ecx.machine));
972
973 let data_race = match &ecx.machine.data_race {
974 GlobalDataRaceHandler::None => AllocDataRaceHandler::None,
975 GlobalDataRaceHandler::Vclocks(data_race) =>
976 AllocDataRaceHandler::Vclocks(
977 data_race::AllocState::new_allocation(
978 data_race,
979 &ecx.machine.threads,
980 size,
981 kind,
982 ecx.machine.current_user_relevant_span(),
983 ),
984 data_race.weak_memory.then(weak_memory::AllocState::new_allocation),
985 ),
986 GlobalDataRaceHandler::Genmc(_genmc_ctx) => {
987 AllocDataRaceHandler::Genmc
990 }
991 };
992
993 let backtrace = if kind.may_leak() || !ecx.machine.collect_leak_backtraces {
997 None
998 } else {
999 Some(ecx.generate_stacktrace())
1000 };
1001
1002 if matches!(kind, MemoryKind::Machine(kind) if kind.should_save_allocation_span()) {
1003 ecx.machine
1004 .allocation_spans
1005 .borrow_mut()
1006 .insert(id, (ecx.machine.current_user_relevant_span(), None));
1007 }
1008
1009 interp_ok(AllocExtra {
1010 borrow_tracker,
1011 data_race,
1012 backtrace,
1013 sync_objs: BTreeMap::default(),
1014 })
1015 }
1016}
1017
1018impl VisitProvenance for MiriMachine<'_> {
1019 fn visit_provenance(&self, visit: &mut VisitWith<'_>) {
1020 #[rustfmt::skip]
1021 let MiriMachine {
1022 threads,
1023 thread_cpu_affinity: _,
1024 tls,
1025 env_vars,
1026 main_fn_ret_place,
1027 argc,
1028 argv,
1029 cmd_line,
1030 extern_statics,
1031 missing_weak_symbol,
1032 dirs,
1033 borrow_tracker,
1034 data_race,
1035 alloc_addresses,
1036 fds,
1037 blocking_io:_,
1038 epoll_interests:_,
1039 tcx: _,
1040 isolated_op: _,
1041 validation: _,
1042 monotonic_clock: _,
1043 layouts: _,
1044 static_roots: _,
1045 profiler: _,
1046 string_cache: _,
1047 exported_symbols_cache: _,
1048 backtrace_style: _,
1049 user_relevant_crates: _,
1050 rng: _,
1051 allocator: _,
1052 tracked_alloc_ids: _,
1053 track_alloc_accesses: _,
1054 check_alignment: _,
1055 cmpxchg_weak_failure_rate: _,
1056 preemption_rate: _,
1057 report_progress: _,
1058 basic_block_count: _,
1059 native_lib: _,
1060 #[cfg(all(feature = "native-lib", unix))]
1061 native_lib_ecx_interchange: _,
1062 gc_interval: _,
1063 since_gc: _,
1064 num_cpus: _,
1065 page_size: _,
1066 stack_addr: _,
1067 stack_size: _,
1068 collect_leak_backtraces: _,
1069 allocation_spans: _,
1070 symbolic_alignment: _,
1071 union_data_ranges: _,
1072 pthread_mutex_sanity: _,
1073 pthread_rwlock_sanity: _,
1074 pthread_condvar_sanity: _,
1075 allocator_shim_symbols: _,
1076 mangle_internal_symbol_cache: _,
1077 force_intrinsic_fallback: _,
1078 float_nondet: _,
1079 float_rounding_error: _,
1080 short_fd_operations: _,
1081 } = self;
1082
1083 threads.visit_provenance(visit);
1084 tls.visit_provenance(visit);
1085 env_vars.visit_provenance(visit);
1086 dirs.visit_provenance(visit);
1087 fds.visit_provenance(visit);
1088 data_race.visit_provenance(visit);
1089 borrow_tracker.visit_provenance(visit);
1090 alloc_addresses.visit_provenance(visit);
1091 main_fn_ret_place.visit_provenance(visit);
1092 argc.visit_provenance(visit);
1093 argv.visit_provenance(visit);
1094 cmd_line.visit_provenance(visit);
1095 missing_weak_symbol.visit_provenance(visit);
1096 for ptr in extern_statics.values() {
1097 ptr.visit_provenance(visit);
1098 }
1099 }
1100}
1101
1102pub type MiriInterpCx<'tcx> = InterpCx<'tcx, MiriMachine<'tcx>>;
1104
1105pub trait MiriInterpCxExt<'tcx> {
1107 fn eval_context_ref<'a>(&'a self) -> &'a MiriInterpCx<'tcx>;
1108 fn eval_context_mut<'a>(&'a mut self) -> &'a mut MiriInterpCx<'tcx>;
1109}
1110impl<'tcx> MiriInterpCxExt<'tcx> for MiriInterpCx<'tcx> {
1111 #[inline(always)]
1112 fn eval_context_ref(&self) -> &MiriInterpCx<'tcx> {
1113 self
1114 }
1115 #[inline(always)]
1116 fn eval_context_mut(&mut self) -> &mut MiriInterpCx<'tcx> {
1117 self
1118 }
1119}
1120
1121impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
1123 type MemoryKind = MiriMemoryKind;
1124 type ExtraFnVal = DynSym;
1125
1126 type FrameExtra = FrameExtra<'tcx>;
1127 type AllocExtra = AllocExtra<'tcx>;
1128
1129 type Provenance = Provenance;
1130 type ProvenanceExtra = ProvenanceExtra;
1131 type Bytes = MiriAllocBytes;
1132
1133 type MemoryMap =
1134 MonoHashMap<AllocId, (MemoryKind, Allocation<Provenance, Self::AllocExtra, Self::Bytes>)>;
1135
1136 const GLOBAL_KIND: Option<MiriMemoryKind> = Some(MiriMemoryKind::Global);
1137
1138 const PANIC_ON_ALLOC_FAIL: bool = false;
1139
1140 #[inline(always)]
1141 fn enforce_alignment(ecx: &MiriInterpCx<'tcx>) -> bool {
1142 ecx.machine.check_alignment != AlignmentCheck::None
1143 }
1144
1145 #[inline(always)]
1146 fn alignment_check(
1147 ecx: &MiriInterpCx<'tcx>,
1148 alloc_id: AllocId,
1149 alloc_align: Align,
1150 alloc_kind: AllocKind,
1151 offset: Size,
1152 align: Align,
1153 ) -> Option<Misalignment> {
1154 if ecx.machine.check_alignment != AlignmentCheck::Symbolic {
1155 return None;
1157 }
1158 if alloc_kind != AllocKind::LiveData {
1159 return None;
1161 }
1162 let (promised_offset, promised_align) = ecx
1164 .machine
1165 .symbolic_alignment
1166 .borrow()
1167 .get(&alloc_id)
1168 .copied()
1169 .unwrap_or((Size::ZERO, alloc_align));
1170 if promised_align < align {
1171 Some(Misalignment { has: promised_align, required: align })
1173 } else {
1174 let distance = offset.bytes().wrapping_sub(promised_offset.bytes());
1176 if distance.is_multiple_of(align.bytes()) {
1178 None
1180 } else {
1181 let distance_pow2 = 1 << distance.trailing_zeros();
1183 Some(Misalignment {
1184 has: Align::from_bytes(distance_pow2).unwrap(),
1185 required: align,
1186 })
1187 }
1188 }
1189 }
1190
1191 #[inline(always)]
1192 fn enforce_validity(ecx: &MiriInterpCx<'tcx>, _layout: TyAndLayout<'tcx>) -> bool {
1193 ecx.machine.validation != ValidationMode::No
1194 }
1195 #[inline(always)]
1196 fn enforce_validity_recursively(
1197 ecx: &InterpCx<'tcx, Self>,
1198 _layout: TyAndLayout<'tcx>,
1199 ) -> bool {
1200 ecx.machine.validation == ValidationMode::Deep
1201 }
1202
1203 #[inline(always)]
1204 fn ignore_optional_overflow_checks(ecx: &MiriInterpCx<'tcx>) -> bool {
1205 !ecx.tcx.sess.overflow_checks()
1206 }
1207
1208 fn check_fn_target_features(
1209 ecx: &MiriInterpCx<'tcx>,
1210 instance: ty::Instance<'tcx>,
1211 ) -> InterpResult<'tcx> {
1212 let attrs = ecx.tcx.codegen_instance_attrs(instance.def);
1213 if attrs
1214 .target_features
1215 .iter()
1216 .any(|feature| !ecx.tcx.sess.target_features.contains(&feature.name))
1217 {
1218 let unavailable = attrs
1219 .target_features
1220 .iter()
1221 .filter(|&feature| {
1222 feature.kind != TargetFeatureKind::Implied
1223 && !ecx.tcx.sess.target_features.contains(&feature.name)
1224 })
1225 .fold(String::new(), |mut s, feature| {
1226 if !s.is_empty() {
1227 s.push_str(", ");
1228 }
1229 s.push_str(feature.name.as_str());
1230 s
1231 });
1232 let msg = format!(
1233 "calling a function that requires unavailable target features: {unavailable}"
1234 );
1235 if ecx.tcx.sess.target.is_like_wasm {
1238 throw_machine_stop!(TerminationInfo::Abort(msg));
1239 } else {
1240 throw_ub_format!("{msg}");
1241 }
1242 }
1243 interp_ok(())
1244 }
1245
1246 #[inline(always)]
1247 fn find_mir_or_eval_fn(
1248 ecx: &mut MiriInterpCx<'tcx>,
1249 instance: ty::Instance<'tcx>,
1250 abi: &FnAbi<'tcx, Ty<'tcx>>,
1251 args: &[FnArg<'tcx>],
1252 dest: &PlaceTy<'tcx>,
1253 ret: Option<mir::BasicBlock>,
1254 unwind: mir::UnwindAction,
1255 ) -> InterpResult<'tcx, Option<(&'tcx mir::Body<'tcx>, ty::Instance<'tcx>)>> {
1256 if ecx.tcx.is_foreign_item(instance.def_id()) {
1258 let _trace = enter_trace_span!("emulate_foreign_item");
1259 let args = MiriInterpCx::copy_fn_args(args); let link_name = Symbol::intern(ecx.tcx.symbol_name(instance).name);
1267 return ecx.emulate_foreign_item(link_name, abi, &args, dest, ret, unwind);
1268 }
1269
1270 if ecx.machine.data_race.as_genmc_ref().is_some()
1271 && ecx.genmc_intercept_function(instance, args, dest)?
1272 {
1273 ecx.return_to_block(ret)?;
1274 return interp_ok(None);
1275 }
1276
1277 let _trace = enter_trace_span!("load_mir");
1279 interp_ok(Some((ecx.load_mir(instance.def, None)?, instance)))
1280 }
1281
1282 #[inline(always)]
1283 fn call_extra_fn(
1284 ecx: &mut MiriInterpCx<'tcx>,
1285 fn_val: DynSym,
1286 abi: &FnAbi<'tcx, Ty<'tcx>>,
1287 args: &[FnArg<'tcx>],
1288 dest: &PlaceTy<'tcx>,
1289 ret: Option<mir::BasicBlock>,
1290 unwind: mir::UnwindAction,
1291 ) -> InterpResult<'tcx> {
1292 let args = MiriInterpCx::copy_fn_args(args); ecx.emulate_dyn_sym(fn_val, abi, &args, dest, ret, unwind)
1294 }
1295
1296 #[inline(always)]
1297 fn call_intrinsic(
1298 ecx: &mut MiriInterpCx<'tcx>,
1299 instance: ty::Instance<'tcx>,
1300 args: &[OpTy<'tcx>],
1301 dest: &PlaceTy<'tcx>,
1302 ret: Option<mir::BasicBlock>,
1303 unwind: mir::UnwindAction,
1304 ) -> InterpResult<'tcx, Option<ty::Instance<'tcx>>> {
1305 ecx.call_intrinsic(instance, args, dest, ret, unwind)
1306 }
1307
1308 #[inline(always)]
1309 fn assert_panic(
1310 ecx: &mut MiriInterpCx<'tcx>,
1311 msg: &mir::AssertMessage<'tcx>,
1312 unwind: mir::UnwindAction,
1313 ) -> InterpResult<'tcx> {
1314 ecx.assert_panic(msg, unwind)
1315 }
1316
1317 fn panic_nounwind(ecx: &mut InterpCx<'tcx, Self>, msg: &str) -> InterpResult<'tcx> {
1318 ecx.start_panic_nounwind(msg)
1319 }
1320
1321 fn unwind_terminate(
1322 ecx: &mut InterpCx<'tcx, Self>,
1323 reason: mir::UnwindTerminateReason,
1324 ) -> InterpResult<'tcx> {
1325 let panic = ecx.tcx.lang_items().get(reason.lang_item()).unwrap();
1327 let panic = ty::Instance::mono(ecx.tcx.tcx, panic);
1328 ecx.call_function(
1329 panic,
1330 ExternAbi::Rust,
1331 &[],
1332 None,
1333 ReturnContinuation::Goto { ret: None, unwind: mir::UnwindAction::Unreachable },
1334 )?;
1335 interp_ok(())
1336 }
1337
1338 #[inline(always)]
1339 fn binary_ptr_op(
1340 ecx: &MiriInterpCx<'tcx>,
1341 bin_op: mir::BinOp,
1342 left: &ImmTy<'tcx>,
1343 right: &ImmTy<'tcx>,
1344 ) -> InterpResult<'tcx, ImmTy<'tcx>> {
1345 ecx.binary_ptr_op(bin_op, left, right)
1346 }
1347
1348 #[inline(always)]
1349 fn generate_nan<F1: Float + FloatConvert<F2>, F2: Float>(
1350 ecx: &InterpCx<'tcx, Self>,
1351 inputs: &[F1],
1352 ) -> F2 {
1353 ecx.generate_nan(inputs)
1354 }
1355
1356 #[inline(always)]
1357 fn apply_float_nondet(
1358 ecx: &mut InterpCx<'tcx, Self>,
1359 val: ImmTy<'tcx>,
1360 ) -> InterpResult<'tcx, ImmTy<'tcx>> {
1361 crate::math::apply_random_float_error_to_imm(ecx, val, 4)
1362 }
1363
1364 #[inline(always)]
1365 fn equal_float_min_max<F: Float>(ecx: &MiriInterpCx<'tcx>, a: F, b: F) -> F {
1366 ecx.equal_float_min_max(a, b)
1367 }
1368
1369 #[inline(always)]
1370 fn float_fuse_mul_add(ecx: &InterpCx<'tcx, Self>) -> bool {
1371 ecx.machine.float_nondet && ecx.machine.rng.borrow_mut().random()
1372 }
1373
1374 #[inline(always)]
1375 fn runtime_checks(
1376 ecx: &InterpCx<'tcx, Self>,
1377 r: mir::RuntimeChecks,
1378 ) -> InterpResult<'tcx, bool> {
1379 interp_ok(r.value(ecx.tcx.sess))
1380 }
1381
1382 #[inline(always)]
1383 fn thread_local_static_pointer(
1384 ecx: &mut MiriInterpCx<'tcx>,
1385 def_id: DefId,
1386 ) -> InterpResult<'tcx, StrictPointer> {
1387 ecx.get_or_create_thread_local_alloc(def_id)
1388 }
1389
1390 fn extern_static_pointer(
1391 ecx: &MiriInterpCx<'tcx>,
1392 def_id: DefId,
1393 ) -> InterpResult<'tcx, StrictPointer> {
1394 let link_name = Symbol::intern(ecx.tcx.symbol_name(Instance::mono(*ecx.tcx, def_id)).name);
1395 let def_ty = ecx.tcx.type_of(def_id).instantiate_identity().skip_norm_wip();
1396 let extern_decl_layout =
1397 ecx.tcx.layout_of(ecx.typing_env().as_query_input(def_ty)).unwrap();
1398
1399 if let Some(&ptr) = ecx.machine.extern_statics.get(&link_name) {
1400 let Provenance::Concrete { alloc_id, .. } = ptr.provenance else {
1404 panic!("extern_statics cannot contain wildcards")
1405 };
1406 let info = ecx.get_alloc_info(alloc_id);
1407 if extern_decl_layout.size != info.size || extern_decl_layout.align.abi != info.align {
1408 throw_unsup_format!(
1409 "extern static `{link_name}` has been declared as `{krate}::{name}` \
1410 with a size of {decl_size} bytes and alignment of {decl_align} bytes, \
1411 but Miri emulates it via an extern static shim \
1412 with a size of {shim_size} bytes and alignment of {shim_align} bytes",
1413 name = ecx.tcx.def_path_str(def_id),
1414 krate = ecx.tcx.crate_name(def_id.krate),
1415 decl_size = extern_decl_layout.size.bytes(),
1416 decl_align = extern_decl_layout.align.bytes(),
1417 shim_size = info.size.bytes(),
1418 shim_align = info.align.bytes(),
1419 )
1420 }
1421 interp_ok(ptr)
1422 } else if ecx.tcx.codegen_fn_attrs(def_id).import_linkage == Some(Linkage::ExternalWeak) {
1423 assert_eq!(
1431 extern_decl_layout.size,
1432 ecx.tcx.data_layout.pointer_size(),
1433 "non-pointer-sized weak static"
1434 );
1435 interp_ok(
1436 ecx.machine
1437 .missing_weak_symbol
1438 .expect("`missing_weak_symbol` should have been initialized"),
1439 )
1440 } else {
1441 throw_unsup_format!("extern static `{link_name}` is not supported by Miri")
1442 }
1443 }
1444
1445 fn init_local_allocation(
1446 ecx: &MiriInterpCx<'tcx>,
1447 id: AllocId,
1448 kind: MemoryKind,
1449 size: Size,
1450 align: Align,
1451 ) -> InterpResult<'tcx, Self::AllocExtra> {
1452 assert!(kind != MiriMemoryKind::Global.into());
1453 MiriMachine::init_allocation(ecx, id, kind, size, align)
1454 }
1455
1456 fn adjust_alloc_root_pointer(
1457 ecx: &MiriInterpCx<'tcx>,
1458 ptr: interpret::Pointer<CtfeProvenance>,
1459 kind: Option<MemoryKind>,
1460 ) -> InterpResult<'tcx, interpret::Pointer<Provenance>> {
1461 let kind = kind.expect("we set our GLOBAL_KIND so this cannot be None");
1462 let alloc_id = ptr.provenance.alloc_id();
1463 if cfg!(debug_assertions) {
1464 match ecx.tcx.try_get_global_alloc(alloc_id) {
1466 Some(GlobalAlloc::Static(def_id)) if ecx.tcx.is_thread_local_static(def_id) => {
1467 panic!("adjust_alloc_root_pointer called on thread-local static")
1468 }
1469 Some(GlobalAlloc::Static(def_id)) if ecx.tcx.is_foreign_item(def_id) => {
1470 panic!("adjust_alloc_root_pointer called on extern static")
1471 }
1472 _ => {}
1473 }
1474 }
1475 let tag = if let Some(borrow_tracker) = &ecx.machine.borrow_tracker {
1477 borrow_tracker.borrow_mut().root_ptr_tag(alloc_id, &ecx.machine)
1478 } else {
1479 BorTag::default()
1481 };
1482 ecx.adjust_alloc_root_pointer(ptr, tag, kind)
1483 }
1484
1485 #[inline(always)]
1487 fn ptr_from_addr_cast(ecx: &MiriInterpCx<'tcx>, addr: u64) -> InterpResult<'tcx, Pointer> {
1488 ecx.ptr_from_addr_cast(addr)
1489 }
1490
1491 #[inline(always)]
1495 fn expose_provenance(
1496 ecx: &InterpCx<'tcx, Self>,
1497 provenance: Self::Provenance,
1498 ) -> InterpResult<'tcx> {
1499 ecx.expose_provenance(provenance)
1500 }
1501
1502 fn ptr_get_alloc(
1514 ecx: &MiriInterpCx<'tcx>,
1515 ptr: StrictPointer,
1516 size: i64,
1517 ) -> Option<(AllocId, Size, Self::ProvenanceExtra)> {
1518 let rel = ecx.ptr_get_alloc(ptr, size);
1519
1520 rel.map(|(alloc_id, size)| {
1521 let tag = match ptr.provenance {
1522 Provenance::Concrete { tag, .. } => ProvenanceExtra::Concrete(tag),
1523 Provenance::Wildcard => ProvenanceExtra::Wildcard,
1524 };
1525 (alloc_id, size, tag)
1526 })
1527 }
1528
1529 fn adjust_global_allocation<'b>(
1538 ecx: &InterpCx<'tcx, Self>,
1539 id: AllocId,
1540 alloc: &'b Allocation,
1541 ) -> InterpResult<'tcx, Cow<'b, Allocation<Self::Provenance, Self::AllocExtra, Self::Bytes>>>
1542 {
1543 let alloc = alloc.adjust_from_tcx(
1544 &ecx.tcx,
1545 |bytes, align| ecx.get_global_alloc_bytes(id, bytes, align),
1546 |ptr| ecx.global_root_pointer(ptr),
1547 )?;
1548 let kind = MiriMemoryKind::Global.into();
1549 let extra = MiriMachine::init_allocation(ecx, id, kind, alloc.size(), alloc.align)?;
1550 interp_ok(Cow::Owned(alloc.with_extra(extra)))
1551 }
1552
1553 #[inline(always)]
1554 fn before_memory_read(
1555 _tcx: TyCtxtAt<'tcx>,
1556 machine: &Self,
1557 alloc_extra: &AllocExtra<'tcx>,
1558 ptr: Pointer,
1559 (alloc_id, prov_extra): (AllocId, Self::ProvenanceExtra),
1560 range: AllocRange,
1561 ) -> InterpResult<'tcx> {
1562 if machine.track_alloc_accesses && machine.tracked_alloc_ids.contains(&alloc_id) {
1563 machine.emit_diagnostic(NonHaltingDiagnostic::AccessedAlloc(
1564 alloc_id,
1565 range,
1566 borrow_tracker::AccessKind::Read,
1567 ));
1568 }
1569 match &machine.data_race {
1571 GlobalDataRaceHandler::None => {}
1572 GlobalDataRaceHandler::Genmc(genmc_ctx) =>
1573 genmc_ctx.memory_load(machine, ptr.addr(), range.size)?,
1574 GlobalDataRaceHandler::Vclocks(_data_race) => {
1575 let _trace = enter_trace_span!(data_race::before_memory_read);
1576 let AllocDataRaceHandler::Vclocks(data_race, _weak_memory) = &alloc_extra.data_race
1577 else {
1578 unreachable!();
1579 };
1580 data_race.read_non_atomic(alloc_id, range, NaReadType::Read, None, machine)?;
1581 }
1582 }
1583 if let Some(borrow_tracker) = &alloc_extra.borrow_tracker {
1584 borrow_tracker.before_memory_read(alloc_id, prov_extra, range, machine)?;
1585 }
1586 for (_offset, obj) in alloc_extra.sync_objs.range(range.start..range.end()) {
1588 obj.on_access(concurrency::sync::AccessKind::Read)?;
1589 }
1590
1591 interp_ok(())
1592 }
1593
1594 #[inline(always)]
1595 fn before_memory_write(
1596 _tcx: TyCtxtAt<'tcx>,
1597 machine: &mut Self,
1598 alloc_extra: &mut AllocExtra<'tcx>,
1599 ptr: Pointer,
1600 (alloc_id, prov_extra): (AllocId, Self::ProvenanceExtra),
1601 range: AllocRange,
1602 ) -> InterpResult<'tcx> {
1603 if machine.track_alloc_accesses && machine.tracked_alloc_ids.contains(&alloc_id) {
1604 machine.emit_diagnostic(NonHaltingDiagnostic::AccessedAlloc(
1605 alloc_id,
1606 range,
1607 borrow_tracker::AccessKind::Write,
1608 ));
1609 }
1610 match &machine.data_race {
1611 GlobalDataRaceHandler::None => {}
1612 GlobalDataRaceHandler::Genmc(genmc_ctx) =>
1613 genmc_ctx.memory_store(machine, ptr.addr(), range.size)?,
1614 GlobalDataRaceHandler::Vclocks(_global_state) => {
1615 let _trace = enter_trace_span!(data_race::before_memory_write);
1616 let AllocDataRaceHandler::Vclocks(data_race, weak_memory) =
1617 &mut alloc_extra.data_race
1618 else {
1619 unreachable!()
1620 };
1621 data_race.write_non_atomic(alloc_id, range, NaWriteType::Write, None, machine)?;
1622 if let Some(weak_memory) = weak_memory {
1623 weak_memory
1624 .non_atomic_write(range, machine.data_race.as_vclocks_ref().unwrap());
1625 }
1626 }
1627 }
1628 if let Some(borrow_tracker) = &mut alloc_extra.borrow_tracker {
1629 borrow_tracker.before_memory_write(alloc_id, prov_extra, range, machine)?;
1630 }
1631 if !alloc_extra.sync_objs.is_empty() {
1634 let mut to_delete = vec![];
1635 for (offset, obj) in alloc_extra.sync_objs.range(range.start..range.end()) {
1636 obj.on_access(concurrency::sync::AccessKind::Write)?;
1637 if obj.delete_on_write() {
1638 to_delete.push(*offset);
1639 }
1640 }
1641 for offset in to_delete {
1642 alloc_extra.sync_objs.remove(&offset);
1643 }
1644 }
1645 interp_ok(())
1646 }
1647
1648 #[inline(always)]
1649 fn before_memory_deallocation(
1650 _tcx: TyCtxtAt<'tcx>,
1651 machine: &mut Self,
1652 alloc_extra: &mut AllocExtra<'tcx>,
1653 ptr: Pointer,
1654 (alloc_id, prove_extra): (AllocId, Self::ProvenanceExtra),
1655 size: Size,
1656 align: Align,
1657 kind: MemoryKind,
1658 ) -> InterpResult<'tcx> {
1659 if machine.tracked_alloc_ids.contains(&alloc_id) {
1660 machine.emit_diagnostic(NonHaltingDiagnostic::FreedAlloc(alloc_id));
1661 }
1662 match &machine.data_race {
1663 GlobalDataRaceHandler::None => {}
1664 GlobalDataRaceHandler::Genmc(genmc_ctx) =>
1665 genmc_ctx.handle_dealloc(machine, alloc_id, ptr.addr(), kind)?,
1666 GlobalDataRaceHandler::Vclocks(_global_state) => {
1667 let _trace = enter_trace_span!(data_race::before_memory_deallocation);
1668 let data_race = alloc_extra.data_race.as_vclocks_mut().unwrap();
1669 data_race.write_non_atomic(
1670 alloc_id,
1671 alloc_range(Size::ZERO, size),
1672 NaWriteType::Deallocate,
1673 None,
1674 machine,
1675 )?;
1676 }
1677 }
1678 if let Some(borrow_tracker) = &mut alloc_extra.borrow_tracker {
1679 borrow_tracker.before_memory_deallocation(alloc_id, prove_extra, size, machine)?;
1680 }
1681 for obj in alloc_extra.sync_objs.values() {
1683 obj.on_access(concurrency::sync::AccessKind::Dealloc)?;
1684 }
1685
1686 if let Some((_, deallocated_at)) = machine.allocation_spans.borrow_mut().get_mut(&alloc_id)
1687 {
1688 *deallocated_at = Some(machine.current_user_relevant_span());
1689 }
1690 machine.free_alloc_id(alloc_id, size, align, kind);
1691 interp_ok(())
1692 }
1693
1694 #[inline(always)]
1695 fn retag_ptr_value(
1696 ecx: &mut InterpCx<'tcx, Self>,
1697 val: &ImmTy<'tcx>,
1698 ty: Ty<'tcx>,
1699 ) -> InterpResult<'tcx, Option<ImmTy<'tcx>>> {
1700 if ecx.machine.borrow_tracker.is_some() {
1701 ecx.retag_ptr_value(val, ty)
1702 } else {
1703 interp_ok(None)
1704 }
1705 }
1706
1707 #[inline(always)]
1708 fn with_retag_mode<T>(
1709 ecx: &mut InterpCx<'tcx, Self>,
1710 mode: RetagMode,
1711 f: impl FnOnce(&mut InterpCx<'tcx, Self>) -> InterpResult<'tcx, T>,
1712 ) -> InterpResult<'tcx, T> {
1713 if ecx.machine.borrow_tracker.is_some() { ecx.with_retag_mode(mode, f) } else { f(ecx) }
1714 }
1715
1716 fn protect_in_place_function_argument(
1717 ecx: &mut InterpCx<'tcx, Self>,
1718 place: &MPlaceTy<'tcx>,
1719 ) -> InterpResult<'tcx> {
1720 let protected_place = if ecx.machine.borrow_tracker.is_some() {
1723 ecx.protect_place(place)?
1724 } else {
1725 place.clone()
1727 };
1728 ecx.write_uninit(&protected_place)?;
1733 interp_ok(())
1735 }
1736
1737 #[inline(always)]
1738 fn init_frame(
1739 ecx: &mut InterpCx<'tcx, Self>,
1740 frame: Frame<'tcx, Provenance>,
1741 ) -> InterpResult<'tcx, Frame<'tcx, Provenance, FrameExtra<'tcx>>> {
1742 let timing = if let Some(profiler) = ecx.machine.profiler.as_ref() {
1744 let fn_name = frame.instance().to_string();
1745 let entry = ecx.machine.string_cache.entry(fn_name.clone());
1746 let name = entry.or_insert_with(|| profiler.alloc_string(&*fn_name));
1747
1748 Some(profiler.start_recording_interval_event_detached(
1749 *name,
1750 measureme::EventId::from_label(*name),
1751 ecx.active_thread().to_u32(),
1752 ))
1753 } else {
1754 None
1755 };
1756
1757 let borrow_tracker = ecx.machine.borrow_tracker.as_ref();
1758
1759 let extra = FrameExtra {
1760 borrow_tracker: borrow_tracker.map(|bt| bt.borrow_mut().new_frame()),
1761 catch_unwind: None,
1762 timing,
1763 user_relevance: ecx.machine.user_relevance(&frame),
1764 data_race: ecx
1765 .machine
1766 .data_race
1767 .as_vclocks_ref()
1768 .map(|_| data_race::FrameState::default()),
1769 };
1770
1771 interp_ok(frame.with_extra(extra))
1772 }
1773
1774 fn stack<'a>(
1775 ecx: &'a InterpCx<'tcx, Self>,
1776 ) -> &'a [Frame<'tcx, Self::Provenance, Self::FrameExtra>] {
1777 ecx.active_thread_stack()
1778 }
1779
1780 fn stack_mut<'a>(
1781 ecx: &'a mut InterpCx<'tcx, Self>,
1782 ) -> &'a mut Vec<Frame<'tcx, Self::Provenance, Self::FrameExtra>> {
1783 ecx.active_thread_stack_mut()
1784 }
1785
1786 fn before_terminator(ecx: &mut InterpCx<'tcx, Self>) -> InterpResult<'tcx> {
1787 ecx.machine.basic_block_count += 1u64; ecx.machine.since_gc += 1;
1789 if let Some(report_progress) = ecx.machine.report_progress {
1791 if ecx.machine.basic_block_count.is_multiple_of(u64::from(report_progress)) {
1792 ecx.emit_diagnostic(NonHaltingDiagnostic::ProgressReport {
1793 block_count: ecx.machine.basic_block_count,
1794 });
1795 }
1796 }
1797
1798 if ecx.machine.gc_interval > 0 && ecx.machine.since_gc >= ecx.machine.gc_interval {
1803 ecx.machine.since_gc = 0;
1804 ecx.run_provenance_gc();
1805 }
1806
1807 ecx.maybe_preempt_active_thread();
1810
1811 ecx.machine.monotonic_clock.tick();
1813
1814 interp_ok(())
1815 }
1816
1817 #[inline(always)]
1818 fn after_stack_push(ecx: &mut InterpCx<'tcx, Self>) -> InterpResult<'tcx> {
1819 if ecx.frame().extra.user_relevance >= ecx.active_thread_ref().current_user_relevance() {
1820 let stack_len = ecx.active_thread_stack().len();
1823 ecx.active_thread_mut().set_top_user_relevant_frame(stack_len - 1);
1824 }
1825 interp_ok(())
1826 }
1827
1828 fn before_stack_pop(ecx: &mut InterpCx<'tcx, Self>) -> InterpResult<'tcx> {
1829 let frame = ecx.frame();
1830 if ecx.machine.borrow_tracker.is_some() {
1833 ecx.on_stack_pop(frame)?;
1834 }
1835 if ecx
1836 .active_thread_ref()
1837 .top_user_relevant_frame()
1838 .expect("there should always be a most relevant frame for a non-empty stack")
1839 == ecx.frame_idx()
1840 {
1841 ecx.active_thread_mut().recompute_top_user_relevant_frame(1);
1847 }
1848 info!("Leaving {}", ecx.frame().instance());
1852 interp_ok(())
1853 }
1854
1855 #[inline(always)]
1856 fn after_stack_pop(
1857 ecx: &mut InterpCx<'tcx, Self>,
1858 frame: Frame<'tcx, Provenance, FrameExtra<'tcx>>,
1859 unwinding: bool,
1860 ) -> InterpResult<'tcx, ReturnAction> {
1861 let res = {
1862 let mut frame = frame;
1864 let timing = frame.extra.timing.take();
1865 let res = ecx.handle_stack_pop_unwind(frame.extra, unwinding);
1866 if let Some(profiler) = ecx.machine.profiler.as_ref() {
1867 profiler.finish_recording_interval_event(timing.unwrap());
1868 }
1869 res
1870 };
1871 if !ecx.active_thread_stack().is_empty() {
1874 info!("Continuing in {}", ecx.frame().instance());
1875 }
1876 res
1877 }
1878
1879 fn after_local_read(
1880 ecx: &InterpCx<'tcx, Self>,
1881 frame: &Frame<'tcx, Provenance, FrameExtra<'tcx>>,
1882 local: mir::Local,
1883 ) -> InterpResult<'tcx> {
1884 if let Some(data_race) = &frame.extra.data_race {
1885 let _trace = enter_trace_span!(data_race::after_local_read);
1886 data_race.local_read(local, &ecx.machine);
1887 }
1888 interp_ok(())
1889 }
1890
1891 fn after_local_write(
1892 ecx: &mut InterpCx<'tcx, Self>,
1893 local: mir::Local,
1894 storage_live: bool,
1895 ) -> InterpResult<'tcx> {
1896 if let Some(data_race) = &ecx.frame().extra.data_race {
1897 let _trace = enter_trace_span!(data_race::after_local_write);
1898 data_race.local_write(local, storage_live, &ecx.machine);
1899 }
1900 interp_ok(())
1901 }
1902
1903 fn after_local_moved_to_memory(
1904 ecx: &mut InterpCx<'tcx, Self>,
1905 local: mir::Local,
1906 mplace: &MPlaceTy<'tcx>,
1907 ) -> InterpResult<'tcx> {
1908 let Some(Provenance::Concrete { alloc_id, .. }) = mplace.ptr().provenance else {
1909 panic!("after_local_allocated should only be called on fresh allocations");
1910 };
1911 let local_decl = &ecx.frame().body().local_decls[local];
1913 let span = local_decl.source_info.span;
1914 ecx.machine.allocation_spans.borrow_mut().insert(alloc_id, (span, None));
1915 let (alloc_info, machine) = ecx.get_alloc_extra_mut(alloc_id)?;
1917 if let Some(data_race) =
1918 &machine.threads.active_thread_stack().last().unwrap().extra.data_race
1919 {
1920 let _trace = enter_trace_span!(data_race::after_local_moved_to_memory);
1921 data_race.local_moved_to_memory(
1922 local,
1923 alloc_info.data_race.as_vclocks_mut().unwrap(),
1924 machine,
1925 );
1926 }
1927 interp_ok(())
1928 }
1929
1930 fn get_global_alloc_salt(
1931 ecx: &InterpCx<'tcx, Self>,
1932 instance: Option<ty::Instance<'tcx>>,
1933 ) -> usize {
1934 let unique = if let Some(instance) = instance {
1935 let is_generic = instance
1948 .args
1949 .into_iter()
1950 .any(|arg| !matches!(arg.kind(), ty::GenericArgKind::Lifetime(_)));
1951 let can_be_inlined = matches!(
1952 ecx.tcx.sess.opts.unstable_opts.cross_crate_inline_threshold,
1953 InliningThreshold::Always
1954 ) || !matches!(
1955 ecx.tcx.codegen_instance_attrs(instance.def).inline,
1956 InlineAttr::Never
1957 );
1958 !is_generic && !can_be_inlined
1959 } else {
1960 false
1962 };
1963 if unique {
1965 CTFE_ALLOC_SALT
1966 } else {
1967 ecx.machine.rng.borrow_mut().random_range(0..ADDRS_PER_ANON_GLOBAL)
1968 }
1969 }
1970
1971 fn cached_union_data_range<'e>(
1972 ecx: &'e mut InterpCx<'tcx, Self>,
1973 ty: Ty<'tcx>,
1974 compute_range: impl FnOnce() -> RangeSet,
1975 ) -> Cow<'e, RangeSet> {
1976 Cow::Borrowed(ecx.machine.union_data_ranges.entry(ty).or_insert_with(compute_range))
1977 }
1978
1979 fn get_default_alloc_params(&self) -> <Self::Bytes as AllocBytes>::AllocParams {
1980 use crate::alloc::MiriAllocParams;
1981
1982 match &self.allocator {
1983 Some(alloc) => MiriAllocParams::Isolated(alloc.clone()),
1984 None => MiriAllocParams::Global,
1985 }
1986 }
1987
1988 fn enter_trace_span(span: impl FnOnce() -> tracing::Span) -> impl EnteredTraceSpan {
1989 #[cfg(feature = "tracing")]
1990 {
1991 span().entered()
1992 }
1993 #[cfg(not(feature = "tracing"))]
1994 #[expect(clippy::unused_unit)]
1995 {
1996 let _ = span; ()
1998 }
1999 }
2000}
2001
2002pub trait MachineCallback<'tcx, T>: VisitProvenance {
2004 fn call(
2006 self: Box<Self>,
2007 ecx: &mut InterpCx<'tcx, MiriMachine<'tcx>>,
2008 arg: T,
2009 ) -> InterpResult<'tcx>;
2010}
2011
2012pub type DynMachineCallback<'tcx, T> = Box<dyn MachineCallback<'tcx, T> + 'tcx>;
2014
2015#[macro_export]
2032macro_rules! callback {
2033 (@capture<$tcx:lifetime $(,)? $($lft:lifetime),*>
2034 { $($name:ident: $type:ty),* $(,)? }
2035 |$this:ident, $arg:ident: $arg_ty:ty| $body:expr $(,)?) => {{
2036 struct Callback<$tcx, $($lft),*> {
2037 $($name: $type,)*
2038 _phantom: std::marker::PhantomData<&$tcx ()>,
2039 }
2040
2041 impl<$tcx, $($lft),*> VisitProvenance for Callback<$tcx, $($lft),*> {
2042 fn visit_provenance(&self, _visit: &mut VisitWith<'_>) {
2043 $(
2044 self.$name.visit_provenance(_visit);
2045 )*
2046 }
2047 }
2048
2049 impl<$tcx, $($lft),*> MachineCallback<$tcx, $arg_ty> for Callback<$tcx, $($lft),*> {
2050 fn call(
2051 self: Box<Self>,
2052 $this: &mut MiriInterpCx<$tcx>,
2053 $arg: $arg_ty
2054 ) -> InterpResult<$tcx> {
2055 #[allow(unused_variables)]
2056 let Callback { $($name,)* _phantom } = *self;
2057 $body
2058 }
2059 }
2060
2061 Box::new(Callback {
2062 $($name,)*
2063 _phantom: std::marker::PhantomData
2064 })
2065 }};
2066}