rsleigh 0.4.2

SLEIGH (.slaspec) parser and Rust decoder/P-code emitter codegen — Ghidra-compatible disassembly in pure Rust
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
pub mod disassembly;
pub mod display;
pub mod execution;
pub mod meaning;
pub mod pattern;
pub mod space;
pub mod table;
pub mod token;
pub mod user_function;
pub mod varnode;

// internal representation used to convert from syntax to semantic
// represenatation
pub(crate) mod inner;

use std::collections::HashMap;

use crate::semantic::inner::SolverStatus;
use crate::{syntax, Endian, NumberNonZeroUnsigned, SleighError, Span};

use self::inner::Solved;
use self::meaning::{AttachLiteral, AttachNumber, AttachVarnode};
use self::space::Space;
use self::table::Table;
use self::token::{Token, TokenField};
use self::user_function::UserFunction;
use self::varnode::{Bitrange, Context, ContextMemoryMapping, Varnode};

#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub enum PrintBase {
    Dec,
    Hex,
}
impl PrintBase {
    pub fn is_hex(&self) -> bool {
        matches!(self, Self::Hex)
    }
    pub fn is_dec(&self) -> bool {
        matches!(self, Self::Dec)
    }
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub struct ValueFmt {
    pub signed: bool,
    pub base: PrintBase,
}

#[derive(Clone, Copy, Debug, Default)]
pub struct InstStart;

#[derive(Clone, Copy, Debug, Default)]
pub struct InstNext;

#[derive(Clone, Copy, Debug, Default)]
pub struct Epsilon;

#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub struct SpaceId(pub usize);

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct TableId(pub usize);

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct TokenId(pub usize);

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct TokenFieldId(pub usize);

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct UserFunctionId(pub usize);

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct BitrangeId(pub usize);

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct ContextId(pub usize);

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct VarnodeId(usize);
impl VarnodeId {
    pub unsafe fn from_raw(idx: usize) -> Self {
        Self(idx)
    }
    pub fn to_raw(&self) -> usize {
        self.0
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct AttachLiteralId(pub usize);

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct AttachNumberId(pub usize);

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct AttachVarnodeId(pub usize);

#[derive(Clone, Copy, Debug)]
pub enum GlobalScope {
    Space(SpaceId),
    Varnode(VarnodeId),
    Context(ContextId),
    Bitrange(BitrangeId),
    Token(TokenId),
    TokenField(TokenFieldId),
    InstStart(InstStart),
    InstNext(InstNext),
    Epsilon(Epsilon),
    UserFunction(UserFunctionId),
    Table(TableId),
}

impl From<inner::GlobalScope> for GlobalScope {
    fn from(value: inner::GlobalScope) -> Self {
        match value {
            inner::GlobalScope::Space(x) => Self::Space(x),
            inner::GlobalScope::Varnode(x) => Self::Varnode(x),
            inner::GlobalScope::Context(x) => Self::Context(x),
            inner::GlobalScope::Bitrange(x) => Self::Bitrange(x),
            inner::GlobalScope::Token(x) => Self::Token(x),
            inner::GlobalScope::TokenField(x) => Self::TokenField(x),
            inner::GlobalScope::InstStart(x) => Self::InstStart(x),
            inner::GlobalScope::InstNext(x) => Self::InstNext(x),
            inner::GlobalScope::Epsilon(x) => Self::Epsilon(x),
            inner::GlobalScope::UserFunction(x) => Self::UserFunction(x),
            inner::GlobalScope::Table(x) => Self::Table(x),

            inner::GlobalScope::PcodeMacro(_) => unreachable!(),
        }
    }
}

impl GlobalScope {
    pub fn token_field(&self) -> Option<TokenFieldId> {
        match self {
            GlobalScope::TokenField(x) => Some(*x),
            _ => None,
        }
    }
    pub fn token(&self) -> Option<TokenId> {
        match self {
            GlobalScope::Token(x) => Some(*x),
            _ => None,
        }
    }
    pub fn space(&self) -> Option<SpaceId> {
        match self {
            GlobalScope::Space(x) => Some(*x),
            _ => None,
        }
    }
    pub fn varnode(&self) -> Option<VarnodeId> {
        match self {
            GlobalScope::Varnode(x) => Some(*x),
            _ => None,
        }
    }
    pub fn context(&self) -> Option<ContextId> {
        match self {
            GlobalScope::Context(x) => Some(*x),
            _ => None,
        }
    }
    pub fn bitrange(&self) -> Option<BitrangeId> {
        match self {
            GlobalScope::Bitrange(x) => Some(*x),
            _ => None,
        }
    }
    pub fn table(&self) -> Option<TableId> {
        match self {
            GlobalScope::Table(x) => Some(*x),
            _ => None,
        }
    }
    pub fn user_function(&self) -> Option<UserFunctionId> {
        match self {
            GlobalScope::UserFunction(x) => Some(*x),
            _ => None,
        }
    }
}

#[derive(Clone, Debug)]
pub struct Sleigh {
    endian: Endian,
    alignment: u8,

    default_space: SpaceId,
    // TODO make it a const value 0, first table is always the instruction table
    instruction_table: TableId,

    spaces: Box<[Space]>,
    varnodes: Box<[Varnode]>,
    contexts: Box<[Context]>,
    bitranges: Box<[Bitrange]>,
    tokens: Box<[Token]>,
    token_fields: Box<[TokenField]>,
    user_functions: Box<[UserFunction]>,
    tables: Box<[Table]>,

    attach_varnodes: Box<[AttachVarnode]>,
    attach_literals: Box<[AttachLiteral]>,
    attach_numbers: Box<[AttachNumber]>,
    //scope with all the global identifiers
    global_scope: HashMap<String, GlobalScope>,
    /// Context mapped into single and packed memory block
    context_memory: ContextMemoryMapping,
}

impl Sleigh {
    pub fn endian(&self) -> Endian {
        self.endian
    }
    pub fn alignemnt(&self) -> u8 {
        self.alignment
    }
    // TODO make it a const value 0, first table is always the instruction table
    pub fn instruction_table(&self) -> TableId {
        self.instruction_table
    }
    pub fn context_memory(&self) -> &ContextMemoryMapping {
        &self.context_memory
    }
    pub fn global_scope_by_name<'a>(&'a self, name: &str) -> Option<&'a GlobalScope> {
        self.global_scope.get(name)
    }
    pub fn space(&self, space: SpaceId) -> &Space {
        &self.spaces[space.0]
    }
    pub fn varnode(&self, varnode: VarnodeId) -> &Varnode {
        &self.varnodes[varnode.0]
    }
    pub fn context(&self, context: ContextId) -> &Context {
        &self.contexts[context.0]
    }
    pub fn bitrange(&self, bitrange: BitrangeId) -> &Bitrange {
        &self.bitranges[bitrange.0]
    }
    pub fn token(&self, token: TokenId) -> &Token {
        &self.tokens[token.0]
    }
    pub fn token_field(&self, token_field: TokenFieldId) -> &TokenField {
        &self.token_fields[token_field.0]
    }
    pub fn user_function(&self, user_function: UserFunctionId) -> &UserFunction {
        &self.user_functions[user_function.0]
    }
    pub fn table(&self, table: TableId) -> &Table {
        &self.tables[table.0]
    }
    pub fn attach_varnode(&self, id: AttachVarnodeId) -> &AttachVarnode {
        &self.attach_varnodes[id.0]
    }
    pub fn attach_number(&self, id: AttachNumberId) -> &AttachNumber {
        &self.attach_numbers[id.0]
    }
    pub fn attach_literal(&self, id: AttachLiteralId) -> &AttachLiteral {
        &self.attach_literals[id.0]
    }
    pub(crate) fn new(value: syntax::Sleigh) -> Result<Self, Box<SleighError>> {
        let inner = inner::Sleigh::new(value)?;
        tracing::trace!("semantic tree constructed sucessfully");
        //HACK: verify that indirect recursion don't happen
        //NOTE we don't need to worry about direct (self) recursion.
        //AKA `Tablea` calling itself
        let tables = inner
            .global_scope
            .values()
            .filter_map(inner::GlobalScope::table);
        for table_id in tables {
            use std::ops::ControlFlow;
            let table = inner.table(table_id);
            if let ControlFlow::Break(rec) = table.pattern_indirect_recursion(&inner, table_id) {
                unimplemented!(
                    "Indirect recursion is not implemented at the moment {:?}",
                    rec
                );
            }
        }

        //TODO: if unable to solve addr size or default space not set, error
        let _ = inner.default_space.as_ref().unwrap();
        //solve all pcodes macros inside the tables
        //solve all tables
        for i in 0.. {
            let mut solved = Solved::default();
            for table in inner.tables.iter() {
                table.solve(&inner, &mut solved)?;
            }
            if solved.we_finished() && !solved.we_did_a_thing() {
                break;
            }
            if i > 100 || !solved.we_did_a_thing() {
                if solved.we_finished() {
                    // All sizes resolved, just no more progress to make
                    break;
                }
                // Some sizes couldn't be resolved — log warning but continue.
                // Unresolved sizes will default to the architecture's address size.
                // This handles MIPS16/microMIPS variable-length encoding edge cases.
                #[cfg(feature = "strict_solve")]
                {
                    let mut solved = crate::semantic::inner::SolvedLocation::default();
                    for table in inner.tables.iter() {
                        table.solve(&inner, &mut solved)?;
                    }
                    return Err(Box::new(SleighError::TableUnsolvable(
                        solved.locations.iter().fold(
                            String::new(),
                            |mut acc, (location, file, line)| {
                                use std::fmt::Write;
                                writeln!(&mut acc, "{}:{}: {location}", file, line + 1).unwrap();
                                acc
                            },
                        ),
                    )));
                }
                // Non-strict mode: break with unresolved sizes
                break;
            }
        }
        tracing::trace!("semantic table solving sucessfully");
        let context_memory = ContextMemoryMapping::map_all(&inner);
        let contexts: Box<[_]> = inner
            .contexts
            .into_iter()
            .map(|context| context.convert())
            .collect();

        let endian = inner.endian.ok_or(SleighError::EndianMissing)?;
        let alignment = inner.alignment.unwrap_or(0).try_into().unwrap();
        // unwrap because should be created on Sleigh::new
        let default_space = inner
            .default_space
            .ok_or(SleighError::SpaceMissingDefault)?;
        //TODO check all constructor for tables have export of the same size
        let token_fields = inner
            .token_fields
            .into_iter()
            .map(|x| x.convert())
            .collect();
        let mut sleigh = Self {
            endian,
            alignment,
            default_space,
            context_memory,
            contexts,
            token_fields,
            tokens: inner.tokens.into(),
            instruction_table: inner.instruction_table,
            spaces: inner.spaces.into(),
            varnodes: inner.varnodes.into(),
            user_functions: inner.user_functions.into(),
            bitranges: inner.bitranges.into(),
            attach_varnodes: inner.attach_varnodes.into(),
            attach_literals: inner.attach_literals.into(),
            attach_numbers: inner.attach_numbers.into(),
            tables: Box::new([]),
            global_scope: inner
                .global_scope
                .into_iter()
                .filter_map(|(k, v)| match v {
                    // PcodeMacro is fully replaced at this point
                    inner::GlobalScope::PcodeMacro(_) => None,
                    v => Some((k, v.into())),
                })
                .collect(),
        };
        let tables = inner
            .tables
            .into_iter()
            .map(|x| x.convert(&sleigh))
            .collect();
        sleigh.tables = tables;

        //TODO remove unused table/macro/pcode_macro/etc?
        Ok(sleigh)
    }
    pub fn addr_bytes(&self) -> NumberNonZeroUnsigned {
        self.space(self.default_space).addr_bytes
    }

