binscout 0.2.0

Headless, agent-first binary analysis: functions, CFGs, xrefs, strings, and IR over a JSON CLI or MCP server
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
//! M2 disassembler: x86_64 via iced-x86, AArch64/ARM32/Thumb via yaxpeax-arm.
//!
//! [`Disassembler::disasm_range`] decodes `bytes` assumed loaded at virtual
//! address `va`. Decoding stops at the first invalid opcode
//! ([`Error::Invalid`]) or when `max_insns` would be exceeded
//! ([`Error::Limit`]). [`Disassembler::disasm_range_partial`] (and the free
//! [`disasm_range_partial`]) instead return the valid prefix plus
//! [`PartialDisasm::error_at`], so agents keep the decodable instructions
//! rather than losing them to the first bad opcode.

use iced_x86::{
    Decoder, DecoderOptions, Formatter as _, Instruction, IntelFormatter, OpKind, Register,
};
use serde::{Deserialize, Serialize};
use yaxpeax_arch::Decoder as ArmDecoder;
use yaxpeax_arm::armv7::{
    InstDecoder as ArmV7Decoder, Instruction as ArmV7Instruction, Opcode as ArmV7Opcode,
    Operand as ArmV7Operand,
};
use yaxpeax_arm::armv8::a64::{InstDecoder, Opcode, Operand};

use crate::error::{Error, Result};

/// Default cap on instructions per [`Disassembler::disasm_range`] call.
pub const DEFAULT_MAX_INSNS: usize = 100_000;

/// Target architecture for disassembly.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Arch {
    /// 64-bit x86.
    X86_64,
    /// 32-bit x86 (LE/DOS code).
    X86_32,
    /// 64-bit ARM.
    Aarch64,
    /// 32-bit ARM (ARM mode, fixed 4-byte instructions).
    Arm32,
    /// 32-bit ARM (Thumb mode, mixed 2/4-byte instructions).
    Thumb,
}

impl Arch {
    /// Default disassembler for this architecture.
    #[must_use]
    pub fn disassembler(self) -> AnyDisasm {
        match self {
            Self::X86_64 => AnyDisasm::X86_64(X86_64Disasm::new()),
            Self::X86_32 => AnyDisasm::X86_32(X86_32Disasm::new()),
            Self::Aarch64 => AnyDisasm::Aarch64(Aarch64Disasm::new()),
            Self::Arm32 => AnyDisasm::Arm32(Arm32Disasm::new()),
            Self::Thumb => AnyDisasm::Thumb(ThumbDisasm::new()),
        }
    }
}

/// One decoded instruction.
///
/// JSON carries every address twice: the integer (`addr`, `branch_target`,
/// `mem_target`) plus its canonical `0x`-hex twin (`addr_hex`, ...), via
/// [`crate::addr_hex`]. Old rows without the twins still deserialize (the
/// twins are recomputed on serialize, never stored).
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct Insn {
    /// Virtual address of the first byte.
    pub addr: u64,
    /// Length in bytes (x86: 1-15, AArch64/ARM32: always 4, Thumb: 2 or 4).
    pub size: u8,
    /// Raw instruction bytes.
    pub bytes: Vec<u8>,
    /// Lowercase mnemonic (`nop`, `ret`, `bl`, ...).
    pub mnem: String,
    /// Operand strings in assembly order.
    pub ops: Vec<String>,
    /// Absolute target for direct branches/calls, if any.
    pub branch_target: Option<u64>,
    /// Absolute address of a data access, if any: x86 RIP-relative memory
    /// operands, x86-32 absolute immediates and displacements
    /// (`push 0x402100`, `mov eax, [0x403000]`), AArch64
    /// `adr`/`adrp`/literal loads, and ARM32/Thumb PC-relative literal
    /// loads (`ldr r0, [pc, ...]`).
    /// Distinct from [`Insn::branch_target`]: an indirect `call [rip+..]`
    /// reports its pointer slot here and `None` for `branch_target`.
    pub mem_target: Option<u64>,
}

impl Serialize for Insn {
    fn serialize<S: serde::Serializer>(
        &self,
        serializer: S,
    ) -> std::result::Result<S::Ok, S::Error> {
        use serde::ser::SerializeStruct;
        let mut st = serializer.serialize_struct("Insn", 10)?;
        st.serialize_field("addr", &self.addr)?;
        st.serialize_field("addr_hex", &crate::addr_hex(self.addr))?;
        st.serialize_field("size", &self.size)?;
        st.serialize_field("bytes", &self.bytes)?;
        st.serialize_field("mnem", &self.mnem)?;
        st.serialize_field("ops", &self.ops)?;
        st.serialize_field("branch_target", &self.branch_target)?;
        st.serialize_field(
            "branch_target_hex",
            &self.branch_target.map(crate::addr_hex),
        )?;
        st.serialize_field("mem_target", &self.mem_target)?;
        st.serialize_field("mem_target_hex", &self.mem_target.map(crate::addr_hex))?;
        st.end()
    }
}

