binscout 0.1.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
#![warn(missing_docs)]
//! binscout core: headless agent-first binary analysis.

pub mod analysis;
pub mod db;
pub mod disasm;
pub mod error;
pub mod ir;
pub mod loader;
pub mod mcp;

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use crate::analysis::{Analysis, Xref, XrefKind, analyze};
use crate::db::{Annotation, BinaryMeta, ProjectInfo};
use crate::disasm::{Disassembler, Insn};
use crate::error::{Error, Result};
use crate::loader::{LoadedBinary, RawOptions, load, load_raw};

/// Crate version, also served via CLI `--version` and MCP `project_info`.
pub fn version() -> &'static str {
    env!("CARGO_PKG_VERSION")
}

/// Opened analysis project: binary plus its cached analysis and annotations.
///
/// The binary file is only ever read; all persistence (analysis snapshot,
/// renames, comments) goes to the `redb` sidecar at [`Project::db_path`],
/// written solely by [`Project::save`] and the annotation setters.
#[derive(Debug)]
pub struct Project {
    path: PathBuf,
    db_path: PathBuf,
    content_hash: u64,
    size: u64,
    binary: LoadedBinary,
    analysis: Analysis,
    annotations: BTreeMap<u64, Annotation>,
}

impl Project {
    /// [`Project::open`] with an optional explicit DB path (`None` = sidecar).
    ///
    /// # Errors
    /// Same as [`Project::open`].
    pub fn open_any(binary: &Path, db: Option<&Path>) -> Result<Self> {
        match db {
            Some(d) => Self::open_with_db(binary, d),
            None => Self::open(binary),
        }
    }

    /// Open a container binary (ELF/PE/Mach-O/LE) with the default sidecar DB.
    ///
    /// Unrecognized bytes fall back to raw x86-64 at base 0 so fixture blobs
    /// and shellcode open without options; explicit bases/arches go through
    /// [`Project::open_raw`]. Reads the sidecar when present: a matching
    /// content hash loads the cached snapshot, a mismatch re-analyzes while
    /// keeping stored annotations. Never writes; call [`Project::save`].
    ///
    /// # Errors
    /// [`Error::NotFound`] if the path does not exist, [`Error::Unsupported`]
    /// if it is not a file or the arch has no disassembler, [`Error::Invalid`]
    /// on undecodable input, [`Error::Storage`] on DB or I/O failure.
    pub fn open(path: &Path) -> Result<Self> {
        Self::open_with_db(path, &db::default_db_path(path))
    }

    /// [`Project::open`] with an explicit DB path (keeps fixture dirs clean).
    ///
    /// # Errors
    /// Same as [`Project::open`].
    pub fn open_with_db(binary: &Path, db_path: &Path) -> Result<Self> {
        Self::open_inner(binary, db_path, None)
    }

    /// Open a raw blob with explicit mapping options plus explicit DB path.
    ///
    /// # Errors
    /// Same as [`Project::open`].
    pub fn open_raw(binary: &Path, db_path: &Path, opts: RawOptions) -> Result<Self> {
        Self::open_inner(binary, db_path, Some(opts))
    }

    fn open_inner(binary: &Path, db_path: &Path, raw: Option<RawOptions>) -> Result<Self> {
        if !binary.exists() {
            return Err(Error::NotFound(binary.display().to_string()));
        }
        if !binary.is_file() {
            return Err(Error::Unsupported(format!(
                "not a file: {}",
                binary.display()
            )));
        }
        let bytes = std::fs::read(binary)
            .map_err(|e| Error::Storage(format!("read {}: {e}", binary.display())))?;
        let hash = db::content_hash(&bytes);
        let size = bytes.len() as u64;

        let stored = if db_path.exists() {
            let db = db::open_db(db_path)?;
            db::load(&db)?
        } else {
            None
        };

        match stored {
            Some(hit) if hit.meta.content_hash == hash => Ok(Self {
                path: binary.to_path_buf(),
                db_path: db_path.to_path_buf(),
                content_hash: hash,
                size,
                binary: reload_binary(&bytes, raw)?,
                analysis: hit.analysis,
                annotations: hit.annotations,
            }),
            stored => {
                let annotations = stored.map(|s| s.annotations).unwrap_or_default();
                let loaded = match raw {
                    Some(opts) => load_raw(&bytes, opts)?,
                    None => load_or_raw(&bytes)?,
                };
                let analysis = analyze(&loaded)?;
                Ok(Self {
                    path: binary.to_path_buf(),
                    db_path: db_path.to_path_buf(),
                    content_hash: hash,
                    size,
                    binary: loaded,
                    analysis,
                    annotations,
                })
            }
        }
    }

