liblisa 0.1.4

A tool for automated discovery and analysis of the ISA of a CPU.
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
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
561
562
563
564
565
566
567
568
use std::fmt::{Debug, Display};

use itertools::Itertools;
use serde::{Deserialize, Serialize};

use crate::arch::{Arch, Register};
use crate::state::{Location, LocationKind};
use crate::value::ValueType;

/// A range of bytes.
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct Size {
    /// The lowest index included in the range.
    pub start_byte: usize,

    /// The highest index included in the range.
    pub end_byte: usize,
}

impl Default for Size {
    fn default() -> Self {
        Size {
            start_byte: usize::MIN,
            end_byte: usize::MAX,
        }
    }
}

impl Size {
    /// Creates a new range from the provided values.
    #[inline]
    pub const fn new(start_byte: usize, end_byte: usize) -> Self {
        Size {
            start_byte,
            end_byte,
        }
    }

    /// Returns `Size::new(0, 7)`.
    #[inline]
    pub const fn qword() -> Self {
        Size {
            start_byte: 0,
            end_byte: 7,
        }
    }

    /// Returns a union of the two ranges.
    #[inline]
    pub fn union(&self, other: &Self) -> Self {
        Size {
            start_byte: self.start_byte.min(other.start_byte),
            end_byte: self.end_byte.max(other.end_byte),
        }
    }

    /// Returns true if the size contains `other`.
    #[inline]
    pub fn contains(&self, other: &Self) -> bool {
        self.start_byte <= other.start_byte && self.end_byte >= other.end_byte
    }

    /// Returns true if the size and `other` overlap.
    #[inline]
    pub fn overlaps(&self, other: &Self) -> bool {
        self.end_byte >= other.start_byte && self.start_byte <= other.end_byte
    }

    /// Returns the overlapping area between the two sizes.
    /// If the two sizes do not overlap, returns None.
    #[inline]
    pub fn overlapping_area(&self, other: Size) -> Option<Size> {
        let start_byte = self.start_byte.max(other.start_byte);
        let end_byte = self.end_byte.min(other.end_byte);

        if start_byte <= end_byte {
            Some(Size {
                start_byte,
                end_byte,
            })
        } else {
            None
        }
    }

    /// Computes the union of the two sizes, and splits it by overlapping area.
    /// Returns None is the sizes do not overlap.
    ///
    /// Returns a triple of `(before, overlapping, after)`.
    /// `before` is the area in the union < `overlapping`.
    /// `overlapping` is the overlapping area between the two sizes.
    /// `after` is the area in the union > `overlapping`.
    pub fn split_by_overlap(&self, existing_size: Size) -> Option<(Option<Size>, Size, Option<Size>)> {
        let union = self.union(&existing_size);
        self.overlapping_area(existing_size).map(|overlapping| {
            let before = if overlapping.start_byte > union.start_byte {
                Some(Size::new(union.start_byte, overlapping.start_byte - 1))
            } else {
                None
            };

            let after = if overlapping.end_byte < union.end_byte {
                Some(Size::new(overlapping.end_byte + 1, union.end_byte))
            } else {
                None
            };

            (before, overlapping, after)
        })
    }

    /// The number of bytes in the size.
    #[inline]
    pub fn num_bytes(&self) -> usize {
        (self.end_byte - self.start_byte) + 1
    }

    /// If the size and `other` occur consecutively one after the other, returns a size that contains both.
    #[inline]
    pub fn try_glue(&self, other: &Size) -> Option<Size> {
        Some(if self.end_byte + 1 == other.start_byte {
            Size::new(self.start_byte, other.end_byte)
        } else if self.start_byte == other.end_byte + 1 {
            Size::new(other.start_byte, self.end_byte)
        } else {
            return None
        })
    }

    /// Crops `mask` to only have bits set in the area described by this size.
    #[inline]
    pub fn select_mask(&self, mask: Option<u64>) -> Option<u64> {
        mask.map(|mask| {
            let shift = self.start_byte * 8;
            let select = (1 << (self.num_bytes() * 8)) - 1;

            (mask >> shift) & select
        })
    }

    /// Returns an iterator that yields all byte indices in this size
    pub fn iter_byte_indices(&self) -> impl Iterator<Item = usize> {
        self.start_byte..=self.end_byte
    }
}