/// Address and reason of the first undecodable instruction in a partial
/// decode. The message names only the virtual address and opcode context;
/// it never carries host paths. JSON pairs `addr` with `addr_hex`
/// (see [`Insn`]).
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct DecodeError {
    /// Virtual address of the failing instruction.
    pub addr: u64,
    /// Human-readable reason (e.g. `x86 invalid opcode at 0x4 (64-bit)`).
    pub message: String,
}

impl Serialize for DecodeError {
    fn serialize<S: serde::Serializer>(
        &self,
        serializer: S,
    ) -> std::result::Result<S::Ok, S::Error> {
        use serde::ser::SerializeStruct;
        let mut st = serializer.serialize_struct("DecodeError", 3)?;
        st.serialize_field("addr", &self.addr)?;
        st.serialize_field("addr_hex", &crate::addr_hex(self.addr))?;
        st.serialize_field("message", &self.message)?;
        st.end()
    }
}

/// Prefix decode: the valid instructions plus the first failure, if any.
///
/// A clean range reports `error_at: None`; a range with trailing garbage
/// keeps every instruction before the first bad opcode and reports it in
/// `error_at`. Instruction-cap overruns are still [`Error::Limit`], never a
/// partial result.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PartialDisasm {
    /// Valid instructions before the first failure (empty when the first
    /// byte fails).
    pub insns: Vec<Insn>,
    /// First failure, or `None` for a clean decode.
    pub error_at: Option<DecodeError>,
}

/// Byte-range disassembler over already-loaded bytes.
pub trait Disassembler {
    /// Decode `bytes` loaded at `va`.
    ///
    /// # Errors
    /// [`Error::Invalid`] on the first undecodable instruction,
    /// [`Error::Limit`] if the range holds more than the cap.
    fn disasm_range(&self, bytes: &[u8], va: u64) -> Result<Vec<Insn>>;

    /// Decode the valid prefix of `bytes` loaded at `va`.
    ///
    /// Returns every instruction before the first bad opcode plus
    /// `error_at` for it (`None` when the whole range decodes).
    ///
    /// # Errors
    /// [`Error::Limit`] if the range holds more than the cap; undecodable
    /// bytes are reported in `error_at`, never as an error.
    fn disasm_range_partial(&self, bytes: &[u8], va: u64) -> Result<PartialDisasm>;
}

/// x86_64 disassembler (iced-x86).
#[derive(Debug, Clone, Copy)]
pub struct X86_64Disasm {
    max_insns: usize,
}

/// 32-bit x86 disassembler (iced-x86; covers DOS4GW/LE code for M1).
#[derive(Debug, Clone, Copy)]
pub struct X86_32Disasm {
    max_insns: usize,
}

/// AArch64 disassembler (yaxpeax-arm).
#[derive(Debug, Clone, Copy)]
pub struct Aarch64Disasm {
    max_insns: usize,
}

/// ARM32 disassembler in ARM mode (yaxpeax-arm armv7, fixed 4-byte words).
#[derive(Debug, Clone, Copy)]
pub struct Arm32Disasm {
    max_insns: usize,
}

/// Thumb disassembler (yaxpeax-arm armv7 in Thumb mode, mixed 2/4-byte).
#[derive(Debug, Clone, Copy)]
pub struct ThumbDisasm {
    max_insns: usize,
}

impl X86_64Disasm {
    /// Default instruction cap ([`DEFAULT_MAX_INSNS`]).
    #[must_use]
    pub fn new() -> Self {
        Self {
            max_insns: DEFAULT_MAX_INSNS,
        }
    }

    /// Custom instruction cap.
    #[must_use]
    pub fn with_limit(max_insns: usize) -> Self {
        Self { max_insns }
    }
}

impl Default for X86_64Disasm {
    fn default() -> Self {
        Self::new()
    }
}

impl X86_32Disasm {
    /// Default instruction cap ([`DEFAULT_MAX_INSNS`]).
    #[must_use]
    pub fn new() -> Self {
        Self {
            max_insns: DEFAULT_MAX_INSNS,
        }
    }

    /// Custom instruction cap.
    #[must_use]
    pub fn with_limit(max_insns: usize) -> Self {
        Self { max_insns }
    }
}

impl Default for X86_32Disasm {
    fn default() -> Self {
        Self::new()
    }
}

impl Aarch64Disasm {
    /// Default instruction cap ([`DEFAULT_MAX_INSNS`]).
    #[must_use]
    pub fn new() -> Self {
        Self {
            max_insns: DEFAULT_MAX_INSNS,
        }
    }