    /// Persist the analysis snapshot and annotations to the sidecar DB.
    ///
    /// Only ever writes [`Project::db_path`]; the binary is untouched.
    ///
    /// # Errors
    /// [`Error::Storage`] on any `redb` or encoding failure.
    pub fn save(&self) -> Result<()> {
        let db = db::open_db(&self.db_path)?;
        db::save(&db, &self.meta(), &self.analysis, &self.annotations)?;
        Ok(())
    }

    fn meta(&self) -> BinaryMeta {
        BinaryMeta {
            version: db::SCHEMA_VERSION,
            binary_path: self.path.display().to_string(),
            content_hash: self.content_hash,
            size: self.size,
            arch: db::arch_name(self.binary.arch).to_string(),
            format: db::format_name(self.binary.format).to_string(),
            entry: self.binary.entry,
        }
    }

    /// Agent-facing summary; always JSON-serializable.
    #[must_use]
    pub fn project_info(&self) -> ProjectInfo {
        let s = self.analysis.summary();
        ProjectInfo {
            binary: self.path.display().to_string(),
            db_path: self.db_path.display().to_string(),
            version: version().to_string(),
            arch: db::arch_name(self.binary.arch).to_string(),
            format: db::format_name(self.binary.format).to_string(),
            entry: self.binary.entry,
            content_hash: self.content_hash,
            size: self.size,
            functions: s.functions,
            blocks: s.blocks,
            insns: s.insns,
            xrefs: s.xrefs,
            strings: s.strings,
            annotations: self.annotations.values().filter(|a| !a.is_empty()).count(),
        }
    }

    /// Path of the analyzed binary.
    #[must_use]
    pub fn path(&self) -> &Path {
        &self.path
    }

    /// Path of the `redb` sidecar (only file this project writes).
    #[must_use]
    pub fn db_path(&self) -> &Path {
        &self.db_path
    }

    /// Content hash the in-memory analysis was taken over.
    #[must_use]
    pub fn content_hash(&self) -> u64 {
        self.content_hash
    }

    /// Parsed binary headers, segments, and symbols.
    #[must_use]
    pub fn binary(&self) -> &LoadedBinary {
        &self.binary
    }

    /// Cached analysis (functions, CFGs, xrefs, strings).
    #[must_use]
    pub fn analysis(&self) -> &Analysis {
        &self.analysis
    }

    /// All annotations keyed by address.
    #[must_use]
    pub fn annotations(&self) -> &BTreeMap<u64, Annotation> {
        &self.annotations
    }

    /// Annotation at `addr`, if any.
    #[must_use]
    pub fn annotation(&self, addr: u64) -> Option<&Annotation> {
        self.annotations.get(&addr).filter(|a| !a.is_empty())
    }

    /// Display name of a function: annotation rename wins over export name.
    #[must_use]
    pub fn function_name(&self, entry: u64) -> Option<String> {
        if let Some(n) = self
            .annotations
            .get(&entry)
            .and_then(|a| a.name.clone())
            .filter(|n| !n.is_empty())
        {
            return Some(n);
        }
        self.analysis.functions.get(&entry)?.name.clone()
    }

    /// Rename a function in the DB only; the binary is never modified.
    ///
    /// An empty `name` clears the rename. Persists immediately.
    ///
    /// # Errors
    /// [`Error::NotFound`] for unknown function entries, [`Error::Storage`]
    /// when the annotation cannot be persisted.
    pub fn rename_function(&mut self, entry: u64, name: &str) -> Result<()> {
        if !self.analysis.functions.contains_key(&entry) {
            return Err(Error::NotFound(format!("function {entry:#x}")));
        }
        let ann = self.annotations.entry(entry).or_default();
        ann.name = (!name.is_empty()).then(|| name.to_string());
        if ann.is_empty() {
            self.annotations.remove(&entry);
        }
        self.save()
    }

