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
254
use yaxpeax_arch::Arch;
use yaxpeax_arm::armv8::a64::ARMv8;

use arch::BaseUpdate;
use arch::{Symbol, SymbolQuery};
use arch::{Function, FunctionQuery};
use arch::CommentQuery;
use arch::FunctionImpl;
use arch::arm::v8::aarch64::analyses::data_flow::DefaultCallingConvention;
use analyses::static_single_assignment::SSA;
use analyses::xrefs;

use analyses::control_flow;

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

use std::collections::HashMap;
use std::rc::Rc;
use std::cell::{Ref, RefCell};

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

pub mod display;
pub mod aarch64;

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

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

impl FunctionQuery<<ARMv8 as Arch>::Address> for MergedContextTable {
    type Function = Function;
    fn function_at(&self, _addr: <ARMv8 as Arch>::Address) -> Option<&Self::Function> {
        unimplemented!("FunctionQuery::function_at for MergedContextTable");
    }
    fn all_functions(&self) -> Vec<&Self::Function> {
        unimplemented!("FunctionQuery::all_functions for MergedContextTable");
    }
}
impl CommentQuery<<ARMv8 as Arch>::Address> for MergedContextTable {
    fn comment_for(&self, addr: <ARMv8 as Arch>::Address) -> Option<&str> {
        self.comments.get(&addr).map(String::as_ref)
    }
}

impl SymbolQuery<<ARMv8 as Arch>::Address> for ARMv8Data {
    fn symbol_for(&self, _addr: <ARMv8 as Arch>::Address) -> Option<&Symbol> {
        None
    }
    fn symbol_addr(&self, _sym: &Symbol) -> Option<<ARMv8 as Arch>::Address> {
        None
    }
}

impl<'a> FunctionQuery<<ARMv8 as Arch>::Address> for DisplayCtx<'a> {
    type Function = FunctionImpl<<ARMv8 as ValueLocations>::Location>;
    fn function_at(&self, addr: <ARMv8 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<<ARMv8 as Arch>::Address> for DisplayCtx<'a> {
    fn comment_for(&self, addr: <ARMv8 as Arch>::Address) -> Option<&str> {
        self.comments.get(&addr).map(String::as_ref)
    }
}

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

        None
    }
}

impl SymbolQuery<<ARMv8 as Arch>::Address> for MergedContextTable {
    fn symbol_for(&self, addr: <ARMv8 as Arch>::Address) -> Option<&Symbol> {
        self.symbols.get(&addr)
    }
    fn symbol_addr(&self, sym: &Symbol) -> Option<<ARMv8 as Arch>::Address> {
        self.reverse_symbols.get(sym).map(|x| *x)
    }
}

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

/*
#[allow(non_snake_case)]
impl <T> control_flow::Determinant<T, <ARMv8 as Arch>::Address> for Instruction
    where T: PartialInstructionContext {

    fn control_flow(&self, _ctx: Option<&T>) -> control_flow::Effect<<ARMv8 as Arch>::Address> {
        match self.opcode {
            Opcode::B |
            Opcode::BR |
            Opcode::BL |
            Opcode::BLR => {
                control_flow::Effect::stop_and(
                    control_flow::Target::Indeterminate
                )
            }
            Opcode::Bcc(_) => {
                control_flow::Effect::cont_and(
                    control_flow::Target::Indeterminate
                )
            },
            _ => {
                control_flow::Effect::cont()
            }
        }
    }
}
*/

#[derive(Serialize, Deserialize)]
pub struct MergedContextTable {
    pub user_contexts: HashMap<<ARMv8 as Arch>::Address, Rc<PartialContext>>,
    pub computed_contexts: HashMap<<ARMv8 as Arch>::Address, Rc<ComputedContext>>,
    pub comments: HashMap<<ARMv8 as Arch>::Address, String>,
    pub symbols: HashMap<<ARMv8 as Arch>::Address, Symbol>,
    #[serde(skip)]
    pub reverse_symbols: HashMap<Symbol, <ARMv8 as Arch>::Address>,
    pub functions: Rc<RefCell<HashMap<<ARMv8 as Arch>::Address, FunctionImpl<<ARMv8 as ValueLocations>::Location>>>>,
    pub function_data: HashMap<<ARMv8 as Arch>::Address, RefCell<InstructionModifiers<ARMv8>>>,
    pub function_hints: Vec<<ARMv8 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(),
            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::None), // TODO: a real calling convention defulat
        }
    }

    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, <ARMv8 as Arch>::Address),
    RemoveXRef(xrefs::RefType, xrefs::RefAction, <ARMv8 as Arch>::Address),
    FunctionHint
}

impl ContextRead<ARMv8, MergedContext> for MergedContextTable {
    fn at(&self, address: &<ARMv8 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<ARMv8, Update> for MergedContextTable {
    fn put(&mut self, address: <ARMv8 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"
    }
}