    pub fn spaces(&self) -> &[Space] {
        &self.spaces
    }
    pub fn varnodes(&self) -> &[Varnode] {
        &self.varnodes
    }
    pub fn contexts(&self) -> &[Context] {
        &self.contexts
    }
    pub fn bitranges(&self) -> &[Bitrange] {
        &self.bitranges
    }
    pub fn user_functions(&self) -> &[UserFunction] {
        &self.user_functions
    }
    pub fn tables(&self) -> &[Table] {
        &self.tables
    }
    pub fn tokens(&self) -> &[Token] {
        &self.tokens
    }
    pub fn token_fields(&self) -> &[TokenField] {
        &self.token_fields
    }
    pub fn attach_varnodes(&self) -> &[AttachVarnode] {
        &self.attach_varnodes
    }
    pub fn attach_numbers(&self) -> &[AttachNumber] {
        &self.attach_numbers
    }
    pub fn attach_literals(&self) -> &[AttachLiteral] {
        &self.attach_literals
    }
    pub fn default_space(&self) -> SpaceId {
        self.default_space
    }

    pub fn attach_varnodes_len_bytes(&self, id: AttachVarnodeId) -> NumberNonZeroUnsigned {
        self.varnode(self.attach_varnode(id).0[0].1).len_bytes
    }
}