binscout 0.9.2

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
//! Diagnostics and discovery-provenance tests (plan §6).
//!
//! Synthetic x86-64 blobs pin the exact deterministic reason and
//! diagnostic sets: direct calls, indirect jumps/calls, invalid decodes,
//! prologue seeds, and exports. Fresh analyses and saved/reopened
//! snapshots must agree bit-identically; the record cap truncates with an
//! explicit omitted count while hard analysis budgets stay errors.

mod common;

use binscout::analysis::{
    Analysis, Diagnostic, DiagnosticKind, DiscoveryReason, MAX_DIAGNOSTICS, XrefKind, analyze,
    analyze_raw,
};
use binscout::disasm::Arch;
use binscout::error::Error;
use binscout::loader::{BinaryFormat, ExportRef, LoadedBinary, RawOptions, Segment};
use redb::ReadableTable;

fn x64(bytes: &[u8], base: u64, entry: Option<u64>) -> Analysis {
    analyze_raw(bytes, base, Arch::X86_64, entry).expect("synthetic blob must analyze")
}

fn reasons_of(a: &Analysis, entry: u64) -> Vec<DiscoveryReason> {
    a.functions
        .get(&entry)
        .expect("function must exist")
        .reason_list()
}

fn diagnostics_of(a: &Analysis) -> Vec<(u64, DiagnosticKind)> {
    a.diagnostics
        .records
        .iter()
        .map(|d| (d.addr, d.kind))
        .collect()
}

fn assert_sorted_deduped(a: &Analysis) {
    let mut ds = a.diagnostics.records.clone();
    ds.sort_unstable();
    ds.dedup();
    assert_eq!(
        a.diagnostics.records, ds,
        "diagnostics must be sorted and deduplicated"
    );
    for f in a.functions.values() {
        let rs = f.reason_list();
        let mut sorted = rs.clone();
        sorted.sort_unstable();
        sorted.dedup();
        assert_eq!(rs, sorted, "reasons must be sorted and deduplicated");
        assert!(!rs.is_empty(), "every function carries a reason");
    }
}

// func@0x1000 calls func@0x1010; no gaps, no indirect control flow.
fn two_funcs() -> Vec<u8> {
    let mut b = vec![
        0x55, 0x48, 0x89, 0xE5, 0xE8, 0x07, 0x00, 0x00, 0x00, 0x5D, 0xC3, 0x90, 0x90, 0x90, 0x90,
        0x90,
    ];
    b.extend([
        0x55, 0x48, 0x89, 0xE5, 0xB8, 0x01, 0x00, 0x00, 0x00, 0x5D, 0xC3,
    ]);
    b
}

#[test]
fn direct_call_yields_entry_and_direct_call_reasons_with_no_diagnostics() {
    let a = x64(&two_funcs(), 0x1000, Some(0x1000));
    assert_eq!(reasons_of(&a, 0x1000), [DiscoveryReason::Entry]);
    assert_eq!(reasons_of(&a, 0x1010), [DiscoveryReason::DirectCall]);
    assert!(a.diagnostics.records.is_empty());
    assert!(!a.diagnostics.truncated);
    assert_eq!(a.diagnostics.omitted, 0);
    assert_sorted_deduped(&a);
}

#[test]
fn prologue_scan_seeds_unreachable_function_with_prologue_reason() {
    // ret | nop*7 | push rbp | mov rbp,rsp | ret: the frame is only found
    // via the prologue scan.
    let mut b = vec![0xC3];
    b.extend([0x90; 7]);
    b.extend([0x55, 0x48, 0x89, 0xE5, 0xC3]);
    let a = x64(&b, 0x3000, None);
    assert_eq!(reasons_of(&a, 0x3000), [DiscoveryReason::Entry]);
    assert_eq!(reasons_of(&a, 0x3008), [DiscoveryReason::Prologue]);
    assert!(a.diagnostics.records.is_empty());
    assert_sorted_deduped(&a);
}

