rapx/analysis/safedrop/
mod.rs

1pub mod alias;
2pub mod bug_records;
3pub mod check_bugs;
4pub mod corner_handle;
5pub mod graph;
6#[allow(clippy::module_inception)]
7pub mod safedrop;
8
9use rustc_hir::def_id::DefId;
10use rustc_middle::ty::TyCtxt;
11
12use crate::analysis::core::{
13    alias_analysis::default::{AliasAnalyzer, MopAAResultMap},
14    ownedheap_analysis::{default::OwnedHeapAnalyzer, OHAResultMap, OwnedHeapAnalysis},
15};
16use graph::SafeDropGraph;
17use safedrop::*;
18
19use super::Analysis;
20
21pub struct SafeDrop<'tcx> {
22    pub tcx: TyCtxt<'tcx>,
23}
24
25impl<'tcx> SafeDrop<'tcx> {
26    pub fn new(tcx: TyCtxt<'tcx>) -> Self {
27        Self { tcx }
28    }
29    pub fn start(&self) {
30        let mut mop = AliasAnalyzer::new(self.tcx);
31        mop.run();
32        let fn_map = mop.get_all_fn_alias_raw();
33
34        let mut heap = OwnedHeapAnalyzer::new(self.tcx);
35        heap.run();
36        let adt_owner = heap.get_all_items();
37
38        let mir_keys = self.tcx.mir_keys(());
39        for local_def_id in mir_keys {
40            query_safedrop(
41                self.tcx,
42                &fn_map,
43                local_def_id.to_def_id(),
44                adt_owner.clone(),
45            );
46        }
47    }
48}
49
50pub fn query_safedrop(
51    tcx: TyCtxt,
52    fn_map: &MopAAResultMap,
53    def_id: DefId,
54    adt_owner: OHAResultMap,
55) {
56    /* filter const mir */
57    if let Some(_other) = tcx.hir_body_const_context(def_id.expect_local()) {
58        return;
59    }
60    if tcx.is_mir_available(def_id) {
61        let body = tcx.optimized_mir(def_id);
62        let mut safedrop_graph = SafeDropGraph::new(body, tcx, def_id, adt_owner);
63        safedrop_graph.solve_scc();
64        safedrop_graph.check(0, tcx, fn_map);
65        if safedrop_graph.visit_times <= VISIT_LIMIT {
66            safedrop_graph.report_bugs();
67        } else {
68            println!("Over visited: {:?}", def_id);
69        }
70    }
71}