rapx/analysis/rcanary/ranalyzer/
ownership.rs

1use std::{collections::HashSet, fmt::Debug};
2use z3::ast;
3
4use rustc_middle::ty::Ty;
5
6use crate::analysis::core::heap_analysis::default::TyWithIndex;
7
8#[derive(Clone, Debug)]
9pub struct Taint<'tcx> {
10    set: HashSet<TyWithIndex<'tcx>>,
11}
12
13impl<'tcx> Default for Taint<'tcx> {
14    fn default() -> Self {
15        Self {
16            set: HashSet::default(),
17        }
18    }
19}
20
21impl<'tcx> Taint<'tcx> {
22    pub fn is_untainted(&self) -> bool {
23        self.set.is_empty()
24    }
25
26    pub fn is_tainted(&self) -> bool {
27        !self.set.is_empty()
28    }
29
30    pub fn contains(&self, k: &TyWithIndex<'tcx>) -> bool {
31        self.set.contains(k)
32    }
33
34    pub fn insert(&mut self, k: TyWithIndex<'tcx>) {
35        self.set.insert(k);
36    }
37
38    pub fn set(&self) -> &HashSet<TyWithIndex<'tcx>> {
39        &self.set
40    }
41
42    pub fn set_mut(&mut self) -> &mut HashSet<TyWithIndex<'tcx>> {
43        &mut self.set
44    }
45}
46
47#[derive(Clone, Debug, Eq, PartialEq, Hash)]
48pub enum IntraVar<'ctx> {
49    Declared,
50    Init(ast::BV<'ctx>),
51    Unsupported,
52}
53
54impl<'ctx> Default for IntraVar<'ctx> {
55    fn default() -> Self {
56        Self::Declared
57    }
58}
59
60impl<'ctx> IntraVar<'ctx> {
61    pub fn is_declared(&self) -> bool {
62        match *self {
63            IntraVar::Declared => true,
64            _ => false,
65        }
66    }
67
68    pub fn is_init(&self) -> bool {
69        match *self {
70            IntraVar::Init(_) => true,
71            _ => false,
72        }
73    }
74
75    pub fn is_unsupported(&self) -> bool {
76        match *self {
77            IntraVar::Unsupported => true,
78            _ => false,
79        }
80    }
81
82    pub fn extract(&self) -> ast::BV<'ctx> {
83        match self {
84            IntraVar::Init(ref ast) => ast.clone(),
85            _ => unreachable!(),
86        }
87    }
88}
89
90#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
91pub enum ContextTypeOwner<'tcx> {
92    Owned { kind: OwnerKind, ty: Ty<'tcx> },
93    Unowned,
94}
95
96#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
97pub enum OwnerKind {
98    Instance,
99    Reference,
100    Pointer,
101}
102
103impl<'tcx> Default for ContextTypeOwner<'tcx> {
104    fn default() -> Self {
105        Self::Unowned
106    }
107}
108
109impl<'tcx> ContextTypeOwner<'tcx> {
110    pub fn is_owned(&self) -> bool {
111        match self {
112            ContextTypeOwner::Owned { .. } => true,
113            ContextTypeOwner::Unowned => false,
114        }
115    }
116
117    pub fn get_ty(&self) -> Option<Ty<'tcx>> {
118        match *self {
119            ContextTypeOwner::Owned { ty, .. } => Some(ty),
120            ContextTypeOwner::Unowned => None,
121        }
122    }
123}