rapx/analysis/rcanary/ranalyzer/
ownership.rs

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