1mod arithmetic;
4mod call_contracts;
5pub(crate) mod constructor_policy;
6pub(crate) mod container;
7pub(crate) mod contract_facts;
8mod contract_expr;
9pub(crate) mod diagnostics;
10pub(crate) mod display;
11pub(crate) mod exec;
12pub(crate) mod inline;
13mod inspection;
14mod iterator;
15pub(crate) mod materialize;
16pub(crate) mod memory;
17pub(crate) mod method_cache;
18mod mir_rules;
19pub(crate) mod parameters;
20pub(crate) mod range;
21pub(crate) mod region;
22pub(crate) mod state;
23mod step;
24pub(crate) use step::RelevantItem;
25
26use rustc_hir::def_id::DefId;
27use rustc_middle::{mir::{Body, Local}, ty::TyCtxt};
28use std::cell::RefCell;
29use z3::{Context, ast::Int};
30
31use crate::{compat::{FxHashMap, FxHashSet}, vm::constructor_policy::ConstructorMirMode};
32
33use self::{
34 diagnostics::Diagnostics,
35 method_cache::CachedMethodResult,
36 state::{AllocId, Constraints, FrameState, InlineCtx, MemoryEvent, MemoryUnit, OwnershipTransfer, SavedCallerFrame},
37};
38
39#[derive(Clone)]
44pub(crate) struct VmExecutor<'z3, 'tcx> {
45 target_def_id: DefId,
47 instantiated_target_body: Option<&'tcx Body<'tcx>>,
51 pub(crate) z3_ctx: &'z3 Context,
53 pub(crate) tcx: TyCtxt<'tcx>,
55 constructor_mir_mode: ConstructorMirMode,
57 path_state: VmState<'z3, 'tcx>,
59}
60
61#[derive(Clone)]
66struct VmState<'z3, 'tcx> {
67 current_frame: FrameState<'tcx>,
69 caller_frames: Vec<SavedCallerFrame<'z3, 'tcx>>,
71 mem_units: Vec<MemoryUnit<'z3, 'tcx>>,
73 ownership_transfers: Vec<OwnershipTransfer>,
75 static_allocs: FxHashMap<DefId, AllocId>,
77 constraints: Constraints<'z3, 'tcx>,
79 inline: InlineCtx<'z3, 'tcx>,
81 method_result_cache: Vec<CachedMethodResult<'z3, 'tcx>>,
84 alias_hazard_allocs: FxHashSet<AllocId>,
86 index_access_facts: Vec<(Int<'z3>, Int<'z3>, Local, Local)>,
88 memory_events: Option<RefCell<Vec<MemoryEvent>>>,
90 diagnostics: Diagnostics,
92}
93
94impl<'z3, 'tcx> VmExecutor<'z3, 'tcx> {
95 pub(crate) fn has_alias_hazard(&self, alloc_id: AllocId) -> bool {
97 self.path_state.alias_hazard_allocs.contains(&self.root_alloc(alloc_id))
98 }
99
100 pub(crate) fn new(
102 z3_ctx: &'z3 Context,
103 tcx: TyCtxt<'tcx>,
104 target_def_id: DefId,
105 instantiated_target_body: Option<&'tcx Body<'tcx>>,
106 constructor_mir_mode: ConstructorMirMode,
107 ) -> Self {
108 let mut constraints = Constraints::default();
112 let uninit = Int::fresh_const(z3_ctx, "uninit_byte");
113 constraints.term_caches.uninit_byte = Some(uninit.clone());
114 constraints.assertions.push(uninit.ge(&Int::from_u64(z3_ctx, 256)));
115 Self {
116 target_def_id,
117 instantiated_target_body,
118 z3_ctx,
119 tcx,
120 constructor_mir_mode,
121 path_state: VmState {
122 current_frame: FrameState {
123 current_def_id: target_def_id,
124 instantiated_body: instantiated_target_body,
125 local_alloc: FxHashMap::default(),
126 },
127 caller_frames: Vec::default(),
128 mem_units: Vec::default(),
129 ownership_transfers: Vec::new(),
130 static_allocs: FxHashMap::default(),
131 constraints,
132 inline: InlineCtx::default(),
133 method_result_cache: Vec::default(),
134 alias_hazard_allocs: FxHashSet::default(),
135 index_access_facts: Vec::new(),
136 memory_events: None,
137 diagnostics: Diagnostics::default(),
138 },
139 }
140 }
141}