binbun 0.1.0

Disassembler and decompiler engine.
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
//! The user's corrections of what recovery found ([`Corrections`]), as
//! the usual disassembler commands make them: code at an address (`C`),
//! bytes undefined (`U`), data of a width or type (`D`, `A`, `*`), a
//! function at an address (`P`), a function's end (Edit Function) and a
//! function deleted.
//!
//! They are inputs to recovery, never edits of its result: a database
//! recovered with them ([`crate::Database::with_corrections`]) takes them
//! as seeds and constraints ([`Corrections::seeds`]: [`Seed::Flow`],
//! [`Seed::NotCode`], [`Seed::Function`] of [`SeedKind::User`],
//! [`Seed::End`], [`Seed::NoFunction`]), and with none it is the database
//! recovered without. The listing reads the data items
//! ([`Corrections::items`]); [`check_item`] and [`check_bounds`] say why one
//! cannot apply to a program as recovered.

use std::collections::{BTreeMap, BTreeSet};

use binbun_aarch64::{AArch64, Mode as AArch64Mode};
use binbun_isa::{Architecture as _, Instruction as _};
use binbun_loader::{Image, Machine};
use binbun_semantics::core::Memory as _;
use binbun_x86::X86;

use crate::analysis::x86_mode;
use crate::cfg::{Program, Seed, SeedKind};
use crate::strings::{Encoding, text_at};

/// What the units of a data item are.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Unit {
    /// One byte (`db`).
    Byte,
    /// Two bytes (`dw`).
    Word,
    /// Four bytes (`dd`).
    Dword,
    /// Eight bytes (`dq`).
    Qword,
    /// A C string's bytes, its NUL the last.
    String,
    /// A UTF-16 string's units, its NUL the last.
    Utf16,
}

impl Unit {
    /// Its size in bytes.
    pub const fn width(self) -> u64 {
        match self {
            Self::Byte | Self::String => 1,
            Self::Word | Self::Utf16 => 2,
            Self::Dword => 4,
            Self::Qword => 8,
        }
    }

    /// Its name in a project file.
    pub const fn name(self) -> &'static str {
        match self {
            Self::Byte => "byte",
            Self::Word => "word",
            Self::Dword => "dword",
            Self::Qword => "qword",
            Self::String => "string",
            Self::Utf16 => "utf16",
        }
    }

    /// The unit `name` names ([`Self::name`]).
    pub fn named(name: &str) -> Option<Self> {
        [
            Self::Byte,
            Self::Word,
            Self::Dword,
            Self::Qword,
            Self::String,
            Self::Utf16,
        ]
        .into_iter()
        .find(|unit| unit.name() == name)
    }

    /// Whether it is a string's.
    pub const fn is_string(self) -> bool {
        matches!(self, Self::String | Self::Utf16)
    }

    /// The width `D` goes to from this one: byte, word, dword, qword,
    /// byte again (a string's goes to byte).
    #[must_use]
    pub const fn cycled(self) -> Self {
        match self {
            Self::Byte => Self::Word,
            Self::Word => Self::Dword,
            Self::Dword => Self::Qword,
            Self::Qword | Self::String | Self::Utf16 => Self::Byte,
        }
    }
}

/// A data item: `count` units (an array if more than one; a string's
/// units, its NUL included).
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Data {
    /// Its units.
    pub unit: Unit,
    /// How many.
    pub count: u64,
}

impl Data {
    /// Its size in bytes.
    pub const fn size(self) -> u64 {
        self.unit.width().saturating_mul(self.count)
    }

    /// The string `bytes` (at `address`) start with, C or UTF-16 by what
    /// the bytes are (as a referred-to literal is read), its NUL included;
    /// `None` if they start no text ending in a NUL.
    pub fn string(address: u64, bytes: &[u8]) -> Option<Self> {
        let (encoding, _, length) = text_at(address, bytes)?;
        Some(match encoding {
            Encoding::Utf8 => Self {
                unit: Unit::String,
                count: length + 1,
            },
            Encoding::Utf16 => Self {
                unit: Unit::Utf16,
                count: length / 2 + 1,
            },
        })
    }
}

