1use rustc_abi::FieldIdx;
2use rustc_hir::def_id::DefId;
3use rustc_index::IndexVec;
4use rustc_middle::{
5 mir::{AggregateKind, BinOp, CastKind, Operand, Place, ProjectionElem, UnOp},
6 ty::{Ty, TyKind},
7};
8use z3::ast::{Ast, Int};
9
10use super::super::state::{
11 AllocId, Provenance, REPEATED_ARRAY_ELEMENT, ValueFacts, ValueSource, VmExecutor, VmValue,
12};
13
14use crate::def_ids::type_ids;
15use crate::util::mir_utils;
16
17pub(crate) fn eval_use<'z3, 'tcx>(
18 state: &mut VmExecutor<'z3, 'tcx>,
19 place: &Place<'tcx>,
20 operand: &Operand<'tcx>,
21) -> VmValue<'z3, 'tcx> {
22 if let Operand::Constant(constant) = operand
23 && let Some(static_id) = constant.check_static_ptr(state.tcx)
24 && state.tcx.is_mutable_static(static_id)
25 {
26 return eval_global_static_ptr(state, place, static_id);
27 }
28 let mut val = state.value_of_operand(operand);
29 if let Some(local) = operand.place().map(|place| place.local)
30 && let Some(alloc_id) = val.provenance_alloc_id()
31 {
32 state.record_memory_use(alloc_id, local);
33 }
34 if let rustc_middle::mir::Operand::Constant(c) = operand
35 && place.projection.is_empty()
36 {
37 state.materialize_const_adt(place.local, &c.const_);
38 }
39 state.try_materialize_const_bytes(&mut val, operand);
40 if let Operand::Constant(_) = operand
41 && let Some(bytes) = mir_utils::const_operand_bytes(state.tcx, operand)
42 {
43 val.source = ValueSource::ConstBytes(bytes);
44 }
45 state.inject_layout_constraints(operand, &val);
46 state.try_emit_gcd_divisibility(operand, &val);
47 val
48}
49
50pub(crate) fn eval_ref<'z3, 'tcx>(
51 state: &mut VmExecutor<'z3, 'tcx>,
52 dest_place: &Place<'tcx>,
53 place: &Place<'tcx>,
54) -> VmValue<'z3, 'tcx> {
55 let dest_ty = dest_place.ty(state.body(), state.tcx).ty;
56 if let Some(addr) = state.address_of_place(place) {
57 if place.projection.iter().any(|projection| matches!(projection.kind(), ProjectionElem::Field(..)))
58 && let Some(value) = state.value_of_place(place)
59 && let Some(alloc_id) = value.provenance_alloc_id()
60 {
61 state.record_memory_use(alloc_id, place.local);
62 }
63 if let Some(alloc_id) = addr.provenance_alloc_id() {
64 state.record_memory_use(alloc_id, place.local);
65 }
66 let alloc_align = state.alloc_align_of(&addr);
67 let has_deref = place
68 .projection
69 .iter()
70 .any(|p| matches!(p.kind(), rustc_middle::mir::ProjectionElem::Deref));
71 let src_ty = state.body().local_decls[place.local].ty;
72 let is_raw_ptr_source =
73 matches!(src_ty.kind(), rustc_middle::ty::TyKind::RawPtr(_, _));
74 let is_slice_ref = if let rustc_middle::ty::TyKind::Ref(_, inner, _) = dest_ty.kind() {
75 matches!(inner.kind(), rustc_middle::ty::TyKind::Slice(_))
76 } else {
77 false
78 };
79 let is_slice_ref_from_raw = is_slice_ref && is_raw_ptr_source && has_deref;
80 let src_in_bounds = if is_slice_ref_from_raw {
81 addr.is_pointer()
82 } else {
83 state
84 .local_value(place.local)
85 .is_some_and(|v| v.facts.in_bounds)
86 };
87 let slice_elem_align = if is_slice_ref_from_raw {
93 if let rustc_middle::ty::TyKind::Ref(_, inner, _) = dest_ty.kind() {
94 if let rustc_middle::ty::TyKind::Slice(elem) = inner.kind() {
95 let a = state.align_sym(*elem);
96 if a.simplify().as_u64() != Some(1) {
97 Some(a)
98 } else {
99 None
100 }
101 } else {
102 None
103 }
104 } else {
105 None
106 }
107 } else {
108 None
109 };
110 let val = VmValue {
111 z3_term: addr.z3_term,
112 ty: dest_ty,
113 provenance: addr.provenance,
114 facts: ValueFacts {
115 non_null: true,
116 init: true,
117 in_bounds: src_in_bounds,
118 align_n: slice_elem_align.or(alloc_align),
119 dangling: false,
120 ownership_transfer: addr.facts.ownership_transfer,
121 },
122 source: match state.local_value(place.local).map(|value| &value.source) {
123 Some(ValueSource::SliceLen(len)) if is_slice_ref && has_deref => {
124 ValueSource::SliceLen(len.clone())
125 }
126 _ => {
127 let fields: Vec<_> = place
128 .projection
129 .iter()
130 .filter_map(|projection| match projection.kind() {
131 rustc_middle::mir::ProjectionElem::Field(field, _) => {
132 Some(field.as_usize())
133 }
134 _ => None,
135 })
136 .collect();
137 if fields.is_empty() {
138 if has_deref {
139 match state.local_value(place.local).map(|value| &value.source) {
140 Some(ValueSource::FieldBorrow { base, fields }) => {
141 ValueSource::FieldBorrow {
142 base: *base,
143 fields: fields.clone(),
144 }
145 }
146 _ => ValueSource::None,
147 }
148 } else {
149 ValueSource::None
150 }
151 } else {
152 ValueSource::FieldBorrow {
153 base: place.local,
154 fields,
155 }
156 }
157 }
158 },
159 };
160 state.propagate_byte_values_to_ref(place, &val);
161 state.propagate_field_values_to_ref(place, dest_place.local);
162 state.set_local(dest_place.local, val.clone());
168 state.assert_pointee_struct_invariants(dest_ty, dest_place.local);
169 val
170 } else {
171 let term = state.fresh_int("ref_addr");
172 VmValue {
173 z3_term: term,
174 ty: dest_ty,
175 provenance: None,
176 facts: ValueFacts {
177 non_null: true,
178 init: true,
179 ..Default::default()
180 },
181 source: ValueSource::None,
182 }
183 }
184}
185
186pub(crate) fn eval_raw_ptr<'z3, 'tcx>(
187 state: &mut VmExecutor<'z3, 'tcx>,
188 dest_place: &Place<'tcx>,
189 place: &Place<'tcx>,
190) -> VmValue<'z3, 'tcx> {
191 let dest_ty = dest_place.ty(state.body(), state.tcx).ty;
192 if let Some(addr) = state.address_of_place(place) {
193 let alloc_align = state.alloc_align_of(&addr);
194 let source_in_bounds = state
195 .local_value(place.local)
196 .is_some_and(|v| v.facts.in_bounds);
197 VmValue {
198 z3_term: addr.z3_term,
199 ty: dest_ty,
200 provenance: addr.provenance,
201 facts: ValueFacts {
202 non_null: true,
203 in_bounds: source_in_bounds,
204 align_n: alloc_align,
205 ..Default::default()
206 },
207 source: ValueSource::None,
208 }
209 } else {
210 let term = state.fresh_int("rawptr_addr");
211 VmValue {
212 z3_term: term,
213 ty: dest_ty,
214 provenance: None,
215 facts: ValueFacts {
216 non_null: true,
217 ..Default::default()
218 },
219 source: ValueSource::None,
220 }
221 }
222}
223
224pub(crate) fn eval_binary_op<'z3, 'tcx>(
225 state: &mut VmExecutor<'z3, 'tcx>,
226 dest_place: &Place<'tcx>,
227 op: BinOp,
228 pair: &(Operand<'tcx>, Operand<'tcx>),
229) -> VmValue<'z3, 'tcx> {
230 let (lhs_op, rhs_op) = pair;
231 let dest_ty = dest_place.ty(state.body(), state.tcx).ty;
232
233 let lhs = state.value_of_operand(lhs_op);
234 let rhs = state.value_of_operand(rhs_op);
235 if op == BinOp::Offset {
240 let pointee = mir_utils::pointee_ty(lhs.ty);
246 let stride = pointee.and_then(|p| {
247 if mir_utils::ty_has_type_param(p)
248 && let Some(alloc_id) = lhs.provenance_alloc_id()
249 && let Some(element) = state.alloc(alloc_id).element_ty.as_ty()
250 && !mir_utils::ty_has_type_param(element)
251 {
252 return Some(state.size_of_ty(element));
253 }
254 let (_, s) = mir_utils::type_layout(state.tcx, state.path_state.current_frame.current_def_id, p)?;
255 if s == 0 && mir_utils::ty_has_type_param(p) {
256 None
257 } else {
258 Some(s)
259 }
260 });
261 state.apply_pointer_add(dest_place.local, &lhs, &rhs, stride, false);
262 return state
263 .local_value(dest_place.local)
264 .cloned()
265 .expect("pointer arithmetic must assign its destination");
266 }
267 let lhs_concrete =
268 matches!(op, BinOp::Shl | BinOp::ShlUnchecked).then(|| lhs.z3_term.simplify());
269 let raw_term = if let Some(value) = lhs_concrete.as_ref().and_then(|term| {
270 term.as_u64()
271 .map(u128::from)
272 .or_else(|| term.to_string().parse::<u128>().ok())
273 }) && let Some(shift) = rhs.z3_term.simplify().as_u64()
274 && let width = state.size_of_ty(lhs.ty) * 8
275 && width > 0
276 && width <= 128
277 && shift < width
278 && matches!(lhs.ty.kind(), TyKind::Uint(_) | TyKind::Int(_))
279 {
280 let shifted = (value << shift) & (u128::MAX >> (128 - width));
281 state
282 .scalar_bits_term(lhs.ty, shifted)
283 .unwrap_or_else(|| state.eval_binary_op(op, &lhs.z3_term, &rhs.z3_term))
284 } else {
285 state.eval_binary_op(op, &lhs.z3_term, &rhs.z3_term)
286 };
287 if matches!(op, BinOp::Shl | BinOp::ShlUnchecked)
288 && lhs.z3_term.simplify().as_u64() == Some(1)
289 && matches!(lhs.ty.kind(), TyKind::Uint(_) | TyKind::Int(_))
290 {
291 let zero = Int::from_u64(state.z3_ctx, 0);
292 state.path_state.constraints.assertions.push(raw_term._eq(&zero).not());
293 }
294 let term = if matches!(op, BinOp::Add | BinOp::Sub | BinOp::Mul) {
295 state.wrap_integer_term(lhs.ty, &raw_term)
296 } else {
297 raw_term
298 };
299 let provenance = state.provenance_for_binary_op(op, &lhs, &rhs);
300 let invariants = state.facts_for_binary_op(op, &lhs, &rhs, &provenance);
301 let lhs_pk = mir_utils::operand_place(lhs_op);
302 let rhs_pk = mir_utils::operand_place(rhs_op);
303 let cmp_cond = state
308 .iter_ptr_comparison(op, &lhs, &rhs)
309 .or_else(|| match op {
310 BinOp::Le => Some(lhs.z3_term.le(&rhs.z3_term)),
311 BinOp::Lt => Some(lhs.z3_term.lt(&rhs.z3_term)),
312 BinOp::Ge => Some(lhs.z3_term.ge(&rhs.z3_term)),
313 BinOp::Gt => Some(lhs.z3_term.gt(&rhs.z3_term)),
314 BinOp::Eq => Some(lhs.z3_term._eq(&rhs.z3_term)),
315 BinOp::Ne => Some(lhs.z3_term._eq(&rhs.z3_term).not()),
316 _ => None,
317 });
318 if matches!(op, BinOp::Div | BinOp::Rem) {
319 state.emit_div_rem_axioms(op, &term, &lhs.z3_term, &rhs.z3_term);
320 }
321 if let rustc_middle::ty::TyKind::Tuple(fields) = dest_ty.kind()
327 && fields.len() == 2
328 {
329 let result_val = VmValue {
330 z3_term: term.clone(),
331 ty: fields[0],
332 provenance: provenance.clone(),
333 facts: invariants.clone(),
334 source: ValueSource::None,
335 };
336 state.set_field_value(dest_place.local, vec![0], result_val);
337 let overflow_term = if matches!(op, BinOp::MulWithOverflow)
338 && (lhs.z3_term.simplify().as_u64().is_some_and(|n| n <= 1)
339 || rhs.z3_term.simplify().as_u64().is_some_and(|n| n <= 1))
340 {
341 Int::from_u64(state.z3_ctx, 0)
342 } else if matches!(
343 op,
344 BinOp::AddWithOverflow | BinOp::SubWithOverflow | BinOp::MulWithOverflow
345 ) && let Some((min, max, _, _)) = state.integer_limits(lhs.ty)
346 {
347 let overflow = z3::ast::Bool::or(state.z3_ctx, &[&term.lt(&min), &term.gt(&max)]);
348 overflow.ite(
349 &Int::from_u64(state.z3_ctx, 1),
350 &Int::from_u64(state.z3_ctx, 0),
351 )
352 } else {
353 state.fresh_int("overflow_flag")
354 };
355 let overflow_val = VmValue::new(overflow_term, fields[1]);
356 state.set_field_value(dest_place.local, vec![1], overflow_val);
357 }
358 VmValue {
359 z3_term: term,
360 ty: dest_ty,
361 provenance,
362 facts: invariants,
363 source: match cmp_cond {
364 Some(cond) => ValueSource::Comparison {
365 lhs: lhs_pk,
366 rhs: rhs_pk,
367 op,
368 cond,
369 },
370 None => ValueSource::BinaryOp {
371 lhs: lhs_pk,
372 rhs: rhs_pk,
373 op,
374 },
375 },
376 }
377}
378
379pub(crate) fn eval_unary_op<'z3, 'tcx>(
380 state: &mut VmExecutor<'z3, 'tcx>,
381 dest_place: &Place<'tcx>,
382 op: UnOp,
383 operand: &Operand<'tcx>,
384) -> VmValue<'z3, 'tcx> {
385 let dest_ty = dest_place.ty(state.body(), state.tcx).ty;
386 let val = state.value_of_operand(operand);
387 let is_bool = matches!(val.ty.kind(), rustc_middle::ty::TyKind::Bool);
388 let mut result_ty = dest_ty;
389 let term = if matches!(op, UnOp::PtrMetadata) {
390 let is_dst = mir_utils::pointee_ty(val.ty).is_some_and(|p| {
396 !p.is_sized(
397 state.tcx,
398 rustc_middle::ty::TypingEnv::post_analysis(
399 state.tcx,
400 state.path_state.current_frame.current_def_id,
401 ),
402 )
403 });
404 if is_dst {
405 state
406 .slice_len_from_value(&val)
407 .unwrap_or_else(|| state.fresh_int("ptr_metadata"))
408 } else {
409 result_ty = state.tcx.types.unit;
414 state.fresh_int("ptr_metadata")
415 }
416 } else {
417 state.eval_unary_op(op, &val.z3_term, is_bool)
418 };
419 VmValue {
420 z3_term: term,
421 ty: result_ty,
422 provenance: val.provenance,
423 facts: val.facts,
424 source: ValueSource::None,
425 }
426}
427
428fn pointer_free_bits<'z3, 'tcx>(state: &VmExecutor<'z3, 'tcx>, ty: Ty<'tcx>, depth: usize) -> bool {
429 if depth > 8 {
430 return false;
431 }
432 match ty.kind() {
433 TyKind::Int(_) | TyKind::Uint(_) | TyKind::Bool | TyKind::Char => true,
434 TyKind::Adt(adt, args) => adt.all_fields().all(|field| {
435 pointer_free_bits(state, mir_utils::field_ty(state.tcx, field, args), depth + 1)
436 }),
437 TyKind::Tuple(fields) => fields
438 .iter()
439 .all(|field| pointer_free_bits(state, field, depth + 1)),
440 TyKind::Array(element, _) => pointer_free_bits(state, *element, depth + 1),
441 _ => false,
442 }
443}
444
445pub(crate) fn eval_cast<'z3, 'tcx>(
446 state: &mut VmExecutor<'z3, 'tcx>,
447 kind: CastKind,
448 operand: &Operand<'tcx>,
449 cast_ty: Ty<'tcx>,
450) -> VmValue<'z3, 'tcx> {
451 let mut src_val = state.value_of_operand(operand);
452 state.try_materialize_const_bytes(&mut src_val, operand);
453 #[cfg(rapx_box_deref_transmute)]
454 let src_val = if matches!(kind, CastKind::BoxDerefTransmute) {
455 mir_utils::extract_local(operand)
456 .and_then(|local| state.field_value(local, &[0]).cloned())
457 .unwrap_or(src_val)
458 } else {
459 src_val
460 };
461 let src_ty = src_val.ty;
462 let nonnull_to_raw = matches!(kind, CastKind::Transmute)
463 && matches!(cast_ty.kind(), TyKind::RawPtr(..))
464 && matches!(src_ty.kind(), TyKind::Adt(adt, _) if type_ids::is_std_nonnull(adt.did()));
465 let numeric_transmute = matches!(kind, CastKind::Transmute)
466 && mir_utils::is_std_crate_def_id(state.tcx, state.path_state.current_frame.current_def_id)
467 && src_val.provenance.is_none()
468 && (pointer_free_bits(state, src_ty, 0)
469 || matches!(src_val.source, ValueSource::KnownDiscriminant(_)))
470 && (matches!(cast_ty.kind(), TyKind::RawPtr(..))
471 || matches!(cast_ty.kind(), TyKind::Adt(adt, _) if type_ids::is_std_nonnull(adt.did())));
472 if let rustc_middle::ty::TyKind::RawPtr(target_ty, _) = cast_ty.kind()
479 && (matches!(target_ty.kind(), TyKind::Tuple(_))
480 || matches!(target_ty.kind(), TyKind::Adt(adt_def, _) if adt_def.is_struct()))
481 && let Some(alloc_id) = src_val.provenance_alloc_id()
482 {
483 state.decompose_pointee_fields(alloc_id, Vec::new(), *target_ty, *target_ty, 0, 0, 0);
484 }
485 let term = mir_utils::extract_local(operand)
489 .and_then(|l| state.field_value(l, &[0]).map(|v| v.z3_term.clone()))
490 .unwrap_or(src_val.z3_term);
491 let src_is_ptr = matches!(
498 src_ty.kind(),
499 rustc_middle::ty::TyKind::RawPtr(..) | rustc_middle::ty::TyKind::Ref(..)
500 );
501 let dest_is_int = matches!(
502 cast_ty.kind(),
503 rustc_middle::ty::TyKind::Uint(_) | rustc_middle::ty::TyKind::Int(_)
504 );
505 if src_is_ptr && dest_is_int {
506 let zero = Int::from_u64(state.z3_ctx, 0);
507 state.path_state.constraints.assertions.push(term.ge(&zero));
508 if src_val.facts.non_null {
509 state.path_state.constraints.assertions.push(term._eq(&zero).not());
510 }
511 }
512 VmValue {
513 z3_term: term,
514 ty: cast_ty,
515 provenance: src_val.provenance,
516 facts: ValueFacts {
517 non_null: src_val.facts.non_null || nonnull_to_raw,
518 dangling: src_val.facts.dangling || numeric_transmute,
519 init: src_val.facts.init,
520 in_bounds: src_val.facts.in_bounds,
521 ownership_transfer: src_val.facts.ownership_transfer,
522 align_n: if mir_utils::pointee_ty(src_ty).is_some_and(|t| t.is_unit()) {
523 None
524 } else {
525 src_val.facts.align_n
526 },
527 },
528 source: match &src_val.source {
529 ValueSource::SliceLen(len)
530 if src_is_ptr
531 && mir_utils::pointee_ty(cast_ty)
532 .is_some_and(|ty| matches!(ty.kind(), TyKind::Slice(_) | TyKind::Str)) =>
533 {
534 ValueSource::SliceLen(len.clone())
535 }
536 _ => src_val.source.field_offset_only(),
537 },
538 }
539}
540
541pub(crate) fn eval_aggregate<'z3, 'tcx>(
542 state: &mut VmExecutor<'z3, 'tcx>,
543 dest_place: &Place<'tcx>,
544 kind: &AggregateKind<'tcx>,
545 operands: &IndexVec<FieldIdx, Operand<'tcx>>,
546) -> VmValue<'z3, 'tcx> {
547 let dest_ty = dest_place.ty(state.body(), state.tcx).ty;
548
549 if let rustc_middle::mir::AggregateKind::RawPtr(pointee_ty, _) = kind
555 && let Some(data_operand) = operands.iter().next()
556 {
557 let sized = (!mir_utils::ty_has_type_param(*pointee_ty)
558 && pointee_ty.is_sized(
559 state.tcx,
560 rustc_middle::ty::TypingEnv::post_analysis(
561 state.tcx,
562 state.path_state.current_frame.current_def_id,
563 ),
564 ))
565 || operands
566 .iter()
567 .nth(1)
568 .is_some_and(|meta| state.value_of_operand(meta).ty.is_unit());
569 if sized {
570 let data_val = state.value_of_operand(data_operand);
571 let mut val = data_val.clone();
572 val.ty = dest_ty;
573 return val;
574 }
575 if let Some(metadata) = operands.iter().nth(1) {
576 let metadata = state.value_of_operand(metadata);
577 let mut val = state.value_of_operand(data_operand);
578 val.ty = dest_ty;
579 if matches!(
580 pointee_ty.kind(),
581 TyKind::Slice(_) | TyKind::Param(_) | TyKind::Alias(..)
582 ) || metadata.ty == state.tcx.types.usize
583 {
584 val.source = ValueSource::SliceLen(metadata.z3_term);
585 }
586 return val;
587 }
588 }
589
590 let data_variant = match kind {
594 rustc_middle::mir::AggregateKind::Adt(did, variant_idx, ..) => {
595 if state.tcx.is_diagnostic_item(rustc_span::sym::Result, *did) {
596 Some(variant_idx.as_usize() == 0)
597 } else if state.tcx.is_diagnostic_item(rustc_span::sym::Option, *did) {
598 Some(variant_idx.as_usize() == 1)
599 } else {
600 None
601 }
602 }
603 _ => None,
604 };
605 if operands.len() == 1
614 && (state.find_nn_pointee(dest_ty).is_some() || state.is_transparent_newtype(dest_ty))
615 {
616 let operand = operands.iter().next().unwrap();
617 let field_val = state.value_of_operand(operand);
618 let dest_local = dest_place.local;
619 state.set_field_value(dest_local, vec![0], field_val.clone());
620 if let Some(source) = operand.place()
621 && source.projection.is_empty()
622 {
623 let nested = state
624 .field_paths(source.local)
625 .into_iter()
626 .filter_map(|path| {
627 state
628 .field_value(source.local, &path)
629 .cloned()
630 .map(|value| (path, value))
631 })
632 .collect::<Vec<_>>();
633 for (path, value) in nested {
634 let mut dest_path = vec![0];
635 dest_path.extend(path);
636 state.set_field_value(dest_local, dest_path, value);
637 }
638 }
639 if let Some(alloc_id) = state.path_state.current_frame.local_alloc.get(&dest_local).copied() {
640 state.content_mut(alloc_id).facts.initialized = true;
641 }
642 return VmValue {
643 z3_term: field_val.z3_term,
644 ty: dest_ty,
645 provenance: field_val.provenance,
646 facts: field_val.facts,
647 source: ValueSource::None,
648 };
649 }
650 let term = state.fresh_int("aggregate");
651 let dest_local = dest_place.local;
652 let dest_prefix = if dest_place.projection.iter().all(|projection| {
653 matches!(
654 projection.kind(),
655 rustc_middle::mir::ProjectionElem::Field(..)
656 | rustc_middle::mir::ProjectionElem::Downcast(..)
657 )
658 }) {
659 dest_place
660 .projection
661 .iter()
662 .filter_map(|projection| match projection.kind() {
663 rustc_middle::mir::ProjectionElem::Field(field, _) => Some(field.as_usize()),
664 _ => None,
665 })
666 .collect::<Vec<_>>()
667 } else {
668 Vec::new()
669 };
670 let dest_alloc_id = state
674 .local_value(dest_local)
675 .and_then(|v| v.provenance_alloc_id())
676 .or_else(|| state.path_state.current_frame.local_alloc.get(&dest_local).copied());
677 let is_byte_array = mir_utils::is_u8_array_or_slice(dest_ty);
678 let field_types: Vec<_> = state.aggregate_field_tys(dest_ty);
679 let mut byte_offset = 0usize;
680 for (i, operand) in operands.iter().enumerate() {
681 let mut field_val = state.value_of_operand(operand);
682 let field_ty_opt = field_types.get(i).copied();
683 if let Some(field_ty) = field_ty_opt {
684 let src_is_ref = matches!(field_val.ty.kind(), rustc_middle::ty::TyKind::Ref(..));
685 let dst_is_raw = matches!(field_ty.kind(), rustc_middle::ty::TyKind::RawPtr(..));
686 if dst_is_raw && (src_is_ref || field_val.facts.non_null) {
687 field_val.facts.in_bounds = true;
688 field_val.ty = field_ty;
689 }
690 }
691 let field_sz = field_ty_opt
692 .map(|ty| state.size_of_ty(ty) as usize)
693 .unwrap_or(1);
694 let field_term = field_val.z3_term.clone();
695 let mut field_path = dest_prefix.clone();
696 field_path.push(i);
697 state.set_field_value(dest_local, field_path.clone(), field_val);
698 if data_variant != Some(false)
706 && let Some(op_place) = operand.place()
707 && op_place.projection.is_empty()
708 {
709 let nested: Vec<(Vec<usize>, VmValue<'z3, 'tcx>)> = state
710 .field_paths(op_place.local)
711 .into_iter()
712 .filter_map(|p| {
713 state
714 .field_value(op_place.local, &p)
715 .cloned()
716 .map(|v| (p, v))
717 })
718 .collect();
719 for (nested_path, nested_val) in nested {
720 let mut full = field_path.clone();
721 full.extend_from_slice(&nested_path);
722 state.set_field_value(dest_local, full, nested_val);
723 }
724 }
725 if let Some(alloc_id) = dest_alloc_id {
726 state.content_mut(alloc_id).facts.initialized = true;
727 if is_byte_array && field_sz == 1 {
728 state.record_byte_value(alloc_id, byte_offset, field_term.clone());
729 }
730 if let Some(int_val) = mir_utils::operand_const_u64(operand) {
731 if field_sz == 1 {
732 if int_val == 0 {
733 if !is_byte_array {
734 state.record_byte_value(
735 alloc_id,
736 byte_offset,
737 Int::from_u64(state.z3_ctx, 0),
738 );
739 }
740 } else if !is_byte_array {
741 state.record_byte_value(
742 alloc_id,
743 byte_offset,
744 Int::from_u64(state.z3_ctx, int_val),
745 );
746 }
747 }
748 for b in 0..field_sz.min(8) {
749 let byte_off = byte_offset + b;
750 let byte_val = (int_val >> (b * 8)) & 0xFF;
751 state.record_byte_value(
752 alloc_id,
753 byte_off,
754 Int::from_u64(state.z3_ctx, byte_val),
755 );
756 }
757 }
758 }
759 byte_offset += field_sz;
760 }
761 let is_slice_ptr = matches!(dest_ty.kind(),
767 rustc_middle::ty::TyKind::RawPtr(inner, _) | rustc_middle::ty::TyKind::Ref(_, inner, _)
768 if matches!(inner.kind(), rustc_middle::ty::TyKind::Slice(_)));
769 let (result_term, result_prov) = if is_slice_ptr {
770 match state.field_value(dest_local, &[0]).cloned() {
771 Some(data) => (data.z3_term.clone(), data.provenance.clone()),
772 None => (term.clone(), None),
773 }
774 } else {
775 let box_prov = if let rustc_middle::ty::TyKind::Adt(adt, _) = dest_ty.kind() {
781 if type_ids::is_std_box(adt.did()) {
782 state
783 .container_ptr_field(dest_ty)
784 .and_then(|path| state.field_value(dest_local, &path))
785 .and_then(|v| v.provenance.clone())
786 } else {
787 None
788 }
789 } else {
790 None
791 };
792 (term.clone(), box_prov)
793 };
794 let source = match kind {
795 rustc_middle::mir::AggregateKind::Adt(did, variant_idx, ..)
796 if state.tcx.adt_def(*did).is_enum() =>
797 {
798 if let Some(term) = state.enum_discriminant_term(dest_ty, *variant_idx) {
799 ValueSource::KnownDiscriminant(term)
800 } else {
801 state.record_unsupported("enum discriminant value");
802 ValueSource::None
803 }
804 }
805 _ => ValueSource::None,
806 };
807 VmValue {
808 z3_term: result_term,
809 ty: dest_ty,
810 provenance: result_prov,
811 facts: ValueFacts::default(),
812 source,
813 }
814}
815
816pub(crate) fn eval_discriminant<'z3, 'tcx>(
817 state: &mut VmExecutor<'z3, 'tcx>,
818 dest_place: &Place<'tcx>,
819 place: &Place<'tcx>,
820) -> VmValue<'z3, 'tcx> {
821 let dest_ty = dest_place.ty(state.body(), state.tcx).ty;
822 let place_val = state
827 .value_of_place(place)
828 .or_else(|| state.local_value(place.local).cloned());
829 let term = place_val
830 .as_ref()
831 .and_then(|v| v.discriminant().cloned())
832 .unwrap_or_else(|| state.fresh_int("discriminant"));
833 if let Some(ref pv) = place_val
838 && !matches!(pv.source, ValueSource::KnownDiscriminant(_))
839 && let rustc_middle::ty::TyKind::Adt(adt_def, _) = pv.ty.kind()
840 && type_ids::is_std_ordering(adt_def.did())
841 && adt_def.is_enum()
842 {
843 let one = Int::from_u64(state.z3_ctx, 1);
844 let discr_minus_one = Int::sub(state.z3_ctx, &[&term, &one]);
845 state.path_state
846 .constraints
847 .assertions
848 .push(pv.z3_term._eq(&discr_minus_one));
849 let zero = Int::from_u64(state.z3_ctx, 0);
850 let two = Int::from_u64(state.z3_ctx, 2);
851 state.path_state.constraints.assertions.push(term.ge(&zero));
852 state.path_state.constraints.assertions.push(term.le(&two));
853 }
854 let mut value = VmValue::new(term, dest_ty);
855 if place_val.is_some_and(|value| matches!(value.source, ValueSource::KnownDiscriminant(_))) {
856 value.source = ValueSource::KnownDiscriminant(value.z3_term.clone());
857 }
858 value
859}
860
861
862pub(crate) fn eval_copy_for_deref<'z3, 'tcx>(
863 state: &mut VmExecutor<'z3, 'tcx>,
864 dest_place: &Place<'tcx>,
865 place: &Place<'tcx>,
866) -> VmValue<'z3, 'tcx> {
867 let dest_ty = dest_place.ty(state.body(), state.tcx).ty;
868 if let Some(val) = state.value_of_place(place) {
869 val
870 } else {
871 let term = state.fresh_int("copy_for_deref");
872 VmValue::new(term, dest_ty)
873 }
874}
875
876pub(crate) fn eval_fresh_value<'z3, 'tcx>(
877 state: &mut VmExecutor<'z3, 'tcx>,
878 dest_place: &Place<'tcx>,
879 name: &str,
880) -> VmValue<'z3, 'tcx> {
881 let dest_ty = dest_place.ty(state.body(), state.tcx).ty;
882 let term = state.fresh_int(name);
883 VmValue::new(term, dest_ty)
884}
885
886fn eval_global_static_ptr<'z3, 'tcx>(
888 state: &mut VmExecutor<'z3, 'tcx>,
889 dest_place: &Place<'tcx>,
890 def_id: DefId,
891) -> VmValue<'z3, 'tcx> {
892 let pointer_ty = dest_place.ty(state.body(), state.tcx).ty;
893 let object_ty = state.tcx.type_of(def_id).skip_binder();
894 let (alloc_id, base) = if let Some(&alloc_id) = state.path_state.static_allocs.get(&def_id) {
895 (alloc_id, state.alloc(alloc_id).base.clone())
896 } else {
897 let size = state.size_of_ty(object_ty);
898 let align = state.align_sym(object_ty);
899 let (alloc_id, base) =
900 state.allocate(Int::from_u64(state.z3_ctx, size), align, Some(object_ty));
901 state.path_state.static_allocs.insert(def_id, alloc_id);
902 state.content_mut(alloc_id).facts.initialized = true;
903 (alloc_id, base)
904 };
905 VmValue {
906 z3_term: base,
907 ty: pointer_ty,
908 provenance: Some(Provenance {
909 alloc_id,
910 offset: Int::from_u64(state.z3_ctx, 0),
911 offset_kind: None,
912 }),
913 facts: ValueFacts {
914 non_null: true,
915 init: true,
916 in_bounds: true,
917 ..Default::default()
918 },
919 source: ValueSource::None,
920 }
921}
922
923pub(crate) fn eval_thread_local_ref<'z3, 'tcx>(
924 state: &mut VmExecutor<'z3, 'tcx>,
925 dest_place: &Place<'tcx>,
926 def_id: DefId,
927) -> VmValue<'z3, 'tcx> {
928 let dest_ty = dest_place.ty(state.body(), state.tcx).ty;
929 let Some(static_ty) = mir_utils::pointee_ty(dest_ty) else {
930 state.path_state.diagnostics.mark_unsupported();
931 return eval_fresh_value(state, dest_place, "thread_local");
932 };
933 let scalar = matches!(
934 static_ty.kind(),
935 TyKind::Int(_) | TyKind::Uint(_) | TyKind::Bool | TyKind::Char
936 );
937 let mutable = state.tcx.is_mutable_static(def_id);
938 let bytes = if scalar && !mutable {
939 state
940 .tcx
941 .eval_static_initializer(def_id)
942 .ok()
943 .map(|allocation| {
944 let allocation = allocation.inner();
945 allocation
946 .inspect_with_uninit_and_ptr_outside_interpreter(0..allocation.len())
947 .to_vec()
948 })
949 } else {
950 None
951 };
952 let size = state.size_of_ty(static_ty);
953 if !scalar || size == 0 || size > 16 {
954 state.record_unsupported("ThreadLocalRef pointee type");
955 return eval_fresh_value(state, dest_place, "thread_local");
956 }
957 let scalar_term = if mutable {
958 Some(state.fresh_int("thread_local_initial"))
959 } else {
960 bytes
961 .filter(|bytes| bytes.len() == size as usize)
962 .and_then(|bytes| {
963 let bits = if state.tcx.data_layout.endian == rustc_abi::Endian::Little {
964 bytes
965 .iter()
966 .rev()
967 .fold(0_u128, |bits, byte| (bits << 8) | u128::from(*byte))
968 } else {
969 bytes
970 .iter()
971 .fold(0_u128, |bits, byte| (bits << 8) | u128::from(*byte))
972 };
973 state.scalar_bits_term(static_ty, bits)
974 })
975 };
976 let Some(scalar_term) = scalar_term else {
977 state.record_unsupported("ThreadLocalRef initializer");
978 return eval_fresh_value(state, dest_place, "thread_local");
979 };
980 let (alloc_id, base) = if let Some(&alloc_id) = state.path_state.static_allocs.get(&def_id) {
981 (alloc_id, state.alloc(alloc_id).base.clone())
982 } else {
983 let align = state.align_sym(static_ty);
984 let (alloc_id, base) =
985 state.allocate(Int::from_u64(state.z3_ctx, size), align, Some(static_ty));
986 state.path_state.static_allocs.insert(def_id, alloc_id);
987 state.content_mut(alloc_id).facts.initialized = true;
988 state.store_value(
989 alloc_id,
990 static_ty,
991 Vec::new(),
992 VmValue::new(scalar_term, static_ty),
993 );
994 (alloc_id, base)
995 };
996 VmValue {
997 z3_term: base,
998 ty: dest_ty,
999 provenance: Some(Provenance {
1000 alloc_id,
1001 offset: Int::from_u64(state.z3_ctx, 0),
1002 offset_kind: None,
1003 }),
1004 facts: ValueFacts {
1005 non_null: true,
1006 init: true,
1007 in_bounds: true,
1008 ..Default::default()
1009 },
1010 source: ValueSource::None,
1011 }
1012}
1013
1014pub(crate) fn eval_repeat<'z3, 'tcx>(
1015 state: &mut VmExecutor<'z3, 'tcx>,
1016 dest_place: &Place<'tcx>,
1017 operand: &Operand<'tcx>,
1018) -> VmValue<'z3, 'tcx> {
1019 let dest_ty = dest_place.ty(state.body(), state.tcx).ty;
1020 let TyKind::Array(..) = dest_ty.kind() else {
1021 state.path_state.diagnostics.mark_unsupported();
1022 return eval_fresh_value(state, dest_place, "repeat");
1023 };
1024 let value = state.value_of_operand(operand);
1025 if let Some((alloc_id, index, count)) = symbolic_repeat_target(state, dest_place) {
1026 let base = Int::mul(state.z3_ctx, &[&index, &Int::from_u64(state.z3_ctx, count)]);
1027 state.store_repeated_indexed_elements(alloc_id, base, count, value);
1028 return eval_fresh_value(state, dest_place, "repeat");
1029 }
1030 let mut field_path = Vec::new();
1031 for projection in dest_place.projection.iter() {
1032 let index = match projection {
1033 rustc_middle::mir::ProjectionElem::Field(index, _) => index.as_usize(),
1034 rustc_middle::mir::ProjectionElem::Index(local) => {
1035 let Some(index) = state
1036 .local_value(local)
1037 .and_then(|value| value.z3_term.simplify().as_u64())
1038 .and_then(|index| usize::try_from(index).ok())
1039 else {
1040 state.path_state.diagnostics.mark_unsupported();
1041 return eval_fresh_value(state, dest_place, "repeat");
1042 };
1043 index
1044 }
1045 rustc_middle::mir::ProjectionElem::ConstantIndex {
1046 offset,
1047 from_end: false,
1048 ..
1049 } => offset as usize,
1050 _ => {
1051 state.path_state.diagnostics.mark_unsupported();
1052 return eval_fresh_value(state, dest_place, "repeat");
1053 }
1054 };
1055 field_path.push(index);
1056 }
1057 if let Some(alloc_id) = state.path_state
1058 .current_frame
1059 .local_alloc
1060 .get(&dest_place.local)
1061 .copied()
1062 {
1063 let view_ty = state.body().local_decls[dest_place.local].ty;
1064 state.content_mut(alloc_id).values.retain(|(ty, path), _| {
1065 *ty != view_ty || path.len() <= field_path.len() || !path.starts_with(&field_path)
1066 });
1067 }
1068 field_path.push(REPEATED_ARRAY_ELEMENT);
1069 state.set_field_value(dest_place.local, field_path.clone(), value);
1070 if let Some(source) = operand.place()
1071 && source.projection.is_empty()
1072 {
1073 let nested = state
1074 .field_paths(source.local)
1075 .into_iter()
1076 .filter_map(|path| {
1077 state
1078 .field_value(source.local, &path)
1079 .cloned()
1080 .map(|value| (path, value))
1081 })
1082 .collect::<Vec<_>>();
1083 for (path, value) in nested {
1084 let mut full = field_path.clone();
1085 full.extend(path);
1086 state.set_field_value(dest_place.local, full, value);
1087 }
1088 }
1089 eval_fresh_value(state, dest_place, "repeat")
1090}
1091
1092pub(crate) fn symbolic_repeat_target<'z3, 'tcx>(
1093 state: &VmExecutor<'z3, 'tcx>,
1094 place: &Place<'tcx>,
1095) -> Option<(AllocId, Int<'z3>, u64)> {
1096 let TyKind::Array(element_ty, length) = place.ty(state.body(), state.tcx).ty.kind() else {
1097 return None;
1098 };
1099 if !matches!(
1100 element_ty.kind(),
1101 TyKind::Int(_) | TyKind::Uint(_) | TyKind::Bool | TyKind::Char
1102 ) || place.projection.len() != 1
1103 {
1104 return None;
1105 }
1106 let rustc_middle::mir::ProjectionElem::Index(index_local) = place.projection[0] else {
1107 return None;
1108 };
1109 let TyKind::Array(outer_element, _) = state.body().local_decls[place.local].ty.kind() else {
1110 return None;
1111 };
1112 if *outer_element != place.ty(state.body(), state.tcx).ty {
1113 return None;
1114 }
1115 let rustc_middle::ty::ConstKind::Value(length) = length.kind() else {
1116 return None;
1117 };
1118 let count = length.try_to_target_usize(state.tcx)?;
1119 if count > 64 {
1120 return None;
1121 }
1122 let index = state.local_value(index_local)?.z3_term.clone();
1123 if index.simplify().as_u64().is_some() {
1124 return None;
1125 }
1126 let alloc_id = *state.path_state.current_frame.local_alloc.get(&place.local)?;
1127 Some((alloc_id, index, count))
1128}
1129
1130pub(crate) fn eval_wrap_unsafe_binder<'z3, 'tcx>(
1131 state: &mut VmExecutor<'z3, 'tcx>,
1132 dest_place: &Place<'tcx>,
1133 operand: &Operand<'tcx>,
1134) -> VmValue<'z3, 'tcx> {
1135 let dest_ty = dest_place.ty(state.body(), state.tcx).ty;
1136 let mut value = state.value_of_operand(operand);
1137 value.ty = dest_ty;
1138 value
1139}
1140
1141
1142pub(crate) fn eval_reborrow<'z3, 'tcx>(
1143 state: &mut VmExecutor<'z3, 'tcx>,
1144 dest_place: &Place<'tcx>,
1145 source_place: &Place<'tcx>,
1146 mutability: rustc_middle::ty::Mutability,
1147) -> VmValue<'z3, 'tcx> {
1148 let dest_ty = dest_place.ty(state.body(), state.tcx).ty;
1149 let source_ty = source_place.ty(state.body(), state.tcx).ty;
1150 let same_adt = matches!(
1151 (source_ty.kind(), dest_ty.kind()),
1152 (rustc_middle::ty::TyKind::Adt(source, _), rustc_middle::ty::TyKind::Adt(dest, _))
1153 if source.did() == dest.did()
1154 );
1155 let compatible_shared = match (source_ty.kind(), dest_ty.kind()) {
1156 (TyKind::Adt(source, source_args), TyKind::Adt(dest, dest_args))
1157 if source.did() != dest.did()
1158 && mutability == rustc_middle::ty::Mutability::Not
1159 && !source.is_enum()
1160 && !dest.is_enum()
1161 && !source.is_union()
1162 && !dest.is_union() =>
1163 {
1164 let source_fields = &source.non_enum_variant().fields;
1165 let dest_fields = &dest.non_enum_variant().fields;
1166 source_fields.len() == dest_fields.len()
1167 && source_fields
1168 .iter()
1169 .zip(dest_fields.iter())
1170 .all(|(source_field, dest_field)| {
1171 if source_field.ident(state.tcx).name != dest_field.ident(state.tcx).name {
1172 return false;
1173 }
1174 let source_field_ty =
1175 mir_utils::field_ty(state.tcx, source_field, source_args);
1176 let dest_field_ty = mir_utils::field_ty(state.tcx, dest_field, dest_args);
1177 source_field_ty == dest_field_ty
1178 || matches!((source_field_ty.kind(), dest_field_ty.kind()),
1179 (TyKind::Ref(_, source_inner, rustc_middle::ty::Mutability::Mut),
1180 TyKind::Ref(_, dest_inner, rustc_middle::ty::Mutability::Not))
1181 if source_inner == dest_inner)
1182 })
1183 }
1184 _ => false,
1185 };
1186 #[cfg(rapx_has_phantom_deref)]
1187 let has_phantom_deref = source_place.projection.iter().any(|projection| {
1188 matches!(
1189 projection.kind(),
1190 rustc_middle::mir::ProjectionElem::PhantomDeref
1191 )
1192 });
1193 #[cfg(not(rapx_has_phantom_deref))]
1194 let has_phantom_deref = false;
1195 if (!same_adt && !compatible_shared) || has_phantom_deref {
1196 state.path_state.diagnostics.mark_unsupported();
1197 return eval_fresh_value(state, dest_place, "reborrow");
1198 }
1199 let mut value = state
1200 .value_of_place(source_place)
1201 .unwrap_or_else(|| eval_fresh_value(state, dest_place, "reborrow"));
1202 value.ty = dest_ty;
1203 value
1204}
1205
1206impl<'z3, 'tcx> VmExecutor<'z3, 'tcx> {
1207 fn provenance_for_binary_op(
1211 &self,
1212 op: BinOp,
1213 lhs: &VmValue<'z3, 'tcx>,
1214 rhs: &VmValue<'z3, 'tcx>,
1215 ) -> Option<Provenance<'z3>> {
1216 if rhs.is_pointer() && !matches!(op, BinOp::Div | BinOp::Rem) {
1217 return None;
1218 }
1219 match op {
1220 BinOp::Add | BinOp::AddWithOverflow | BinOp::AddUnchecked | BinOp::Offset => {
1221 lhs.provenance.as_ref().map(|prov| Provenance {
1222 alloc_id: prov.alloc_id,
1223 offset: Int::add(self.z3_ctx, &[&prov.offset, &rhs.z3_term]),
1224 offset_kind: None,
1225 })
1226 }
1227 BinOp::Sub | BinOp::SubWithOverflow | BinOp::SubUnchecked => {
1228 lhs.provenance.as_ref().map(|prov| Provenance {
1229 alloc_id: prov.alloc_id,
1230 offset: Int::sub(self.z3_ctx, &[&prov.offset, &rhs.z3_term]),
1231 offset_kind: None,
1232 })
1233 }
1234 BinOp::BitAnd => {
1235 lhs.provenance.as_ref().map(|prov| Provenance {
1236 alloc_id: prov.alloc_id,
1237 offset: self.fresh_int("align_offset"),
1241 offset_kind: None,
1242 })
1243 }
1244 BinOp::BitXor | BinOp::Shr | BinOp::ShrUnchecked => lhs.provenance.clone(),
1245 BinOp::BitOr | BinOp::Shl | BinOp::ShlUnchecked => {
1246 lhs.provenance.as_ref().map(|prov| Provenance {
1247 alloc_id: prov.alloc_id,
1248 offset: Int::add(self.z3_ctx, &[&prov.offset, &rhs.z3_term]),
1249 offset_kind: None,
1250 })
1251 }
1252 BinOp::Mul | BinOp::MulWithOverflow | BinOp::MulUnchecked => {
1253 lhs.provenance.as_ref().map(|prov| Provenance {
1254 alloc_id: prov.alloc_id,
1255 offset: Int::mul(self.z3_ctx, &[&prov.offset, &rhs.z3_term]),
1256 offset_kind: None,
1257 })
1258 }
1259 BinOp::Div | BinOp::Rem => lhs.provenance.clone(),
1260 _ => None,
1261 }
1262 }
1263
1264 fn facts_for_binary_op(
1267 &self,
1268 op: BinOp,
1269 lhs: &VmValue<'z3, 'tcx>,
1270 rhs: &VmValue<'z3, 'tcx>,
1271 provenance: &Option<Provenance<'z3>>,
1272 ) -> ValueFacts<'z3> {
1273 let non_null = !matches!(op, BinOp::BitAnd) && provenance.is_some() && lhs.facts.non_null;
1279
1280 let align_n = match op {
1281 BinOp::Add
1282 | BinOp::AddWithOverflow
1283 | BinOp::AddUnchecked
1284 | BinOp::Sub
1285 | BinOp::SubWithOverflow
1286 | BinOp::SubUnchecked
1287 | BinOp::Offset => {
1288 match (&lhs.facts.align_n, &rhs.facts.align_n) {
1289 (Some(a), Some(b)) if a == b => Some(a.clone()),
1290 (Some(a), None) => match a.simplify().as_u64() {
1291 Some(au) => {
1292 let c = rhs.z3_term.as_u64().unwrap_or(1);
1293 if c.is_multiple_of(au) {
1294 Some(a.clone())
1295 } else {
1296 None
1297 }
1298 }
1299 None => {
1302 if rhs.z3_term.as_u64() == Some(0) {
1303 Some(a.clone())
1304 } else {
1305 None
1306 }
1307 }
1308 },
1309 (Some(a), _) if self.rhs_is_aligned_multiple(rhs, a) => Some(a.clone()),
1310 _ => None,
1311 }
1312 }
1313 BinOp::Mul | BinOp::MulWithOverflow | BinOp::MulUnchecked => match rhs.z3_term.as_u64()
1314 {
1315 Some(c) => pow2_factor(c).map(|p| Int::from_u64(self.z3_ctx, p)),
1316 None => lhs
1317 .z3_term
1318 .as_u64()
1319 .and_then(pow2_factor)
1320 .map(|p| Int::from_u64(self.z3_ctx, p)),
1321 },
1322 _ => lhs.facts.align_n.clone(),
1323 };
1324
1325 ValueFacts {
1326 non_null,
1327 align_n,
1328 ..Default::default()
1329 }
1330 }
1331
1332 fn rhs_is_aligned_multiple(&self, val: &VmValue<'z3, 'tcx>, align: &Int<'z3>) -> bool {
1335 if let Some(au) = align.simplify().as_u64() {
1336 if let Some(a) = val
1337 .facts
1338 .align_n
1339 .as_ref()
1340 .and_then(|a| a.simplify().as_u64())
1341 && a >= au
1342 && a % au == 0
1343 {
1344 return true;
1345 }
1346 if let Some(c) = val.z3_term.as_u64()
1347 && c % au == 0
1348 {
1349 return true;
1350 }
1351 }
1352 false
1353 }
1354}
1355
1356fn pow2_factor(c: u64) -> Option<u64> {
1360 if c > 0 && c.is_power_of_two() {
1361 Some(c)
1362 } else if c > 0 {
1363 let factor = 1u64 << c.trailing_zeros();
1364 if factor > 1 { Some(factor) } else { None }
1365 } else {
1366 None
1367 }
1368}