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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
use yaxpeax_arch::Arch;
use yaxpeax_arm::armv7::ARMv7;

use arch::{Symbol, SymbolQuery};
use arch::Function;
use arch::FunctionQuery;
use arch::BaseUpdate;
use arch::CommentQuery;
use arch::FunctionImpl;
use arch::arm::v7::analyses::data_flow::DefaultCallingConvention;

use data::ValueLocations;
use data::modifier::InstructionModifiers;

use std::cell::{Ref, RefCell};

use analyses::control_flow;
use analyses::static_single_assignment::SSA;
use analyses::xrefs;

use std::collections::HashMap;
use std::rc::Rc;

use petgraph::graphmap::GraphMap;

use num_traits::Zero;
use ContextRead;
use ContextWrite;

pub mod display;
pub mod analyses;
pub mod semantic;

// #[derive(Serialize, Deserialize)]
// pub struct Function { }

#[derive(Serialize, Deserialize)]
pub struct ARMv7Data {
    pub preferred_addr: <ARMv7 as Arch>::Address,
    pub contexts: MergedContextTable,
    pub cfg: control_flow::ControlFlowGraph<<ARMv7 as Arch>::Address>,
    pub ssa: HashMap<<ARMv7 as Arch>::Address, (
        control_flow::ControlFlowGraph<<ARMv7 as Arch>::Address>,
        SSA<ARMv7>
    )>,
}

pub struct DisplayCtx<'a> {
    functions: Ref<'a, HashMap<<ARMv7 as Arch>::Address, FunctionImpl<<ARMv7 as ValueLocations>::Location>>>,
    comments: &'a HashMap<<ARMv7 as Arch>::Address, String>,
    symbols: &'a HashMap<<ARMv7 as Arch>::Address, Symbol>,
}

impl FunctionQuery<<ARMv7 as Arch>::Address> for ARMv7Data {
    type Function = Function;
    fn function_at(&self, addr: <ARMv7 as Arch>::Address) -> Option<&Self::Function> {
        self.contexts.function_at(addr)
    }
    fn all_functions(&self) -> Vec<&Self::Function> {
        self.contexts.all_functions()
    }
}
impl CommentQuery<<ARMv7 as Arch>::Address> for ARMv7Data {
    fn comment_for(&self, addr: <ARMv7 as Arch>::Address) -> Option<&str> {
        self.contexts.comment_for(addr)
    }
}
impl SymbolQuery<<ARMv7 as Arch>::Address> for ARMv7Data {
    fn symbol_for(&self, addr: <ARMv7 as Arch>::Address) -> Option<&Symbol> {
        self.contexts.symbol_for(addr)
    }
    fn symbol_addr(&self, sym: &Symbol) -> Option<<ARMv7 as Arch>::Address> {
        self.contexts.symbol_addr(sym)
    }
}

impl<'a> FunctionQuery<<ARMv7 as Arch>::Address> for DisplayCtx<'a> {
    type Function = FunctionImpl<<ARMv7 as ValueLocations>::Location>;
    fn function_at(&self, addr: <ARMv7 as Arch>::Address) -> Option<&Self::Function> {
        self.functions.function_at(addr)
    }
    fn all_functions(&self) -> Vec<&Self::Function> {
        self.functions.all_functions()
    }
}

impl<'a> CommentQuery<<ARMv7 as Arch>::Address> for DisplayCtx<'a> {
    fn comment_for(&self, addr: <ARMv7 as Arch>::Address) -> Option<&str> {
        self.comments.get(&addr).map(String::as_ref)
    }
}

impl<'a> SymbolQuery<<ARMv7 as Arch>::Address> for DisplayCtx<'a> {
    fn symbol_for(&self, addr: <ARMv7 as Arch>::Address) -> Option<&Symbol> {
        self.symbols.get(&addr)
    }
    fn symbol_addr(&self, sym: &Symbol) -> Option<<ARMv7 as Arch>::Address> {
        for (k, v) in self.symbols.iter() {
            if v == sym {
                return Some(*k);
            }
        }

        None
    }
}

impl FunctionQuery<<ARMv7 as Arch>::Address> for MergedContextTable {
    type Function = Function;
    fn function_at(&self, _addr: <ARMv7 as Arch>::Address) -> Option<&Self::Function> {
        None
    }
    fn all_functions(&self) -> Vec<&Self::Function> {
        unimplemented!()
    }
}
impl CommentQuery<<ARMv7 as Arch>::Address> for MergedContextTable {
    fn comment_for(&self, addr: <ARMv7 as Arch>::Address) -> Option<&str> {
        self.comments.get(&addr).map(String::as_ref)
    }
}
impl SymbolQuery<<ARMv7 as Arch>::Address> for MergedContextTable {
    fn symbol_for(&self, addr: <ARMv7 as Arch>::Address) -> Option<&Symbol> {
        self.symbols.get(&addr)
    }
    fn symbol_addr(&self, sym: &Symbol) -> Option<<ARMv7 as Arch>::Address> {
        self.reverse_symbols.get(sym).map(|x| *x)
    }
}