    /// Custom instruction cap.
    #[must_use]
    pub fn with_limit(max_insns: usize) -> Self {
        Self { max_insns }
    }
}

impl Default for Aarch64Disasm {
    fn default() -> Self {
        Self::new()
    }
}

impl Arm32Disasm {
    /// Default instruction cap ([`DEFAULT_MAX_INSNS`]).
    #[must_use]
    pub fn new() -> Self {
        Self {
            max_insns: DEFAULT_MAX_INSNS,
        }
    }

    /// Custom instruction cap.
    #[must_use]
    pub fn with_limit(max_insns: usize) -> Self {
        Self { max_insns }
    }
}

impl Default for Arm32Disasm {
    fn default() -> Self {
        Self::new()
    }
}

impl ThumbDisasm {
    /// Default instruction cap ([`DEFAULT_MAX_INSNS`]).
    #[must_use]
    pub fn new() -> Self {
        Self {
            max_insns: DEFAULT_MAX_INSNS,
        }
    }

    /// Custom instruction cap.
    #[must_use]
    pub fn with_limit(max_insns: usize) -> Self {
        Self { max_insns }
    }
}

impl Default for ThumbDisasm {
    fn default() -> Self {
        Self::new()
    }
}

impl Disassembler for X86_64Disasm {
    fn disasm_range(&self, bytes: &[u8], va: u64) -> Result<Vec<Insn>> {
        disasm_x86(bytes, va, self.max_insns, 64)
    }

    fn disasm_range_partial(&self, bytes: &[u8], va: u64) -> Result<PartialDisasm> {
        disasm_x86_partial(bytes, va, self.max_insns, 64)
    }
}

impl Disassembler for X86_32Disasm {
    fn disasm_range(&self, bytes: &[u8], va: u64) -> Result<Vec<Insn>> {
        disasm_x86(bytes, va, self.max_insns, 32)
    }

    fn disasm_range_partial(&self, bytes: &[u8], va: u64) -> Result<PartialDisasm> {
        disasm_x86_partial(bytes, va, self.max_insns, 32)
    }
}

impl Disassembler for Aarch64Disasm {
    fn disasm_range(&self, bytes: &[u8], va: u64) -> Result<Vec<Insn>> {
        disasm_aarch64(bytes, va, self.max_insns)
    }

    fn disasm_range_partial(&self, bytes: &[u8], va: u64) -> Result<PartialDisasm> {
        disasm_aarch64_partial(bytes, va, self.max_insns)
    }
}

impl Disassembler for Arm32Disasm {
    fn disasm_range(&self, bytes: &[u8], va: u64) -> Result<Vec<Insn>> {
        disasm_arm32(bytes, va, self.max_insns)
    }

    fn disasm_range_partial(&self, bytes: &[u8], va: u64) -> Result<PartialDisasm> {
        disasm_arm32_partial(bytes, va, self.max_insns)
    }
}

impl Disassembler for ThumbDisasm {
    fn disasm_range(&self, bytes: &[u8], va: u64) -> Result<Vec<Insn>> {
        disasm_thumb(bytes, va, self.max_insns)
    }

    fn disasm_range_partial(&self, bytes: &[u8], va: u64) -> Result<PartialDisasm> {
        disasm_thumb_partial(bytes, va, self.max_insns)
    }
}

/// Arch-dispatched disassembler (object-safe use of [`Disassembler`]).
#[derive(Debug, Clone, Copy)]
pub enum AnyDisasm {
    /// 64-bit x86 backend.
    X86_64(X86_64Disasm),
    /// 32-bit x86 backend.
    X86_32(X86_32Disasm),
    /// AArch64 backend.
    Aarch64(Aarch64Disasm),
    /// ARM32 (ARM mode) backend.
    Arm32(Arm32Disasm),
    /// Thumb backend.
    Thumb(ThumbDisasm),
}

impl Disassembler for AnyDisasm {
    fn disasm_range(&self, bytes: &[u8], va: u64) -> Result<Vec<Insn>> {
        match self {
            Self::X86_64(d) => d.disasm_range(bytes, va),
            Self::X86_32(d) => d.disasm_range(bytes, va),
            Self::Aarch64(d) => d.disasm_range(bytes, va),
            Self::Arm32(d) => d.disasm_range(bytes, va),
            Self::Thumb(d) => d.disasm_range(bytes, va),
        }
    }

    fn disasm_range_partial(&self, bytes: &[u8], va: u64) -> Result<PartialDisasm> {
        match self {
            Self::X86_64(d) => d.disasm_range_partial(bytes, va),
            Self::X86_32(d) => d.disasm_range_partial(bytes, va),
            Self::Aarch64(d) => d.disasm_range_partial(bytes, va),
            Self::Arm32(d) => d.disasm_range_partial(bytes, va),
            Self::Thumb(d) => d.disasm_range_partial(bytes, va),
        }
    }
}

