1use crate::analysis::alias::hazard::{self as alias_hazard, AliasProducer, HazardKind};
7use crate::analysis::alias::FieldOrigin;
8use crate::analysis::dataflow::pointer_flow;
9use crate::compat::GenericArgsRef;
10use crate::def_ids::type_ids;
11use crate::util::def_use::PlaceKey;
12use crate::util::mir_utils;
13use crate::verify::checkpoint::Checkpoint;
14use crate::vm::state::{VmExecutor, VmValue};
15use rustc_hir::def_id::DefId;
16use rustc_middle::mir::{Local, Operand, ProjectionElem, Rvalue, StatementKind};
17
18#[derive(Clone, Debug)]
20pub(crate) struct VmOrigin {
21 pub local: Local,
23 pub kind: VmOriginKind,
25}
26
27#[derive(Clone, Copy, Debug, PartialEq, Eq)]
28pub(crate) enum VmOriginKind {
29 MutRef,
30 SharedRef,
31 RawPtr,
35 Owned(DefId),
36 Unknown,
37}
38
39impl VmOrigin {
40 pub(crate) fn is_mut_ref(&self) -> bool {
42 matches!(self.kind, VmOriginKind::MutRef)
43 }
44
45 pub(crate) fn is_shared_ref(&self) -> bool {
47 matches!(self.kind, VmOriginKind::SharedRef)
48 }
49
50 pub(crate) fn is_owned(&self) -> bool {
52 matches!(self.kind, VmOriginKind::Owned(_))
53 }
54}
55
56impl<'z3, 'tcx> VmExecutor<'z3, 'tcx> {
57 pub(crate) fn resolve_origin(&self, value: &VmValue<'z3, 'tcx>) -> Option<VmOrigin> {
62 let Some(prov) = &value.provenance else {
63 return None;
64 };
65
66 let alloc_id = prov.alloc_id;
67 let mut best: Option<VmOrigin> = None;
68 for (local, val) in self.all_local_values() {
69 let Some(val_prov) = &val.provenance else {
70 continue;
71 };
72 if val_prov.alloc_id != alloc_id {
73 continue;
74 }
75
76 let kind = self.classify_local(&local);
77 let candidate = VmOrigin { local, kind };
78 let is_param = local.as_usize() >= 1 && local.as_usize() <= self.body().arg_count;
79 let is_owned = candidate.is_owned();
80 match &best {
81 None => best = Some(candidate),
82 Some(existing) => {
83 let ex_is_param = existing.local.as_usize() >= 1
84 && existing.local.as_usize() <= self.body().arg_count;
85 let ex_is_owned = existing.is_owned();
86 let rank = |p: bool, o: bool| {
87 if p {
88 0
89 } else if o {
90 1
91 } else {
92 2
93 }
94 };
95 let cand_rank = rank(is_param, is_owned);
96 let ex_rank = rank(ex_is_param, ex_is_owned);
97 if cand_rank < ex_rank
98 || (cand_rank == ex_rank && local.as_usize() < existing.local.as_usize())
99 {
100 best = Some(candidate);
101 }
102 }
103 }
104 }
105
106 best
107 }
108
109 fn classify_local(&self, local: &Local) -> VmOriginKind {
111 let ty = self.body().local_decls[*local].ty;
112 match ty.kind() {
113 rustc_middle::ty::TyKind::Ref(_, _, rustc_middle::ty::Mutability::Mut) => {
114 VmOriginKind::MutRef
115 }
116 rustc_middle::ty::TyKind::Ref(_, _, rustc_middle::ty::Mutability::Not) => {
117 VmOriginKind::SharedRef
118 }
119 rustc_middle::ty::TyKind::RawPtr(..) => VmOriginKind::RawPtr,
120 rustc_middle::ty::TyKind::Adt(adt_def, _) => VmOriginKind::Owned(adt_def.did()),
121 _ => VmOriginKind::Unknown,
122 }
123 }
124}
125
126pub(crate) enum VmAliasResult {
128 Proved,
129 Failed(String),
130 Unknown,
131}
132
133fn origin_alias_hazard_accepted<'z3, 'tcx>(
134 vm_state: &VmExecutor<'z3, 'tcx>,
135 origin_val: &VmValue<'z3, 'tcx>,
136) -> bool {
137 origin_val.provenance_alloc_id().is_some_and(|alloc_id| vm_state.has_alias_hazard(alloc_id))
138}
139
140fn flow_xor_violation<'z3, 'tcx>(
146 vm_state: &VmExecutor<'z3, 'tcx>,
147 checkpoint: &Checkpoint<'tcx>,
148 unique: bool,
149 stmt_index: usize,
150) -> Option<String> {
151 let origin_arg = checkpoint.args.first()?;
152 let origin_place = alias_hazard::operand_mir_place(origin_arg)?;
153 let origin_local = origin_place.local;
154 let origin_alloc = vm_state.value_of_operand(origin_arg).provenance_alloc_id()?;
155 let origin_root = vm_state.root_alloc(origin_alloc);
156 let live = alias_hazard::live_locals_at(
157 vm_state.tcx,
158 checkpoint.caller,
159 checkpoint.block,
160 stmt_index,
161 false,
162 false,
163 );
164 let body = vm_state.body();
165 for (local, val) in &vm_state.all_local_values() {
166 if *local == origin_local {
167 continue;
168 }
169 if !live.contains(local) {
170 continue;
171 }
172 let Some(prov) = &val.provenance else {
173 continue;
174 };
175 let mutability = match body.local_decls[*local].ty.kind() {
176 rustc_middle::ty::TyKind::Ref(_, _, m) => *m,
177 _ => continue,
178 };
179 let same_alloc = prov.alloc_id == origin_alloc;
180 let same_root = vm_state.root_alloc(prov.alloc_id) == origin_root;
181 if !same_alloc && !same_root {
182 continue;
183 }
184 let view_mut = mutability == rustc_middle::ty::Mutability::Mut;
185 let conflicts = if unique { !view_mut } else { view_mut };
186 if conflicts {
187 let produced = if unique { "&mut" } else { "&" };
188 let live_kind = if view_mut { "&mut" } else { "&" };
189 return Some(format!("producing {produced} while a live {live_kind} aliases the same data"));
190 }
191 }
192 None
193}
194
195fn escaped_parameter_origin<'z3, 'tcx>(
196 vm_state: &VmExecutor<'z3, 'tcx>,
197 checkpoint: &Checkpoint<'tcx>,
198 origin_arg: &Operand<'tcx>,
199) -> Option<(usize, Vec<usize>)> {
200 if !alias_hazard::destination_flows_to_return(vm_state.tcx, checkpoint.caller, checkpoint.destination) {
201 return None;
202 }
203 let origin = mir_utils::operand_mir_place(origin_arg)?;
204 let (root, fields) = crate::analysis::alias::tree::AliasTree::build(vm_state.tcx, checkpoint.caller)
205 .resolve_local_to_root(origin.local);
206 (root >= 1 && root <= vm_state.body().arg_count).then_some((root, fields))
207}
208
209pub(crate) fn check_alias_vm<'z3, 'tcx>(vm_state: &VmExecutor<'z3, 'tcx>, checkpoint: &Checkpoint<'tcx>) -> VmAliasResult {
213 let callee = match checkpoint.callee {
214 Some(c) => c,
215 None => {
216 let Some(origin_arg) = checkpoint.args.first() else {
217 return VmAliasResult::Unknown;
218 };
219 let origin_val = vm_state.value_of_operand(origin_arg);
220 if let Some(origin_place) = alias_hazard::operand_place(origin_arg) {
223 let kind = if checkpoint.is_mut_ref {
224 HazardKind::UniqueView
225 } else {
226 HazardKind::SharedView
227 };
228 if let Some(reason) = alias_hazard::local_hazard_violation(
229 vm_state.tcx,
230 checkpoint.caller,
231 checkpoint.block,
232 checkpoint.destination,
233 &[origin_place],
234 kind,
235 None,
236 ) {
237 return VmAliasResult::Failed(reason);
238 }
239 }
240 if let Some(origin) = vm_state.resolve_origin(&origin_val) {
241 if !origin_alias_hazard_accepted(vm_state, &origin_val)
246 && let Some(reason) = flow_xor_violation(
247 vm_state,
248 checkpoint,
249 checkpoint.is_mut_ref,
250 checkpoint.stmt_index,
251 )
252 {
253 return VmAliasResult::Failed(reason);
254 }
255 if origin.is_mut_ref() {
256 if let Some((root, fields)) = escaped_parameter_origin(vm_state, checkpoint, origin_arg) {
260 if !fields.is_empty() {
261 let root_ty = vm_state.body().local_decls[Local::from_usize(root)].ty;
262 if let rustc_middle::ty::TyKind::Ref(
263 _,
264 _,
265 rustc_middle::ty::Mutability::Not,
266 ) = root_ty.kind()
267 && checkpoint.is_mut_ref
268 {
269 return VmAliasResult::Failed("&mut deref through a shared reference writes immutable data".into());
270 }
271 }
272 }
273 return VmAliasResult::Proved;
274 }
275 if origin.is_shared_ref() {
276 if checkpoint.is_mut_ref {
277 return VmAliasResult::Failed("&mut deref through a shared reference writes immutable data".into());
278 }
279 if let Some((root, fields)) = escaped_parameter_origin(vm_state, checkpoint, origin_arg) {
286 if !fields.is_empty() {
287 let resolved = PlaceKey::from_origin(root, fields);
288 if let Some(sfo) = alias_hazard::self_field_origin(vm_state.tcx, checkpoint.caller, &resolved) {
289 if !origin_alias_hazard_accepted(vm_state, &origin_val) {
294 return check_escaped_field(vm_state.tcx, checkpoint.caller, &sfo, HazardKind::SharedView);
295 }
296 }
297 } else {
298 if let Some(reason) = escape_region_violation(vm_state.tcx, checkpoint.caller, root) {
303 return VmAliasResult::Failed(reason);
304 }
305 }
306 }
307 return VmAliasResult::Proved;
308 }
309 if origin.is_owned() {
310 return VmAliasResult::Proved;
311 }
312 if matches!(origin.kind, VmOriginKind::RawPtr)
323 && origin.local.as_usize() <= vm_state.body().arg_count
324 {
325 if origin_alias_hazard_accepted(vm_state, &origin_val) {
326 return VmAliasResult::Proved;
327 }
328 let escapes = alias_hazard::destination_flows_to_return(vm_state.tcx, checkpoint.caller, checkpoint.destination);
329 if !escapes {
330 return VmAliasResult::Proved;
331 }
332 return VmAliasResult::Unknown;
333 }
334 }
335 if vm_state.body().arg_count >= 1 {
343 let self_ty = vm_state.body().local_decls[Local::from_usize(1)].ty;
344 let nonnull_adt = match self_ty.kind() {
345 rustc_middle::ty::TyKind::Adt(adt_def, _) => Some(*adt_def),
346 rustc_middle::ty::TyKind::Ref(_, pointee, _) => match pointee.kind() {
347 rustc_middle::ty::TyKind::Adt(adt_def, _) => Some(*adt_def),
348 _ => None,
349 },
350 _ => None,
351 };
352 if nonnull_adt.is_some_and(|adt| type_ids::is_std_nonnull(adt.did())) {
353 return VmAliasResult::Proved;
354 }
355 }
356 if let Some(prov) = &origin_val.provenance {
357 let is_external = vm_state.alloc(prov.alloc_id).is_external();
358 if !is_external {
359 return VmAliasResult::Proved;
360 }
361 let has_shared_ref = vm_state.body().local_decls.iter().any(|d| {
363 matches!(
364 d.ty.kind(),
365 rustc_middle::ty::TyKind::Ref(_, _, rustc_middle::ty::Mutability::Not)
366 )
367 });
368 if has_shared_ref {
369 return VmAliasResult::Proved;
370 }
371 }
372 if origin_val.provenance.is_none() {
373 for decl in &vm_state.body().local_decls {
374 if matches!(decl.ty.kind(), rustc_middle::ty::TyKind::Ref(..)) {
375 return VmAliasResult::Proved;
376 }
377 }
378 }
379 let tcx = vm_state.tcx;
382 let caller = checkpoint.caller;
383 let arg_place = alias_hazard::operand_mir_place(origin_arg).map(PlaceKey::from_mir_place);
384 if let Some(mir_place) = arg_place {
385 let tree = crate::analysis::alias::tree::AliasTree::build(tcx, caller);
386 let local = mir_place.local().unwrap_or(Local::from_usize(1));
387 let (root, fields) = tree.resolve_local_to_root(local);
388 if !fields.is_empty() {
389 let root_ty = vm_state.body().local_decls[Local::from_usize(root)].ty;
397 if !matches!(root_ty.kind(), rustc_middle::ty::TyKind::Ref(..)) {
398 let typing_env = rustc_middle::ty::TypingEnv::post_analysis(tcx, caller);
399 if !tcx.type_is_copy_modulo_regions(typing_env, root_ty) {
400 return VmAliasResult::Proved;
401 }
402 }
403 let resolved = PlaceKey::from_origin(root, fields);
404 let sfo = alias_hazard::self_field_origin(tcx, caller, &resolved);
405 if let Some(sfo) = sfo
406 && let Some(is_shared) = is_self_field_shared_ref(tcx, caller, &sfo)
407 && is_shared {
408 return VmAliasResult::Proved;
409 }
410 }
411 }
412 return VmAliasResult::Unknown;
413 }
414 };
415
416 if let Some(mutability) = pointer_flow::nonnull_ref_return(vm_state.tcx, callee) {
424 let ret_ty = vm_state.body().local_decls[rustc_middle::mir::RETURN_PLACE].ty;
425 if mir_utils::type_contains_reference(ret_ty) {
426 if mutability == rustc_middle::mir::Mutability::Mut {
427 return VmAliasResult::Failed("escaping `&mut` derived from a raw pointer without borrow information".into());
428 }
429 return VmAliasResult::Unknown;
430 }
431 return VmAliasResult::Proved;
432 }
433
434 let Some(producer) = alias_hazard::alias_producer(vm_state.tcx, callee) else {
435 return VmAliasResult::Unknown;
436 };
437
438 match producer {
439 AliasProducer::View(kind, source_arg) => check_view_alias(vm_state, checkpoint, kind, source_arg),
440 AliasProducer::OwnershipTransfer(source_arg) => check_ownership_transfer_alias(vm_state, checkpoint, source_arg),
441 AliasProducer::ReadMemory(source_arg) => check_read_memory_alias(vm_state, checkpoint, source_arg),
442 AliasProducer::CopyMemory(source_arg) => check_copy_memory_alias(vm_state, checkpoint, source_arg),
443 }
444}
445
446fn check_view_alias<'z3, 'tcx>(
447 vm_state: &VmExecutor<'z3, 'tcx>,
448 checkpoint: &Checkpoint<'tcx>,
449 kind: HazardKind,
450 source_arg: usize,
451) -> VmAliasResult {
452 let Some(origin_arg) = checkpoint.args.get(source_arg) else {
453 return VmAliasResult::Unknown;
454 };
455 let origin_val = vm_state.value_of_operand(origin_arg);
456
457 let tcx = vm_state.tcx;
458 let caller = checkpoint.caller;
459 let call_block = checkpoint.block;
460 let destination = checkpoint.destination;
461
462 if !origin_alias_hazard_accepted(vm_state, &origin_val)
469 && let Some(reason) = flow_xor_violation(
470 vm_state,
471 checkpoint,
472 kind == HazardKind::UniqueView,
473 usize::MAX,
474 )
475 {
476 return VmAliasResult::Failed(reason);
477 }
478
479 let origin_place = alias_hazard::operand_place(origin_arg).unwrap_or_else(|| {
480 PlaceKey::from_origin(mir_utils::extract_local(origin_arg).map(|l| l.as_usize()).unwrap_or(1), vec![])
481 });
482
483 let resolved_origin = resolve_origin_place_mir(tcx, caller, &origin_place);
486 let mut origins = vec![origin_place.clone()];
487 if resolved_origin != origin_place {
488 origins.push(resolved_origin.clone());
489 }
490
491 let mir_place_from_arg = alias_hazard::operand_mir_place(origin_arg);
494 if let Some(place) = mir_place_from_arg
495 && !place.projection.is_empty()
496 && place.local == Local::from_usize(1)
497 {
498 let field_key = PlaceKey::from_mir_place(place);
499 if !field_key.fields.is_empty() && !origins.contains(&field_key) {
500 origins.push(field_key);
501 }
502 }
503
504 if let Some(origin) = vm_state.resolve_origin(&origin_val) {
505 match (kind, origin.kind) {
506 (HazardKind::UniqueView, VmOriginKind::MutRef) => return VmAliasResult::Proved,
507 (HazardKind::SharedView, VmOriginKind::SharedRef) => {
508 if let Some(reason) = shared_view_escape_region_violation(tcx, caller, destination, origin.local.as_usize()) {
511 return VmAliasResult::Failed(reason);
512 }
513 return VmAliasResult::Proved;
514 }
515 (HazardKind::UniqueView, VmOriginKind::SharedRef) => {
516 return VmAliasResult::Failed("shared reference cannot produce a unique mutable view".into());
520 }
521 _ => {}
522 }
523 if origin.is_owned() {
524 let check = alias_hazard::alias_proved_for_param_local(
525 tcx,
526 caller,
527 origin.local.as_usize(),
528 kind,
529 );
530 let is_reallocatable = match &origin.kind {
533 VmOriginKind::Owned(def_id) => {
534 type_ids::is_std_vec(*def_id) || type_ids::is_std_cstring(*def_id)
535 }
536 _ => false,
537 };
538 if matches!(check, alias_hazard::HazardCheck::Safe(_)) && !is_reallocatable {
539 return VmAliasResult::Proved;
540 }
541 }
542 }
543
544 let view_len_place = if source_arg == 0 {
545 checkpoint.args.get(1).and_then(alias_hazard::operand_place)
546 } else {
547 None
548 };
549
550 if let Some(reason) = alias_hazard::local_hazard_violation(
551 tcx,
552 caller,
553 call_block,
554 destination,
555 &origins,
556 kind,
557 view_len_place,
558 ) {
559 return VmAliasResult::Failed(reason);
560 }
561
562 let origin_pk = alias_hazard::resolve_param_origin(tcx, caller, &origin_place);
563 if let Some(local_index) = origin_pk {
564 if let alias_hazard::HazardCheck::Safe(_) = alias_hazard::alias_proved_for_param_local(tcx, caller, local_index, kind)
565 {
566 return VmAliasResult::Proved;
567 }
568 }
569
570 let dest_escapes = alias_hazard::destination_flows_to_return(tcx, caller, destination);
571 if dest_escapes {
572 let field_origin = resolve_escaped_field_origin(tcx, caller, &resolved_origin, &origin_place, checkpoint);
573 if let Some(sfo) = field_origin {
574 return check_escaped_field(tcx, caller, &sfo, kind);
575 }
576 let any_field = alias_hazard::any_struct_field_origin(tcx, caller, &resolved_origin)
577 .or_else(|| alias_hazard::any_struct_field_origin(tcx, caller, &origin_place));
578 if let Some(sfo) = any_field {
579 return check_escaped_field(tcx, caller, &sfo, kind);
580 }
581 if let Some(reason) = alias_hazard::private_fn_callsite_delegation(tcx, caller, &origin_place, kind)
582 {
583 return VmAliasResult::Failed(reason);
584 }
585 if kind == HazardKind::SharedView {
586 let param_origin = alias_hazard::resolve_param_origin(tcx, caller, &origin_place);
587 if let Some(local) = param_origin
588 && alias_hazard::is_origin_a_reference(
589 tcx,
590 caller,
591 &PlaceKey::from_origin(local, vec![]),
592 )
593 {
594 if let Some(reason) = escape_region_violation(tcx, caller, local) {
599 return VmAliasResult::Failed(reason);
600 }
601 return VmAliasResult::Proved;
602 }
603 }
604 }
605
606 if !dest_escapes {
607 return VmAliasResult::Proved;
608 }
609
610 if kind == HazardKind::UniqueView {
613 let body = tcx.optimized_mir(caller);
614 if body.arg_count >= 1 {
615 let self_ty = body.local_decls[Local::from_usize(1)].ty;
616 if let rustc_middle::ty::TyKind::Adt(adt_def, _) = self_ty.kind()
620 && type_ids::is_std_nonnull(adt_def.did()) {
621 return VmAliasResult::Proved;
622 }
623 }
624 return VmAliasResult::Failed(format!(
625 "returned unique view escapes while the original pointer is not owned by a private self field [origin={:?}]",
626 origin_place
627 ));
628 }
629
630 VmAliasResult::Proved
631}
632
633fn shared_view_escape_region_violation(
637 tcx: rustc_middle::ty::TyCtxt<'_>,
638 caller: DefId,
639 destination: Option<Local>,
640 local: usize,
641) -> Option<String> {
642 if !alias_hazard::destination_flows_to_return(tcx, caller, destination) {
643 return None;
644 }
645 escape_region_violation(tcx, caller, local)
646}
647
648fn escape_region_violation(
651 tcx: rustc_middle::ty::TyCtxt<'_>,
652 caller: DefId,
653 local: usize,
654) -> Option<String> {
655 let src_region =
658 crate::vm::region::fn_arg_ty(tcx, caller, local - 1).and_then(|ty| match ty.kind() {
659 rustc_middle::ty::TyKind::Ref(region, _, _) => Some(*region),
660 _ => None,
661 })?;
662 let ret_region = crate::vm::region::fn_return_region(tcx, caller)?;
663 if !crate::vm::region::region_outlives(tcx, caller, src_region, ret_region) {
664 return Some(format!(
665 "returned region `{ret_region:?}` outlives the source reference's region `{src_region:?}`"
666 ));
667 }
668 None
669}
670
671fn self_field_origin(
672 tcx: rustc_middle::ty::TyCtxt<'_>,
673 struct_def_id: DefId,
674 field_index: usize,
675) -> Option<FieldOrigin> {
676 let field = tcx.adt_def(struct_def_id).all_fields().nth(field_index)?;
677 Some(FieldOrigin {
678 struct_def_id,
679 field_index,
680 field_name: field.name.to_string(),
681 })
682}
683
684fn find_struct_field_origin_for_param<'tcx>(
686 tcx: rustc_middle::ty::TyCtxt<'tcx>,
687 caller: DefId,
688 checkpoint: &Checkpoint<'tcx>,
689) -> Option<FieldOrigin> {
690 let body = tcx.optimized_mir(caller);
691 let (adt_def, _) = self_adt(tcx, caller)?;
692
693 let arg0 = checkpoint.args.first()?;
694 let arg_place = match arg0 {
695 Operand::Copy(p) | Operand::Move(p) => p,
696 _ => return None,
697 };
698
699 if !arg_place.projection.is_empty() && arg_place.local == Local::from_usize(1) {
700 let field_index = arg_place
701 .projection
702 .iter()
703 .find_map(|p| match p {
704 ProjectionElem::Field(idx, _) => Some(idx.as_usize()),
705 _ => None,
706 });
707 if let Some(field_index) = field_index {
708 return self_field_origin(tcx, adt_def, field_index);
709 }
710 }
711
712 let arg_local = arg_place.local;
713 if arg_place.projection.is_empty() && arg_local != Local::from_usize(1) {
714 for block in body.basic_blocks.iter() {
715 for stmt in &block.statements {
716 let StatementKind::Assign(assign) = &stmt.kind else {
717 continue;
718 };
719 let (target, rvalue) = assign.as_ref();
720 if target.local != arg_local {
721 continue;
722 }
723 let source = match rvalue {
724 Rvalue::Use(Operand::Copy(p) | Operand::Move(p), ..) => p,
725 Rvalue::CopyForDeref(p) => p,
726 _ => continue,
727 };
728 if source.local != Local::from_usize(1) {
729 continue;
730 }
731 let field_index = source
732 .projection
733 .iter()
734 .find_map(|p| match p {
735 ProjectionElem::Field(idx, _) => Some(idx.as_usize()),
736 _ => None,
737 });
738 let Some(field_index) = field_index else {
739 continue;
740 };
741 return self_field_origin(tcx, adt_def, field_index);
742 }
743 }
744 }
745
746 None
747}
748
749fn is_self_field_shared_ref(
753 tcx: rustc_middle::ty::TyCtxt<'_>,
754 caller: DefId,
755 origin: &FieldOrigin,
756) -> Option<bool> {
757 let (adt_def, args) = self_adt(tcx, caller)?;
758 if adt_def != origin.struct_def_id {
759 return Some(false);
760 }
761 let adt = tcx.adt_def(adt_def);
762 let field = adt.all_fields().nth(origin.field_index)?;
763 let field_ty = mir_utils::field_ty(tcx, field, args);
764 Some(matches!(field_ty.kind(), rustc_middle::ty::TyKind::Ref(_, _, rustc_middle::ty::Mutability::Not)))
765}
766
767fn check_escaped_field(
772 tcx: rustc_middle::ty::TyCtxt<'_>,
773 caller: DefId,
774 sfo: &FieldOrigin,
775 kind: HazardKind,
776) -> VmAliasResult {
777 if let Some(reason) = alias_hazard::escaped_self_field_violation(tcx, caller, sfo) {
778 return VmAliasResult::Failed(reason);
779 }
780 if kind == HazardKind::UniqueView {
781 return VmAliasResult::Failed("unique view escapes through a private raw field".into());
782 }
783 VmAliasResult::Proved
784}
785
786fn resolve_escaped_field_origin<'tcx>(
790 tcx: rustc_middle::ty::TyCtxt<'tcx>,
791 caller: DefId,
792 resolved_origin: &PlaceKey,
793 origin_place: &PlaceKey,
794 checkpoint: &Checkpoint<'tcx>,
795) -> Option<FieldOrigin> {
796 alias_hazard::self_field_origin(tcx, caller, resolved_origin)
797 .or_else(|| alias_hazard::self_field_origin(tcx, caller, origin_place))
798 .or_else(|| find_struct_field_origin_for_param(tcx, caller, checkpoint))
799}
800
801fn resolve_origin_place_mir(
803 tcx: rustc_middle::ty::TyCtxt<'_>,
804 caller: DefId,
805 place: &PlaceKey,
806) -> PlaceKey {
807 let Some(local) = place.local() else {
808 return place.clone();
809 };
810 let tree = crate::analysis::alias::tree::AliasTree::build(tcx, caller);
811 let (root_local, mut root_fields) = tree.resolve_local_to_root(local);
812
813 if root_local == local.as_usize() && root_fields.is_empty() && !place.fields.is_empty() {
814 root_fields = place.fields.clone();
815 }
816
817 if root_fields.is_empty() && !place.fields.is_empty() {
820 return place.clone();
821 }
822
823 PlaceKey::from_origin(root_local, root_fields)
824}
825
826fn self_adt<'tcx>(
829 tcx: rustc_middle::ty::TyCtxt<'tcx>,
830 caller: DefId,
831) -> Option<(DefId, GenericArgsRef<'tcx>)> {
832 let body = tcx.optimized_mir(caller);
833 if body.arg_count == 0 {
834 return None;
835 }
836 let self_ty = body.local_decls[Local::from_usize(1)].ty;
837 let inner = match self_ty.kind() {
838 rustc_middle::ty::TyKind::Ref(_, inner, _) => *inner,
839 _ => return None,
840 };
841 crate::analysis::alias::adt_from_ty(inner)
842}
843
844fn check_ownership_transfer_alias<'z3, 'tcx>(
845 vm_state: &VmExecutor<'z3, 'tcx>,
846 checkpoint: &Checkpoint<'tcx>,
847 source_arg: usize,
848) -> VmAliasResult {
849 let Some(origin_arg) = checkpoint.args.get(source_arg) else {
850 return VmAliasResult::Unknown;
851 };
852
853 let tcx = vm_state.tcx;
854 let caller = checkpoint.caller;
855 let call_block = checkpoint.block;
856 let destination = checkpoint.destination;
857
858 let origin_place = alias_hazard::operand_place(origin_arg);
859 let Some(origin_place) = origin_place else {
860 return VmAliasResult::Unknown;
861 };
862
863 if let Some(reason) = alias_hazard::ownership_transfer_violation(
864 tcx,
865 caller,
866 call_block,
867 destination,
868 &origin_place,
869 ) {
870 return VmAliasResult::Failed(reason);
871 }
872
873 VmAliasResult::Proved
874}
875
876fn check_read_memory_alias<'z3, 'tcx>(
877 vm_state: &VmExecutor<'z3, 'tcx>,
878 checkpoint: &Checkpoint<'tcx>,
879 source_arg: usize,
880) -> VmAliasResult {
881 let Some(origin_arg) = checkpoint.args.get(source_arg) else {
882 return VmAliasResult::Unknown;
883 };
884
885 let origin_val = vm_state.value_of_operand(origin_arg);
886
887 if let Some(alloc_id) = origin_val.provenance_alloc_id()
891 && vm_state.has_alias_hazard(alloc_id)
892 {
893 return VmAliasResult::Proved;
894 }
895
896 if let rustc_middle::ty::TyKind::RawPtr(pointee, _) = origin_val.ty.kind() {
897 let tcx = vm_state.tcx;
898 let typing_env = rustc_middle::ty::TypingEnv::post_analysis(tcx, checkpoint.caller);
899 if tcx.type_is_copy_modulo_regions(typing_env, *pointee) {
900 return VmAliasResult::Proved;
901 }
902 }
903
904 let tcx = vm_state.tcx;
905 let destination = checkpoint.destination;
906 if !alias_hazard::destination_flows_to_return(tcx, checkpoint.caller, destination) {
907 return VmAliasResult::Proved;
908 }
909
910 VmAliasResult::Failed("read API value escapes while the source pointer persists — structural alias hazard".into())
911}
912
913fn check_copy_memory_alias<'z3, 'tcx>(
914 vm_state: &VmExecutor<'z3, 'tcx>,
915 checkpoint: &Checkpoint<'tcx>,
916 source_arg: usize,
917) -> VmAliasResult {
918 let Some(origin_arg) = checkpoint.args.get(source_arg) else {
919 return VmAliasResult::Unknown;
920 };
921 let origin_val = vm_state.value_of_operand(origin_arg);
922 if let Some(alloc_id) = origin_val.provenance_alloc_id()
923 && vm_state.has_alias_hazard(alloc_id)
924 {
925 return VmAliasResult::Proved;
926 }
927 if let rustc_middle::ty::TyKind::RawPtr(pointee, _) = origin_val.ty.kind() {
928 let typing_env = rustc_middle::ty::TypingEnv::post_analysis(vm_state.tcx, checkpoint.caller);
929 if vm_state.tcx.type_is_copy_modulo_regions(typing_env, *pointee) {
930 return VmAliasResult::Proved;
931 }
932 }
933 VmAliasResult::Unknown
934}