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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
use std::cell::{Cell, RefCell};
use std::ops::ControlFlow;

use crate::pattern::BitConstraint;
use crate::semantic::display::Display;
use crate::semantic::pattern::PatternLen;
use crate::semantic::table::Constructor as FinalConstructor;
use crate::semantic::{Sleigh as FinalSleigh, Table as FinalTable, TableId};
use crate::table::{ConstructorId, Matcher, VariantId};
use crate::{syntax, ExecutionError, PatternError, SleighError, Span, TableError};

use super::disassembly;
use super::execution::{Execution, ExecutionBuilder, FieldSize, FieldSizeMut, TableExportType};
use super::pattern::{Pattern, PatternWalker};
use super::with_block::WithBlockCurrent;
use super::{GlobalScope, Sleigh, SolverStatus};

pub mod execution;

#[derive(Clone)]
pub struct Table {
    pub is_root: bool,
    pub name: String,
    pub constructors: RefCell<Vec<Constructor>>,
    /* TODO:
     * export need to be identified by type, such Varnode, Const, Value, etc.
     * and should by identified by context of its use.
     * eg: in `ARMinstructions.sinc` the `rn` table returns a value or a
     * register depending if the token `c1619=15` or not.
     * With this diference being clear on the `addrmode2`.
     * In this case, if the context is `c1619=15`, `rn` returns a value,
     * otherwise it returns a varnode. */
    //None means no constructors is able to define the return type, because
    //this table is empty or contructors are all `unimpl`.
    //Some(ExecutionExport::None) mean that this table doesn't export
    pub export: RefCell<Option<TableExportType>>,

    //HACK: pattern indirect recursion helper
    pub pattern_recursion_checked: RefCell<bool>,

    pub pattern_len: Cell<Option<PatternLen>>,
}

impl std::fmt::Debug for Table {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Table returning: {:?}", self.export)
    }
}

//7.8.1. Matching
//one pattern contains the other if all the cases that match the contained,
//also match the pattern.
//eg: `a` contains `b` if all cases that match `b` also match `a`. In other
//words `a` is a special case of `b`.
//NOTE the opose don't need to be true.
fn is_first_then(
    constructors: &[FinalConstructor],
    matcher_a: Matcher,
    matcher_b: Matcher,
) -> bool {
    let constructor_a = &constructors[matcher_a.constructor.0];
    let constructor_b = &constructors[matcher_b.constructor.0];
    let pattern_len_a = constructor_a.pattern.bits_produced();
    let pattern_len_b = constructor_b.pattern.bits_produced();
    let pattern_len_max = pattern_len_a.max(pattern_len_b);
    let extend_len_a = pattern_len_max - pattern_len_a;
    let extend_len_b = pattern_len_max - pattern_len_b;

    fn produce_iter(
        constructor: &FinalConstructor,
        variant: VariantId,
        extend: usize,
    ) -> impl Iterator<Item = BitConstraint> + '_ {
        let context_bits = &constructor.variants_bits[variant.0].1;
        let token_bits = &constructor.variants_bits[variant.0].2;
        context_bits
            .iter()
            .chain(token_bits.iter())
            .cloned()
            .chain((0..extend).map(|_| BitConstraint::Unrestrained))
    }
    let bits_a = produce_iter(constructor_a, matcher_a.variant_id, extend_len_a);
    let bits_b = produce_iter(constructor_b, matcher_b.variant_id, extend_len_b);
    use BitConstraint::*;
    bits_a.zip(bits_b).all(|(x, y)| {
        match (x, y) {
            // matcher_a is equal to b, or is tighter, so continue testing
            (Defined(x), Defined(y)) if x == y => true,
            (Unrestrained, Unrestrained)
            | (Defined(_), Unrestrained)
            | (Restrained, Unrestrained) => true,
            // diferent, a can't contain b
            (Defined(_), Defined(_)) => false,
            // verify that this complex restraint is equal in both
            // TODO: deep test, checking if the complex constraint can be
            // compatible. For now, just assume they are different.
            (Defined(_), Restrained) | (Restrained, Defined(_)) | (Restrained, Restrained) => false,
            // matcher_a don't constraint something in b.
            (Unrestrained, Restrained) | (Unrestrained, Defined(_)) => false,
        }
    })
}