/// What the user says the bytes at an address are.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Item {
    /// Code, decoded and followed from there (`C`).
    Code,
    /// Bytes nothing types (`U`): no code, no data item.
    Undefined {
        /// How many.
        size: u64,
    },
    /// A data item (`D`, `A`, `*`).
    Data(Data),
}

impl Item {
    /// The bytes it covers (code: its first byte; what follows is what the
    /// code decodes to).
    pub const fn size(self) -> u64 {
        match self {
            Self::Code => 1,
            Self::Undefined { size } => size,
            Self::Data(data) => data.size(),
        }
    }
}

/// What the user says of a function.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Bounds {
    /// A function starts here (`P`), ending at `end` if given (Edit
    /// Function): control leaving its range is a tail call.
    Function {
        /// The address after its last byte.
        end: Option<u64>,
    },
    /// No function starts here (Delete Function); its code stays code.
    Deleted,
}

/// The user's corrections: items by address (their bytes disjoint),
/// functions' bounds by entry, and the functions known not to return
/// (a library function a signature identified, whose declaration says
/// so).
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Corrections {
    items: BTreeMap<u64, Item>,
    functions: BTreeMap<u64, Bounds>,
    no_return: BTreeSet<u64>,
}

impl Corrections {
    /// Whether there are none.
    pub fn is_empty(&self) -> bool {
        self.items.is_empty() && self.functions.is_empty() && self.no_return.is_empty()
    }

    /// The items, by address.
    pub fn items(&self) -> &BTreeMap<u64, Item> {
        &self.items
    }

    /// The functions' bounds, by entry.
    pub fn functions(&self) -> &BTreeMap<u64, Bounds> {
        &self.functions
    }

    /// The item starting at `address`.
    pub fn item(&self, address: u64) -> Option<Item> {
        self.items.get(&address).copied()
    }

    /// The item holding `address`, and its start.
    pub fn holding(&self, address: u64) -> Option<(u64, Item)> {
        let (&start, &item) = self.items.range(..=address).next_back()?;
        (address < start.saturating_add(item.size())).then_some((start, item))
    }

    /// The bounds of the function at `entry`.
    pub fn bounds(&self, entry: u64) -> Option<Bounds> {
        self.functions.get(&entry).copied()
    }

    /// The entries of the functions known not to return.
    pub fn no_return(&self) -> &BTreeSet<u64> {
        &self.no_return
    }

    /// Says whether the function at `entry` is known not to return.
    pub fn set_no_return(&mut self, entry: u64, no_return: bool) {
        if no_return {
            self.no_return.insert(entry);
        } else {
            self.no_return.remove(&entry);
        }
    }

    /// Says the bytes at `address` are `item` in place of the items whose
    /// bytes it overlaps (`None`: no longer the one starting there).
    pub fn set_item(&mut self, address: u64, item: Option<Item>) {
        let Some(item) = item else {
            self.items.remove(&address);
            return;
        };
        let end = address.saturating_add(item.size());
        // Disjoint items end in the order they start: those ending after
        // `address`, from the last before `end` back, overlap it.
        let overlapping: Vec<u64> = self
            .items
            .range(..end)
            .rev()
            .take_while(|&(&start, &other)| start.saturating_add(other.size()) > address)
            .map(|(&start, _)| start)
            .collect();
        for start in overlapping {
            self.items.remove(&start);
        }
        self.items.insert(address, item);
    }

    /// Says `bounds` of the function at `entry` (`None`: nothing).
    pub fn set_bounds(&mut self, entry: u64, bounds: Option<Bounds>) {
        match bounds {
            Some(bounds) => self.functions.insert(entry, bounds),
            None => self.functions.remove(&entry),
        };
    }