/// One-shot decode without holding a disassembler value.
pub fn disasm_range(arch: Arch, bytes: &[u8], va: u64, max_insns: usize) -> Result<Vec<Insn>> {
    match arch {
        Arch::X86_64 => disasm_x86(bytes, va, max_insns, 64),
        Arch::X86_32 => disasm_x86(bytes, va, max_insns, 32),
        Arch::Aarch64 => disasm_aarch64(bytes, va, max_insns),
        Arch::Arm32 => disasm_arm32(bytes, va, max_insns),
        Arch::Thumb => disasm_thumb(bytes, va, max_insns),
    }
}

/// One-shot prefix decode: valid instructions plus `error_at` for the first
/// bad opcode (`None` when the whole range decodes).
///
/// Cap overruns are still [`Error::Limit`]; undecodable bytes are data, not
/// an error.
///
/// # Errors
/// [`Error::Limit`] if the range holds more than `max_insns`.
pub fn disasm_range_partial(
    arch: Arch,
    bytes: &[u8],
    va: u64,
    max_insns: usize,
) -> Result<PartialDisasm> {
    match arch {
        Arch::X86_64 => disasm_x86_partial(bytes, va, max_insns, 64),
        Arch::X86_32 => disasm_x86_partial(bytes, va, max_insns, 32),
        Arch::Aarch64 => disasm_aarch64_partial(bytes, va, max_insns),
        Arch::Arm32 => disasm_arm32_partial(bytes, va, max_insns),
        Arch::Thumb => disasm_thumb_partial(bytes, va, max_insns),
    }
}

fn limit_err(max_insns: usize) -> Error {
    Error::Limit(format!("more than {max_insns} instructions in range"))
}

/// Split `mnem op1, op2` into a lowercase mnemonic and operand strings.
///
/// Operand immediates leave iced-x86 in MASM suffix form (`402100h`); each
/// operand goes through [`masm_to_hex`] so agents only ever see `0x` form.
fn split_asm(text: &str) -> (String, Vec<String>) {
    let text = text.trim();
    let (head, tail) = match text.find(char::is_whitespace) {
        Some(i) => (&text[..i], text[i..].trim()),
        None => (text, ""),
    };
    let ops = if tail.is_empty() {
        Vec::new()
    } else {
        tail.split(',').map(|s| masm_to_hex(s.trim())).collect()
    };
    (head.to_lowercase(), ops)
}

/// Rewrite MASM-suffixed hex inside one operand as canonical `0x`-hex.
///
/// iced-x86's `IntelFormatter` emits `402100h` / `0052C2E0h` (with a leading
/// `0` when the digits start with `A`-`F`, e.g. `0FFFFFFFFh`). Only words
/// that start with a decimal digit qualify, so registers (`ah`, `bh`),
/// keywords (`short`, `near`, `ptr`), and symbols pass through untouched;
/// every other character (brackets, `+`, spaces) is preserved verbatim.
fn masm_to_hex(op: &str) -> String {
    let mut out = String::with_capacity(op.len());
    let mut word = String::new();
    for c in op.chars() {
        if c.is_ascii_alphanumeric() || c == '_' {
            word.push(c);
        } else {
            out.push_str(&masm_word(&word));
            word.clear();
            out.push(c);
        }
    }
    out.push_str(&masm_word(&word));
    out
}

/// Rewrite one word of [`masm_to_hex`]: `402100h` becomes `0x402100`.
fn masm_word(word: &str) -> String {
    let b = word.as_bytes();
    let is_suffixed = b.len() >= 2
        && b[0].is_ascii_digit()
        && (b[b.len() - 1] == b'h' || b[b.len() - 1] == b'H')
        && b[..b.len() - 1].iter().all(|c| c.is_ascii_hexdigit());
    if !is_suffixed {
        return word.to_string();
    }
    let digits = &word[..word.len() - 1];
    match u64::from_str_radix(digits, 16) {
        Ok(v) => crate::addr_hex(v),
        Err(_) => format!("0x{}", digits.to_ascii_lowercase()),
    }
}

fn disasm_x86(bytes: &[u8], va: u64, max_insns: usize, bitness: u32) -> Result<Vec<Insn>> {
    let partial = disasm_x86_partial(bytes, va, max_insns, bitness)?;
    match partial.error_at {
        Some(e) => Err(Error::Invalid(e.message)),
        None => Ok(partial.insns),
    }
}

