rapx/analysis/rcanary/ranalyzer/
ownership.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use std::collections::HashSet;
use std::fmt::Debug;
use z3::ast;

use rustc_middle::ty::Ty;

use crate::analysis::core::heap_item::type_visitor::TyWithIndex;

#[derive(Clone, Debug)]
pub struct Taint<'tcx> {
    set: HashSet<TyWithIndex<'tcx>>,
}

impl<'tcx> Default for Taint<'tcx> {
    fn default() -> Self {
        Self {
            set: HashSet::default(),
        }
    }
}

impl<'tcx> Taint<'tcx> {
    pub fn is_untainted(&self) -> bool {
        self.set.is_empty()
    }

    pub fn is_tainted(&self) -> bool {
        !self.set.is_empty()
    }

    pub fn contains(&self, k: &TyWithIndex<'tcx>) -> bool {
        self.set.contains(k)
    }

    pub fn insert(&mut self, k: TyWithIndex<'tcx>) {
        self.set.insert(k);
    }

    pub fn set(&self) -> &HashSet<TyWithIndex<'tcx>> {
        &self.set
    }

    pub fn set_mut(&mut self) -> &mut HashSet<TyWithIndex<'tcx>> {
        &mut self.set
    }
}

#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub enum IntraVar<'ctx> {
    Declared,
    Init(ast::BV<'ctx>),
    Unsupported,
}

impl<'ctx> Default for IntraVar<'ctx> {
    fn default() -> Self {
        Self::Declared
    }
}

impl<'ctx> IntraVar<'ctx> {
    pub fn is_declared(&self) -> bool {
        match *self {
            IntraVar::Declared => true,
            _ => false,
        }
    }

    pub fn is_init(&self) -> bool {
        match *self {
            IntraVar::Init(_) => true,
            _ => false,
        }
    }

    pub fn is_unsupported(&self) -> bool {
        match *self {
            IntraVar::Unsupported => true,
            _ => false,
        }
    }

    pub fn extract(&self) -> ast::BV<'ctx> {
        match self {
            IntraVar::Init(ref ast) => ast.clone(),
            _ => unreachable!(),
        }
    }
}

#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub enum ContextTypeOwner<'tcx> {
    Owned { kind: OwnerKind, ty: Ty<'tcx> },
    Unowned,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub enum OwnerKind {
    Instance,
    Reference,
    Pointer,
}

impl<'tcx> Default for ContextTypeOwner<'tcx> {
    fn default() -> Self {
        Self::Unowned
    }
}

impl<'tcx> ContextTypeOwner<'tcx> {
    pub fn is_owned(&self) -> bool {
        match self {
            ContextTypeOwner::Owned { .. } => true,
            ContextTypeOwner::Unowned => false,
        }
    }

    pub fn get_ty(&self) -> Option<Ty<'tcx>> {
        match *self {
            ContextTypeOwner::Owned { ty, .. } => Some(ty),
            ContextTypeOwner::Unowned => None,
        }
    }
}