    /// The bytes no instruction may overlap: the items undefined and the
    /// data items, as `(start, end)` by start.
    pub fn not_code(&self) -> impl Iterator<Item = (u64, u64)> + '_ {
        self.items.iter().filter_map(|(&start, &item)| match item {
            Item::Code => None,
            Item::Undefined { .. } | Item::Data(_) => {
                Some((start, start.saturating_add(item.size())))
            }
        })
    }

    /// Recovery's seeds for them: [`Seed::Flow`] for code,
    /// [`Seed::NotCode`] for bytes undefined or data, [`Seed::Function`]
    /// ([`SeedKind::User`]) and [`Seed::End`] for a function, and
    /// [`Seed::NoFunction`] with [`Seed::Flow`] for one deleted (its code
    /// stays code).
    pub fn seeds(&self) -> Vec<Seed> {
        let mut seeds = Vec::new();
        for (&start, &item) in &self.items {
            seeds.push(match item {
                Item::Code => Seed::Flow(start),
                Item::Undefined { .. } | Item::Data(_) => Seed::NotCode {
                    start,
                    end: start.saturating_add(item.size()),
                },
            });
        }
        for (&entry, &bounds) in &self.functions {
            match bounds {
                Bounds::Function { end } => {
                    seeds.push(Seed::Function(entry, SeedKind::User));
                    if let Some(end) = end {
                        seeds.push(Seed::End {
                            function: entry,
                            end,
                        });
                    }
                }
                Bounds::Deleted => {
                    seeds.push(Seed::NoFunction(entry));
                    seeds.push(Seed::Flow(entry));
                }
            }
        }
        seeds
    }
}

/// The length of the instruction at `address` of `image`'s code, if it is
/// executable and decodes.
pub fn instruction_length(image: &Image<'_>, address: u64) -> Option<u64> {
    let memory = image.code_view();
    if !memory.region(address)?.permissions.execute {
        return None;
    }
    let mut buffer = [0u8; 16];
    let read = memory.read(address, &mut buffer).ok()?;
    let bytes = buffer.get(..read)?;
    let length = match image.machine {
        Machine::Aarch64 => AArch64.decode(bytes, AArch64Mode::A64).ok()?.len(),
        Machine::X86 | Machine::X86_64 | Machine::Arm | Machine::Other => {
            X86.decode(bytes, x86_mode(image.machine).ok()?).ok()?.len()
        }
    };
    Some(length as u64)
}

/// The start of the recovered instruction `address` is inside of (not at
/// its start), if any.
fn inside_instruction(image: &Image<'_>, program: &Program, address: u64) -> Option<u64> {
    let block = program.blocks.get(program.block_containing(address)?)?;
    let mut pc = block.start;
    while pc < address {
        let next = pc.checked_add(instruction_length(image, pc)?)?;
        if next > address {
            return Some(pc);
        }
        pc = next;
    }
    None
}

/// Whether `address` is mapped in `image`.
fn mapped(image: &Image<'_>, address: u64) -> bool {
    image
        .segments
        .iter()
        .any(|segment| segment.start <= address && address < segment.end())
}

/// Why code cannot start at `address` of `program` (recovered from
/// `image`): not executable, inside a recovered instruction, or no
/// instruction decodes there.
fn check_code(image: &Image<'_>, program: &Program, address: u64) -> Result<(), String> {
    if !image.is_code(address) {
        return Err(format!("{address:#x} is not in executable code"));
    }
    if let Some(start) = inside_instruction(image, program, address) {
        return Err(format!(
            "{address:#x} is inside the instruction at {start:#x}"
        ));
    }
    if instruction_length(image, address).is_none() {
        return Err(format!("no instruction decodes at {address:#x}"));
    }
    Ok(())
}

/// Why `item` cannot be what the bytes at `address` of `program`
/// (recovered from `image`) are: code where [`check_code`] says, bytes
/// not all mapped, none, or a string that does not end in its NUL.
///
/// # Errors
///
/// The reason, for the user.
pub fn check_item(
    image: &Image<'_>,
    program: &Program,
    address: u64,
    item: Item,
) -> Result<(), String> {
    match item {
        Item::Code => check_code(image, program, address),
        Item::Undefined { size: 0 } => Err("no bytes to undefine".to_owned()),
        Item::Data(Data { count: 0, .. }) => Err("an array of no elements".to_owned()),
        Item::Undefined { .. } | Item::Data(_) => {
            let last = address.saturating_add(item.size()) - 1;
            if !mapped(image, address) || !mapped(image, last) {
                return Err(format!(
                    "{address:#x}..{:#x} is not all mapped",
                    last.saturating_add(1)
                ));
            }
            Ok(())
        }
    }
}