/// Prefix decode over x86 bytes: each position decodes independently via
/// iced-x86, so trailing garbage cannot change the first decode (the same
/// prefix property [`crate::analysis`] relies on for single-instruction
/// `decode_one`). The first invalid opcode ends the prefix with `error_at`.
fn disasm_x86_partial(
    bytes: &[u8],
    va: u64,
    max_insns: usize,
    bitness: u32,
) -> Result<PartialDisasm> {
    let mut decoder = Decoder::new(bitness, bytes, DecoderOptions::NONE);
    decoder.set_ip(va);
    let mut formatter = IntelFormatter::new();
    let mut text = String::new();
    let mut out = Vec::new();
    while decoder.position() < bytes.len() {
        if out.len() >= max_insns {
            return Err(limit_err(max_insns));
        }
        let addr = decoder.ip();
        let start = decoder.position();
        let insn = decoder.decode();
        if insn.is_invalid() {
            return Ok(PartialDisasm {
                insns: out,
                error_at: Some(DecodeError {
                    addr,
                    message: format!("x86 invalid opcode at {addr:#x} ({bitness}-bit)"),
                }),
            });
        }
        let size = insn.len();
        let raw = match bytes.get(start..start + size) {
            Some(b) => b.to_vec(),
            None => {
                return Ok(PartialDisasm {
                    insns: out,
                    error_at: Some(DecodeError {
                        addr,
                        message: format!("x86 invalid opcode at {addr:#x} ({bitness}-bit)"),
                    }),
                });
            }
        };
        text.clear();
        formatter.format(&insn, &mut text);
        let (mnem, ops) = split_asm(&text);
        let branch_target = match insn.op0_kind() {
            OpKind::NearBranch16 | OpKind::NearBranch32 | OpKind::NearBranch64 => {
                Some(insn.near_branch_target())
            }
            _ => None,
        };
        let mem_target = insn
            .is_ip_rel_memory_operand()
            .then(|| insn.ip_rel_memory_address());
        let mem_target = match mem_target {
            Some(_) => mem_target,
            None if bitness == 32 => absolute_target32(&insn),
            None => None,
        };
        out.push(Insn {
            addr,
            size: size as u8,
            bytes: raw,
            mnem,
            ops,
            branch_target,
            mem_target,
        });
    }
    Ok(PartialDisasm {
        insns: out,
        error_at: None,
    })
}

/// Absolute data address referenced by a 32-bit x86 instruction, if any.
///
/// 32-bit code addresses data absolutely (`push 0x402100`,
/// `mov eax, 0x403000`, `mov eax, [0x403000]`); 64-bit code uses
/// RIP-relative operands instead (handled by the caller), so 64-bit
/// immediates are never reported here. Absolute memory operands win over
/// immediates: in `mov [0x403000], 0x1` the store slot is the reference,
/// not the stored constant.
fn absolute_target32(insn: &Instruction) -> Option<u64> {
    for op in 0..insn.op_count() {
        if insn.op_kind(op) == OpKind::Memory
            && insn.memory_base() == Register::None
            && insn.memory_index() == Register::None
        {
            return Some(u64::from(insn.memory_displacement32()));
        }
    }
    for op in 0..insn.op_count() {
        if insn.op_kind(op) == OpKind::Immediate32 {
            return Some(u64::from(insn.immediate32()));
        }
    }
    None
}

fn disasm_aarch64(bytes: &[u8], va: u64, max_insns: usize) -> Result<Vec<Insn>> {
    let partial = disasm_aarch64_partial(bytes, va, max_insns)?;
    match partial.error_at {
        Some(e) => Err(Error::Invalid(e.message)),
        None => Ok(partial.insns),
    }
}

/// Prefix decode over AArch64 bytes: fixed 4-byte words decode
/// independently, so the first bad word ends the prefix with `error_at`.
/// A short tail (< 4 bytes) fails its word decode the same way.
fn disasm_aarch64_partial(bytes: &[u8], va: u64, max_insns: usize) -> Result<PartialDisasm> {
    let mut reader = <&[u8] as yaxpeax_arch::ReaderBuilder<u64, u8>>::read_from(bytes);
    let decoder = InstDecoder::default();
    let mut out = Vec::new();
    let mut addr = va;
    let mut off = 0usize;
    while off < bytes.len() {
        if out.len() >= max_insns {
            return Err(limit_err(max_insns));
        }
        let inst = match ArmDecoder::decode(&decoder, &mut reader) {
            Ok(inst) => inst,
            Err(e) => {
                return Ok(PartialDisasm {
                    insns: out,
                    error_at: Some(DecodeError {
                        addr,
                        message: format!("aarch64 decode error at {addr:#x}: {e:?}"),
                    }),
                });
            }
        };
        let (mnem, ops) = split_asm(&inst.to_string());
        let Some(raw) = bytes.get(off..off + 4).map(<[u8]>::to_vec) else {
            return Ok(PartialDisasm {
                insns: out,
                error_at: Some(DecodeError {
                    addr,
                    message: format!("aarch64 decode error at {addr:#x}: truncated word"),
                }),
            });
        };
        out.push(Insn {
            addr,
            size: 4,
            bytes: raw,
            mnem,
            ops,
            branch_target: arm_branch_target(&inst, addr),
            mem_target: arm_data_target(&inst, addr),
        });
        addr = addr.wrapping_add(4);
        off += 4;
    }
    Ok(PartialDisasm {
        insns: out,
        error_at: None,
    })
}

