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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use crate::{
    abstract_domain::{AbstractDomain, DomainInsertion, HasTop},
    analysis::string_abstraction::state::State,
    intermediate_representation::{Blk, Def, Expression, Jmp, Term},
};

use super::Context;

impl<'a, T: AbstractDomain + DomainInsertion + HasTop + Eq + From<String>>
    crate::analysis::forward_interprocedural_fixpoint::Context<'a> for Context<'a, T>
{
    type Value = State<T>;

    /// Get the underlying graph on which the analysis operates.
    fn get_graph(&self) -> &crate::analysis::graph::Graph<'a> {
        self.pointer_inference_results.get_graph()
    }

    /// Merge two state values.
    fn merge(&self, state1: &Self::Value, state2: &Self::Value) -> State<T> {
        state1.merge(state2)
    }

    fn update_def(&self, state: &State<T>, def: &Term<Def>) -> Option<State<T>> {
        let mut new_state = state.clone();
        if state.get_pointer_inference_state().is_none() {
            if self.block_first_def_set.contains(&(
                def.tid.clone(),
                state.get_current_sub().unwrap().tid.clone(),
            )) {
                if let Some(pi_state) = self.get_current_pointer_inference_state(state, &def.tid) {
                    new_state.set_pointer_inference_state(Some(pi_state));
                } else {
                    return None;
                }
            } else {
                return None;
            }
        }
        self.update_pointer_inference_state(&mut new_state, def);
        match &def.term {
            Def::Assign {
                var: output,
                value: input,
            } => {
                new_state.handle_assign_and_load(
                    output,
                    input,
                    &self.project.runtime_memory_image,
                    &self.block_first_def_set,
                    true,
                );
            }
            Def::Load {
                var: output,
                address: input,
            } => {
                new_state.handle_assign_and_load(
                    output,
                    input,
                    &self.project.runtime_memory_image,
                    &self.block_first_def_set,
                    false,
                );
            }
            Def::Store { address, value } => new_state.handle_store(
                address,
                value,
                &self.project.runtime_memory_image,
                &self.block_first_def_set,
            ),
        }

        Some(new_state)
    }

    fn update_jump(
        &self,
        state: &State<T>,
        _jump: &Term<Jmp>,
        _untaken_conditional: Option<&Term<Jmp>>,
        _target: &Term<Blk>,
    ) -> Option<State<T>> {
        let mut new_state = state.clone();
        new_state.set_pointer_inference_state(None);
        Some(new_state)
    }

    fn update_call(
        &self,
        _state: &State<T>,
        _call: &Term<Jmp>,
        _target: &crate::analysis::graph::Node,
        _calling_convention: &Option<String>,
    ) -> Option<State<T>> {
        None
    }

    fn update_return(
        &self,
        _state: Option<&State<T>>,
        state_before_call: Option<&State<T>>,
        _call_term: &Term<Jmp>,
        _return_term: &Term<Jmp>,
        _calling_convention: &Option<String>,
    ) -> Option<State<T>> {
        if let Some(state) = state_before_call {
            let mut new_state = state.clone();
            self.handle_unknown_symbol_calls(&mut new_state);
            new_state.set_pointer_inference_state(None);
            return Some(new_state);
        }

        None
    }

    fn update_call_stub(&self, state: &State<T>, call: &Term<Jmp>) -> Option<State<T>> {
        let mut new_state = state.clone();
        match &call.term {
            Jmp::Call { target, .. } => match self.extern_symbol_map.get(target) {
                Some(symbol) => {
                    if let Some(string_symbol) = self.string_symbol_map.get(target) {
                        new_state = self.handle_string_symbol_calls(string_symbol, &new_state);
                    } else {
                        new_state = self.handle_generic_symbol_calls(symbol, &new_state);
                    }
                }
                None => panic!("Extern symbol not found."),
            },
            Jmp::CallInd { .. } => self.handle_unknown_symbol_calls(&mut new_state),
            _ => panic!("Malformed control flow graph encountered."),
        }

        new_state.set_pointer_inference_state(None);
        Some(new_state)
    }

    fn specialize_conditional(
        &self,
        state: &State<T>,
        _condition: &Expression,
        _block_before_condition: &Term<Blk>,
        _is_true: bool,
    ) -> Option<State<T>> {
        Some(state.clone())
    }
}

#[cfg(test)]
mod tests;