rapx/vm/
call_contracts.rs1use rustc_middle::mir::Local;
2
3use super::state::{VmExecutor, VmValue};
4use crate::contract::{ContractKind, PropertyKind};
5use crate::contract::place::{bind_call_property, index_access_locals, rebind_property_place, slice_offset_locals};
6
7impl<'z3, 'tcx> VmExecutor<'z3, 'tcx> {
8 pub(crate) fn assert_call_result_invariants(&mut self, destination: Local) {
9 let Some(result) = self.local_value(destination) else {
10 return;
11 };
12 if self.path_state.inline.inline_truncated_terms.contains(&result.z3_term) {
13 return;
14 }
15 let ty = result.ty;
16 let rustc_middle::ty::TyKind::Adt(adt_def, _) = ty.kind() else {
17 return;
18 };
19 let invariants = crate::contract::annotation::get_struct_invariants_from_annotation(
20 self.tcx,
21 adt_def.did(),
22 adt_def.did(),
23 );
24 for invariant in invariants {
25 let mut bound = invariant;
26 rebind_property_place(&mut bound, destination);
27 self.assert_contract_fact(&bound);
28 }
29 }
30
31 pub(crate) fn assert_summary_result_type_invariants(
32 &mut self,
33 callee: rustc_hir::def_id::DefId,
34 destination: Local,
35 ) {
36 let Some(result) = self.local_value(destination) else {
37 return;
38 };
39 if self.path_state.inline.inline_truncated_terms.contains(&result.z3_term) {
40 return;
41 }
42 let ty = result.ty;
43 let invariants =
44 crate::contract::type_invariants::build_type_invariants_for_ty(self.tcx, callee, ty);
45 for mut invariant in invariants {
46 rebind_property_place(&mut invariant, destination);
47 self.assert_contract_fact(&invariant);
48 }
49 }
50
51 pub(crate) fn assert_call_postconditions(
52 &mut self,
53 callee: rustc_hir::def_id::DefId,
54 args: &[Option<Local>],
55 destination: Local,
56 ) {
57 let ensures = crate::contract::ensures::get_call_ensures(self.tcx, callee);
58 if ensures.is_empty() {
59 return;
60 }
61 if self.tcx.is_foreign_item(callee) && self.local_value(destination).is_none() {
62 let ty = self.body().local_decls[destination].ty;
63 let term = self.fresh_int("ffi_return");
64 self.set_local(destination, VmValue::new(term, ty));
65 }
66 let bound_index_requires: Vec<(Local, Local)> = if ensures
67 .iter()
68 .any(|property| property.kind() == Some(PropertyKind::SliceOffset))
69 {
70 let mut requires = crate::contract::annotation::get_trait_method_requires(self.tcx, callee);
71 if requires.is_empty() {
72 requires = crate::contract::json::query_json_contracts(self.tcx, callee);
73 }
74 requires
75 .into_iter()
76 .filter_map(|mut requirement| {
77 bind_call_property(&mut requirement, args, destination)
78 .then(|| index_access_locals(&requirement))
79 .flatten()
80 })
81 .collect()
82 } else {
83 Vec::new()
84 };
85 for mut property in ensures {
86 if bind_call_property(&mut property, args, destination) {
87 let bounded_result =
88 slice_offset_locals(&property).is_some_and(|(_, base, index)| {
89 bound_index_requires.contains(&(base, index))
90 && self
91 .local_value(base)
92 .zip(self.local_value(index))
93 .is_some_and(|(base_value, index_value)| {
94 self.slice_index_start(index_value, index).is_some()
95 || self.slice_len_from_value(base_value).is_some()
96 })
97 });
98 property.set_contract_kind(ContractKind::Precond);
99 self.assert_contract_fact(&property);
100 if bounded_result {
101 self.record_bounded_result_span(destination);
102 }
103 } else {
104 self.record_unsupported(&format!(
105 "postcondition argument binding for {}",
106 self.tcx.def_path_str(callee)
107 ));
108 }
109 }
110 }
111}