impl Table {
    pub fn new_empty(is_root: bool, name: String) -> Self {
        Self {
            is_root,
            name,
            constructors: RefCell::new(vec![]),
            //it does not export, unless a exporting constructor is added
            export: RefCell::new(None),
            //HACK
            pattern_recursion_checked: RefCell::new(false),
            pattern_len: Cell::default(),
            //export_time: RefCell::default(),
        }
    }
    pub fn is_root(&self) -> bool {
        self.is_root
    }
    pub fn add_constructor(&mut self, constructor: Constructor) -> Result<(), Box<SleighError>> {
        //all the constructor need to export or none can export
        //if this constructor is not `unimpl` update/verify the return type
        if let Some(execution) = constructor.execution() {
            let mut export = self.export.borrow_mut();
            if let Some(export) = export.as_mut() {
                //if this table exports, the new constructor need to be
                //compatible
                *export = export.combine(execution.return_value).ok_or_else(|| {
                    Box::new(SleighError::new_table(
                        constructor.src.clone(),
                        ExecutionError::InvalidExport,
                    ))
                })?;
            } else {
                //first constructor will define the table export type
                *export = Some(execution.return_value);
            }
        }
        self.constructors.borrow_mut().push(constructor);
        Ok(())
    }
    //HACK
    pub fn pattern_indirect_recursion(
        &self,
        sleigh: &Sleigh,
        me: TableId,
    ) -> ControlFlow<Vec<TableId>> {
        struct FindIndirectRecursion<'a>(&'a Sleigh, TableId);
        impl<'a> PatternWalker<Vec<TableId>> for FindIndirectRecursion<'a> {
            fn table(&mut self, table: TableId) -> ControlFlow<Vec<TableId>> {
                if table == self.1 {
                    //we don't care about self reference, only indirect ones
                    return ControlFlow::Continue(());
                }
                self.0
                    .table(table)
                    .pattern_indirect_recursion(self.0, table)
            }
        }
        //This borrow will detect the recursion, we hold the lock, while the
        //sub patterns are checked, we are only unable to borrow, if the lock
        //is hold by a previous interation.
        let mut checked = if let Ok(checked) = self.pattern_recursion_checked.try_borrow_mut() {
            //if this table was already checked, then we don't need to check
            //again
            if *checked {
                return ControlFlow::Continue(());
            }
            checked
        } else {
            //unable to lock, this means that we got here though some kind of
            //recursion
            return ControlFlow::Break(vec![me]);
        };
        let mut find = FindIndirectRecursion(sleigh, me);
        let constructors = self.constructors.borrow();
        for constructor in constructors.iter() {
            find.pattern(&constructor.pattern)?;
        }
        //table verified all the leading branches, they never call recursives
        *checked = true;
        ControlFlow::Continue(())
    }
    pub fn pattern_len(&self) -> Option<PatternLen> {
        self.pattern_len.get()
    }
    fn solve_pattern_len<T>(&self, sleigh: &Sleigh, solved: &mut T) -> Result<(), Box<SleighError>>
    where
        T: SolverStatus + Default,
    {
        //update all constructors
        self.constructors
            .borrow_mut()
            .iter_mut()
            .try_for_each(|constructor| constructor.solve_pattern(sleigh, solved).map(|_| ()))?;

        //if already solved, do nothing
        if self.pattern_len.get().is_some() {
            return Ok(());
        };

        //TODO improve this to not require collect
        //all the lens from constructors
        let constructors = self.constructors.borrow();
        let lens: Result<Vec<_>, &Span> = constructors
            .iter()
            //get all the lens, returning none if is undefined len,
            //in this case abort the whole len calculation
            //indexs are 1/1
            .map(|constructor| constructor.pattern.len.ok_or_else(|| constructor.src()))
            .collect();
        let lens: Vec<_> = match lens {
            Ok(lens) => lens,
            Err(src) => {
                //found one undefined len, unable to calculate the table pattern
                //len range for now
                solved.iam_not_finished(src, file!(), line!());
                return Ok(());
            }
        };
        //the smallest possible table pattern_len, except from recursives
        let min = lens.iter().filter_map(|len| len.min()).min();

        //the biggest possible table pattern_len
        //FUTURE use try_reduce instead
        let mut iter = lens.iter().map(|len| len.max());
        let max = iter
            .next()
            .unwrap()
            .and_then(|first| iter.try_fold(first, |acc, len| len.map(|len| len.max(acc))));

        match (min, max) {
            //impossible to happen, only invalid logic can generate that
            (None, Some(_)) => unreachable!(),
            //no smallest, means that all constructors are recursives, what is
            //invalid, because the pattern will never end to match
            (None, None) => {
                //TODO error here
                panic!("Table is composed exclusivelly of recursive patterns");
            }
            //if there is no biggest, means at least one constructor
            //will always result in a Min (recursive).
            //if always recursive, len is Min(smallest_len)
            (Some(min), None) => {
                self.pattern_len.set(Some(PatternLen::Min(min)));
                solved.i_did_a_thing();
            }

            //if both are Some, we can return a range
            (Some(min), Some(max)) => {
                let len = PatternLen::new(min, max);
                self.pattern_len.set(Some(len));
                solved.i_did_a_thing();
            }
        }

        Ok(())
    }
    pub fn solve<T>(&self, sleigh: &Sleigh, solved: &mut T) -> Result<(), Box<SleighError>>
    where
        T: SolverStatus + Default,
    {
        //empty table can't be solved
        if self.constructors.borrow().is_empty() {
            return Ok(());
        }

        //TODO only solve, if necessary
        //if self.fully_solved_already() {
        //  return Ok(());
        //}

        self.solve_pattern_len(sleigh, solved)?;

        //TODO move this into solve_execution
        //update all constructors
        self.constructors
            .borrow_mut()
            .iter_mut()
            .try_for_each(|constructor| constructor.solve_execution(sleigh, solved))?;
        //TODO update the FieldSize::all_same_size to use iterators and use it
        //here
        //update all the constructors return size, if none/undefined return just
        //finish solving
        let mut export = self.export.borrow_mut();
        let Some(export_size) = export.as_mut().and_then(|x| x.size_mut()) else {
            return Ok(());
        };

        // calculate the new exported value
        let mut inputs: Vec<FieldSize> = self
            .constructors
            .borrow()
            .iter()
            .filter_map(|con| con.execution())
            .map(|exe| exe.return_value.size().unwrap().clone())
            .collect();
        let modified = super::execution::len::n_generate_a(inputs.as_mut_slice(), export_size)
            .ok_or_else(|| SleighError::TableUnsolvable(self.name.clone()))?;

        // update all the constructors
        if modified {
            solved.i_did_a_thing();
            let mut constructors = self.constructors.borrow_mut();
            let execs = constructors
                .iter_mut()
                .filter_map(|con| con.execution_mut());
            for (new_size, exe) in inputs.into_iter().zip(execs) {
                let old_size = exe.return_value.size_mut().unwrap();
                *old_size = new_size;
            }
        }

        if export_size.is_undefined() {
            for constructor in self.constructors.borrow().iter() {
                solved.iam_not_finished(&constructor.src, file!(), line!());
            }
        }
        Ok(())
    }
    pub fn convert(self, sleigh: &FinalSleigh) -> FinalTable {
        let constructors: Box<[_]> = self
            .constructors
            .take()
            .into_iter()
            .map(|constructor| constructor.convert(sleigh))
            .collect();
        let matchers_num = constructors.iter().map(|c| c.variants_bits.len()).sum();
        let mut matcher_order = Vec::with_capacity(matchers_num);

        let matcher_a_iter = constructors.iter().enumerate().flat_map(|(i, c)| {
            c.variants().map(move |x| Matcher {
                constructor: ConstructorId(i),
                variant_id: x.0,
            })
        });
        for matcher_a in matcher_a_iter {
            //TODO detect conflicting instead of just looking for contains
            let pos = matcher_order
                .iter()
                .position(|matcher_b| is_first_then(&constructors, matcher_a, *matcher_b));
            //insert constructors in the correct order accordingly with the
            //rules of `7.8.1. Matching`
            if let Some(pos) = pos {
                matcher_order.insert(pos, matcher_a);
            } else {
                matcher_order.push(matcher_a);
            }
        }

        FinalTable {
            is_root: self.is_root,
            constructors,
            export: self
                .export
                .borrow()
                .unwrap_or(TableExportType::None)
                .convert(),
            pattern_len: self.pattern_len.get().unwrap(),
            name: self.name.into(),
            matcher_order: matcher_order.into(),
        }
    }
}