/// Why `bounds` cannot be the function at `entry`'s in `program`
/// (recovered from `image`): a function where code cannot start
/// ([`check_code`]), an end not after its entry, inside an instruction
/// or past another function's entry; deleting where no function starts.
///
/// # Errors
///
/// The reason, for the user.
pub fn check_bounds(
    image: &Image<'_>,
    program: &Program,
    entry: u64,
    bounds: Bounds,
) -> Result<(), String> {
    match bounds {
        Bounds::Deleted => program
            .function_at(entry)
            .map(|_| ())
            .ok_or_else(|| format!("no function starts at {entry:#x}")),
        Bounds::Function { end } => {
            check_code(image, program, entry)?;
            let Some(end) = end else {
                return Ok(());
            };
            if end <= entry {
                return Err(format!("its end {end:#x} is not after its start"));
            }
            if let Some(other) = program
                .functions
                .values()
                .map(|function| function.entry)
                .find(|&other| entry < other && other < end)
            {
                return Err(format!("it would overlap the function at {other:#x}"));
            }
            if let Some(start) = inside_instruction(image, program, end) {
                return Err(format!(
                    "its end {end:#x} is inside the instruction at {start:#x}"
                ));
            }
            Ok(())
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn data(unit: Unit, count: u64) -> Item {
        Item::Data(Data { unit, count })
    }

    #[test]
    fn an_item_replaces_those_it_overlaps_and_no_other() {
        let mut c = Corrections::default();
        c.set_item(0x10, Some(data(Unit::Dword, 1)));
        c.set_item(0x14, Some(data(Unit::Byte, 1)));
        c.set_item(0x20, Some(Item::Code));
        // A qword at 0x12 overlaps the dword (from before) and the byte.
        c.set_item(0x12, Some(data(Unit::Qword, 1)));
        assert_eq!(
            c.items().iter().map(|(&a, &i)| (a, i)).collect::<Vec<_>>(),
            [(0x12, data(Unit::Qword, 1)), (0x20, Item::Code)]
        );
        assert_eq!(c.holding(0x19), Some((0x12, data(Unit::Qword, 1))));
        assert_eq!(c.holding(0x1a), None);
        c.set_item(0x12, None);
        assert_eq!(c.items().len(), 1);
    }

    #[test]
    fn seeds_say_what_recovery_takes() {
        let mut c = Corrections::default();
        c.set_item(0x10, Some(Item::Code));
        c.set_item(0x20, Some(Item::Undefined { size: 3 }));
        c.set_item(0x30, Some(data(Unit::Word, 4)));
        c.set_bounds(0x40, Some(Bounds::Function { end: Some(0x48) }));
        c.set_bounds(0x50, Some(Bounds::Deleted));
        assert_eq!(
            c.seeds(),
            [
                Seed::Flow(0x10),
                Seed::NotCode {
                    start: 0x20,
                    end: 0x23
                },
                Seed::NotCode {
                    start: 0x30,
                    end: 0x38
                },
                Seed::Function(0x40, SeedKind::User),
                Seed::End {
                    function: 0x40,
                    end: 0x48
                },
                Seed::NoFunction(0x50),
                Seed::Flow(0x50),
            ]
        );
        assert_eq!(Corrections::default().seeds(), []);
    }

    #[test]
    fn a_string_is_c_or_utf16_by_its_bytes() {
        assert_eq!(
            Data::string(0x1000, b"hi\0rest"),
            Some(Data {
                unit: Unit::String,
                count: 3
            })
        );
        assert_eq!(
            Data::string(0x1000, b"h\0i\0!\0\0\0"),
            Some(Data {
                unit: Unit::Utf16,
                count: 4
            })
        );
        // At an odd address it can only be C (one character, `h`).
        assert_eq!(
            Data::string(0x1001, b"h\0i\0!\0\0\0").map(|d| d.unit),
            Some(Unit::String)
        );
        assert_eq!(Data::string(0x1000, b"\x01\x02\0"), None);
    }

    #[test]
    fn d_cycles_the_widths() {
        let mut unit = Unit::Byte;
        let mut seen = Vec::new();
        for _ in 0..5 {
            seen.push(unit);
            unit = unit.cycled();
        }
        assert_eq!(
            seen,
            [Unit::Byte, Unit::Word, Unit::Dword, Unit::Qword, Unit::Byte]
        );
        assert_eq!(Unit::String.cycled(), Unit::Byte);
    }
}