#[test]
fn export_and_direct_call_reasons_coexist_sorted() {
    let bytes = two_funcs();
    let bin = LoadedBinary {
        format: BinaryFormat::Raw,
        arch: Arch::X86_64,
        entry: Some(0x1000),
        segments: vec![Segment {
            vaddr: 0x1000,
            mem_size: bytes.len() as u64,
            data: bytes,
            readable: true,
            writable: false,
            executable: true,
        }],
        sections: Vec::new(),
        imports: Vec::new(),
        exports: vec![ExportRef {
            name: "myexport".to_string(),
            address: Some(0x1010),
            ordinal: None,
            demangled: None,
        }],
        symbols: Vec::new(),
        pdata_funcs: Vec::new(),
        tls_callbacks: Vec::new(),
        init_funcs: Vec::new(),
        func_starts: Vec::new(),
        debug_funcs: Vec::new(),
        relocatable: false,
        arm_hints: Default::default(),
    };
    let a = analyze(&bin).expect("raw with export must analyze");
    assert_eq!(reasons_of(&a, 0x1000), [DiscoveryReason::Entry]);
    assert_eq!(
        reasons_of(&a, 0x1010),
        [DiscoveryReason::Export, DiscoveryReason::DirectCall]
    );
    assert_sorted_deduped(&a);
}

#[test]
fn entry_and_direct_call_reasons_coexist_on_recursive_entry() {
    // 0x1000 calls 0x1008; 0x1008 calls 0x1000 back.
    let b = vec![
        0xE8, 0x03, 0x00, 0x00, 0x00, // call 0x1008
        0xC3, // ret
        0x90, 0x90, // pad
        0xE8, 0xF3, 0xFF, 0xFF, 0xFF, // call 0x1000
        0xC3, // ret
    ];
    let a = x64(&b, 0x1000, Some(0x1000));
    assert_eq!(
        reasons_of(&a, 0x1000),
        [DiscoveryReason::Entry, DiscoveryReason::DirectCall]
    );
    assert_eq!(reasons_of(&a, 0x1008), [DiscoveryReason::DirectCall]);
    assert_sorted_deduped(&a);
}

#[test]
fn indirect_call_records_site_and_stays_edgeless() {
    // call rax | ret.
    let a = x64(&[0xFF, 0xD0, 0xC3], 0x1000, Some(0x1000));
    assert_eq!(
        diagnostics_of(&a),
        [(0x1000, DiagnosticKind::UnresolvedIndirectCall)]
    );
    assert!(!a.diagnostics.truncated);
    assert_eq!(a.diagnostics.omitted, 0);
    assert_eq!(reasons_of(&a, 0x1000), [DiscoveryReason::Entry]);
    // Unresolved control flow is never upgraded to a proven edge.
    assert!(a.call_edges().is_empty());
    assert!(a.calls_of(0x1000).is_empty());
    assert!(
        a.xrefs_page(Some(XrefKind::Code), 0, 64)
            .iter()
            .all(|x| x.from != 0x1000),
        "indirect call must not produce a code xref"
    );
    assert_sorted_deduped(&a);
}

#[test]
fn indirect_jump_records_site_and_stays_edgeless() {
    // jmp rax (fallthrough bytes never decode: the path ends).
    let a = x64(&[0xFF, 0xE0, 0xC3], 0x1000, Some(0x1000));
    assert_eq!(
        diagnostics_of(&a),
        [(0x1000, DiagnosticKind::UnresolvedIndirectJump)]
    );
    let f = &a.functions[&0x1000];
    assert_eq!(f.insn_count(), 1);
    assert!(f.edges().is_empty());
    assert!(a.call_edges().is_empty());
    assert_sorted_deduped(&a);
}

#[test]
fn decode_failure_records_queued_address_only() {
    // nop | invalid (0x06 is push-es, invalid in 64-bit) | ret: the ret is
    // never queued, so only the invalid address is recorded.
    let a = x64(&[0x90, 0x06, 0xC3], 0x1000, Some(0x1000));
    assert_eq!(
        diagnostics_of(&a),
        [(0x1001, DiagnosticKind::DecodeFailure)]
    );
    assert_eq!(a.functions[&0x1000].insn_count(), 1);
    assert_sorted_deduped(&a);
}

#[test]
fn valid_termination_is_not_a_decode_failure() {
    let a = x64(&[0xC3], 0x1000, Some(0x1000));
    assert!(a.diagnostics.records.is_empty());
    assert_eq!(reasons_of(&a, 0x1000), [DiscoveryReason::Entry]);
    let b = x64(&[0x90, 0x90, 0xC3], 0x1000, Some(0x1000));
    assert!(b.diagnostics.records.is_empty());
}

