Skip to main content

rapx/check/rcanary/
mod.rs

1pub mod ranalyzer;
2
3use crate::analysis::{
4    Analysis,
5    heap_ownership::{
6        HeapOwnershipAnalysis, HeapOwnershipResultMap, default::HeapOwnershipAnalyzer,
7    },
8};
9use ranalyzer::{FlowAnalysis, IcxSliceFroBlock, IntraFlowContext, MirGraph};
10use rustc_middle::ty::TyCtxt;
11use std::collections::HashMap;
12
13#[allow(non_camel_case_types)]
14#[derive(Clone)]
15pub struct rCanary<'tcx> {
16    tcx: TyCtxt<'tcx>,
17    adt_owner: HeapOwnershipResultMap,
18    mir_graph: MirGraph,
19}
20
21impl<'tcx> rCanary<'tcx> {
22    pub fn new(tcx: TyCtxt<'tcx>, adt_owner: HeapOwnershipResultMap) -> Self {
23        Self {
24            tcx,
25            adt_owner,
26            mir_graph: HashMap::default(),
27        }
28    }
29
30    pub fn start(&mut self) {
31        let mut heap = HeapOwnershipAnalyzer::new(self.tcx);
32        heap.run();
33        let adt_owner = heap.get_all_items();
34        let rcx_boxed = Box::new(rCanary::new(self.tcx, adt_owner));
35        // Leak to create a 'static reference needed for FlowAnalysis lifetime.
36        let rcx = Box::leak(rcx_boxed);
37        FlowAnalysis::new(rcx).start();
38    }
39
40    pub fn tcx(&self) -> TyCtxt<'tcx> {
41        self.tcx
42    }
43
44    pub fn adt_owner(&self) -> &HeapOwnershipResultMap {
45        &self.adt_owner
46    }
47
48    pub fn mir_graph(&self) -> &MirGraph {
49        &self.mir_graph
50    }
51
52    pub fn mir_graph_mut(&mut self) -> &mut MirGraph {
53        &mut self.mir_graph
54    }
55}
56
57pub trait Tcx<'tcx, 'o, 'a> {
58    fn tcx(&'o self) -> TyCtxt<'tcx>;
59}
60
61pub trait Rcx<'tcx, 'o, 'a> {
62    fn rcx(&'o self) -> &'a rCanary<'tcx>;
63
64    fn tcx(&'o self) -> TyCtxt<'tcx>;
65}
66
67pub trait RcxMut<'tcx, 'o, 'a> {
68    fn rcx(&'o self) -> &'o rCanary<'tcx>;
69
70    fn rcx_mut(&'o mut self) -> &'o mut rCanary<'tcx>;
71
72    fn tcx(&'o self) -> TyCtxt<'tcx>;
73}
74
75pub trait IcxMut<'tcx, 'z3, 'o> {
76    fn icx(&'o self) -> &'o IntraFlowContext<'tcx, 'z3>;
77
78    fn icx_mut(&'o mut self) -> &'o mut IntraFlowContext<'tcx, 'z3>;
79}
80
81pub trait IcxSliceMut<'tcx, 'z3, 'o> {
82    fn icx_slice(&'o self) -> &'o IcxSliceFroBlock<'tcx, 'z3>;
83
84    fn icx_slice_mut(&'o mut self) -> &'o mut IcxSliceFroBlock<'tcx, 'z3>;
85}