rsleigh 0.4.0

SLEIGH (.slaspec) parser and Rust decoder/P-code emitter codegen — Ghidra-compatible disassembly in pure Rust
Documentation
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
use crate::semantic::disassembly::VariableId;
use crate::semantic::{ContextId, Sleigh as FinalSleigh, TableId, TokenFieldId};
use crate::{field_in_be, field_in_le, value_in_context, value_in_token, NumberUnsigned, Span};

use super::disassembly::{Assertation, Expr, Variable};
use super::InstStart;

/// Represent how a bit is limited in a pattern
#[derive(Clone, Copy, Debug, Default)]
pub enum BitConstraint {
    //can have any value
    #[default]
    Unrestrained,
    //only one value possible 0->false, 1->true
    Defined(bool),
    //the value is limited depending on other bits.
    Restrained,
}

impl BitConstraint {
    pub fn value(&self) -> Option<bool> {
        match self {
            Self::Defined(value) => Some(*value),
            _ => None,
        }
    }
    pub fn restrained(&self) -> bool {
        match self {
            BitConstraint::Unrestrained => false,
            BitConstraint::Defined(_) | BitConstraint::Restrained => true,
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PatternLen {
    Defined(NumberUnsigned),
    Range {
        min: NumberUnsigned,
        max: NumberUnsigned,
    },
    Min(NumberUnsigned),
}

impl PatternLen {
    /// new range/defined len for a pattern
    pub fn new(min: NumberUnsigned, max: NumberUnsigned) -> Self {
        match min.cmp(&max) {
            std::cmp::Ordering::Greater => {
                unreachable!("PatternLen min({}) > max({})", min, max)
            }
            std::cmp::Ordering::Equal => Self::Defined(min),
            std::cmp::Ordering::Less => Self::Range { min, max },
        }
    }
    /// is this pattern can contain itself and grown to infinite
    pub fn is_recursive(&self) -> bool {
        matches!(self, Self::Min(_))
    }
    /// min size of the token this patterns requires to match
    pub fn min(&self) -> NumberUnsigned {
        match self {
            Self::Min(min) | Self::Defined(min) | Self::Range { min, max: _ } => *min,
        }
    }
    /// max size of the token this patterns requires to match
    pub fn max(&self) -> Option<NumberUnsigned> {
        match self {
            Self::Defined(value) => Some(*value),
            Self::Range { min: _, max } => Some(*max),
            Self::Min(_) => None,
        }
    }
    /// token size, if this pattern is non-growing
    pub fn single_len(&self) -> Option<NumberUnsigned> {
        match self {
            Self::Defined(value) => Some(*value),
            Self::Min(_) | Self::Range { .. } => None,
        }
    }
    /// create a new range calculated from the concatenation of the two original
    /// ranges
    pub fn concat(self, other: Self) -> Self {
        match (self, other) {
            (Self::Defined(x), Self::Defined(y)) => Self::Defined(x + y),
            (
                Self::Defined(ix @ ax) | Self::Range { min: ix, max: ax },
                Self::Defined(iy @ ay) | Self::Range { min: iy, max: ay },
            ) => {
                let min = ix + iy;
                let max = ax + ay;
                Self::new(min, max)
            }
            (
                Self::Min(x) | Self::Defined(x) | Self::Range { min: x, .. },
                Self::Min(y) | Self::Defined(y) | Self::Range { min: y, .. },
            ) => Self::Min(x + y),
        }
    }
    /// creates a new range that is the greater of the two
    pub(crate) fn greater(self, other: Self) -> Self {
        match (self, other) {
            (Self::Defined(x), Self::Defined(y)) => Self::Defined(x.max(y)),
            (
                Self::Defined(ix @ ax) | Self::Range { min: ix, max: ax },
                Self::Defined(iy @ ay) | Self::Range { min: iy, max: ay },
            ) => {
                let min = ix.max(iy);
                let max = ax.max(ay);
                Self::new(min, max)
            }
            (
                Self::Min(x) | Self::Defined(x) | Self::Range { min: x, .. },
                Self::Min(y) | Self::Defined(y) | Self::Range { min: y, .. },
            ) => Self::Min(x.max(y)),
        }
    }
    /// creates a new range that only include the intersection of the orignal
    /// ranges
    pub(crate) fn intersection(self, other: Self) -> Self {
        match (self, other) {
            (
                Self::Defined(ix @ ax) | Self::Range { min: ix, max: ax },
                Self::Defined(iy @ ay) | Self::Range { min: iy, max: ay },
            ) => {
                let min = ix.min(iy);
                let max = ax.max(ay);
                Self::new(min, max)
            }
            (
                Self::Min(x) | Self::Defined(x) | Self::Range { min: x, .. },
                Self::Min(y) | Self::Defined(y) | Self::Range { min: y, .. },
            ) => Self::Min(x.min(y)),
        }
    }
}

#[derive(Clone, Debug)]
pub struct ProducedTable {
    pub table: TableId,
    pub location: Span,
    pub always: bool,
    pub recursive: bool,
}

#[derive(Clone, Debug)]
pub struct ProducedTokenField {
    pub field: TokenFieldId,
    //if this field is produced explicitly (on pattern) or implicitly deduced
    //the existence of by the use of it in a desassembly/execution
    pub source: Option<Span>,
    pub local: bool,
}

#[derive(Clone, Debug)]
pub struct Pattern {
    pub len: PatternLen,
    pub disassembly_vars: Box<[Variable]>,
    pub blocks: Box<[Block]>,
    pub pos: Box<[Assertation]>,
}

impl Pattern {
    pub fn blocks(&self) -> &[Block] {
        &self.blocks
    }

    pub fn disassembly_var(&self, id: VariableId) -> &Variable {
        &self.disassembly_vars[id.0]
    }

    pub fn disassembly_vars(&self) -> &[Variable] {
        &self.disassembly_vars
    }

    pub fn disassembly_pos_match(&self) -> &[Assertation] {
        &self.pos
    }

    pub fn produced_tables(&self) -> impl Iterator<Item = &ProducedTable> {
        self.blocks().iter().flat_map(Block::tables)
    }

    pub fn produced_token_fields(&self) -> impl Iterator<Item = &ProducedTokenField> {
        self.blocks()
            .iter()
            .flat_map(|block| block.token_fields().iter())
    }

    pub fn variants_num(&self) -> usize {
        self.blocks
            .iter()
            .fold(1, |acc, block| acc * block.variants_number())
    }

    pub fn bits_produced(&self) -> usize {
        let len = self.len.single_len().unwrap_or_else(|| self.len.min());
        usize::try_from(len).unwrap() * 8
    }

    pub(crate) fn constraint(
        &self,
        sleigh: &FinalSleigh,
        variant_id: usize,
        context: &mut [BitConstraint],
        constraint: &mut [BitConstraint],
    ) -> Option<()> {
        let mut current = constraint;
        for block in self.blocks.iter() {
            block.constraint(sleigh, variant_id, context, current);
            let next_offset = block.bits_produced();
            current = &mut current[next_offset..];
        }
        Some(())
    }

    /// the bit pattern for all variants.
    pub(crate) fn pattern_bits_variants<'a>(
        &'a self,
        sleigh: &'a FinalSleigh,
    ) -> impl Iterator<Item = (usize, Vec<BitConstraint>, Vec<BitConstraint>)> + 'a {
        let context_bits = usize::try_from(sleigh.context_memory.memory_bits).unwrap();
        let pattern_bits = self.bits_produced();
        let mut context_buf = vec![BitConstraint::default(); context_bits];
        let mut pattern_buf = vec![BitConstraint::default(); pattern_bits];
        (0..self.variants_num()).filter_map(move |i| {
            context_buf.fill(BitConstraint::default());
            pattern_buf.fill(BitConstraint::default());
            self.constraint(sleigh, i, &mut context_buf, &mut pattern_buf)?;
            Some((i, context_buf.clone(), pattern_buf.clone()))
        })
    }
}

#[derive(Clone, Debug)]
pub enum Block {
    And {
        len: PatternLen,
        token_fields: Box<[ProducedTokenField]>,
        tables: Box<[ProducedTable]>,
        verifications: Box<[Verification]>,
        pre: Box<[Assertation]>,
        pos: Box<[Assertation]>,
        /// zero means it's the first in the chain
        variants_prior: usize,
        /// number of variants this block produce
        variants_number: usize,
    },
    //TODO `OR` block can produce token_fields?
    Or {
        len: PatternLen,
        token_fields: Box<[ProducedTokenField]>,
        tables: Box<[ProducedTable]>,
        branches: Box<[Verification]>,
        pos: Box<[Assertation]>,
        /// zero means it's the first in the chain
        variants_prior: usize,
        /// number of variants this block produce
        variants_number: usize,
    },
}
impl Block {
    pub fn tables(&self) -> &[ProducedTable] {
        match self {
            Block::And { tables, .. } | Block::Or { tables, .. } => tables,
        }
    }
    pub fn token_fields(&self) -> &[ProducedTokenField] {
        match self {
            Block::And { token_fields, .. } | Block::Or { token_fields, .. } => token_fields,
        }
    }
    pub fn len(&self) -> PatternLen {
        match self {
            Block::And { len, .. } | Block::Or { len, .. } => *len,
        }
    }
    pub fn pre_disassembler(&self) -> &[Assertation] {
        match self {
            Block::And { pre, .. } => pre,
            Block::Or { .. } => &[],
        }
    }
    pub fn post_disassembler(&self) -> &[Assertation] {
        match self {
            Block::And { pos, .. } | Block::Or { pos, .. } => pos,
        }
    }
    pub fn verifications(&self) -> &[Verification] {
        match self {
            Block::And { verifications, .. }
            | Block::Or {
                branches: verifications,
                ..
            } => verifications,
        }
    }