impl FieldSizeMut for &Table {
    fn get(&self) -> FieldSize {
        match self.export.borrow().as_ref().and_then(|x| x.size()) {
            Some(size) => *size,
            None => FieldSize::default(),
        }
    }

    fn set(&mut self, size: FieldSize) -> Option<bool> {
        let mut self_export = self.export.borrow_mut();
        let Some(export) = self_export.as_mut() else {
            return Some(false);
        };
        let Some(self_ref) = export.size_mut() else {
            return Some(false);
        };
        let modify = *self_ref != size;
        if modify {
            let _ = std::mem::replace(self_ref, size);
        }
        Some(modify)
    }
}

#[derive(Clone, Debug)]
pub struct Constructor {
    //pub table: Weak<Table>,
    pub display: Display,
    pub pattern: Pattern,
    pub execution: Option<Execution>,
    src: Span,
}
impl Constructor {
    pub fn new(
        display: Display,
        pattern: Pattern,
        execution: Option<Execution>,
        src: Span,
    ) -> Self {
        Self {
            display,
            pattern,
            execution,
            src,
        }
    }

    pub fn execution(&self) -> Option<&Execution> {
        self.execution.as_ref()
    }

    pub fn execution_mut(&mut self) -> Option<&mut Execution> {
        self.execution.as_mut()
    }

