Skip to main content

rapx/check/rcanary/
mod.rs

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