#[test]
fn empty_decode_still_reports_seed_failure() {
    // The entry byte itself is invalid: no functions, but the failure is
    // retained as evidence rather than a silent empty result.
    let a = x64(&[0x06], 0x1000, Some(0x1000));
    assert!(a.functions.is_empty());
    assert_eq!(
        diagnostics_of(&a),
        [(0x1000, DiagnosticKind::DecodeFailure)]
    );
}

#[test]
fn fresh_save_and_reopen_agree_on_diagnostics_and_reasons() {
    // call rax | ret | pad | frame func: Entry + Prologue reasons and one
    // indirect-call site in a single blob.
    let mut b = vec![0xFF, 0xD0, 0xC3];
    b.extend([0x90; 5]);
    b.extend([0x55, 0x48, 0x89, 0xE5, 0xC3]);
    let dir = common::tmpdir("diag-roundtrip");
    let bin = common::write_bin(&dir, "r.bin", &b);
    let db = common::temp_db(&dir, "r");
    let opts = RawOptions {
        base: 0x1000,
        arch: Arch::X86_64,
        entry: Some(0x1000),
    };

    let mut p = binscout::Project::open_raw(&bin, &db, opts).expect("raw open must analyze");
    let fresh: Analysis = p.analysis().clone();
    assert_eq!(
        diagnostics_of(&fresh),
        [(0x1000, DiagnosticKind::UnresolvedIndirectCall)]
    );
    assert_eq!(reasons_of(&fresh, 0x1000), [DiscoveryReason::Entry]);
    assert_eq!(reasons_of(&fresh, 0x1008), [DiscoveryReason::Prologue]);

    // Renames must not disturb provenance.
    p.rename_function(0x1008, "framed").expect("rename");
    drop(p);
    let q = binscout::Project::open_raw(&bin, &db, opts).expect("reopen must hit");
    assert_eq!(q.analysis().diagnostics, fresh.diagnostics);
    for (entry, func) in &fresh.functions {
        assert_eq!(
            q.analysis().functions[entry].reason_list(),
            func.reason_list(),
            "reasons must survive save/reopen at {entry:#x}"
        );
    }
    assert_eq!(q.function_name(0x1008).as_deref(), Some("framed"));

    // Same-mapping re-analysis after a patch keeps the shape: diagnostics
    // follow the bytes, annotations follow the address.
    let mut patched = b.clone();
    patched[0] = 0x90;
    patched[1] = 0x90;
    std::fs::write(&bin, &patched).expect("patch fixture");
    let r = binscout::Project::open_raw(&bin, &db, opts).expect("stale must reanalyze");
    assert!(r.analysis().diagnostics.records.is_empty());
    assert_eq!(r.function_name(0x1008).as_deref(), Some("framed"));
    common::cleanup(&dir);
}

#[test]
fn revision_mismatch_reanalyzes_instead_of_synthesizing_clean() {
    const BINARIES: redb::TableDefinition<u64, String> = redb::TableDefinition::new("binaries");

    let dir = common::tmpdir("diag-rev");
    let b = vec![0xFF, 0xD0, 0xC3];
    let bin = common::write_bin(&dir, "r.bin", &b);
    let db = common::temp_db(&dir, "r");
    let opts = RawOptions {
        base: 0x1000,
        arch: Arch::X86_64,
        entry: Some(0x1000),
    };
    let p = binscout::Project::open_raw(&bin, &db, opts).expect("open");
    let fresh_diagnostics = p.analysis().diagnostics.clone();
    assert_eq!(fresh_diagnostics.records.len(), 1);
    p.save().expect("save");
    drop(p);

    // Simulate a pre-diagnostics snapshot: a stale analysis revision must
    // re-analyze from the bytes, never synthesize a clean record set.
    {
        let handle = binscout::db::open_db(&db).expect("open db");
        let wtxn = handle.begin_write().expect("begin write");
        {
            let mut t = wtxn.open_table(BINARIES).expect("open binaries");
            let raw: String = t.get(0u64).expect("read").expect("row 0").value();
            let mut v: serde_json::Value = serde_json::from_str(&raw).expect("meta is json");
            v["analysis_rev"] = 1.into();
            t.insert(0u64, v.to_string()).expect("rewrite rev");
        }
        wtxn.commit().expect("commit");
    }

    let q = binscout::Project::open_raw(&bin, &db, opts).expect("stale must reanalyze");
    assert_eq!(q.analysis().diagnostics, fresh_diagnostics);
    common::cleanup(&dir);
}

