rapx/vm/mir_rules/call/
function_var.rs1use rustc_hir::def_id::DefId;
2use rustc_middle::{mir::Local, ty::TyKind};
3use z3::ast::Int;
4
5use super::super::super::state::{AllocId, VmExecutor, VmValue};
6use crate::compat::FxHashSet;
7
8impl<'z3, 'tcx> VmExecutor<'z3, 'tcx> {
9 pub(crate) fn apply_function_var_effect(
15 &mut self,
16 args: &[VmValue<'z3, 'tcx>],
17 caller_arg_locals: &[Option<Local>],
18 dest: Local,
19 callee: DefId,
20 ) {
21 let ty = self.body().local_decls[dest].ty;
22 if !crate::analysis::dataflow::call_summary::function_var::supports_result(ty) {
23 self.record_missing_mir(callee);
24 return;
25 }
26 let input_receiver = caller_arg_locals
27 .first()
28 .copied()
29 .flatten()
30 .is_some_and(|local| local.as_usize() <= self.body().arg_count)
31 || args
32 .first()
33 .and_then(VmValue::provenance_alloc_id)
34 .is_some_and(|id| {
35 (1..=self.body().arg_count).any(|index| {
36 self.path_state.current_frame
37 .local_alloc
38 .get(&Local::from_usize(index))
39 == Some(&id)
40 })
41 });
42 if !input_receiver {
43 self.record_missing_mir(callee);
44 return;
45 }
46 for (index, arg) in args.iter().enumerate().skip(1) {
50 let resolved =
51 match arg.ty.kind() {
52 TyKind::Ref(..) | TyKind::RawPtr(..) => arg.provenance.is_some(),
53 TyKind::Tuple(fields) => fields.iter().enumerate().all(|(field, ty)| match ty
54 .kind()
55 {
56 TyKind::Ref(..) | TyKind::RawPtr(..) => caller_arg_locals
57 .get(index)
58 .copied()
59 .flatten()
60 .and_then(|local| self.path_state.current_frame.local_alloc.get(&local))
61 .and_then(|id| self.load_value(*id, arg.ty, &[field]))
62 .and_then(VmValue::provenance_alloc_id)
63 .is_some(),
64 TyKind::Bool
65 | TyKind::Char
66 | TyKind::Int(_)
67 | TyKind::Uint(_)
68 | TyKind::Float(_)
69 | TyKind::Never => true,
70 _ => false,
71 }),
72 _ => false,
73 };
74 if !resolved {
75 self.record_missing_mir(callee);
76 return;
77 }
78 }
79
80 let private: FxHashSet<_> = self.path_state.current_frame.local_alloc.values().copied().collect();
81 let borrowed: FxHashSet<_> = self.path_state
82 .current_frame
83 .local_alloc
84 .iter()
85 .filter_map(|(local, id)| {
86 (matches!(self.body().local_decls[*local].ty.kind(), TyKind::Ref(..))
87 && !self.alloc(*id).facts.dead)
88 .then(|| self.local_value(*local))
89 .flatten()
90 .and_then(VmValue::provenance_alloc_id)
91 .map(|id| self.root_alloc(id))
92 })
93 .collect();
94 let mut affected: Vec<_> = (0..self.path_state.mem_units.len())
95 .map(AllocId)
96 .filter(|id| !private.contains(id))
97 .collect();
98 affected.extend(args.iter().filter_map(VmValue::provenance_alloc_id));
99 for local in caller_arg_locals.iter().flatten() {
100 if let Some(id) = self.path_state.current_frame.local_alloc.get(local) {
101 affected.extend(
102 self.content(*id)
103 .values
104 .values()
105 .filter_map(VmValue::provenance_alloc_id),
106 );
107 }
108 }
109 affected = affected.into_iter().map(|id| self.root_alloc(id)).collect();
110 affected.sort_by_key(|id| id.0);
111 affected.dedup();
112 for id in affected {
113 let may_drop = !borrowed.contains(&id);
114 if may_drop {
115 self.alloc_mut(id).facts.function_var_may_drop = true;
116 }
117 self.forget_memory_values(id);
118 for alias in 0..self.path_state.mem_units.len() {
119 let alias = AllocId(alias);
120 if self.root_alloc(alias) == id {
121 let facts = &mut self.alloc_mut(alias).facts;
122 facts.owning = None;
123 if may_drop {
124 facts.liveness = None;
125 }
126 }
127 }
128 }
129
130 let term = self.fresh_int("function_var_result");
131 if matches!(ty.kind(), TyKind::Bool) {
132 let zero = Int::from_u64(self.z3_ctx, 0);
133 let one = Int::from_u64(self.z3_ctx, 1);
134 self.path_state.constraints.assertions.push(term.ge(&zero));
135 self.path_state.constraints.assertions.push(term.le(&one));
136 }
137 let mut result = VmValue::new(term, ty);
138 result.facts.init = true;
139 self.set_local(dest, result);
140 }
141}