    /// Comment an address in the DB only; the binary is never modified.
    ///
    /// Any address is commentable (instructions, functions, strings, data).
    /// An empty `comment` clears the comment. Persists immediately.
    ///
    /// # Errors
    /// [`Error::Storage`] when the annotation cannot be persisted.
    pub fn set_comment(&mut self, addr: u64, comment: &str) -> Result<()> {
        let ann = self.annotations.entry(addr).or_default();
        ann.comment = (!comment.is_empty()).then(|| comment.to_string());
        if ann.is_empty() {
            self.annotations.remove(&addr);
        }
        self.save()
    }

    /// File-backed bytes mapped at `va` (zero-padded past file data).
    ///
    /// # Errors
    /// [`Error::Limit`] for empty/oversize ranges, [`Error::NotFound`] when
    /// the range is not fully mapped.
    pub fn bytes_at(&self, va: u64, len: usize) -> Result<Vec<u8>> {
        if len == 0 || len > MAX_RANGE_BYTES {
            return Err(Error::Limit(format!("range length {len} out of bounds")));
        }
        let end = va.wrapping_add(len as u64);
        let seg = self
            .binary
            .segments
            .iter()
            .find(|s| va >= s.vaddr && end <= s.vaddr.wrapping_add(s.mem_size))
            .ok_or_else(|| Error::NotFound(format!("no bytes mapped at {va:#x}")))?;
        let mut out = vec![0u8; len];
        let off = (va - seg.vaddr) as usize;
        let have = seg.data.len().saturating_sub(off).min(len);
        out[..have].copy_from_slice(&seg.data[off..off + have]);
        Ok(out)
    }

    /// Disassemble `len` bytes at `va` with the binary's architecture.
    ///
    /// # Errors
    /// Same as [`Project::bytes_at`], plus [`Error::Invalid`] on undecodable
    /// bytes.
    pub fn disasm_at(&self, addr: u64, len: usize) -> Result<Vec<Insn>> {
        let bytes = self.bytes_at(addr, len)?;
        self.binary.arch.disassembler().disasm_range(&bytes, addr)
    }

    /// Function rows with annotation renames resolved, ordered by entry.
    #[must_use]
    pub fn functions_resolved(&self, offset: usize, limit: usize) -> Vec<serde_json::Value> {
        self.analysis
            .functions_page(offset, limit.min(MAX_ROWS))
            .into_iter()
            .map(|f| {
                serde_json::json!({
                    "id": f.id, "entry": f.entry,
                    "name": self.function_name(f.entry),
                    "blocks": f.blocks, "insns": f.insns, "calls": f.calls,
                })
            })
            .collect()
    }

    /// Full function detail: blocks with instructions in address order.
    ///
    /// # Errors
    /// [`Error::NotFound`] for unknown function entries.
    pub fn func_detail(&self, entry: u64) -> Result<serde_json::Value> {
        let f = self
            .analysis
            .functions
            .get(&entry)
            .ok_or_else(|| Error::NotFound(format!("function {entry:#x}")))?;
        let blocks: Vec<serde_json::Value> = f
            .blocks
            .values()
            .map(|b| {
                serde_json::json!({
                    "id": b.id(entry), "start": b.start, "end": b.end(),
                    "insns": b.insns, "succs": f.successors(b.start),
                })
            })
            .collect();
        Ok(serde_json::json!({
            "id": f.id(), "entry": f.entry,
            "name": self.function_name(entry),
            "blocks": blocks, "edges": f.edges(),
            "calls": self.analysis.calls_of(entry),
        }))
    }

    /// CFG of one function: block nodes plus `(from, to)` block edges.
    ///
    /// # Errors
    /// [`Error::NotFound`] for unknown function entries.
    pub fn cfg_of(&self, entry: u64) -> Result<serde_json::Value> {
        let f = self
            .analysis
            .functions
            .get(&entry)
            .ok_or_else(|| Error::NotFound(format!("function {entry:#x}")))?;
        let nodes: Vec<serde_json::Value> = f
            .blocks
            .values()
            .map(|b| {
                serde_json::json!({
                    "start": b.start, "end": b.end(),
                    "insns": b.insns.len(), "succs": f.successors(b.start),
                })
            })
            .collect();
        Ok(serde_json::json!({ "entry": entry, "nodes": nodes, "edges": f.edges() }))
    }

