1use rustc_middle::{
2 mir::{Local, Operand, Rvalue, StatementKind},
3 ty::{Ty, TyKind},
4};
5use z3::ast::Int;
6
7use crate::def_ids::type_ids;
8use crate::compat::FxHashMap;
9use crate::util::mir_utils;
10
11use super::state::{AllocId, Provenance, ValueFacts, ValueSource, VmExecutor, VmValue};
12
13impl<'z3, 'tcx> VmExecutor<'z3, 'tcx> {
14 pub(super) fn init_struct_param(
17 &mut self,
18 local: Local,
19 ty: Ty<'tcx>,
20 local_idx: usize,
21 invariants: &mut ValueFacts<'z3>,
22 ) -> bool {
23 let rustc_middle::ty::TyKind::Adt(adt_def, _) = ty.kind() else {
24 return false;
25 };
26 if adt_def.is_enum() {
27 let term = self.fresh_int(&format!("param_{}", local_idx));
28 self.set_local(
29 local,
30 VmValue {
31 z3_term: term,
32 ty,
33 provenance: None,
34 facts: invariants.clone(),
35 source: ValueSource::None,
36 },
37 );
38 return true;
39 }
40 let mut elem_alloc: FxHashMap<Ty<'tcx>, (AllocId, Int<'z3>)> = FxHashMap::default();
41 self.decompose_adt_fields(local, vec![], ty, local_idx, &mut elem_alloc, 0);
42 if mir_utils::is_raw_ptr_wrapper(self.tcx, adt_def.did())
47 && let Some(f0) = self.field_value(local, &[0]).cloned() {
48 let prov = f0.provenance.clone();
49 self.set_local(
50 local,
51 VmValue {
52 z3_term: f0.z3_term,
53 ty,
54 provenance: prov,
55 facts: ValueFacts {
56 init: true,
57 ..Default::default()
58 },
59 source: ValueSource::None,
60 },
61 );
62 return true;
63 }
64 let term = self.fresh_int(&format!("param_{}", local_idx));
65 self.set_local(
66 local,
67 VmValue {
68 z3_term: term,
69 ty,
70 provenance: None,
71 facts: ValueFacts {
72 init: true,
73 ..Default::default()
74 },
75 source: ValueSource::None,
76 },
77 );
78 true
79 }
80
81 pub(crate) fn init_box_vec_param(
85 &mut self,
86 local: Local,
87 ty: Ty<'tcx>,
88 local_idx: usize,
89 invariants: &mut ValueFacts<'z3>,
90 ) -> bool {
91 let rustc_middle::ty::TyKind::Adt(adt_def, substs) = ty.kind() else {
92 return false;
93 };
94 let is_vec = type_ids::is_std_vec(adt_def.did());
95 let is_string = type_ids::is_std_string(adt_def.did());
96 let is_cstring = type_ids::is_std_cstring(adt_def.did());
97 if !(type_ids::is_std_box(adt_def.did()) || is_vec || is_string || is_cstring) {
98 return false;
99 }
100 let heap_ty = if is_string {
101 self.tcx.types.u8
102 } else {
103 substs.first().and_then(|s| s.as_type()).unwrap_or(ty)
104 };
105 let slice_elem = match heap_ty.kind() {
106 TyKind::Slice(elem) if !is_vec && !is_cstring => Some(*elem),
107 TyKind::Str if !is_vec && !is_cstring => Some(self.tcx.types.u8),
108 _ => None,
109 };
110 let heap_size = self.size_of_ty(heap_ty);
111 let heap_align = self.align_sym(slice_elem.unwrap_or(heap_ty));
112 let heap_size_term = Int::from_u64(self.z3_ctx, heap_size.max(1));
113 let (heap_alloc_id, heap_base) = if let Some(elem) = slice_elem {
116 let len = self.fresh_int(&format!("slice_len_{}", local_idx));
117 let zero = Int::from_u64(self.z3_ctx, 0);
118 self.path_state.constraints.assertions.push(len.ge(&zero));
119 let elem_size = self.size_sym(elem);
120 self.allocate_slice(len, elem_size, heap_align, Some(elem))
121 } else if is_vec || is_string {
122 let cap = self.fresh_int(&format!("vec_cap_{}", local_idx));
123 let len = self.fresh_int(&format!("vec_len_{}", local_idx));
124 self.materialize_vec_len_cap(cap.clone(), len.clone(), heap_size);
125 let elem_size = self.size_sym(heap_ty);
126 let bytes = Int::mul(self.z3_ctx, &[&cap, &elem_size]);
127 let (alloc_id, base) = self.allocate_external(bytes, heap_align, Some(heap_ty));
128 self.alloc_mut(alloc_id).set_slice_len(len);
129 (alloc_id, base)
130 } else if is_cstring {
131 let max_size = Int::from_u64(self.z3_ctx, i64::MAX as u64);
132 self.allocate_external(max_size, heap_align.clone(), Some(heap_ty))
133 } else {
134 self.allocate(heap_size_term, heap_align, Some(heap_ty))
135 };
136 invariants.non_null = true;
137 invariants.init = true;
138 self.content_mut(heap_alloc_id).facts.initialized = true;
139 let owner_path = self
145 .container_ptr_field(ty)
146 .expect("container parameter has no owning pointer field");
147 self.set_field_value(
148 local,
149 owner_path,
150 VmValue {
151 z3_term: heap_base.clone(),
152 ty,
153 provenance: Some(Provenance {
154 alloc_id: heap_alloc_id,
155 offset: Int::from_u64(self.z3_ctx, 0),
156 offset_kind: None,
157 }),
158 facts: ValueFacts {
159 non_null: true,
160 init: true,
161 ..Default::default()
162 },
163 source: ValueSource::None,
164 },
165 );
166 self.set_local(
167 local,
168 VmValue {
169 z3_term: heap_base,
170 ty,
171 provenance: Some(Provenance {
172 alloc_id: heap_alloc_id,
173 offset: Int::from_u64(self.z3_ctx, 0),
174 offset_kind: None,
175 }),
176 facts: invariants.clone(),
177 source: ValueSource::None,
178 },
179 );
180 true
181 }
182
183 pub(super) fn init_array_param(
186 &mut self,
187 local: Local,
188 ty: Ty<'tcx>,
189 local_idx: usize,
190 invariants: &mut ValueFacts<'z3>,
191 ) -> bool {
192 let rustc_middle::ty::TyKind::Array(elem_ty, const_len) = ty.kind() else {
193 return false;
194 };
195 let n: Option<usize> = mir_utils::eval_array_len(self.tcx, const_len).map(|v| v as usize);
196 let elem_size = self.size_of_ty(*elem_ty);
197 let step = (elem_size.max(1)) as usize;
198 let align = self.align_sym(*elem_ty);
199 let elem_sym = self.size_sym(*elem_ty);
200 let n_term = self.const_len_term(const_len);
201 let (alloc_id, base) = self.allocate_slice(n_term, elem_sym, align, Some(*elem_ty));
202 self.content_mut(alloc_id).facts.initialized = true;
203 self.path_state.current_frame.local_alloc.insert(local, alloc_id);
204 invariants.init = true;
205 for i in 0..n.unwrap_or(16) {
206 let off = i * step;
207 let elem_term = self.fresh_int(&format!("array_{}_idx_{}", local_idx, i));
208 self.record_byte_value(alloc_id, off, elem_term);
209 }
210 self.set_local(
211 local,
212 VmValue {
213 z3_term: base,
214 ty,
215 provenance: Some(Provenance {
216 alloc_id,
217 offset: Int::from_u64(self.z3_ctx, 0),
218 offset_kind: None,
219 }),
220 facts: invariants.clone(),
221 source: ValueSource::None,
222 },
223 );
224 true
225 }
226
227 pub(super) fn init_reference_param(
231 &mut self,
232 local: Local,
233 ty: Ty<'tcx>,
234 local_idx: usize,
235 invariants: &mut ValueFacts<'z3>,
236 ) -> bool {
237 let rustc_middle::ty::TyKind::Ref(..) = ty.kind() else {
238 return false;
239 };
240 let precise_pointee = crate::vm::region::fn_arg_ty(
241 self.tcx,
242 self.path_state.current_frame.current_def_id,
243 local_idx - 1,
244 )
245 .and_then(|precise_ty| match precise_ty.kind() {
246 rustc_middle::ty::TyKind::Ref(_, inner, _) => Some(*inner),
247 _ => None,
248 });
249 let pointee_ty = precise_pointee.unwrap_or_else(|| {
250 if let rustc_middle::ty::TyKind::Ref(_, inner_ty, _) = ty.kind() {
251 *inner_ty
252 } else {
253 ty
254 }
255 });
256 let pointee_is_maybe_uninit = crate::def_ids::strips_to_maybe_uninit(pointee_ty);
260
261 invariants.non_null = true;
262 if !pointee_is_maybe_uninit {
263 invariants.init = true;
264 }
265 invariants.in_bounds = true;
270
271 if let rustc_middle::ty::TyKind::Slice(elem_ty) = pointee_ty.kind() {
272 let elem_size = self.size_of_ty(*elem_ty);
273 let len = self.fresh_int(&format!("slice_len_{}", local_idx));
274 let zero = Int::from_u64(self.z3_ctx, 0);
275 self.path_state.constraints.assertions.push(len.ge(&zero));
276 let isize_max = Int::from_i64(self.z3_ctx, i64::MAX);
277 let elem_sz = if elem_size > 0 {
278 elem_size
279 } else {
280 mir_utils::size_of_generic_param(
281 self.tcx,
282 self.path_state.current_frame.current_def_id,
283 *elem_ty,
284 )
285 .max(1)
286 };
287 let elem_sz_term = Int::from_u64(self.z3_ctx, elem_sz);
288 self.path_state.constraints
289 .assertions
290 .push(Int::mul(self.z3_ctx, &[&len, &elem_sz_term]).le(&isize_max));
291 if let TyKind::Array(inner_ty, count) = elem_ty.kind() {
292 let inner_size = self.size_sym(*inner_ty);
293 let count = self.const_len_term(count);
294 self.path_state.constraints.assertions.push(count.ge(&zero));
295 let flat_len = Int::mul(self.z3_ctx, &[&len, &count]);
296 self.path_state.constraints
297 .assertions
298 .push(inner_size.gt(&zero).implies(&flat_len.le(&isize_max)));
299 }
300 let elem_align = self.align_sym(*elem_ty);
308 let elem_size_sym = self.size_sym(*elem_ty);
309 let (data_alloc_id, data_base) =
310 self.allocate_slice(len, elem_size_sym, elem_align, Some(*elem_ty));
311 if !pointee_is_maybe_uninit {
312 self.content_mut(data_alloc_id).facts.initialized = true;
313 }
314 let step = (elem_size.max(1)) as usize;
320 let m = 16usize;
321 for i in 0..m {
322 let off = i * step;
323 let elem_term = self.fresh_int(&format!("slice_{}_idx_{}", local_idx, i));
324 self.record_byte_value(data_alloc_id, off, elem_term);
325 }
326 self.set_local(
327 local,
328 VmValue {
329 z3_term: data_base,
330 ty,
331 provenance: Some(Provenance {
332 alloc_id: data_alloc_id,
333 offset: Int::from_u64(self.z3_ctx, 0),
334 offset_kind: None,
335 }),
336 facts: invariants.clone(),
337 source: ValueSource::None,
338 },
339 );
340 return true;
341 }
342
343 let (pointee_alloc_id, pointee_base) = if let rustc_middle::ty::TyKind::Array(elem_ty, const_len) = pointee_ty.kind() {
344 let n = self.const_len_term(const_len);
345 let elem_size = self.size_sym(*elem_ty);
346 let elem_align = self.align_sym(*elem_ty);
347 self.allocate_slice(n, elem_size, elem_align, Some(*elem_ty))
348 } else {
349 let pointee_align = self.align_sym(pointee_ty);
350 let pointee_size_term = self
351 .struct_size_sym(pointee_ty)
352 .unwrap_or_else(|| self.size_sym(pointee_ty));
353 self.allocate(pointee_size_term, pointee_align, Some(pointee_ty))
354 };
355 if !pointee_is_maybe_uninit {
356 self.content_mut(pointee_alloc_id).facts.initialized = true;
357 }
358 self.set_local(
359 local,
360 VmValue {
361 z3_term: pointee_base,
362 ty,
363 provenance: Some(Provenance {
364 alloc_id: pointee_alloc_id,
365 offset: Int::from_u64(self.z3_ctx, 0),
366 offset_kind: None,
367 }),
368 facts: invariants.clone(),
369 source: ValueSource::None,
370 },
371 );
372
373 if matches!(ty.kind(), TyKind::Ref(_, _, rustc_middle::ty::Mutability::Not))
377 && matches!(pointee_ty.kind(), TyKind::Uint(_) | TyKind::Int(_) | TyKind::Bool | TyKind::Char)
378 {
379 let scalar = self.fresh_int(&format!("ref_arg_{local_idx}_value"));
380 let mut value = VmValue::new(scalar, pointee_ty);
381 value.source = ValueSource::SharedScalarReferent;
382 self.store_value(
383 pointee_alloc_id,
384 pointee_ty,
385 Vec::new(),
386 value,
387 );
388 }
389
390 if let rustc_middle::ty::TyKind::Adt(adt_def, substs) = pointee_ty.kind()
391 && !adt_def.is_enum() {
392 let variant = adt_def.non_enum_variant();
393 let mut elem_alloc: FxHashMap<Ty<'tcx>, (AllocId, Int<'z3>)> =
394 FxHashMap::default();
395 for (idx, field_def) in variant.fields.iter().enumerate() {
396 let field_ty: Ty<'tcx> = mir_utils::field_ty(self.tcx, field_def, substs);
397 if let rustc_middle::ty::TyKind::RawPtr(inner, _) = field_ty.kind() {
398 self.init_ptr_field(
399 local,
400 vec![idx],
401 field_ty,
402 *inner,
403 local_idx,
404 idx,
405 &mut elem_alloc,
406 true,
407 "field_nn",
408 );
409 } else if let Some(pointee) = self.find_nn_pointee(field_ty) {
410 self.init_ptr_field(
411 local,
412 vec![idx],
413 field_ty,
414 pointee,
415 local_idx,
416 idx,
417 &mut elem_alloc,
418 false,
419 "ref_field",
420 );
421 } else if let rustc_middle::ty::TyKind::Ref(region, pointee, _) =
422 field_ty.kind()
423 {
424 let elem_ty = match pointee.kind() {
425 rustc_middle::ty::TyKind::Slice(e) => *e,
426 _ => *pointee,
427 };
428 self.materialize_external_field(local, idx, field_ty, elem_ty, Some(*region));
429 } else if let rustc_middle::ty::TyKind::Slice(elem_ty) = field_ty.kind() {
430 self.materialize_external_field(local, idx, field_ty, *elem_ty, None);
435 } else if let rustc_middle::ty::TyKind::Adt(adt, substs) = field_ty.kind() {
436 if (type_ids::is_std_box(adt.did())
441 || type_ids::is_std_vec(adt.did()))
442 && let Some(pointee) = substs.first().and_then(|s| s.as_type())
443 {
444 let elem_ty = match pointee.kind() {
445 rustc_middle::ty::TyKind::Slice(e) => *e,
446 _ => pointee,
447 };
448 self.materialize_external_field(local, idx, field_ty, elem_ty, None);
449 } else if !adt.is_enum() && type_ids::is_std_vec(adt.did()) {
450 self.materialize_vec_ptr_field(local, vec![idx], field_ty);
451 }
452 } else if matches!(
453 field_ty.kind(),
454 rustc_middle::ty::TyKind::Uint(_)
455 | rustc_middle::ty::TyKind::Int(_)
456 | rustc_middle::ty::TyKind::Float(_)
457 | rustc_middle::ty::TyKind::Bool
458 | rustc_middle::ty::TyKind::Char
459 ) {
460 let field_term =
467 self.fresh_int(&format!("ref_field_{}_{}", local_idx, idx));
468 self.set_field_value(
469 local,
470 vec![idx],
471 VmValue {
472 z3_term: field_term,
473 ty: field_ty,
474 provenance: None,
475 facts: ValueFacts {
476 init: true,
477 ..Default::default()
478 },
479 source: ValueSource::None,
480 },
481 );
482 }
483 }
484 }
485 true
486 }
487
488 pub(super) fn propagate_entry_block_provenance(&mut self) {
492 let Some(entry_bb) = self.body().basic_blocks.iter().next() else {
493 return;
494 };
495 for stmt in &entry_bb.statements {
496 let StatementKind::Assign(assign) = &stmt.kind else {
497 continue;
498 };
499 let (dest, rvalue) = &**assign;
500 let dest_local = dest.local;
501 let src = match rvalue {
502 Rvalue::Use(operand, ..) => Some(operand),
503 Rvalue::Cast(_, operand, _) => Some(operand),
504 _ => None,
505 }
506 .and_then(|operand| match operand {
507 Operand::Copy(place) | Operand::Move(place) if place.projection.is_empty() => {
508 Some(place.local)
509 }
510 _ => None,
511 });
512 if let Some(src_local) = src
513 && let Some(src_val) = self.local_value(src_local) {
514 let has_better_prov = src_val.is_pointer()
515 && src_val.facts.non_null
516 && self.local_value(dest_local).is_none_or(|d| {
517 d.provenance.is_none() || !d.facts.non_null
518 });
519 if has_better_prov {
520 self.set_local(
521 dest_local,
522 VmValue {
523 z3_term: src_val.z3_term.clone(),
524 ty: dest.ty(self.body(), self.tcx).ty,
525 provenance: src_val.provenance.clone(),
526 facts: src_val.facts.clone(),
527 source: ValueSource::None,
528 },
529 );
530 }
531 }
532 }
533 }
534
535}
536
537impl<'z3, 'tcx> VmExecutor<'z3, 'tcx> {
538 pub(crate) fn init_parameters(&mut self) {
539 let arg_count = self.body().arg_count;
540 let local_count = self.body().local_decls.len();
541
542 for local_idx in 1..local_count {
543 let local = Local::from_usize(local_idx);
544 if self.path_state.current_frame.local_alloc.contains_key(&local) {
545 continue;
546 }
547 let decl = &self.body().local_decls[local];
548 let ty = decl.ty;
549
550 self.ensure_local_allocation(local);
551
552 let mut invariants = ValueFacts::default();
553 if local_idx <= arg_count {
554 if self.init_box_vec_param(local, ty, local_idx, &mut invariants) {
555 continue;
556 }
557 if self.init_struct_param(local, ty, local_idx, &mut invariants) {
558 continue;
559 }
560 if self.init_reference_param(local, ty, local_idx, &mut invariants) {
561 continue;
562 }
563 let is_scalar = matches!(
564 ty.kind(),
565 rustc_middle::ty::TyKind::Uint(_)
566 | rustc_middle::ty::TyKind::Int(_)
567 | rustc_middle::ty::TyKind::Float(_)
568 | rustc_middle::ty::TyKind::Bool
569 | rustc_middle::ty::TyKind::Char
570 );
571 if is_scalar {
572 let val = self.fresh_int(&format!("arg_{}", local_idx));
573 self.set_local(
574 local,
575 VmValue {
576 z3_term: val,
577 ty,
578 provenance: None,
579 facts: invariants,
580 source: ValueSource::None,
581 },
582 );
583 continue;
584 }
585 if let rustc_middle::ty::TyKind::RawPtr(pointee, _mutbl) = ty.kind() {
586 let max_size = Int::from_u64(self.z3_ctx, i64::MAX as u64);
587 let pointee_align = self.align_sym(*pointee);
588 let (alloc_id, base) =
589 self.allocate_external(max_size, pointee_align, Some(*pointee));
590 self.set_local(
591 local,
592 VmValue {
593 z3_term: base,
594 ty,
595 provenance: Some(Provenance {
596 alloc_id,
597 offset: Int::from_u64(self.z3_ctx, 0),
598 offset_kind: None,
599 }),
600 facts: invariants,
601 source: ValueSource::None,
602 },
603 );
604 continue;
605 }
606 if self.init_array_param(local, ty, local_idx, &mut invariants) {
607 continue;
608 }
609 let term = self.fresh_int(&format!("param_{}", local_idx));
610 self.set_local(
611 local,
612 VmValue {
613 z3_term: term,
614 ty,
615 provenance: None,
616 facts: invariants,
617 source: ValueSource::None,
618 },
619 );
620 continue;
621 }
622 let term = match ty.kind() {
623 rustc_middle::ty::TyKind::Ref(..) | rustc_middle::ty::TyKind::RawPtr(..) => {
624 self.local_address(local)
625 }
626 _ => self.fresh_int(&format!("local_{}", local_idx)),
627 };
628 self.set_local(
629 local,
630 VmValue {
631 z3_term: term,
632 ty,
633 provenance: None,
634 facts: invariants,
635 source: ValueSource::None,
636 },
637 );
638 }
639
640 self.propagate_entry_block_provenance();
641 self.prewarm_generic_size_align();
642 }
643}