    pub fn variants_prior(&self) -> usize {
        match self {
            Block::And { variants_prior, .. } | Block::Or { variants_prior, .. } => *variants_prior,
        }
    }
    pub fn variants_number(&self) -> usize {
        match self {
            Block::And {
                variants_number, ..
            }
            | Block::Or {
                variants_number, ..
            } => *variants_number,
        }
    }

    fn bits_produced(&self) -> usize {
        let len = self.len().single_len().unwrap_or_else(|| self.len().min());
        usize::try_from(len).unwrap() * 8
    }

    fn constraint(
        &self,
        sleigh: &FinalSleigh,
        variant_id: usize,
        context_bits: &mut [BitConstraint],
        constraint_bits: &mut [BitConstraint],
    ) -> Option<()> {
        match self {
            Self::And { verifications, .. } => {
                for verification in verifications.iter() {
                    apply_verification(
                        sleigh,
                        variant_id,
                        verification,
                        context_bits,
                        constraint_bits,
                    )?;
                }
                Some(())
            }
            Self::Or {
                branches,
                variants_prior,
                variants_number,
                ..
            } => {
                //find the correct verification in the OR to constraint
                let mut verification_id = (variant_id / variants_prior) % variants_number;
                for branch in branches.iter() {
                    let verification_variants = branch.variants_number();
                    if verification_id < verification_variants {
                        return apply_verification(
                            sleigh,
                            variant_id,
                            branch,
                            context_bits,
                            constraint_bits,
                        );
                    }
                    verification_id -= verification_variants;
                }
                unreachable!()
            }
        }
    }
}

fn apply_verification(
    sleigh: &FinalSleigh,
    variant_id: usize,
    verification: &Verification,
    context_bits: &mut [BitConstraint],
    constraint_bits: &mut [BitConstraint],
) -> Option<()> {
    match verification {
        Verification::TableBuild { .. } => Some(()),
        Verification::ContextCheck { context, op, value } => {
            let bits = sleigh.context_memory.context(*context);
            super::inner::pattern::apply_value(
                context_bits,
                field_in_le,
                value_in_context,
                bits,
                *op,
                value,
            )
        }
        Verification::TokenFieldCheck { field, op, value } => {
            let token_field = sleigh.token_field(*field);
            let token = sleigh.token(token_field.token);
            let token_len: usize = token.len_bytes.get().try_into().unwrap();
            let bit_order = match token.endian {
                crate::Endian::Little => field_in_le,
                crate::Endian::Big => field_in_be,
            };
            super::inner::pattern::apply_value(
                &mut constraint_bits[0..token_len * 8],
                bit_order,
                value_in_token,
                token_field.bits.clone(),
                *op,
                value,
            )
        }
        Verification::SubPattern { pattern, .. } => {
            pattern.constraint(sleigh, variant_id, context_bits, constraint_bits)
        }
    }
}

#[derive(Clone, Debug)]
pub enum Verification {
    ContextCheck {
        context: ContextId,
        op: CmpOp,
        value: ConstraintValue,
    },
    TableBuild {
        produced_table: ProducedTable,
        verification: Option<(CmpOp, ConstraintValue)>,
    },
    TokenFieldCheck {
        field: TokenFieldId,
        op: CmpOp,
        value: ConstraintValue,
    },
    SubPattern {
        location: Span,
        pattern: Pattern,
    },
}
impl Verification {
    pub fn variants_number(&self) -> usize {
        match self {
            Self::SubPattern { pattern, .. } => pattern.variants_num(),
            _ => 1,
        }
    }
}

#[derive(Clone, Debug)]
pub enum ConstraintField {
    TokenField(TokenFieldId),
    Context(ContextId),
    InstStart(InstStart),
    Table(TableId),
}

#[derive(Clone, Debug)]
pub struct ConstraintValue {
    expr: Expr,
}

impl ConstraintValue {
    pub(crate) fn new(expr: Expr) -> Self {
        Self { expr }
    }
    pub fn expr(&self) -> &Expr {
        &self.expr
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum CmpOp {
    Eq,
    Ne,
    Lt,
    Gt,
    Le,
    Ge,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Ellipsis {
    Left,
    Right,
}