impl Debug for Size {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if self.end_byte < self.start_byte {
            write!(f, "-")
        } else {
            write!(f, "{}..{}", self.start_byte, self.end_byte)
        }
    }
}

/// A destination in a dataflow.
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(
    feature = "schemars",
    schemars(bound = "A: schemars::JsonSchema, A::Reg: schemars::JsonSchema")
)]
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(bound(serialize = "", deserialize = ""))]
pub enum Dest<A: Arch> {
    /// A specific area of a register.
    Reg(A::Reg, Size),

    /// A specific area of a memory access.
    Mem(usize, Size),
}

/// A source in a dataflow
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(
    feature = "schemars",
    schemars(bound = "A: schemars::JsonSchema, A::Reg: schemars::JsonSchema")
)]
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(bound(serialize = "", deserialize = ""))]
pub enum Source<A: Arch> {
    /// A destination.
    Dest(Dest<A>),

    /// The value of an immediate
    Imm(usize),

    /// A constant value.
    Const {
        /// The actual constant value.
        /// Should not have any bits above `num_bits` set.
        value: u64,

        /// The bitsize of the constant value.
        num_bits: usize,
    },
}

impl<A: Arch> PartialEq<Location<A>> for Dest<A> {
    fn eq(&self, other: &Location<A>) -> bool {
        match (self, other) {
            (Dest::Reg(a, _), Location::Reg(b)) => a == b,
            (Dest::Mem(a, _), Location::Memory(b)) => a == b,
            _ => false,
        }
    }
}

impl<R: Register, A: Arch<Reg = R>> PartialEq<R> for Dest<A> {
    fn eq(&self, other: &R) -> bool {
        match self {
            Dest::Reg(reg, _) => reg == other,
            _ => false,
        }
    }
}

impl<A: Arch> PartialEq<Location<A>> for Source<A> {
    fn eq(&self, other: &Location<A>) -> bool {
        match self {
            Source::Dest(d) => d == other,
            Source::Imm(_)
            | Source::Const {
                ..
            } => false,
        }
    }
}

impl<R: Register, A: Arch<Reg = R>> PartialEq<R> for Source<A> {
    fn eq(&self, other: &R) -> bool {
        match self {
            Source::Dest(Dest::Reg(reg, _)) => reg == other,
            Source::Imm(_)
            | Source::Const {
                ..
            }
            | Source::Dest {
                ..
            } => false,
        }
    }
}

impl<A: Arch> Dest<A> {
    /// The size of the destination.
    #[inline]
    pub fn size(&self) -> Size {
        match self {
            Dest::Reg(_, size) | Dest::Mem(_, size) => *size,
        }
    }

    /// Replaces the size of the destination with the provided `size`.
    #[inline]
    pub fn with_size(&self, size: Size) -> Self {
        match self {
            Dest::Reg(r, _) => Dest::Reg(*r, size),
            Dest::Mem(a, _) => Dest::Mem(*a, size),
        }
    }

    /// Returns true if the destination fully contains `other`.
    #[inline]
    pub fn contains(&self, other: &Dest<A>) -> bool {
        match (self, other) {
            (Dest::Reg(r1, s1), Dest::Reg(r2, s2)) => r1 == r2 && s1.contains(s2),
            (Dest::Mem(m1, s1), Dest::Mem(m2, s2)) => m1 == m2 && s1.contains(s2),
            _ => false,
        }
    }

    /// Returns true if the destination is a flags register.
    #[inline]
    pub fn is_flags(&self) -> bool {
        match self {
            Dest::Reg(reg, _) => reg.is_flags(),
            Dest::Mem(..) => false,
        }
    }

    /// Returns the mask of the destination, if it has one.
    /// The value of the destination must always be masked with the mask before setting it.
    #[inline]
    pub fn mask(&self) -> Option<u64> {
        match self {
            Dest::Reg(reg, size) => reg
                .mask()
                .map(|m| (m >> (size.start_byte * 8)) & (u64::MAX >> (64 - size.num_bytes() * 8))),
            _ => None,
        }
    }