#[test]
fn missing_diagnostics_table_errors_instead_of_clean_synthesis() {
    const DIAGNOSTICS: redb::TableDefinition<(u64, u8), ()> =
        redb::TableDefinition::new("diagnostics");

    let dir = common::tmpdir("diag-missing-table");
    let b = vec![0xFF, 0xD0, 0xC3];
    let bin = common::write_bin(&dir, "r.bin", &b);
    let db = common::temp_db(&dir, "r");
    let opts = RawOptions {
        base: 0x1000,
        arch: Arch::X86_64,
        entry: Some(0x1000),
    };
    let p = binscout::Project::open_raw(&bin, &db, opts).expect("open");
    assert_eq!(p.analysis().diagnostics.records.len(), 1);
    p.save().expect("save");
    drop(p);

    {
        let handle = binscout::db::open_db(&db).expect("open db");
        let wtxn = handle.begin_write().expect("begin write");
        wtxn.delete_table(DIAGNOSTICS).expect("drop diagnostics");
        wtxn.commit().expect("commit");
    }

    // Same bytes and mapping: the snapshot claims a hit but its
    // diagnostics table is gone, so the open must error rather than
    // report a clean bill of analysis.
    let err = binscout::Project::open_raw(&bin, &db, opts).unwrap_err();
    assert!(
        err.to_string().contains("diagnostics"),
        "missing diagnostics must error, got: {err:?}"
    );
    common::cleanup(&dir);
}

#[test]
fn diagnostic_cap_truncates_with_omitted_count_and_continues() {
    // `call rax` falls through, so N chained calls yield N sites.
    fn chain(n: usize) -> Vec<u8> {
        let mut b = Vec::with_capacity(n * 2);
        for _ in 0..n {
            b.extend([0xFF, 0xD0]);
        }
        b
    }
    let exact = x64(&chain(MAX_DIAGNOSTICS), 0x1000, Some(0x1000));
    assert_eq!(exact.diagnostics.records.len(), MAX_DIAGNOSTICS);
    assert!(!exact.diagnostics.truncated);
    assert_eq!(exact.diagnostics.omitted, 0);
    assert_sorted_deduped(&exact);

    let over = x64(&chain(MAX_DIAGNOSTICS + 1), 0x1000, Some(0x1000));
    assert_eq!(over.diagnostics.records.len(), MAX_DIAGNOSTICS);
    assert!(over.diagnostics.truncated);
    assert_eq!(over.diagnostics.omitted, 1);
    // Analysis continued past the cap: every call still decoded.
    assert_eq!(over.functions[&0x1000].insn_count(), MAX_DIAGNOSTICS + 1);
    assert_sorted_deduped(&over);

    // The truncated set round-trips through the sidecar bit-identically.
    let dir = common::tmpdir("diag-cap");
    let over_bytes = chain(MAX_DIAGNOSTICS + 1);
    let bin = common::write_bin(&dir, "r.bin", &over_bytes);
    let db = common::temp_db(&dir, "r");
    let opts = RawOptions {
        base: 0x1000,
        arch: Arch::X86_64,
        entry: Some(0x1000),
    };
    let p = binscout::Project::open_raw(&bin, &db, opts).expect("open");
    assert!(p.analysis().diagnostics.truncated);
    p.save().expect("save truncated snapshot");
    drop(p);
    let q = binscout::Project::open_raw(&bin, &db, opts).expect("reopen");
    assert_eq!(q.analysis().diagnostics, over.diagnostics);
    assert_eq!(q.analysis().diagnostics.omitted, 1);
    common::cleanup(&dir);
}

#[test]
fn hard_instruction_budget_stays_an_error() {
    // One nop past the decoded-instruction cap: Error::Limit, never a
    // truncated partial analysis masquerading as clean.
    let b = vec![0x90u8; binscout::analysis::MAX_TOTAL_INSNS + 1];
    let err = analyze_raw(&b, 0x1000, Arch::X86_64, Some(0x1000)).unwrap_err();
    assert!(
        matches!(err, Error::Limit(_)),
        "instruction budget must stay Limit, got: {err:?}"
    );
}