impl Default for ARMv7Data {
    fn default() -> Self {
        ARMv7Data {
            preferred_addr: <ARMv7 as Arch>::Address::zero(),
            contexts: MergedContextTable::create_empty(),
            cfg: control_flow::ControlFlowGraph::new(),
            ssa: HashMap::new()
        }
    }
}

#[derive(Serialize, Deserialize)]
pub struct MergedContextTable {
    pub user_contexts: HashMap<<ARMv7 as Arch>::Address, Rc<PartialContext>>,
    pub computed_contexts: HashMap<<ARMv7 as Arch>::Address, Rc<ComputedContext>>,
    pub comments: HashMap<<ARMv7 as Arch>::Address, String>,
    #[serde(skip)]
    pub call_graph: GraphMap<<ARMv7 as Arch>::Address, (), petgraph::Directed>,
    #[serde(skip)]
    pub xrefs: xrefs::XRefCollection<<ARMv7 as Arch>::Address>,
    pub symbols: HashMap<<ARMv7 as Arch>::Address, Symbol>,
    #[serde(skip)]
    pub reverse_symbols: HashMap<Symbol, <ARMv7 as Arch>::Address>,
    pub functions: Rc<RefCell<HashMap<<ARMv7 as Arch>::Address, FunctionImpl<<ARMv7 as ValueLocations>::Location>>>>,
    pub function_data: HashMap<<ARMv7 as Arch>::Address, RefCell<InstructionModifiers<ARMv7>>>,
    pub function_hints: Vec<<ARMv7 as Arch>::Address>,
    pub default_abi: Option<DefaultCallingConvention>,
}

impl MergedContextTable {
    pub fn create_empty() -> MergedContextTable {
        MergedContextTable {
            user_contexts: HashMap::new(),
            computed_contexts: HashMap::new(),
            comments: HashMap::new(),
            call_graph: GraphMap::new(),
            xrefs: xrefs::XRefCollection::new(),
            symbols: HashMap::new(),
            reverse_symbols: HashMap::new(),
            functions: Rc::new(RefCell::new(HashMap::new())),
            function_data: HashMap::new(),
            function_hints: Vec::new(),
            default_abi: Some(DefaultCallingConvention::Standard),
        }
    }

    pub fn display_ctx(&self) -> DisplayCtx {
        DisplayCtx {
            functions: self.functions.borrow(),
            symbols: &self.symbols,
            comments: &self.comments,
        }
    }
}

pub type Update = BaseUpdate<ArmUpdate>;

#[derive(Debug)]
#[allow(non_camel_case_types)]
pub enum ArmUpdate {
    AddXRef(xrefs::RefType, xrefs::RefAction, <ARMv7 as Arch>::Address),
    RemoveXRef(xrefs::RefType, xrefs::RefAction, <ARMv7 as Arch>::Address),
    FunctionHint
}

impl ContextRead<ARMv7, MergedContext> for MergedContextTable {
    fn at(&self, address: &<ARMv7 as Arch>::Address) -> MergedContext {
        MergedContext {
            user: self.user_contexts.get(address).map(|v| Rc::clone(v)),
            computed: self.computed_contexts.get(address).map(|v| Rc::clone(v))
        }
    }
}

impl ContextWrite<ARMv7, Update> for MergedContextTable {
    fn put(&mut self, address: <ARMv7 as Arch>::Address, update: Update) {
        match update {
            BaseUpdate::DefineSymbol(sym) => {
                self.symbols.insert(address, sym.clone());
                self.reverse_symbols.insert(sym, address);
            }
            BaseUpdate::AddCodeComment(comment) => {
                self.comments.insert(address, comment);
            }
            _ => { }
        }
    }
}

pub trait PartialInstructionContext {
    fn indicator_tag(&self) -> &'static str;
}

#[derive(Debug)]
pub struct MergedContext {
    pub computed: Option<Rc<ComputedContext>>,
    pub user: Option<Rc<PartialContext>>
}

impl PartialInstructionContext for MergedContext {
    fn indicator_tag(&self) -> &'static str {
        "+"
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ComputedContext {

}
impl PartialInstructionContext for ComputedContext {
    fn indicator_tag(&self) -> &'static str {
        "@"
    }
}

#[derive(Debug, Deserialize, Serialize)]
pub struct PartialContext {
}
impl PartialInstructionContext for PartialContext {
    fn indicator_tag(&self) -> &'static str {
        "m"
    }
}