/// Absolute target of PC-relative data access (`adr`/`adrp`/literal loads),
/// else `None`. Branch opcodes are excluded (see [`arm_branch_target`).
/// `adrp` carries a page offset, so the PC is page-masked first.
fn arm_data_target(inst: &yaxpeax_arm::armv8::a64::Instruction, addr: u64) -> Option<u64> {
    if matches!(
        inst.opcode,
        Opcode::B
            | Opcode::BL
            | Opcode::Bcc(_)
            | Opcode::BCcc(_)
            | Opcode::CBZ
            | Opcode::CBNZ
            | Opcode::TBZ
            | Opcode::TBNZ
    ) {
        return None;
    }
    inst.operands.iter().find_map(|op| match op {
        Operand::PCOffset(off) if inst.opcode == Opcode::ADRP => {
            Some((addr & !0xfff).wrapping_add_signed(*off))
        }
        Operand::PCOffset(off) => Some(addr.wrapping_add_signed(*off)),
        _ => None,
    })
}

/// Absolute target for direct PC-relative branches, else `None`.
fn arm_branch_target(inst: &yaxpeax_arm::armv8::a64::Instruction, addr: u64) -> Option<u64> {
    if !matches!(
        inst.opcode,
        Opcode::B
            | Opcode::BL
            | Opcode::Bcc(_)
            | Opcode::BCcc(_)
            | Opcode::CBZ
            | Opcode::CBNZ
            | Opcode::TBZ
            | Opcode::TBNZ
    ) {
        return None;
    }
    inst.operands.iter().find_map(|op| match op {
        Operand::PCOffset(off) => Some(addr.wrapping_add_signed(*off)),
        _ => None,
    })
}

fn disasm_arm32(bytes: &[u8], va: u64, max_insns: usize) -> Result<Vec<Insn>> {
    let partial = disasm_arm32_partial(bytes, va, max_insns)?;
    match partial.error_at {
        Some(e) => Err(Error::Invalid(e.message)),
        None => Ok(partial.insns),
    }
}

/// Prefix decode over ARM32 bytes: fixed 4-byte words decode
/// independently, so the first bad word ends the prefix with `error_at`.
/// A short tail (< 4 bytes) fails its word decode the same way.
fn disasm_arm32_partial(bytes: &[u8], va: u64, max_insns: usize) -> Result<PartialDisasm> {
    disasm_armv7_partial(bytes, va, max_insns, false)
}

fn disasm_thumb(bytes: &[u8], va: u64, max_insns: usize) -> Result<Vec<Insn>> {
    let partial = disasm_thumb_partial(bytes, va, max_insns)?;
    match partial.error_at {
        Some(e) => Err(Error::Invalid(e.message)),
        None => Ok(partial.insns),
    }
}

/// Prefix decode over Thumb bytes: the first halfword decides the length
/// (2 or 4 bytes), so the first bad halfword pair ends the prefix with
/// `error_at`. A short tail, including a lone first halfword of a 32-bit
/// instruction, fails its decode the same way.
fn disasm_thumb_partial(bytes: &[u8], va: u64, max_insns: usize) -> Result<PartialDisasm> {
    disasm_armv7_partial(bytes, va, max_insns, true)
}

