Skip to main content

rapx/vm/
inspection.rs

1//! VM observations and optional tracing used by verification and other checkers.
2
3use std::cell::{Ref, RefCell};
4
5use rustc_hir::def_id::DefId;
6use rustc_middle::{mir::Local, ty::Ty};
7use z3::ast::Int;
8
9use crate::compat::FxHashSet;
10use crate::vm::{VmExecutor, diagnostics::Diagnostics, state::{AllocId, MemoryEvent, VmValue}};
11
12impl<'z3, 'tcx> VmExecutor<'z3, 'tcx> {
13    pub(crate) fn diagnostics(&self) -> &Diagnostics {
14        &self.path_state.diagnostics
15    }
16
17    pub(crate) fn recover_copy_memory_target(&mut self, before: &Diagnostics) {
18        self.path_state.diagnostics.recover_copy_memory_target(before);
19    }
20
21    pub(crate) fn recover_missing_mir(&mut self, before: &Diagnostics) {
22        self.path_state.diagnostics.recover_missing_mir(before);
23    }
24
25    pub(crate) fn current_def_id(&self) -> DefId {
26        self.path_state.current_frame.current_def_id
27    }
28
29    pub(crate) fn local_alloc_id(&self, local: Local) -> Option<AllocId> {
30        self.path_state.current_frame.local_alloc.get(&local).copied()
31    }
32
33    pub(crate) fn local_allocations(&self) -> impl Iterator<Item = (Local, AllocId)> + '_ {
34        self.path_state.current_frame.local_alloc.iter().map(|(&local, &id)| (local, id))
35    }
36
37    pub(crate) fn cached_size(&self, ty: Ty<'tcx>) -> Option<&Int<'z3>> {
38        self.path_state.constraints.term_caches.sizes.get(&ty)
39    }
40
41    pub(crate) fn incomplete_inline_terms(&self) -> impl Iterator<Item = &FxHashSet<Int<'z3>>> {
42        [&self.path_state.inline.inline_truncated_terms, &self.path_state.inline.inline_branch_terms].into_iter()
43    }
44
45    pub(crate) fn opaque_intrinsic_terms(&self) -> &FxHashSet<Int<'z3>> {
46        &self.path_state.inline.opaque_intrinsic_terms
47    }
48
49    pub(crate) fn has_matching_index_access_fact(&self, slice_val: &VmValue<'z3, 'tcx>, index_val: &VmValue<'z3, 'tcx>) -> bool {
50        let Some(slice_len) = self.slice_len_from_value(slice_val) else { return false };
51        self.path_state.index_access_facts.iter().any(|(slice, index, slice_local, _)| {
52            slice.to_string() == slice_val.z3_term.to_string()
53                && index.to_string() == index_val.z3_term.to_string()
54                && self.local_value(*slice_local).and_then(|value| self.slice_len_from_value(value)) == Some(slice_len.clone())
55        })
56    }
57
58    pub(crate) fn enable_memory_events(&mut self) {
59        self.path_state.memory_events = Some(RefCell::default());
60    }
61
62    pub(crate) fn memory_events(&self) -> Option<Ref<'_, Vec<MemoryEvent>>> {
63        self.path_state.memory_events.as_ref().map(RefCell::borrow)
64    }
65}