    /// Returns the [`LocationKind`] of the destination.
    #[inline]
    pub fn kind(&self) -> LocationKind {
        match self {
            Dest::Reg(..) => LocationKind::Reg,
            Dest::Mem(..) => LocationKind::Memory,
        }
    }

    /// Returns true if the destination (partially or completely) overlaps with `other`.
    /// Destinations overlap if both destinations are the same register or the same memory access, and the size overlaps.
    pub fn overlaps(&self, other: &Dest<A>) -> bool {
        self.overlapping_area(other).is_some()
    }

    /// Returns the overlapping area between this destination and `other`.
    /// Returns `None` if there is no overlap.
    pub fn overlapping_area(&self, other: &Dest<A>) -> Option<Size> {
        match (self, other) {
            (Dest::Reg(reg_a, size_a), Dest::Reg(reg_b, size_b)) if reg_a == reg_b => size_a.overlapping_area(*size_b),
            (Dest::Mem(index_a, size_a), Dest::Mem(index_b, size_b)) if index_a == index_b => size_a.overlapping_area(*size_b),
            _ => None,
        }
    }

    /// Returns the [`ValueType`] of the destination.
    pub fn value_type(&self) -> ValueType {
        match self {
            Dest::Reg(reg, size) => match reg.reg_type() {
                ValueType::Num => ValueType::Num,
                ValueType::Bytes(_) => ValueType::Bytes(size.num_bytes()),
            },
            Dest::Mem(_, size) => ValueType::Bytes(size.num_bytes()),
        }
    }
}

impl<A: Arch> Source<A> {
    /// Returns the size of the source, if it has one.
    #[inline]
    pub fn size(&self) -> Option<Size> {
        match self {
            Source::Dest(d) => Some(d.size()),
            Source::Const {
                num_bits, ..
            } => Some(Size::new(0, (num_bits - 1) / 8)),
            Source::Imm(_) => None,
        }
    }

    /// Returns true if this source fully contains `other`.
    #[inline]
    pub fn contains(&self, other: &Source<A>) -> bool {
        match (self, other) {
            (Source::Dest(a), Source::Dest(b)) => a.contains(b),
            _ => false,
        }
    }

    /// Returns the destination if this source is a `Source::Dest`.
    /// Panics otherwise.
    #[inline]
    pub fn unwrap_dest(self) -> Dest<A> {
        match self {
            Source::Dest(d) => d,
            other => panic!("Tried to unwrap a Dest in {other:?}, but it contains no Dest"),
        }
    }

    /// Returns true if the source is a flags register.
    #[inline]
    pub fn is_flags(&self) -> bool {
        match self {
            Source::Dest(d) => d.is_flags(),
            _ => false,
        }
    }

    /// Returns the mask of the source.
    /// See [`Dest::mask`].
    #[inline]
    pub fn mask(&self) -> Option<u64> {
        match self {
            Source::Dest(d) => d.mask(),
            _ => panic!(),
        }
    }

    /// Returns true if the value of this source can be changed in the CPU state.
    /// Returns false if the value of this source is a constant or an immediate value from the instruction bitstring.
    #[inline]
    pub fn can_modify(&self) -> bool {
        match self {
            Source::Dest(_) => true,
            Source::Imm(_)
            | Source::Const {
                ..
            } => false,
        }
    }

    /// Replaces the size of this source with `size`, if possible.
    /// If the source has no size, the original source is returned.
    pub fn with_size(&self, size: Size) -> Source<A> {
        match self {
            Source::Dest(d) => Source::Dest(d.with_size(size)),
            other => *other,
        }
    }
}

impl<A: Arch> Debug for Dest<A> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Dest::Reg(reg, size) => write!(f, "Reg({reg})[{size:?}]"),
            Dest::Mem(index, size) => write!(f, "Mem{index}[{size:?}]"),
        }
    }
}

impl<A: Arch> Debug for Source<A> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Source::Imm(index) => write!(f, "v{index}"),
            Source::Dest(d) => write!(f, "{d:?}"),
            Source::Const {
                value, ..
            } => write!(f, "0x{value:X}"),
        }
    }
}