#[test]
fn diagnostics_serialize_with_hex_twins() {
    let d = Diagnostic {
        addr: 0x1000,
        kind: DiagnosticKind::UnresolvedIndirectCall,
    };
    let v = serde_json::to_value(d).expect("diagnostic serializes");
    assert_eq!(v["addr"], 0x1000);
    assert_eq!(v["addr_hex"], "0x1000");
    // Rows without the twin still deserialize (forward-compatible reads).
    let back: Diagnostic = serde_json::from_value(serde_json::json!({
        "addr": 0x1000u64,
        "kind": "UnresolvedIndirectCall",
    }))
    .expect("diagnostic without twin must decode");
    assert_eq!(back, d);
}

// §7 query layer: one blob with all three diagnostic kinds across two
// functions. Base 0x1000, entry 0x1000:
// 0x1000: call 0x1010; 0x1005: call rax (indirect call site);
// 0x1007: 0x06, invalid in 64-bit (decode failure, in no function);
// 0x1010: jmp rax (indirect jump site).
fn three_site_blob() -> Vec<u8> {
    let mut b = vec![0xE8, 0x0B, 0x00, 0x00, 0x00, 0xFF, 0xD0, 0x06];
    b.extend([0x90; 8]);
    b.extend([0xFF, 0xE0]);
    b
}

fn three_site_opts() -> RawOptions {
    RawOptions {
        base: 0x1000,
        arch: Arch::X86_64,
        entry: Some(0x1000),
    }
}

fn open_three_site(tag: &str) -> (binscout::Project, std::path::PathBuf) {
    let dir = common::tmpdir(tag);
    let bin = common::write_bin(&dir, "three.bin", &three_site_blob());
    let db = common::temp_db(&dir, "three");
    let p = binscout::Project::open_raw(&bin, &db, three_site_opts()).expect("open");
    (p, dir)
}

fn site_tuples(
    page: &binscout::Page<binscout::analysis::DiagnosticRow>,
) -> Vec<(u64, DiagnosticKind)> {
    page.items.iter().map(|r| (r.addr, r.kind)).collect()
}

#[test]
fn query_orders_by_kind_then_addr_and_pages() {
    let (p, dir) = open_three_site("diag-query-order");
    let page = p.query_diagnostics(None, None, 0, 50).expect("query");
    assert_eq!(page.protocol, binscout::WIRE_PROTOCOL);
    assert_eq!(page.total, 3);
    assert_eq!(
        site_tuples(&page),
        [
            (0x1010, DiagnosticKind::UnresolvedIndirectJump),
            (0x1005, DiagnosticKind::UnresolvedIndirectCall),
            (0x1007, DiagnosticKind::DecodeFailure),
        ]
    );
    // Paging slices the same order: successive pages concatenate exactly.
    let first = p.query_diagnostics(None, None, 0, 2).expect("page 1");
    assert_eq!(first.limit, 2);
    assert_eq!(first.next_offset, Some(2));
    let second = p.query_diagnostics(None, None, 2, 2).expect("page 2");
    assert_eq!(second.next_offset, None);
    let mut joined = site_tuples(&first);
    joined.extend(site_tuples(&second));
    assert_eq!(joined, site_tuples(&page));
    // Zero limit and past-end offsets are normal empty pages, not errors.
    let zero = p.query_diagnostics(None, None, 0, 0).expect("zero");
    assert_eq!((zero.total, zero.limit, zero.items.len()), (3, 0, 0));
    assert_eq!(zero.next_offset, None);
    let past = p.query_diagnostics(None, None, 99, 50).expect("past end");
    assert_eq!((past.total, past.items.len()), (3, 0));
    assert_eq!(past.next_offset, None);
    common::cleanup(&dir);
}

#[test]
fn query_kind_filter_precedes_pagination() {
    let (p, dir) = open_three_site("diag-query-kind");
    for (kind, addr) in [
        (DiagnosticKind::UnresolvedIndirectJump, 0x1010u64),
        (DiagnosticKind::UnresolvedIndirectCall, 0x1005u64),
        (DiagnosticKind::DecodeFailure, 0x1007u64),
    ] {
        let page = p
            .query_diagnostics(Some(kind), None, 0, 50)
            .expect("filter");
        assert_eq!(page.total, 1, "filtered total for {kind:?}");
        assert_eq!(site_tuples(&page), [(addr, kind)]);
        // The filtered total survives a zero limit: the count is exact
        // but no rows come back.
        let zero = p.query_diagnostics(Some(kind), None, 0, 0).expect("zero");
        assert_eq!((zero.total, zero.items.len()), (1, 0));
    }
    common::cleanup(&dir);
}

