paralegal_flow/ann/db/
reachable.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
use crate::{
    ann::side_effect_detection,
    utils::{func_of_term, type_for_constructor},
    HashSet,
};
use flowistry::mir::FlowistryInput;
use flowistry_pdg_construction::{
    determine_async,
    utils::{handle_shims, try_monomorphize, try_resolve_function, ShimResult},
};
use paralegal_spdg::Identifier;

use rustc_data_structures::fx::FxHashSet;
use rustc_middle::{
    mir,
    ty::{self, TypingEnv},
};

use std::borrow::Cow;

use super::{MarkerCtx, MaybeMonomorphized};

impl<'tcx> MarkerCtx<'tcx> {
    /// Queries the transitive marker cache.
    pub fn has_transitive_reachable_markers(
        &self,
        res: impl Into<MaybeMonomorphized<'tcx>>,
    ) -> bool {
        !self.get_reachable_markers(res).is_empty()
    }

    // XXX: This code duplicates the auto-marker assignment logic from
    // GraphAssembler::handle_node_annotations_for_regular_location, but really
    // there should be only one source of truth.
    pub fn get_reachable_markers(&self, res: impl Into<MaybeMonomorphized<'tcx>>) -> &[Identifier] {
        let res = res.into();
        let def_id = res.def_id();
        let mark_side_effects = self.db().config.marker_control().mark_side_effects();
        if self.is_marked(def_id) {
            trace!("  Is marked");
            return &[];
        }
        if let Some(marker) = self.marker_if_unloadable(def_id) {
            trace!("  Is unloadable");
            return if mark_side_effects {
                std::slice::from_ref(marker)
            } else {
                &[]
            };
        }
        if !self.crate_is_included(def_id.krate) {
            trace!("  Is excluded");
            return &[];
        }
        self.db()
            .reachable_markers
            .get_maybe_recursive(&res, |_| self.compute_reachable_markers(res))
            .map_or(&[], Box::as_ref)
    }

    fn get_reachable_and_self_markers<'a, M: Into<MaybeMonomorphized<'tcx>>>(
        &'a self,
        res: M,
    ) -> impl Iterator<Item = Identifier> + use<'a, 'tcx, M> {
        let res = res.into();
        let mut direct_markers = self.all_markers_associated_with(res.def_id()).peekable();
        let is_self_marked = direct_markers.peek().is_some();
        if is_self_marked {
            let mut stat = self.borrow_function_marker_stat(res);
            if stat.markers_on_self.is_empty() {
                stat.markers_on_self
                    .extend(self.all_markers_associated_with(res.def_id()));
            }
        }
        let non_direct = (!is_self_marked).then(|| self.get_reachable_markers(res));

        direct_markers.chain(non_direct.into_iter().flatten().copied())
    }

    /// If the transitive marker cache did not contain the answer, this is what
    /// computes it.
    fn compute_reachable_markers(&self, res: MaybeMonomorphized<'tcx>) -> Box<[Identifier]> {
        trace!("Computing reachable markers for {res:?}");

        if self.tcx().is_constructor(res.def_id()) {
            let parent = type_for_constructor(self.tcx(), res.def_id());
            return self.all_markers_associated_with(parent).collect::<Box<_>>();
        }
        let body = self.db().body_cache.get(res.def_id());

        if self.db().config.dbg().dump_mir() {
            use rustc_utils::BodyExt;
            use std::io::Write;
            let path = self.tcx().def_path_str(res.def_id()) + ".mir";
            let mut f = std::fs::File::create(path.as_str()).unwrap();
            write!(f, "{}", body.body().to_string(self.tcx()).unwrap()).unwrap();
        }

        let param_env = TypingEnv::post_analysis(self.tcx(), res.def_id())
            .with_post_analysis_normalized(self.tcx());
        let mono_body = match res {
            MaybeMonomorphized::Monomorphized(res) => Cow::Owned(
                try_monomorphize(
                    res,
                    self.tcx(),
                    param_env,
                    body.body(),
                    self.tcx().def_span(res.def_id()),
                )
                .unwrap(),
            ),
            MaybeMonomorphized::Plain(_) => Cow::Borrowed(body.body()),
        };
        if let Some((async_fn, _, _)) = determine_async(self.tcx(), res.def_id(), &mono_body) {
            self.borrow_function_marker_stat(res).is_async = Some(async_fn);
            return self.get_reachable_markers(async_fn).into();
        }
        let mut vis = BodyAnalyzer::new(self.clone(), res, &mono_body, param_env);
        use mir::visit::Visitor;
        vis.visit_body(&mono_body);
        let found = vis.found_markers;
        found.into_iter().collect()
    }

