rapx/analysis/dataflow/call_summary/
dependency_semantics.rs1use rustc_hir::def_id::DefId;
4use rustc_middle::{
5 mir::{Body, Operand},
6 ty::{Mutability, TyCtxt, TyKind},
7};
8
9use crate::analysis::dataflow::{
10 call::{CallDependencies, CallSemantics},
11 call_write::WriteSemantics,
12 pointer_copy, pointer_flow, pointer_offset,
13};
14use crate::compat::Spanned;
15use crate::def_ids::{self, intrinsic_api, other_api};
16use crate::util::mir_utils as helpers;
17
18use super::{CallEffect, effects, function_var};
19
20pub(crate) struct VerifierCallSemantics;
21
22impl CallSemantics for VerifierCallSemantics {
23 fn special_dependencies<'tcx>(
24 &self,
25 tcx: TyCtxt<'tcx>,
26 caller: DefId,
27 callee: DefId,
28 func: &Operand<'tcx>,
29 args: &[Spanned<Operand<'tcx>>],
30 body: &Body<'tcx>,
31 ) -> Option<CallDependencies> {
32 function_var::estimate(tcx, callee, args, body)
33 .or_else(|| intrinsic_dependency_summary(tcx, caller, callee, func, args.len()))
34 }
35
36 fn skip_mir_dependencies(&self, tcx: TyCtxt<'_>, callee: DefId) -> bool {
37 helpers::is_drop_in_place(tcx, callee)
38 }
39
40 fn preserve_all_return_args(&self, tcx: TyCtxt<'_>, callee: DefId) -> bool {
41 other_api::is_box_new(tcx, callee)
42 }
43}
44
45impl WriteSemantics for VerifierCallSemantics {
46 fn traces_base_pointer<'tcx>(
47 &self,
48 tcx: TyCtxt<'tcx>,
49 caller: DefId,
50 func: &Operand<'tcx>,
51 ) -> bool {
52 let callee = helpers::dep_callee_def_id(func);
53 pointer_flow::return_traces_first_arg(tcx, callee)
54 || callee.is_some_and(|id| pointer_offset::pointer_offset_direction(tcx, id).is_some())
55 || helpers::resolve_callee_def_id(tcx, caller, func).and_then(|resolved| {
56 pointer_flow::interior_mutable_borrow_source_arg(tcx, resolved)
57 }) == Some(0)
58 }
59
60 fn direct_write_arg(&self, tcx: TyCtxt<'_>, func: &Operand<'_>) -> Option<usize> {
61 let callee = helpers::dep_callee_def_id(func);
62 if def_ids::is_ptr_write(tcx, callee) {
63 Some(0)
64 } else if let Some(flow) = callee.and_then(|id| pointer_copy::copy_flow(tcx, id)) {
65 Some(flow.destination_arg)
66 } else {
67 None
68 }
69 }
70
71 fn special_write_args<'tcx>(
72 &self,
73 tcx: TyCtxt<'tcx>,
74 caller: DefId,
75 func: &Operand<'tcx>,
76 args: &[Spanned<Operand<'tcx>>],
77 body: &Body<'tcx>,
78 ) -> Option<Vec<usize>> {
79 let callee = helpers::dep_callee_def_id(func)?;
80 self.special_dependencies(tcx, caller, callee, func, args, body)
81 .map(|summary| summary.write_args)
82 }
83
84 fn skip_mir_write(&self, tcx: TyCtxt<'_>, callee: DefId) -> bool {
85 intrinsic_api::is_intrinsic(tcx, callee) || self.skip_mir_dependencies(tcx, callee)
86 }
87}
88
89fn intrinsic_dependency_summary<'tcx>(
91 tcx: TyCtxt<'tcx>,
92 caller: DefId,
93 callee: DefId,
94 func: &Operand<'tcx>,
95 arg_count: usize,
96) -> Option<CallDependencies> {
97 let is_ptr_copy = pointer_copy::direct_copy_flow(tcx, callee).is_some();
98 if !intrinsic_api::is_intrinsic(tcx, callee) && !is_ptr_copy {
99 return None;
100 }
101 let modeled = effects::lookup_effect(tcx, caller, Some(callee), func);
102 if is_ptr_copy && modeled.is_none() {
103 return Some(CallDependencies {
104 return_depends_on_args: (0..arg_count).collect(),
105 write_args: (arg_count > 1).then_some(1).into_iter().collect(),
106 write_inputs: (0..arg_count.min(3)).collect(),
107 });
108 }
109 let write_args: Vec<usize> = if let Some(ref effects) = modeled {
110 effects
111 .iter()
112 .flat_map(CallEffect::written_args)
113 .filter(|arg| *arg < arg_count)
114 .collect()
115 } else {
116 tcx.fn_sig(callee)
117 .skip_binder()
118 .inputs()
119 .skip_binder()
120 .iter()
121 .take(arg_count)
122 .enumerate()
123 .filter_map(|(index, ty)| {
124 matches!(
125 ty.kind(),
126 TyKind::RawPtr(_, Mutability::Mut) | TyKind::Ref(_, _, Mutability::Mut)
127 )
128 .then_some(index)
129 })
130 .collect()
131 };
132 let write_inputs = if let Some(ref effects) = modeled {
133 effects
134 .iter()
135 .flat_map(CallEffect::write_inputs)
136 .filter(|arg| *arg < arg_count)
137 .collect()
138 } else {
139 write_args.clone()
140 };
141 Some(CallDependencies {
142 return_depends_on_args: (0..arg_count).collect(),
143 write_args,
144 write_inputs,
145 })
146}