#[test]
fn query_func_filter_snaps_and_rejects_gaps() {
    let (p, dir) = open_three_site("diag-query-func");
    // Only the indirect call site lies in func 0x1000; the decode
    // failure at 0x1007 is in no function.
    let page = p
        .query_diagnostics(None, Some(0x1000), 0, 50)
        .expect("func");
    assert_eq!(page.total, 1);
    assert_eq!(
        site_tuples(&page),
        [(0x1005, DiagnosticKind::UnresolvedIndirectCall)]
    );
    // An interior address snaps to the same entry.
    let snapped = p
        .query_diagnostics(None, Some(0x1006), 0, 50)
        .expect("snap");
    assert_eq!(site_tuples(&snapped), site_tuples(&page));
    // The jump site's function keeps only its own row.
    let other = p
        .query_diagnostics(None, Some(0x1011), 0, 50)
        .expect("other");
    assert_eq!(
        site_tuples(&other),
        [(0x1010, DiagnosticKind::UnresolvedIndirectJump)]
    );
    // A failure site in no function, and an unmapped address, are
    // NotFound rather than empty pages.
    for addr in [0x1007u64, 0x2000] {
        let err = p.query_diagnostics(None, Some(addr), 0, 50).unwrap_err();
        assert!(
            matches!(err, Error::NotFound(_)),
            "func {addr:#x} must be NotFound, got: {err:?}"
        );
    }
    common::cleanup(&dir);
}

#[test]
fn query_rows_carry_hex_twins_func_and_detail() {
    let (p, dir) = open_three_site("diag-query-rows");
    let page = p.query_diagnostics(None, None, 0, 50).expect("query");
    let v = serde_json::to_value(&page).expect("page serializes");
    let items = v["items"].as_array().expect("items array");
    assert_eq!(items.len(), 3);
    let call = &items[1];
    assert_eq!(call["kind"], "unresolved-indirect-call");
    assert_eq!(call["addr"], 0x1005);
    assert_eq!(call["addr_hex"], "0x1005");
    assert_eq!(call["func"], 0x1000);
    assert_eq!(call["func_hex"], "0x1000");
    assert!(
        call["detail"].as_str().is_some_and(|d| !d.is_empty()),
        "kind-specific detail: {call}"
    );
    // The decode failure lies in no function: null on both sides.
    let failure = &items[2];
    assert_eq!(failure["kind"], "decode-failure");
    assert!(failure["func"].is_null() && failure["func_hex"].is_null());
    common::cleanup(&dir);
}

#[test]
fn func_meta_carries_reasons_and_survives_rename_and_reopen() {
    let (mut p, dir) = open_three_site("diag-meta-rename");
    let meta = p.func_meta(0x1010).expect("meta");
    assert_eq!(meta.reasons, [DiscoveryReason::DirectCall]);
    let v = serde_json::to_value(&meta).expect("meta serializes");
    assert_eq!(v["reasons"], serde_json::json!(["direct-call"]));
    let entry_meta =
        serde_json::to_value(p.func_meta(0x1000).expect("entry meta")).expect("meta serializes");
    assert_eq!(entry_meta["reasons"], serde_json::json!(["entry"]));
    // Renames change the name, never the provenance.
    p.rename_function(0x1010, "jump_target").expect("rename");
    let meta = p.func_meta(0x1010).expect("meta after rename");
    assert_eq!(meta.name.as_deref(), Some("jump_target"));
    assert_eq!(meta.reasons, [DiscoveryReason::DirectCall]);
    // Reasons and diagnostics survive a snapshot reopen bit-identically.
    let before = p.query_diagnostics(None, None, 0, 50).expect("before");
    p.save().expect("save");
    drop(p);
    let dir_bin = dir.join("three.bin");
    let db = common::temp_db(&dir, "three");
    let q = binscout::Project::open_raw(&dir_bin, &db, three_site_opts()).expect("reopen");
    assert_eq!(q.function_name(0x1010).as_deref(), Some("jump_target"));
    assert_eq!(
        q.func_meta(0x1010).expect("reopened meta").reasons,
        [DiscoveryReason::DirectCall]
    );
    assert_eq!(
        q.query_diagnostics(None, None, 0, 50).expect("after"),
        before
    );
    common::cleanup(&dir);
}