    /// Lift `len` bytes at `va` to IR.
    ///
    /// # Errors
    /// Same as [`Project::disasm_at`].
    pub fn ir_at(&self, addr: u64, len: usize) -> Result<serde_json::Value> {
        let insns = self.disasm_at(addr, len)?;
        let lifted = crate::ir::lift_range(self.binary.arch, &insns);
        Ok(serde_json::json!({ "addr": addr, "insns": insns.len(), "ir": lifted }))
    }

    /// Xrefs filtered by endpoint/kind, paged.
    #[must_use]
    pub fn query_xrefs(
        &self,
        to: Option<u64>,
        from: Option<u64>,
        kind: Option<XrefKind>,
        offset: usize,
        limit: usize,
    ) -> Vec<Xref> {
        self.analysis
            .xrefs
            .iter()
            .copied()
            .filter(|x| to.is_none_or(|t| x.to == t))
            .filter(|x| from.is_none_or(|f| x.from == f))
            .filter(|x| kind.is_none_or(|k| x.kind == k))
            .skip(offset)
            .take(limit.min(MAX_ROWS))
            .collect()
    }

    /// Imports plus exports for symbol views.
    #[must_use]
    pub fn symbols_json(&self) -> serde_json::Value {
        serde_json::json!({ "imports": self.binary.imports, "exports": self.binary.exports })
    }

    /// Current annotation at `addr`.
    #[must_use]
    pub fn annotation_json(&self, addr: u64) -> serde_json::Value {
        match self.annotation(addr) {
            Some(a) => serde_json::json!({ "addr": addr, "name": a.name, "comment": a.comment }),
            None => serde_json::json!({ "addr": addr, "name": null, "comment": null }),
        }
    }
}

/// Cap on raw bytes per [`Project::bytes_at`] call.
pub const MAX_RANGE_BYTES: usize = 1 << 20;
/// Cap on rows per paged query.
const MAX_ROWS: usize = 1024;

/// Parse a CLI/MCP address: `0x`-prefixed hex or plain decimal.
///
/// # Errors
/// [`Error::Invalid`] when the string is neither.
pub fn parse_addr(s: &str) -> Result<u64> {
    let s = s.trim();
    let v = if let Some(hex) = s.strip_prefix("0x").or_else(|| s.strip_prefix("0X")) {
        u64::from_str_radix(hex, 16)
    } else {
        s.parse::<u64>()
    };
    v.map_err(|_| Error::Invalid(format!("bad address: {s:?}")))
}

/// Parse an xref kind filter: `code`, `data`, or `string`.
///
/// # Errors
/// [`Error::Invalid`] for anything else.
pub fn parse_xref_kind(s: &str) -> Result<XrefKind> {
    match s.to_lowercase().as_str() {
        "code" => Ok(XrefKind::Code),
        "data" => Ok(XrefKind::Data),
        "string" => Ok(XrefKind::String),
        _ => Err(Error::Invalid(format!("bad xref kind: {s:?}"))),
    }
}

/// Re-read the binary bytes for a hash-matching cached snapshot.
///
/// Raw blobs need their mapping options again; containers re-parse.
fn reload_binary(bytes: &[u8], raw: Option<RawOptions>) -> Result<LoadedBinary> {
    match raw {
        Some(opts) => load_raw(bytes, opts),
        None => load_or_raw(bytes),
    }
}

/// Container parse with raw x86-64 fallback for unrecognized bytes.
///
/// Only [`Error::Invalid`] (not a container) falls back; recognized
/// containers with unsupported arches still report [`Error::Unsupported`].
fn load_or_raw(bytes: &[u8]) -> Result<LoadedBinary> {
    match load(bytes) {
        Ok(b) => Ok(b),
        Err(Error::Invalid(_)) => load_raw(
            bytes,
            RawOptions {
                base: 0,
                arch: crate::disasm::Arch::X86_64,
                entry: None,
            },
        ),
        Err(e) => Err(e),
    }
}