/// Shared ARMv7 prefix decode; `thumb` selects the decoder mode.
///
/// The reader is stateful across decodes, and each successful decode
/// consumes exactly its instruction (`inst.thumb() && !inst.wide()` is 2
/// bytes, anything else 4), so the byte window always matches what the
/// decoder read — including a 2-byte tail left by a 32-bit prefix, which
/// fails as exhausted input.
fn disasm_armv7_partial(
    bytes: &[u8],
    va: u64,
    max_insns: usize,
    thumb: bool,
) -> Result<PartialDisasm> {
    let mut reader = <&[u8] as yaxpeax_arch::ReaderBuilder<u32, u8>>::read_from(bytes);
    let decoder = ArmV7Decoder::default().with_thumb_mode(thumb);
    let label = if thumb { "thumb" } else { "arm32" };
    let mut out = Vec::new();
    let mut addr = va;
    let mut off = 0usize;
    while off < bytes.len() {
        if out.len() >= max_insns {
            return Err(limit_err(max_insns));
        }
        let inst = match ArmDecoder::decode(&decoder, &mut reader) {
            Ok(inst) => inst,
            Err(e) => {
                return Ok(PartialDisasm {
                    insns: out,
                    error_at: Some(DecodeError {
                        addr,
                        message: format!("{label} decode error at {addr:#x}: {e:?}"),
                    }),
                });
            }
        };
        let size: usize = if inst.thumb() && !inst.wide() { 2 } else { 4 };
        let (mnem, ops) = split_asm(&inst.to_string());
        let Some(raw) = bytes.get(off..off + size).map(<[u8]>::to_vec) else {
            return Ok(PartialDisasm {
                insns: out,
                error_at: Some(DecodeError {
                    addr,
                    message: format!("{label} decode error at {addr:#x}: truncated word"),
                }),
            });
        };
        out.push(Insn {
            addr,
            size: size as u8,
            bytes: raw,
            mnem,
            ops,
            branch_target: armv7_branch_target(&inst, addr),
            mem_target: armv7_data_target(&inst, addr),
        });
        addr = addr.wrapping_add(size as u64);
        off += size;
    }
    Ok(PartialDisasm {
        insns: out,
        error_at: None,
    })
}

/// Absolute target for direct ARMv7 branches, else `None`.
///
/// `BranchOffset` (ARM `b`/`bl`) counts words from the instruction address:
/// the decoder already folded the architectural PC+8 bias in. Thumb
/// `BranchThumbOffset` counts halfwords from PC (`addr + 4`); the ARM
/// immediate `blx` instead carries a byte offset from PC (`addr + 8`),
/// told apart by the decoded mode.
fn armv7_branch_target(inst: &ArmV7Instruction, addr: u64) -> Option<u64> {
    if !matches!(
        inst.opcode,
        ArmV7Opcode::B | ArmV7Opcode::BL | ArmV7Opcode::BLX | ArmV7Opcode::CBZ | ArmV7Opcode::CBNZ
    ) {
        return None;
    }
    inst.operands.iter().find_map(|op| match op {
        ArmV7Operand::BranchOffset(off) => Some(addr.wrapping_add_signed(i64::from(*off) * 4)),
        ArmV7Operand::BranchThumbOffset(off) if inst.thumb() => Some(
            addr.wrapping_add(4)
                .wrapping_add_signed(i64::from(*off) * 2),
        ),
        ArmV7Operand::BranchThumbOffset(off) => {
            Some(addr.wrapping_add(8).wrapping_add_signed(i64::from(*off)))
        }
        _ => None,
    })
}