#[test]
fn project_info_carries_diagnostic_totals_and_revision() {
    let (p, dir) = open_three_site("diag-info");
    let info = p.project_info();
    assert_eq!(info.diagnostics, 3);
    assert!(!info.diagnostics_truncated);
    assert_eq!(info.diagnostics_omitted, 0);
    assert_eq!(
        info.analysis_revision,
        binscout::analysis::ANALYSIS_REVISION
    );
    let v = serde_json::to_value(&info).expect("info serializes");
    assert_eq!(v["diagnostics"], 3);
    assert_eq!(v["diagnostics_truncated"], false);
    assert_eq!(v["diagnostics_omitted"], 0);
    assert_eq!(
        v["analysis_revision"],
        binscout::analysis::ANALYSIS_REVISION
    );
    // `stats` reports the same four fields.
    let stats = p.stats_json().expect("stats");
    for k in [
        "diagnostics",
        "diagnostics_truncated",
        "diagnostics_omitted",
        "analysis_revision",
    ] {
        assert_eq!(stats[k], v[k], "stats {k} matches project_info");
    }
    common::cleanup(&dir);
}

#[test]
fn truncated_totals_round_trip_through_info_and_pages() {
    // `call rax` falls through, so chained calls yield one site each.
    let mut b = Vec::with_capacity((MAX_DIAGNOSTICS + 1) * 2);
    for _ in 0..MAX_DIAGNOSTICS + 1 {
        b.extend([0xFF, 0xD0]);
    }
    let dir = common::tmpdir("diag-info-cap");
    let bin = common::write_bin(&dir, "chain.bin", &b);
    let db = common::temp_db(&dir, "chain");
    let opts = RawOptions {
        base: 0x1000,
        arch: Arch::X86_64,
        entry: Some(0x1000),
    };
    let p = binscout::Project::open_raw(&bin, &db, opts).expect("open");
    let info = p.project_info();
    assert_eq!(info.diagnostics, MAX_DIAGNOSTICS);
    assert!(info.diagnostics_truncated);
    assert_eq!(info.diagnostics_omitted, 1);
    // The page total is the retained (capped) count.
    let page = p
        .query_diagnostics(Some(DiagnosticKind::UnresolvedIndirectCall), None, 0, 50)
        .expect("filtered page");
    assert_eq!(page.total, MAX_DIAGNOSTICS);
    assert_eq!(page.items.len(), 50);
    assert_eq!(page.next_offset, Some(50));
    p.save().expect("save truncated snapshot");
    drop(p);
    let q = binscout::Project::open_raw(&bin, &db, opts).expect("reopen");
    assert_eq!(q.project_info(), info);
    common::cleanup(&dir);
}

#[test]
fn querying_never_writes_a_sidecar() {
    let dir = common::tmpdir("diag-no-write");
    let bin = common::write_bin(&dir, "three.bin", &three_site_blob());
    let db = common::temp_db(&dir, "fresh");
    let p = binscout::Project::open_raw(&bin, &db, three_site_opts()).expect("open");
    let _ = p.query_diagnostics(None, None, 0, 50).expect("query");
    let _ = p.func_meta(0x1000).expect("meta");
    let _ = p.project_info();
    assert!(!db.exists(), "queries must never create the sidecar");
    common::cleanup(&dir);
}

#[test]
fn wire_spellings_are_kebab_case_with_camel_aliases() {
    assert_eq!(
        serde_json::to_value(DiscoveryReason::DirectCall).expect("reason serializes"),
        serde_json::json!("direct-call")
    );
    assert_eq!(
        serde_json::to_value(DiagnosticKind::DecodeFailure).expect("kind serializes"),
        serde_json::json!("decode-failure")
    );
    // Rows persisted under the historical CamelCase spelling still decode.
    let reason: DiscoveryReason =
        serde_json::from_value(serde_json::json!("DirectCall")).expect("alias decodes");
    assert_eq!(reason, DiscoveryReason::DirectCall);
    let kind: DiagnosticKind =
        serde_json::from_value(serde_json::json!("DecodeFailure")).expect("alias decodes");
    assert_eq!(kind, DiagnosticKind::DecodeFailure);
}