    pub fn src(&self) -> &Span {
        &self.src
    }

    pub fn solve_pattern<T>(
        &mut self,
        sleigh: &Sleigh,
        solved: &mut T,
    ) -> Result<bool, Box<SleighError>>
    where
        T: SolverStatus + Default,
    {
        self.pattern
            .calculate_len(sleigh, solved)
            .map_err(|e| Box::new(SleighError::new_table(self.src.clone(), *e)))
    }

    pub fn solve_execution<T>(
        &mut self,
        sleigh: &Sleigh,
        solved: &mut T,
    ) -> Result<(), Box<SleighError>>
    where
        T: SolverStatus + Default,
    {
        if let Some(execution) = &mut self.execution {
            execution
                .solve(sleigh, solved)
                .map_err(|e| SleighError::new_table(self.src.clone(), *e))?
        }
        Ok(())
    }

    pub fn convert(mut self, sleigh: &FinalSleigh) -> FinalConstructor {
        self.pattern.calculate_bits(0);
        let pattern = self.pattern.convert();
        let execution = self.execution.map(|x| x.convert());
        let src = self.src;

        //TODO detect export type and apply to the disassembly, if the case

        let variants_bits = pattern
            .pattern_bits_variants(sleigh)
            .map(|(i, c, v)| (VariantId(i), c.into(), v.into()))
            .collect();

        FinalConstructor {
            pattern,
            display: self.display,
            execution,
            location: src,
            variants_bits,
        }
    }
}

impl Sleigh {
    pub(crate) fn insert_table_constructor(
        &mut self,
        with_block_current: &mut WithBlockCurrent,
        constructor: syntax::block::table::Constructor,
    ) -> Result<(), Box<SleighError>> {
        let table_name = with_block_current.table_name(constructor.table_name());
        let table_id = self.get_table_or_create_empty(table_name, &constructor.src)?;

        let pattern = with_block_current.pattern(&constructor.pattern);
        let mut pattern = Pattern::new(self, pattern, table_id)
            .map_err(|e| Box::new(SleighError::new_table(constructor.src.clone(), *e)))?;
        //TODO move this into the Pattern::new function
        pattern
            .unresolved_token_fields(self)
            .try_for_each(|(token_field, location)| {
                let token_produced = pattern
                    .produce_token_field(self, token_field)
                    .map_err(|e| Box::new(SleighError::new_table(constructor.src.clone(), *e)))?;
                if token_produced.is_none() {
                    //TODO error with the list of unresolved fields instead of
                    //only the first
                    return Err(Box::new(SleighError::new_table(
                        constructor.src.clone(),
                        PatternError::MissingRef(location),
                    )));
                }
                Ok(())
            })?;

        let disassembly_raw = with_block_current.disassembly(constructor.disassembly);
        if let Some(disassembly_raw) = disassembly_raw {
            disassembly::Builder::new(self, &mut pattern)
                .build(disassembly_raw)
                .map_err(|e| Box::new(SleighError::new_table(constructor.src.clone(), *e)))?;
        }

        let is_root = self.instruction_table == table_id;
        let display = self
            .new_display(constructor.display, &mut pattern, is_root)
            .map_err(|e| Box::new(SleighError::new_table(constructor.src.clone(), *e)))?;

        let execution = constructor
            .execution
            .map(|x| -> Result<Execution, Box<ExecutionError>> {
                let mut execution =
                    execution::Builder::new(self, &mut pattern, constructor.src.clone());
                execution.extend(x)?;
                Ok(execution.into())
            })
            .transpose()
            .map_err(|e| Box::new(SleighError::new_table(constructor.src.clone(), *e)))?;

        let constructor = Constructor::new(display, pattern, execution, constructor.src);
        let table = self.table_mut(table_id);
        table.add_constructor(constructor)?;
        Ok(())
    }
    pub fn get_table_or_create_empty(
        &mut self,
        name: &str,
        location: &Span,
    ) -> Result<TableId, Box<SleighError>> {
        //TODO check if creating the table is always required, or we can create
        //it only if a contructor is added to it. Or just remove empty tables
        //and check the Rc counts before converting into the final sleigh struct
        match self.global_scope.get(name) {
            Some(GlobalScope::Table(id)) => Ok(*id),
            Some(_) => Err(Box::new(SleighError::new_table(
                location.clone(),
                TableError::TableNameInvalid,
            ))),
            None => {
                let table = Table::new_empty(false, name.to_owned());
                self.tables.push(table);
                let table_id = TableId(self.tables.len() - 1);
                self.global_scope
                    .insert(name.to_owned(), GlobalScope::Table(table_id));
                Ok(table_id)
            }
        }
    }
}