impl<A: Arch> Display for Dest<A> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Dest::Reg(reg, size) => {
                if reg.is_flags() && size.num_bytes() == 1 {
                    let flags = A::flagreg_to_flags(*reg, size.start_byte, size.end_byte);
                    write!(f, "Flag({})", flags.iter().map(|f| f.to_string()).join(", "))
                } else {
                    write!(f, "Reg({reg})[{size:?}]")
                }
            },
            Dest::Mem(index, size) => write!(f, "Mem{index}[{size:?}]"),
        }
    }
}

impl<A: Arch> Display for Source<A> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Source::Imm(index) => write!(f, "v{index}"),
            Source::Dest(d) => write!(f, "{d}"),
            Source::Const {
                value, ..
            } => write!(f, "0x{value:X}"),
        }
    }
}

impl<A: Arch> From<Dest<A>> for Source<A> {
    #[inline]
    fn from(d: Dest<A>) -> Source<A> {
        Source::Dest(d)
    }
}

impl<A: Arch> TryFrom<Source<A>> for Dest<A> {
    type Error = ();

    #[inline]
    fn try_from(s: Source<A>) -> Result<Dest<A>, ()> {
        match s {
            Source::Dest(d) => Ok(d),
            _ => Err(()),
        }
    }
}

impl<A: Arch> From<Location<A>> for Dest<A> {
    #[inline]
    fn from(location: Location<A>) -> Dest<A> {
        match location {
            Location::Reg(reg) => Dest::Reg(reg, Size::new(0, reg.byte_size() - 1)),
            Location::Memory(index) => Dest::Mem(index, Size::qword()),
        }
    }
}

impl<A: Arch> From<Dest<A>> for Location<A> {
    #[inline]
    fn from(dest: Dest<A>) -> Location<A> {
        match dest {
            Dest::Reg(reg, _) => Location::Reg(reg),
            Dest::Mem(index, _) => Location::Memory(index),
        }
    }
}

impl<A: Arch> From<&Source<A>> for Option<Location<A>> {
    #[inline]
    fn from(v: &Source<A>) -> Self {
        Location::try_from(v).ok()
    }
}

/// Types that implement this trait can be converted into destinations, given a size.
pub trait IntoDestWithSize<A: Arch> {
    /// Converts `self` into a [`Dest`].
    fn into_dest_with_size(self, size: Size) -> Dest<A>;
}

/// Types that implement this trait can be converted into sources, given a size.
pub trait IntoSourceWithSize<A: Arch> {
    /// Converts `self` into a [`Source`].
    fn into_source_with_size(self, size: Size) -> Source<A>;
}

impl<A: Arch> IntoDestWithSize<A> for Location<A> {
    #[inline]
    fn into_dest_with_size(self, size: Size) -> Dest<A> {
        match self {
            Location::Reg(reg) => Dest::Reg(reg, size),
            Location::Memory(index) => Dest::Mem(index, size),
        }
    }
}

impl<A: Arch> IntoSourceWithSize<A> for Location<A> {
    #[inline]
    fn into_source_with_size(self, size: Size) -> Source<A> {
        Source::Dest(self.into_dest_with_size(size))
    }
}

impl<A: Arch> From<&Dest<A>> for Location<A> {
    #[inline]
    fn from(dest: &Dest<A>) -> Self {
        match dest {
            Dest::Reg(reg, _) => Location::Reg(*reg),
            Dest::Mem(index, _) => Location::Memory(*index),
        }
    }
}

impl<A: Arch> TryFrom<&Source<A>> for Location<A> {
    type Error = ();

    #[inline]
    fn try_from(source: &Source<A>) -> Result<Self, Self::Error> {
        match source {
            Source::Dest(d) => Ok(d.into()),
            Source::Imm(_)
            | Source::Const {
                ..
            } => Err(()),
        }
    }
}

#[cfg(test)]
mod test {
    use crate::encoding::dataflows::Size;

    #[test]
    pub fn size_contains() {
        assert!(Size::new(0, 7).contains(&Size::new(0, 5)));
        assert!(Size::new(0, 7).contains(&Size::new(0, 7)));
        assert!(Size::new(0, 7).contains(&Size::new(0, 6)));
        assert!(!Size::new(0, 7).contains(&Size::new(0, 8)));
        assert!(Size::new(0, 7).contains(&Size::new(1, 7)));
        assert!(!Size::new(1, 7).contains(&Size::new(0, 7)));
    }
}