flowistry/infoflow/
recursive.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
use log::{debug, info};
use rustc_middle::{
  mir::*,
  ty::{ClosureKind, GenericArgKind, TyKind},
};
use rustc_mir_dataflow::JoinSemiLattice;
use rustc_utils::{mir::borrowck_facts::get_body_with_borrowck_facts, PlaceExt};

use super::{analysis::FlowAnalysis, BODY_STACK};
use crate::{
  extensions::REACHED_LIBRARY,
  infoflow::{
    mutation::{Mutation, MutationStatus, Reason},
    FlowDomain,
  },
  mir::utils,
};

impl<'tcx> FlowAnalysis<'tcx> {
  pub(crate) fn recurse_into_call(
    &self,
    state: &mut FlowDomain<'tcx>,
    call: &TerminatorKind<'tcx>,
    location: Location,
  ) -> bool {
    let tcx = self.tcx;
    let (func, parent_args, destination) = match call {
      TerminatorKind::Call {
        func,
        args,
        destination,
        ..
      } => (func, args, destination),
      _ => unreachable!(),
    };
    debug!("Checking whether can recurse into {func:?}");

    let func = match func.constant() {
      Some(func) => func,
      None => {
        debug!("  Func is not constant");
        return false;
      }
    };

    let def_id = match func.const_.ty().kind() {
      TyKind::FnDef(def_id, _) => def_id,
      _ => {
        debug!("  Func is not a FnDef");
        return false;
      }
    };

    // If a function returns never (fn () -> !) then there are no exit points,
    // so we can't analyze effects on exit
    let fn_sig = tcx.fn_sig(*def_id);
    if fn_sig.skip_binder().output().skip_binder().is_never() {
      debug!("  Func returns never");
      return false;
    }

    let node = match tcx.hir().get_if_local(*def_id) {
      Some(node) => node,
      None => {
        debug!("  Func is not in local crate");
        REACHED_LIBRARY.get(|reached_library| {
          if let Some(reached_library) = reached_library {
            *reached_library.borrow_mut() = true;
          }
        });
        return false;
      }
    };

    let body_id = match node.body_id() {
      Some(body_id) => body_id,
      None => {
        debug!("  Func does not have a BodyId");
        return false;
      }
    };

    // TODO(wcrichto, 2024-12-02): mir_unsafety_check_result got removed, need to find a replacement
    // let unsafety = tcx.mir_unsafety_check_result(def_id.expect_local());
    // if !unsafety.used_unsafe_blocks.is_empty() {
    //   debug!("  Func contains unsafe blocks");
    //   return false;
    // }

    let parent_arg_places = utils::arg_places(parent_args);
    let any_closure_inputs = parent_arg_places.iter().any(|(_, place)| {
      let ty = place.ty(self.body.local_decls(), tcx).ty;
      ty.walk().any(|arg| match arg.unpack() {
        GenericArgKind::Type(ty) => match ty.kind() {
          TyKind::Closure(_, substs) => matches!(
            substs.as_closure().kind(),
            ClosureKind::FnOnce | ClosureKind::FnMut
          ),
          _ => false,
        },
        _ => false,
      })
    });
    if any_closure_inputs {
      debug!("  Func has closure inputs");
      return false;
    }

    let recursive = BODY_STACK.with(|body_stack| {
      let body_stack = body_stack.borrow();
      body_stack.iter().any(|visited_id| *visited_id == body_id)
    });
    if recursive {
      debug!("  Func is a recursive call");
      return false;
    }

    let body_with_facts = get_body_with_borrowck_facts(tcx, def_id.expect_local());
    let mut recurse_cache = self.recurse_cache.borrow_mut();
    let flow = recurse_cache.entry(body_id).or_insert_with(|| {
      info!("Recursing into {}", tcx.def_path_debug_str(*def_id));
      super::compute_flow(tcx, body_id, body_with_facts)
    });
    let body = &body_with_facts.body;

    let mut return_state = FlowDomain::new(flow.analysis.location_domain());
    {
      let return_locs = body
        .basic_blocks
        .iter_enumerated()
        .filter_map(|(bb, data)| match data.terminator().kind {
          TerminatorKind::Return => Some(body.terminator_loc(bb)),
          _ => None,
        });

      for loc in return_locs {
        return_state.join(flow.state_at(loc));
      }
    };

    let translate_child_to_parent = |child: Place<'tcx>,
                                     mutated: bool|
     -> Option<Place<'tcx>> {
      let child_ty = child.ty(body.local_decls(), tcx).ty;
      if child_ty.is_unit() {
        return None;
      }

      let can_translate = child.local == RETURN_PLACE
        || (child.is_arg(body) && (!mutated || child.is_indirect()));
      if !can_translate {
        return None;
      }

      // For example, say we're calling f(_5.0) and child = (*_1).1 where
      // .1 is private to parent. Then:
      //    parent_toplevel_arg = _5.0
      //    child.projection = (*□).1
      //    parent_arg_projected = (*_5.0)

      let parent_toplevel_arg = if child.local == RETURN_PLACE {
        *destination
      } else {
        parent_arg_places
          .iter()
          .find(|(j, _)| child.local.as_usize() - 1 == *j)
          .map(|(_, place)| *place)?
      };

      let mut projection = parent_toplevel_arg.projection.to_vec();
      let mut ty = parent_toplevel_arg.ty(self.body.local_decls(), tcx);
      log::debug!("Adding child {child:?} to parent {parent_toplevel_arg:?}");
      for elem in child.projection.iter() {
        // Don't continue if we reach a private field
        if let ProjectionElem::Field(field, _) = elem {
          if let Some(adt_def) = ty.ty.ty_adt_def() {
            let field = adt_def.all_fields().nth(field.as_usize()).unwrap();
            if !field.vis.is_accessible_from(self.def_id, self.tcx) {
              break;
            }
          }
        }

        ty = ty.projection_ty_core(
          tcx,
          &elem,
          |_, field, _| ty.field_ty(tcx, field),
          |_, ty| ty,
        );
        let elem = match elem {
          ProjectionElem::Field(field, _) => ProjectionElem::Field(field, ty.ty),
          elem => elem,
        };
        projection.push(elem);
      }

      let parent_arg_projected = Place::make(parent_toplevel_arg.local, &projection, tcx);
      Some(parent_arg_projected)
    };

    let mutations = return_state.rows().filter_map(|(child, _)| {
      let parent = translate_child_to_parent(*child, true)?;

      let was_return: bool = child.local == RETURN_PLACE;
      // > 1 because arguments will always have their synthetic location in their dep set
      let was_mutated = return_state.row_set(child).len() > 1;
      if !was_mutated && !was_return {
        return None;
      }

      let child_deps = return_state.row_set(child);
      let parent_deps = return_state
        .rows()
        .filter(|(_, deps)| child_deps.is_superset(deps))
        .filter_map(|(row, _)| translate_child_to_parent(*row, false))
        .collect::<Vec<_>>();

      debug!(
        "child {child:?} \n  / child_deps {child_deps:?}\n-->\nparent {parent:?}\n   / parent_deps {parent_deps:?}"
      );

      Some(Mutation {
        mutated: parent,
        inputs: parent_deps.clone(),
        reason: if was_return {
          Reason::AssignTarget
        } else {
          Reason::Argument(parent.local.as_u32() as u8 - 1)
        },
        status: if was_return {
          MutationStatus::Definitely
        } else {
          MutationStatus::Possibly
        },
      })
    }).collect::<Vec<_>>();

    self.transfer_function(state, mutations, location);

    true
  }
}