/// Absolute target of a PC-relative literal load (`ldr` family with a `pc`
/// base), else `None`. Branch opcodes are excluded (see
/// [`armv7_branch_target`]). ARM reads PC as `addr + 8`; Thumb reads an
/// aligned PC (`addr + 4` rounded down to 4) and the decoder already scaled
/// 16-bit offsets to bytes.
///
/// `adr` is deliberately excluded: its Display folds the add/sub form away
/// (`adr r0, pc, 0x4` for both), so the sign is unrecoverable from the
/// decoded instruction.
fn armv7_data_target(inst: &ArmV7Instruction, addr: u64) -> Option<u64> {
    if matches!(
        inst.opcode,
        ArmV7Opcode::B | ArmV7Opcode::BL | ArmV7Opcode::BLX | ArmV7Opcode::CBZ | ArmV7Opcode::CBNZ
    ) {
        return None;
    }
    if !matches!(
        inst.opcode,
        ArmV7Opcode::LDR
            | ArmV7Opcode::LDRB
            | ArmV7Opcode::LDRH
            | ArmV7Opcode::LDRSB
            | ArmV7Opcode::LDRSH
    ) {
        return None;
    }
    inst.operands.iter().find_map(|op| match op {
        ArmV7Operand::RegDerefPreindexOffset(reg, off, add, _) if reg.number() == 15 => {
            let base = if inst.thumb() {
                addr.wrapping_add(4) & !3
            } else {
                addr.wrapping_add(8)
            };
            let delta = u64::from(*off);
            Some(if *add {
                base.wrapping_add(delta)
            } else {
                base.wrapping_sub(delta)
            })
        }
        _ => None,
    })
}

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

    #[test]
    fn x86_32_push_imm32_reports_mem_target() {
        // push 0x402100 at 0x401003.
        let v = disasm_x86(&[0x68, 0x00, 0x21, 0x40, 0x00], 0x401003, 8, 32).expect("push decodes");
        assert_eq!(v.len(), 1);
        assert_eq!(v[0].mem_target, Some(0x402100));
        assert_eq!(v[0].branch_target, None);
    }

    #[test]
    fn x86_32_mov_abs_mem_reports_mem_target() {
        // mov eax, [0x403000] at 0x401000.
        let v = disasm_x86(&[0xA1, 0x00, 0x30, 0x40, 0x00], 0x401000, 8, 32)
            .expect("moffs load decodes");
        assert_eq!(v.len(), 1);
        assert_eq!(v[0].mem_target, Some(0x403000));
    }

    #[test]
    fn x86_64_push_imm32_is_not_a_data_ref() {
        // Same bytes in 64-bit mode: a sign-extended constant, not an address.
        let v = disasm_x86(&[0x68, 0x00, 0x21, 0x40, 0x00], 0x401003, 8, 64).expect("push decodes");
        assert_eq!(v.len(), 1);
        assert_eq!(v[0].mem_target, None);
    }

    #[test]
    fn arm32_mov_bx_snapshot() {
        // mov r0, r1 ; bx lr at 0x10000.
        let v = disasm_arm32(
            &[0x01, 0x00, 0xA0, 0xE1, 0x1E, 0xFF, 0x2F, 0xE1],
            0x10000,
            8,
        )
        .expect("arm32 must decode");
        assert_eq!(v.len(), 2);
        assert_eq!(
            v.iter().map(|i| (i.addr, i.size)).collect::<Vec<_>>(),
            [(0x10000, 4), (0x10004, 4)]
        );
        assert_eq!(
            v.iter().map(|i| i.mnem.as_str()).collect::<Vec<_>>(),
            ["mov", "bx"]
        );
        assert_eq!(v[0].ops, ["r0", "r1"]);
        assert_eq!(v[0].bytes, [0x01, 0x00, 0xA0, 0xE1]);
        assert!(v.iter().all(|i| i.branch_target.is_none()));
    }

    #[test]
    fn arm32_b_targets_self() {
        // b . at 0x10000: word offset 0 already folds the PC+8 bias in.
        let v = disasm_arm32(&[0xFE, 0xFF, 0xFF, 0xEA], 0x10000, 8).expect("b decodes");
        assert_eq!(v.len(), 1);
        assert_eq!(v[0].mnem, "b");
        assert_eq!(v[0].branch_target, Some(0x10000));
    }

    #[test]
    fn arm32_bl_folds_pc_bias() {
        // bl with zero field at 0x10000 targets addr+8.
        let v = disasm_arm32(&[0x00, 0x00, 0x00, 0xEB], 0x10000, 8).expect("bl decodes");
        assert_eq!(v[0].mnem, "bl");
        assert_eq!(v[0].branch_target, Some(0x10008));
    }

    #[test]
    fn arm32_literal_ldr_reports_mem_target() {
        // ldr r0, [pc, #4] at 0x10000 reads PC (addr+8) + 4.
        let v = disasm_arm32(&[0x04, 0x00, 0x9F, 0xE5], 0x10000, 8).expect("ldr decodes");
        assert_eq!(v[0].mem_target, Some(0x1000c));
        assert_eq!(v[0].branch_target, None);
        // Sub form subtracts.
        let v = disasm_arm32(&[0x04, 0x00, 0x1F, 0xE5], 0x10000, 8).expect("ldr decodes");
        assert_eq!(v[0].mem_target, Some(0x10004));
    }

    #[test]
    fn thumb_mixed_width_snapshot() {
        // nop (2B) ; bl #0 (4B) ; bx lr (2B) at 0x20000.
        let bytes = [0x00, 0xBF, 0x00, 0xF0, 0x00, 0xF8, 0x70, 0x47];
        let v = disasm_thumb(&bytes, 0x20000, 8).expect("thumb must decode");
        assert_eq!(
            v.iter().map(|i| (i.addr, i.size)).collect::<Vec<_>>(),
            [(0x20000, 2), (0x20002, 4), (0x20006, 2)]
        );
        assert_eq!(v[0].mnem, "nop");
        assert_eq!(v[2].mnem, "bx");
        assert_eq!(v[2].ops, ["lr"]);
        assert_eq!(v[1].branch_target, Some(0x20006));
        assert_eq!(v[1].bytes, [0x00, 0xF0, 0x00, 0xF8]);
    }

    #[test]
    fn thumb_b_targets_pc_relative() {
        // b . (0xE7FE) at 0x20000: halfword offset -2 from PC (addr+4).
        let v = disasm_thumb(&[0xFE, 0xE7], 0x20000, 8).expect("b decodes");
        assert_eq!(v[0].mnem, "b");
        assert_eq!(v[0].branch_target, Some(0x20000));
    }

    #[test]
    fn thumb_literal_ldr_aligns_pc() {
        // ldr r0, [pc, #16] at 0x20001: base is addr+4 rounded down to 4.
        let v = disasm_thumb(&[0x04, 0x48], 0x20001, 8).expect("ldr decodes");
        assert_eq!(v[0].mem_target, Some(((0x20001 + 4) & !3) + 16));
    }
}