    // XXX: This code duplicates the auto-marker assignment logic from
    // GraphAssembler::handle_node_annotations_for_regular_location, but really
    // there should be only one source of truth.
    /// Does this terminator carry a marker?
    fn terminator_reachable_markers(
        &self,
        parent: MaybeMonomorphized<'tcx>,
        local_decls: &mir::LocalDecls,
        terminator: &mir::Terminator<'tcx>,
        param_env: ty::TypingEnv<'tcx>,
        expect_resolve: bool,
    ) -> impl Iterator<Item = Identifier> + '_ {
        let mut v = vec![];
        if !matches!(terminator.kind, mir::TerminatorKind::Call { .. }) {
            return v.into_iter();
        }
        trace!(
            "  Finding reachable markers for terminator {:?}",
            terminator.kind
        );
        let Some((def_id, gargs)) = func_of_term(self.tcx(), terminator) else {
            if self.0.config.marker_control().mark_side_effects() {
                v.push(self.db().auto_markers.side_effect_unknown_fn_ptr);
            }
            return v.into_iter();
        };
        let mut res = if expect_resolve {
            let Some(instance) = try_resolve_function(self.tcx(), def_id, param_env, gargs) else {
                self.span_err(
                    terminator.source_info.span,
                    format!("cannot determine reachable markers, failed to resolve {def_id:?} with {gargs:?}")
                );
                return v.into_iter();
            };
            let new_instance = match handle_shims(
                instance,
                self.tcx(),
                param_env,
                terminator.source_info.span,
            ) {
                ShimResult::IsHandledShim { instance, .. } => instance,
                ShimResult::IsNonHandleableShim => {
                    self.span_err(
                        terminator.source_info.span,
                        format!("cannot determine reachable markers, failed to handle shim {def_id:?} with {gargs:?}")
                    );
                    return v.into_iter();
                }
                ShimResult::IsNotShim => instance,
            };
            if side_effect_detection::is_allowed_as_clone_unit_instance(self.tcx(), new_instance) {
                return v.into_iter();
            }
            MaybeMonomorphized::Monomorphized(new_instance)
        } else {
            MaybeMonomorphized::Plain(def_id)
        };
        trace!(
            "    Checking function {} for markers",
            self.tcx().def_path_debug_str(res.def_id())
        );

        if let Some(model) = self.has_stub(res.def_id()) {
            if let MaybeMonomorphized::Monomorphized(instance) = &mut res {
                if let Ok(new_instance) = model.resolve_alternate_instance(
                    self.tcx(),
                    *instance,
                    param_env,
                    terminator.source_info.span,
                ) {
                    self.borrow_function_marker_stat(res).is_stubbed = Some(new_instance);
                    v.extend(self.get_reachable_and_self_markers(new_instance));
                }
            } else {
                self.span_err(
                    terminator.source_info.span,
                    "Could not apply stub to an partially resolved function",
                );
            };
            return v.into_iter();
        }

        v.extend(self.get_reachable_and_self_markers(res));

        // We have to proceed differently than graph construction,
        // because we are not given the closure function, instead
        // we are provided the id of the function that creates the
        // future. As such we can't just monomorphize and traverse,
        // we have to find the generator first.
        if let ty::TyKind::Alias(ty::AliasTyKind::Opaque, alias) =
            local_decls[mir::RETURN_PLACE].ty.kind()
            && let ty::TyKind::Coroutine(closure_fn, substs) =
                self.tcx().type_of(alias.def_id).skip_binder().kind()
        {
            trace!("    fits opaque type");
            let async_closure_fn =
                try_resolve_function(self.tcx(), *closure_fn, param_env, substs).unwrap();
            v.extend(self.get_reachable_and_self_markers(async_closure_fn));
            self.borrow_function_marker_stat(res).is_async = Some(async_closure_fn);
        };
        if !v.is_empty() {
            self.borrow_function_marker_stat(parent)
                .calls_with_reachable_markers
                .push((res, terminator.source_info.span));
        }
        v.into_iter()
    }
}

struct BodyAnalyzer<'tcx, 'b> {
    ctx: MarkerCtx<'tcx>,
    res: MaybeMonomorphized<'tcx>,
    mono_body: &'b mir::Body<'tcx>,
    param_env: ty::TypingEnv<'tcx>,
    found_markers: FxHashSet<Identifier>,
}

impl<'tcx, 'b> BodyAnalyzer<'tcx, 'b> {
    fn expect_resolve(&self) -> bool {
        self.res.is_monomorphized()
    }

    fn new(
        ctx: MarkerCtx<'tcx>,
        res: MaybeMonomorphized<'tcx>,
        mono_body: &'b mir::Body<'tcx>,
        param_env: ty::TypingEnv<'tcx>,
    ) -> Self {
        Self {
            ctx,
            res,
            mono_body,
            param_env,
            found_markers: HashSet::default(),
        }
    }
}

impl<'tcx> mir::visit::Visitor<'tcx> for BodyAnalyzer<'tcx, '_> {
    fn visit_local_decl(&mut self, l: mir::Local, v: &mir::LocalDecl<'tcx>) {
        let markers = self.ctx.deep_type_markers(v.ty);
        if !markers.is_empty() {
            self.ctx
                .borrow_function_marker_stat(self.res)
                .markers_from_variables
                .push((l, v.ty, markers.iter().map(|v| v.1).collect()));
        }
        self.found_markers.extend(markers.iter().map(|v| v.1));
        self.super_local_decl(l, v);
    }

    fn visit_terminator(&mut self, terminator: &mir::Terminator<'tcx>, location: mir::Location) {
        let expect_resolve = self.expect_resolve();
        let markers = self.ctx.terminator_reachable_markers(
            self.res,
            &self.mono_body.local_decls,
            terminator,
            self.param_env,
            expect_resolve,
        );
        self.found_markers.extend(markers);
        self.super_terminator(terminator, location);
    }
}