1use rustc_hir::{def::DefKind, def_id::DefId};
2use rustc_middle::{
3 mir::{
4 BasicBlock, Local, Operand, ProjectionElem, Rvalue, StatementKind, Terminator,
5 TerminatorKind,
6 },
7 ty::{EarlyBinder, Instance, TyKind, TypingEnv},
8};
9use z3::ast::{Bool, Int};
10use z3::{SatResult, Solver};
11
12use super::state::{AllocId, MemoryContent, VmExecutor, VmValue};
13use crate::compat::{FxHashSet, Spanned};
14use crate::util::mir_utils;
15use crate::limit::MAX_INLINE_DEPTH;
16
17type InlineWorkItem<'z3, 'tcx> = (VmExecutor<'z3, 'tcx>, BasicBlock, FxHashSet<BasicBlock>);
18
19enum InlineStep<'z3, 'tcx> {
20 Complete(bool),
21 Fork(Vec<InlineWorkItem<'z3, 'tcx>>),
22 Incomplete,
23}
24
25impl<'z3, 'tcx> VmExecutor<'z3, 'tcx> {
26 pub(crate) fn eval_contract_method(
27 &self,
28 method: DefId,
29 receiver: VmValue<'z3, 'tcx>,
30 receiver_local: Option<Local>,
31 args: Vec<Int<'z3>>,
32 ) -> Option<Int<'z3>> {
33 if !super::method_cache::stable_contract_query(self.tcx, method) {
34 return None;
35 }
36 let sig = self.tcx.fn_sig(method).skip_binder();
37 let inputs = sig.inputs().skip_binder();
38 if inputs.len() != args.len() + 1 {
39 return None;
40 }
41 let mut values = vec![receiver];
42 values.extend(
43 args.into_iter()
44 .zip(inputs[1..].iter())
45 .map(|(term, ty)| VmValue::new(term, *ty)),
46 );
47 self.eval_contract_method_paths(method, &values, receiver_local)
48 }
49
50 fn eval_contract_method_paths(
51 &self,
52 method: DefId,
53 values: &[VmValue<'z3, 'tcx>],
54 receiver_local: Option<Local>,
55 ) -> Option<Int<'z3>> {
56 let mut state = self.clone();
57 let frame = state.enter_frame(method, None);
58 for (index, value) in values.iter().enumerate() {
59 let local = Local::from_usize(index + 1);
60 state.ensure_local_allocation(local);
61 state.set_local(local, value.clone());
62 }
63 if let Some(source) = receiver_local {
64 for path in self.frame_field_paths(&frame, source) {
65 if let Some(value) = self.frame_field_value(&frame, source, &path) {
66 state.set_field_value(Local::from_usize(1), path, value.clone());
67 }
68 }
69 }
70 let baseline = state.path_state.constraints.assertions.len();
71 let body = self.tcx.optimized_mir(method);
72 let mut pending = vec![(state, BasicBlock::from_usize(0), FxHashSet::default())];
73 let mut returns = Vec::new();
74 let mut explored = 0;
75 while let Some((mut state, block, mut visited)) = pending.pop() {
76 if explored >= 128 || !visited.insert(block) {
77 return None;
78 }
79 explored += 1;
80 let data = &body.basic_blocks[block];
81 for statement in &data.statements {
82 crate::vm::mir_rules::exec_contract_method_statement(&mut state, statement);
83 }
84 let terminator = data.terminator();
85 match &terminator.kind {
86 TerminatorKind::Return => {
87 if state.path_state.diagnostics.is_unsupported() || state.path_state.diagnostics.has_incomplete_inline_branch() {
88 return None;
89 }
90 let term = state.local_value(Local::from_usize(0))?.z3_term.clone();
91 let constraints = &state.path_state.constraints.assertions[baseline..];
92 let guard = Bool::and(self.z3_ctx, &constraints.iter().collect::<Vec<_>>());
93 returns.push((guard, term));
94 }
95 TerminatorKind::Goto { target, .. } => pending.push((state, *target, visited)),
96 TerminatorKind::SwitchInt { targets, .. } => {
97 for target in targets.all_targets().iter().copied() {
98 let mut branch = state.clone();
99 if crate::vm::mir_rules::exec_terminator(
100 &mut branch,
101 terminator,
102 Some(target),
103 ).continues() {
104 pending.push((branch, target, visited.clone()));
105 }
106 }
107 }
108 TerminatorKind::Unreachable => {}
109 _ => return None,
110 }
111 }
112 if returns.is_empty() {
113 return None;
114 }
115 let guards: Vec<&Bool<'z3>> = returns.iter().map(|(guard, _)| guard).collect();
116 let coverage = Bool::or(self.z3_ctx, &guards);
117 let solver = Solver::new(self.z3_ctx);
118 self.assert_all(&solver);
119 solver.assert(&coverage.not());
120 if solver.check() != SatResult::Unsat {
121 return None;
122 }
123 let mut result = None;
124 for (guard, term) in returns.into_iter().rev() {
125 result = Some(match result {
126 Some(other) => guard.ite(&term, &other),
127 None => term,
128 });
129 }
130 result
131 }
132
133 pub(super) fn exec_inline_call(
139 &mut self,
140 callee_def_id: DefId,
141 instance: Option<Instance<'tcx>>,
142 call_args: &[Spanned<Operand<'tcx>>],
143 arg_values: &[VmValue<'z3, 'tcx>],
144 caller_arg_locals: &[Option<Local>],
145 dest: Local,
146 ) -> bool {
147 if self.path_state.inline.inline_depth >= MAX_INLINE_DEPTH {
148 let dest_ty = self.body().local_decls[dest].ty;
152 let truncated_term = self.fresh_int("inline_truncated");
153 self.path_state.inline
154 .inline_truncated_terms
155 .insert(truncated_term.clone());
156 self.set_local(dest, VmValue::new(truncated_term, dest_ty));
157 return true;
158 }
159 let instantiated_body = if let Some(instance) = instance {
160 let caller_def_id = self.path_state.current_frame.current_def_id;
161 if mir_utils::has_unresolved_const_args(self.tcx, caller_def_id, instance.args) {
162 self.record_missing_mir(callee_def_id);
163 return true;
164 }
165 let body = self.tcx.optimized_mir(callee_def_id).clone();
166 let Ok(body) = instance.try_instantiate_mir_and_normalize_erasing_regions(
167 self.tcx,
168 TypingEnv::post_analysis(self.tcx, caller_def_id),
169 EarlyBinder::bind(self.tcx, body),
170 ) else {
171 self.record_missing_mir(callee_def_id);
172 return true;
173 };
174 Some(&*self.tcx.arena.alloc(body))
175 } else {
176 None
177 };
178 let before_inline = self.clone();
179 self.path_state.inline.inline_depth += 1;
180 self.path_state.inline.symbolic_generic_const_inline |= instance.as_ref().is_some_and(|instance| {
181 instance.args.types().any(mir_utils::ty_has_type_param)
182 && mir_utils::has_generic_const_call_argument(self.tcx, callee_def_id)
183 });
184
185 let caller_local_count = self.body().local_decls.len();
190 let resolved_arg_referents: Vec<Option<Local>> = arg_values
191 .iter()
192 .enumerate()
193 .map(|(index, value)| {
194 if let super::state::ValueSource::FieldBorrow { base, .. } = &value.source {
195 return Some(*base);
196 }
197 let arg_local = caller_arg_locals.get(index).copied().flatten();
198 let address_referent = self.find_local_by_address(&value.z3_term);
199 let borrow_referent =
200 arg_local.and_then(|local| self.find_whole_borrow_referent(local));
201 let referent = if address_referent == arg_local {
202 borrow_referent.or(address_referent)
203 } else {
204 address_referent.or(borrow_referent)
205 };
206 referent.or_else(|| {
207 let local = caller_arg_locals.get(index).copied().flatten()?;
208 local
209 .as_usize()
210 .checked_sub(1)
211 .and_then(|index| self.path_state.inline.arg_referents.get(index))
212 .copied()
213 .flatten()?;
214 let provenance = value.provenance_alloc_id()?;
215 (self
216 .local_value(local)
217 .and_then(VmValue::provenance_alloc_id)
218 == Some(provenance))
219 .then_some(local)
220 })
221 })
222 .collect();
223 if resolved_arg_referents
224 .iter()
225 .flatten()
226 .any(|local| local.as_usize() >= caller_local_count)
227 {
228 self.path_state.diagnostics.mark_unsupported();
229 }
230 let inline_arg_referents: Vec<Option<Local>> = resolved_arg_referents
231 .into_iter()
232 .map(|local| local.filter(|local| local.as_usize() < caller_local_count))
233 .collect();
234 let caller_referents = inline_arg_referents.clone();
235 let reborrow_referents: Vec<Option<Local>> = caller_arg_locals
239 .iter()
240 .map(|arg_opt| arg_opt.and_then(|a| self.find_whole_borrow_referent(a)))
241 .collect();
242 let arg_inner_uninit: Vec<bool> = caller_arg_locals
243 .iter()
244 .map(|arg| {
245 arg.and_then(|local| self.path_state.current_frame.local_alloc.get(&local))
246 .is_some_and(|id| self.content(*id).facts.inner_uninit)
247 })
248 .collect();
249 let frame = self.enter_frame(callee_def_id, instantiated_body);
250 let saved_inline_arg_referents =
251 std::mem::replace(&mut self.path_state.inline.arg_referents, inline_arg_referents);
252 let saved_deferred_field_writes = std::mem::take(&mut self.path_state.inline.deferred_field_writes);
253
254 for (i, arg_val) in arg_values.iter().enumerate() {
255 let callee_local = Local::from_usize(i + 1);
256 self.ensure_local_allocation(callee_local);
257 let mut arg_val = arg_val.clone();
258 if let super::state::ValueSource::FieldBorrow { base, .. } = &mut arg_val.source {
259 *base = callee_local;
260 }
261 self.set_local(callee_local, arg_val);
262 if arg_inner_uninit.get(i) == Some(&true)
263 && let Some(&id) = self.path_state.current_frame.local_alloc.get(&callee_local)
264 {
265 self.content_mut(id).facts.inner_uninit = true;
266 }
267 if let Some(Operand::Constant(constant)) = call_args.get(i).map(|arg| &arg.node) {
268 self.materialize_const_adt(callee_local, &constant.const_);
269 }
270 }
271
272 for (i, caller_arg_opt) in caller_arg_locals.iter().enumerate() {
276 let callee_param = Local::from_usize(i + 1);
277 let Some(caller_arg) = caller_arg_opt else {
278 continue;
279 };
280 let mut source_locals = vec![*caller_arg];
285 if let Some(r) = reborrow_referents.get(i).copied().flatten()
286 && r != *caller_arg
287 {
288 source_locals.push(r);
289 }
290 for src in source_locals {
291 let caller_field_keys: Vec<Vec<usize>> = self.frame_field_paths(&frame, src);
292 for fields in caller_field_keys {
293 if let Some(fv) = self.frame_field_value(&frame, src, &fields).cloned() {
294 self.set_field_value(callee_param, fields, fv);
295 }
296 }
297 }
298 }
299
300 let pre_uninit: Vec<AllocId> = caller_referents
301 .iter()
302 .flatten()
303 .filter(|local| {
304 crate::def_ids::strips_to_maybe_uninit(before_inline.body().local_decls[**local].ty)
305 })
306 .filter_map(|local| before_inline.path_state.current_frame.local_alloc.get(local).copied())
307 .filter(|id| before_inline.allocation_needs_write(*id))
308 .collect();
309 let may_recurse = self.body().basic_blocks.iter().any(|block| {
310 let TerminatorKind::Call { func, .. } = &block.terminator().kind else {
311 return false;
312 };
313 mir_utils::resolve_callee_def_id(self.tcx, callee_def_id, func)
314 == Some(callee_def_id)
315 });
316 let mut returns = self.inline_execute_body(&pre_uninit);
317 let return_ty = self.body().local_decls[Local::from_usize(0)].ty;
318 if crate::util::mir_utils::layout_of_ty(self.tcx, callee_def_id, return_ty)
319 .is_some_and(|layout| layout.size.bytes() == 0)
320 && let Some(states) = returns.as_mut()
321 {
322 for state in states {
323 if state.local_value(Local::from_usize(0)).is_none() {
324 let term = state.fresh_int("zst_return");
325 state.set_local(Local::from_usize(0), VmValue::new(term, return_ty));
326 }
327 }
328 }
329 if returns.as_ref().is_none_or(|states| {
330 states
331 .iter()
332 .any(|state| state.local_value(Local::from_usize(0)).is_none())
333 }) {
334 let witnessed: Vec<_> = self.path_state
335 .mem_units
336 .iter()
337 .enumerate()
338 .filter_map(|(index, unit)| {
339 unit.content.facts.may_be_uninit.then_some(AllocId(index))
340 })
341 .collect();
342 let explored_diagnostics = self.path_state.diagnostics.clone();
343 *self = before_inline;
344 for id in witnessed {
345 if let Some(unit) = self.path_state.mem_units.get_mut(id.0) {
346 unit.content.facts.may_be_uninit = true;
347 }
348 }
349 self.path_state.diagnostics.merge_inline_fallback(&explored_diagnostics);
350 let dest_ty = self.body().local_decls[dest].ty;
351 let unknown_term = self.fresh_int("inline_branch_unknown");
352 self.path_state.inline.inline_branch_terms.insert(unknown_term.clone());
353 if may_recurse {
354 self.path_state.inline
355 .inline_truncated_terms
356 .insert(unknown_term.clone());
357 }
358 self.set_local(dest, VmValue::new(unknown_term, dest_ty));
359 let mut unknown_effect = matches!(
360 self.tcx.def_kind(callee_def_id),
361 DefKind::Fn | DefKind::AssocFn
362 ) && self.tcx.fn_sig(callee_def_id).skip_binder().safety()
363 == rustc_hir::Safety::Unsafe;
364 for (arg, referent) in arg_values.iter().zip(&caller_referents) {
365 match arg.ty.kind() {
366 TyKind::Bool
367 | TyKind::Char
368 | TyKind::Int(_)
369 | TyKind::Uint(_)
370 | TyKind::Float(_) => {}
371 TyKind::Ref(..) | TyKind::RawPtr(..) => {
372 if let Some(local) = referent {
373 self.forget_inline_referent(*local);
374 } else {
375 unknown_effect = true;
376 }
377 }
378 _ => unknown_effect = true,
379 }
380 }
381 self.path_state.diagnostics.mark_unsupported_if(unknown_effect);
382 return true;
383 }
384 let mut completed = Vec::new();
385 for mut state in returns.unwrap() {
386 state.recover_leaked_owner_shares(callee_def_id);
387 let return_val = state.local_value(Local::from_usize(0)).cloned();
388 let return_inner_uninit = state.path_state
389 .current_frame
390 .local_alloc
391 .get(&Local::from_usize(0))
392 .is_some_and(|id| state.content(*id).facts.inner_uninit);
393 let return_fields: Vec<_> = state
394 .field_paths(Local::from_usize(0))
395 .into_iter()
396 .filter_map(|path| {
397 state
398 .field_value(Local::from_usize(0), &path)
399 .cloned()
400 .map(|val| (path, val))
401 })
402 .collect();
403 state.restore_frame(frame.clone());
404 let writes = std::mem::take(&mut state.path_state.inline.deferred_field_writes);
405 state.path_state.inline.arg_referents = saved_inline_arg_referents.clone();
406 state.path_state.inline.deferred_field_writes = saved_deferred_field_writes.clone();
407 state.replay_deferred_writes(writes);
408 let mut val = return_val.unwrap();
409 let dest_ty = state.body().local_decls[dest].ty;
410 val.ty = dest_ty;
411 if val
412 .provenance
413 .as_ref()
414 .is_some_and(|p| p.offset.as_u64() == Some(0))
415 {
416 val.facts.non_null = true;
417 }
418 state.set_local(dest, val);
419 if return_inner_uninit
420 && let Some(&dest_id) = state.path_state.current_frame.local_alloc.get(&dest)
421 {
422 state.content_mut(dest_id).facts.inner_uninit = true;
423 }
424 for (path, fv) in return_fields {
425 state.set_field_value(dest, path, fv);
426 }
427 state.materialize_slice_ref_return(dest);
428 if let Some(dest_alloc_id) = state.path_state.current_frame.local_alloc.get(&dest).copied() {
429 state.content_mut(dest_alloc_id).facts.initialized = true;
430 }
431 state.path_state.inline.inline_depth -= 1;
432 state.path_state.inline.symbolic_generic_const_inline =
433 before_inline.path_state.inline.symbolic_generic_const_inline;
434 completed.push(state);
435 }
436 if completed.is_empty() {
437 *self = before_inline;
438 return false;
439 }
440 *self = completed.remove(0);
441 self.path_state.inline.pending_states.extend(completed.into_iter().map(Box::new));
442 true
443 }
444
445 fn forget_inline_referent(&mut self, local: Local) {
446 let Some(alloc_id) = self.path_state.current_frame.local_alloc.get(&local).copied() else {
447 self.path_state.diagnostics.mark_unsupported();
448 return;
449 };
450 if self.content(alloc_id).facts.may_be_uninit {
451 return;
452 }
453 let prior = self.local_value(local).cloned();
454 *self.content_mut(alloc_id) = MemoryContent::default();
455 let ty = self.body().local_decls[local].ty;
456 let unknown_term = self.fresh_int("inline_write_unknown");
457 self.path_state.inline.inline_branch_terms.insert(unknown_term.clone());
458 let mut unknown = VmValue::new(unknown_term, ty);
459 if let Some(prior) = prior
460 && prior.provenance_alloc_id() == Some(alloc_id)
461 {
462 unknown.provenance = prior.provenance;
463 }
464 self.set_local(local, unknown);
465 }
466
467 fn inline_execute_body(&mut self, pre_uninit: &[AllocId]) -> Option<Vec<Self>> {
468 match self.inline_execute_single() {
469 InlineStep::Complete(true) => Some(vec![self.clone()]),
470 InlineStep::Complete(false) => Some(Vec::new()),
471 InlineStep::Fork(pending) => self.inline_execute_branches(pre_uninit, pending),
472 InlineStep::Incomplete => None,
473 }
474 }
475
476 fn inline_execute_single(&mut self) -> InlineStep<'z3, 'tcx> {
477 let mut visited = FxHashSet::default();
478 let mut queue = vec![BasicBlock::from_usize(0)];
479 let mut returned = false;
480 while let Some(block) = queue.pop() {
481 if !visited.insert(block) {
482 continue;
483 }
484 let bb_data = &self.body().basic_blocks[block];
485 for stmt in &bb_data.statements {
486 crate::vm::mir_rules::exec_statement(self, stmt);
487 }
488 let terminator = bb_data.terminator();
489 match &terminator.kind {
490 TerminatorKind::Goto { target, .. } => queue.push(*target),
491 TerminatorKind::Return => returned = true,
492 TerminatorKind::Assert { target, .. } => {
493 if crate::vm::mir_rules::exec_terminator(self, terminator, None).continues() {
494 queue.push(*target);
495 }
496 }
497 TerminatorKind::SwitchInt { discr, targets } => {
498 if let Some(target) = super::mir_rules::forced_inline_switch_target(
499 self,
500 self.body(),
501 discr,
502 targets,
503 ) {
504 queue.push(target);
505 continue;
506 }
507 if mir_utils::switch_targets_unreachable(self.tcx, self.body(), targets) {
508 queue.push(targets.otherwise());
509 continue;
510 }
511 let mut feasible = Vec::new();
512 for target in targets.all_targets().iter().copied() {
513 let mut candidate = self.clone();
514 if !crate::vm::mir_rules::exec_terminator(
515 &mut candidate,
516 terminator,
517 Some(target),
518 ).continues() {
519 continue;
520 }
521 let solver = Solver::new(self.z3_ctx);
522 if candidate.path_state.inline.symbolic_generic_const_inline {
523 candidate.assert_path_constraints(&solver);
524 } else {
525 candidate.assert_all(&solver);
526 }
527 match solver.check() {
528 SatResult::Sat => feasible.push((target, candidate)),
529 SatResult::Unknown => return InlineStep::Incomplete,
530 SatResult::Unsat => {}
531 }
532 }
533 if feasible.is_empty() {
534 return InlineStep::Complete(false);
535 }
536 if feasible.len() > 1 {
537 return InlineStep::Fork(
538 feasible
539 .into_iter()
540 .map(|(target, state)| (state, target, visited.clone()))
541 .collect(),
542 );
543 }
544 let (target, state) = feasible.pop().unwrap();
545 *self = state;
546 queue.push(target);
547 }
548 TerminatorKind::Call { target, .. } => {
549 let viable = crate::vm::mir_rules::exec_terminator(self, terminator, None).continues();
550 if !self.path_state.inline.pending_states.is_empty() {
551 let Some(target) = target else {
552 return InlineStep::Complete(false);
553 };
554 let mut states = std::mem::take(&mut self.path_state.inline.pending_states)
555 .into_iter()
556 .map(|state| (*state, *target, visited.clone()))
557 .collect::<Vec<_>>();
558 if viable {
559 states.push((self.clone(), *target, visited));
560 }
561 return InlineStep::Fork(states);
562 }
563 if !viable {
564 return InlineStep::Complete(false);
565 }
566 if let Some(target) = target {
567 queue.push(*target);
568 }
569 }
570 TerminatorKind::Drop { target, .. } => {
571 if crate::vm::mir_rules::exec_terminator(self, terminator, None).continues() {
572 queue.push(*target);
573 }
574 }
575 _ => {
576 if !crate::vm::mir_rules::exec_terminator(self, terminator, None).continues() {
577 return InlineStep::Complete(false);
578 }
579 }
580 }
581 }
582 InlineStep::Complete(returned)
583 }
584
585 fn inline_execute_branches(
586 &mut self,
587 pre_uninit: &[AllocId],
588 mut pending: Vec<InlineWorkItem<'z3, 'tcx>>,
589 ) -> Option<Vec<Self>> {
590 let mut returns = Vec::new();
591 while let Some((mut state, block, mut visited)) = pending.pop() {
592 if !visited.insert(block) {
593 continue;
594 }
595 let bb_data = &state.body().basic_blocks[block];
596 for stmt in &bb_data.statements {
597 crate::vm::mir_rules::exec_statement(&mut state, stmt);
598 }
599 let terminator = bb_data.terminator();
600 match &terminator.kind {
601 TerminatorKind::Goto { target, .. } => pending.push((state, *target, visited)),
602 TerminatorKind::Return => {
603 let solver = Solver::new(self.z3_ctx);
604 if state.path_state.inline.symbolic_generic_const_inline {
605 state.assert_path_constraints(&solver);
606 } else {
607 state.assert_all(&solver);
608 }
609 match solver.check() {
610 SatResult::Sat => returns.push(state),
611 SatResult::Unknown => return None,
612 SatResult::Unsat => {}
613 }
614 }
615 TerminatorKind::Assert { target, .. } => {
616 if crate::vm::mir_rules::exec_terminator(&mut state, terminator, None).continues() {
617 pending.push((state, *target, visited));
618 }
619 }
620 TerminatorKind::SwitchInt { discr, targets } => {
621 if let Some(target) = super::mir_rules::forced_inline_switch_target(
622 &state,
623 state.body(),
624 discr,
625 targets,
626 ) {
627 pending.push((state, target, visited));
628 continue;
629 }
630 if mir_utils::switch_targets_unreachable(state.tcx, state.body(), targets) {
631 pending.push((state, targets.otherwise(), visited));
632 continue;
633 }
634 for target in targets.all_targets().iter().copied() {
635 let mut candidate = state.clone();
636 if !crate::vm::mir_rules::exec_terminator(
637 &mut candidate,
638 terminator,
639 Some(target),
640 ).continues() {
641 continue;
642 }
643 let solver = Solver::new(self.z3_ctx);
644 if candidate.path_state.inline.symbolic_generic_const_inline {
645 candidate.assert_path_constraints(&solver);
646 } else {
647 candidate.assert_all(&solver);
648 }
649 match solver.check() {
650 SatResult::Sat => pending.push((candidate, target, visited.clone())),
651 SatResult::Unknown => return None,
652 SatResult::Unsat => {}
653 }
654 }
655 if pre_uninit
656 .iter()
657 .any(|id| state.allocation_needs_write(*id))
658 {
659 let witness = state.find_uninit_witness(terminator, pre_uninit);
660 if !witness.is_empty() {
661 for id in witness {
662 self.content_mut(id).facts.may_be_uninit = true;
663 }
664 return None;
665 }
666 }
667 }
668 TerminatorKind::Call { target, .. } => {
669 let viable =
670 crate::vm::mir_rules::exec_terminator(&mut state, terminator, None).continues();
671 if let Some(target) = target {
672 for alternative in std::mem::take(&mut state.path_state.inline.pending_states) {
673 pending.push((*alternative, *target, visited.clone()));
674 }
675 if viable {
676 pending.push((state, *target, visited));
677 }
678 }
679 }
680 TerminatorKind::Drop { target, .. } => {
681 if crate::vm::mir_rules::exec_terminator(&mut state, terminator, None).continues() {
682 pending.push((state, *target, visited));
683 }
684 }
685 _ => {
686 let _ = crate::vm::mir_rules::exec_terminator(&mut state, terminator, None);
687 }
688 }
689 }
690 Some(returns)
691 }
692
693 fn find_uninit_witness(
694 &self,
695 switch: &Terminator<'tcx>,
696 candidates: &[AllocId],
697 ) -> FxHashSet<AllocId> {
698 let TerminatorKind::SwitchInt { targets, .. } = &switch.kind else {
699 return FxHashSet::default();
700 };
701 if candidates.is_empty() {
702 return FxHashSet::default();
703 }
704 let mut found = FxHashSet::default();
705 let mut stack = Vec::new();
706 for target in targets.all_targets().iter().copied() {
707 let mut state = self.clone();
708 if crate::vm::mir_rules::exec_terminator(&mut state, switch, Some(target)).continues() {
709 stack.push((state, target, FxHashSet::default()));
710 }
711 }
712 let mut explored = 0;
713 while let Some((mut state, block, mut visited)) = stack.pop() {
714 if explored >= 512 || !visited.insert(block) {
715 continue;
716 }
717 explored += 1;
718 let data = &state.body().basic_blocks[block];
719 for statement in &data.statements {
720 crate::vm::mir_rules::exec_statement(&mut state, statement);
721 }
722 let terminator = data.terminator();
723 match &terminator.kind {
724 TerminatorKind::Return => {
725 let solver = Solver::new(state.z3_ctx);
726 state.assert_all(&solver);
727 if solver.check() == SatResult::Sat {
728 for id in candidates {
729 if state.allocation_needs_write(*id) {
730 found.insert(*id);
731 }
732 }
733 }
734 if found.len() == candidates.len() {
735 break;
736 }
737 }
738 TerminatorKind::Goto { target, .. } => stack.push((state, *target, visited)),
739 TerminatorKind::SwitchInt { targets, .. } => {
740 for target in targets.all_targets().iter().copied() {
741 let mut branch = state.clone();
742 if crate::vm::mir_rules::exec_terminator(
743 &mut branch,
744 terminator,
745 Some(target),
746 ).continues() {
747 stack.push((branch, target, visited.clone()));
748 }
749 }
750 }
751 TerminatorKind::Assert { target, .. } => {
752 if crate::vm::mir_rules::exec_terminator(&mut state, terminator, None).continues() {
753 stack.push((state, *target, visited));
754 }
755 }
756 TerminatorKind::Call {
757 func,
758 target: Some(target),
759 ..
760 } => {
761 let callee = mir_utils::resolve_callee_def_id(
762 state.tcx,
763 state.path_state.current_frame.current_def_id,
764 func,
765 );
766 if callee.is_some_and(|def_id| state.tcx.is_mir_available(def_id)) {
767 let incomplete = state.path_state.diagnostics.has_incomplete_inline_branch();
768 let truncated = state.path_state.inline.inline_truncated_terms.len();
769 let viable =
770 crate::vm::mir_rules::exec_terminator(&mut state, terminator, None).continues();
771 if viable
772 && !state.path_state.diagnostics.is_unsupported()
773 && state.path_state.diagnostics.has_incomplete_inline_branch() == incomplete
774 && state.path_state.inline.inline_truncated_terms.len() == truncated
775 {
776 stack.push((state, *target, visited));
777 }
778 }
779 }
780 _ => {}
781 }
782 }
783 found
784 }
785
786 fn allocation_needs_write(&self, id: AllocId) -> bool {
787 let facts = &self.content(id).facts;
788 !facts.initialized || (facts.inner_uninit && facts.written_pointee_ty.is_none())
789 }
790
791 pub(crate) fn replay_deferred_writes(
792 &mut self,
793 writes: Vec<(Local, Vec<usize>, VmValue<'z3, 'tcx>)>,
794 ) {
795 for (local, path, value) in writes {
796 let arg_idx = local.as_usize().checked_sub(1);
797 let outer_referent = arg_idx
798 .and_then(|i| self.path_state.inline.arg_referents.get(i).copied().flatten())
799 .filter(|_| {
800 matches!(
801 self.body().local_decls[local].ty.kind(),
802 TyKind::Ref(..) | TyKind::RawPtr(..)
803 )
804 });
805 if let Some(referent) = outer_referent {
806 self.path_state.inline
807 .deferred_field_writes
808 .push((referent, path, value));
809 } else {
810 self.write_local_path(local, path, value);
811 }
812 }
813 }
814
815 pub(crate) fn find_whole_borrow_referent(&self, local: Local) -> Option<Local> {
825 for bb in self.body().basic_blocks.iter() {
826 for stmt in &bb.statements {
827 if let StatementKind::Assign(assign) = &stmt.kind {
828 let (dest, rvalue) = &**assign;
829 if dest.local == local
830 && dest.projection.is_empty()
831 && let Rvalue::Ref(_, _, place) | Rvalue::RawPtr(_, place) = rvalue
832 && ((place.projection.is_empty()
833 && matches!(
834 self.body().local_decls[local].ty.kind(),
835 TyKind::Ref(_, _, rustc_middle::ty::Mutability::Mut)
836 | TyKind::RawPtr(_, rustc_middle::ty::Mutability::Mut)
837 ))
838 || (place.projection.len() == 1
839 && matches!(place.projection[0].kind(), ProjectionElem::Deref)))
840 {
841 return Some(place.local);
842 }
843 }
844 }
845 }
846 None
847 }
848
849 pub(crate) fn find_field_reborrow_referent(&self, local: Local) -> Option<(Local, Vec<usize>)> {
852 for bb in self.body().basic_blocks.iter() {
853 for stmt in &bb.statements {
854 if let StatementKind::Assign(assign) = &stmt.kind {
855 let (dest, rvalue) = &**assign;
856 if dest.local == local
857 && dest.projection.is_empty()
858 && let Rvalue::Ref(_, _, place) | Rvalue::RawPtr(_, place) = rvalue
859 {
860 let mut proj = place.projection.iter();
861 if !matches!(proj.next().map(|p| p.kind()), Some(ProjectionElem::Deref)) {
862 continue;
863 }
864 let mut fields = Vec::new();
865 for p in proj {
866 if let ProjectionElem::Field(f, _) = p.kind() {
867 fields.push(f.as_usize());
868 } else {
869 fields.clear();
870 break;
871 }
872 }
873 if !fields.is_empty() {
874 return Some((place.local, fields));
875 }
876 }
877 }
878 }
879 }
880 None
881 }
882
883 pub(crate) fn find_copy_root(&self, local: Local) -> Option<Local> {
891 for bb in self.body().basic_blocks.iter() {
892 for stmt in &bb.statements {
893 if let StatementKind::Assign(assign) = &stmt.kind {
894 let (dest, rvalue) = &**assign;
895 if dest.local == local && dest.projection.is_empty() {
896 let op = match rvalue {
897 Rvalue::Use(op, ..) => Some(op),
898 _ => None,
899 };
900 if let Some(Operand::Copy(p) | Operand::Move(p)) = op
901 && p.projection.is_empty()
902 {
903 return Some(p.local);
904 }
905 }
906 }
907 }
908 }
909 None
910 }
911}