use std::collections::{BTreeMap, BTreeSet};
use petgraph::graphmap::DiGraphMap;
use serde::{Deserialize, Serialize};
use crate::disasm::{Arch, Disassembler, Insn};
use crate::error::{Error, Result};
use crate::ir::IrOp;
use crate::loader::strings::{self, Encoding, StringHit};
use crate::loader::{LoadedBinary, RawOptions, Section, Segment, load_raw};
pub const MAX_TOTAL_INSNS: usize = 1_000_000;
pub const MAX_FUNCTIONS: usize = 100_000;
pub const MAX_XREFS_PER_KIND: usize = 1_000_000;
pub const MAX_SEEDS: usize = 100_000;
pub const MAX_PAGE: usize = 1024;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum XrefKind {
Code,
Data,
String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
pub struct Xref {
pub from: u64,
pub to: u64,
pub kind: XrefKind,
}
impl Serialize for Xref {
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("Xref", 5)?;
st.serialize_field("from", &self.from)?;
st.serialize_field("from_hex", &crate::addr_hex(self.from))?;
st.serialize_field("to", &self.to)?;
st.serialize_field("to_hex", &crate::addr_hex(self.to))?;
st.serialize_field("kind", &self.kind)?;
st.end()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BasicBlock {
pub start: u64,
pub insns: Vec<Insn>,
}
impl BasicBlock {
#[must_use]
pub fn end(&self) -> u64 {
self.insns
.last()
.map(|i| i.addr.wrapping_add(u64::from(i.size)))
.unwrap_or(self.start)
}
#[must_use]
pub fn id(&self, func_entry: u64) -> String {
format!("block@{func_entry:#x}+{:#x}", self.start)
}
}
#[derive(Debug, Clone)]
pub struct Function {
pub entry: u64,
pub name: Option<String>,
pub blocks: BTreeMap<u64, BasicBlock>,
cfg: DiGraphMap<u64, ()>,
}
impl Function {
#[must_use]
pub fn id(&self) -> String {
format!("func@{:#x}", self.entry)
}
#[must_use]
pub fn insn_count(&self) -> usize {
self.blocks.values().map(|b| b.insns.len()).sum()
}
#[must_use]
pub fn successors(&self, block: u64) -> Vec<u64> {
self.cfg.neighbors(block).collect()
}
#[must_use]
pub fn edges(&self) -> Vec<(u64, u64)> {
self.cfg.all_edges().map(|(a, b, _)| (a, b)).collect()
}
#[must_use]
pub fn from_parts(
entry: u64,
name: Option<String>,
blocks: BTreeMap<u64, BasicBlock>,
edges: &[(u64, u64)],
) -> Self {
let mut cfg = DiGraphMap::new();
for &start in blocks.keys() {
cfg.add_node(start);
}
for &(a, b) in edges {
if blocks.contains_key(&a) && blocks.contains_key(&b) {
cfg.add_edge(a, b, ());
}
}
Self {
entry,
name,
blocks,
cfg,
}
}
}
#[derive(Debug, Clone)]
pub struct Analysis {
pub arch: Arch,
pub functions: BTreeMap<u64, Function>,
callgraph: DiGraphMap<u64, ()>,
pub xrefs: Vec<Xref>,
pub strings: Vec<StringHit>,
}
impl Analysis {
#[must_use]
pub fn containing_function(&self, va: u64) -> Option<u64> {
let (&entry, func) = self.functions.range(..=va).next_back()?;
func.blocks
.values()
.any(|b| va >= b.start && va < b.end())
.then_some(entry)
}
#[must_use]
pub fn calls_of(&self, entry: u64) -> Vec<u64> {
let mut v: Vec<u64> = self.callgraph.neighbors(entry).collect();
v.sort_unstable();
v
}
#[must_use]
pub fn callers_of(&self, entry: u64) -> Vec<u64> {
let mut v: Vec<u64> = self
.callgraph
.neighbors_directed(entry, petgraph::Direction::Incoming)
.collect();
v.sort_unstable();
v
}
#[must_use]
pub fn call_edges(&self) -> Vec<(u64, u64)> {
self.callgraph.all_edges().map(|(a, b, _)| (a, b)).collect()
}
#[must_use]
pub fn from_parts(
arch: Arch,
functions: BTreeMap<u64, Function>,
call_edges: &[(u64, u64)],
xrefs: Vec<Xref>,
strings: Vec<StringHit>,
) -> Self {
let mut callgraph = DiGraphMap::<u64, ()>::new();
for e in functions.keys() {
callgraph.add_node(*e);
}
for &(a, b) in call_edges {
if functions.contains_key(&a) && functions.contains_key(&b) {
callgraph.add_edge(a, b, ());
}
}
Self {
arch,
functions,
callgraph,
xrefs,
strings,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct AnalysisSummary {
pub functions: usize,
pub blocks: usize,
pub insns: usize,
pub xrefs: usize,
pub strings: usize,
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct FunctionSummary {
pub id: String,
pub entry: u64,
pub name: Option<String>,
pub blocks: usize,
pub insns: usize,
pub calls: Vec<u64>,
}
impl Serialize for FunctionSummary {
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("FunctionSummary", 7)?;
st.serialize_field("id", &self.id)?;
st.serialize_field("entry", &self.entry)?;
st.serialize_field("entry_hex", &crate::addr_hex(self.entry))?;
st.serialize_field("name", &self.name)?;
st.serialize_field("blocks", &self.blocks)?;
st.serialize_field("insns", &self.insns)?;
st.serialize_field("calls", &self.calls)?;
st.end()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct BlockSummary {
pub id: String,
pub start: u64,
pub end: u64,
pub insns: usize,
pub succs: Vec<u64>,
}
impl Serialize for BlockSummary {
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("BlockSummary", 7)?;
st.serialize_field("id", &self.id)?;
st.serialize_field("start", &self.start)?;
st.serialize_field("start_hex", &crate::addr_hex(self.start))?;
st.serialize_field("end", &self.end)?;
st.serialize_field("end_hex", &crate::addr_hex(self.end))?;
st.serialize_field("insns", &self.insns)?;
st.serialize_field("succs", &self.succs)?;
st.end()
}
}
fn truncate_limit(limit: usize) -> usize {
limit.min(MAX_PAGE)
}
#[derive(Debug, Clone)]
pub struct StringsFilter {
pub pattern: Option<String>,
pub min_len: Option<usize>,
pub section: Option<String>,
pub include_code: bool,
pub start: usize,
pub limit: usize,
}
impl Default for StringsFilter {
fn default() -> Self {
Self {
pattern: None,
min_len: None,
section: None,
include_code: false,
start: 0,
limit: 50,
}
}
}
fn page_slice<T: Clone>(items: &[T], offset: usize, limit: usize) -> Vec<T> {
items
.iter()
.skip(offset)
.take(truncate_limit(limit))
.cloned()
.collect()
}
impl Analysis {
#[must_use]
pub fn summary(&self) -> AnalysisSummary {
AnalysisSummary {
functions: self.functions.len(),
blocks: self.functions.values().map(|f| f.blocks.len()).sum(),
insns: self.functions.values().map(|f| f.insn_count()).sum(),
xrefs: self.xrefs.len(),
strings: self.strings.len(),
}
}
#[must_use]
pub fn functions_page(&self, offset: usize, limit: usize) -> Vec<FunctionSummary> {
let rows: Vec<FunctionSummary> = self
.functions
.values()
.map(|f| FunctionSummary {
id: f.id(),
entry: f.entry,
name: f.name.clone(),
blocks: f.blocks.len(),
insns: f.insn_count(),
calls: self.calls_of(f.entry),
})
.collect();
page_slice(&rows, offset, limit)
}
#[must_use]
pub fn blocks_page(
&self,
func_entry: u64,
offset: usize,
limit: usize,
) -> Option<Vec<BlockSummary>> {
let f = self.functions.get(&func_entry)?;
let rows: Vec<BlockSummary> = f
.blocks
.values()
.map(|b| BlockSummary {
id: b.id(func_entry),
start: b.start,
end: b.end(),
insns: b.insns.len(),
succs: f.successors(b.start),
})
.collect();
Some(page_slice(&rows, offset, limit))
}
#[must_use]
pub fn block_insns(
&self,
func_entry: u64,
block_start: u64,
offset: usize,
limit: usize,
) -> Option<Vec<Insn>> {
let b = self.functions.get(&func_entry)?.blocks.get(&block_start)?;
Some(page_slice(&b.insns, offset, limit))
}
#[must_use]
pub fn xrefs_page(&self, kind: Option<XrefKind>, offset: usize, limit: usize) -> Vec<Xref> {
let rows: Vec<Xref> = match kind {
Some(k) => self.xrefs.iter().copied().filter(|x| x.kind == k).collect(),
None => self.xrefs.clone(),
};
page_slice(&rows, offset, limit)
}
#[must_use]
pub fn xrefs_to(&self, addr: u64) -> Vec<Xref> {
self.xrefs
.iter()
.copied()
.filter(|x| x.to == addr)
.collect()
}
#[must_use]
pub fn xrefs_from(&self, addr: u64) -> Vec<Xref> {
self.xrefs
.iter()
.copied()
.filter(|x| x.from == addr)
.collect()
}
#[must_use]
pub fn strings_page(&self, offset: usize, limit: usize) -> Vec<StringHit> {
page_slice(&self.strings, offset, limit)
}
pub fn strings_page_filtered(&self, filter: &StringsFilter) -> Result<Vec<StringHit>> {
let show_code = filter.include_code || filter.section.is_some() || filter.pattern.is_some();
let mut rows: Vec<StringHit> = Vec::new();
for h in &self.strings {
if h.executable && !show_code {
continue;
}
if let Some(sec) = &filter.section
&& h.section.as_deref() != Some(sec.as_str())
{
continue;
}
if let Some(min) = filter.min_len
&& h.value.chars().count() < min
{
continue;
}
if let Some(pat) = &filter.pattern
&& !strings::matches_pattern(&h.value, pat)?
{
continue;
}
rows.push(h.clone());
}
Ok(page_slice(&rows, filter.start, filter.limit))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Flow {
Seq,
Branch { target: Option<u64> },
CBranch { target: Option<u64> },
Call { target: Option<u64> },
Ret,
}
impl Flow {
fn ends_block(self) -> bool {
!matches!(self, Self::Seq)
}
}
fn classify(arch: Arch, insn: &Insn) -> Flow {
match crate::ir::lift(arch, insn)
.first()
.map(|i| (i.op, i.target))
{
Some((IrOp::Branch, target)) => Flow::Branch { target },
Some((IrOp::CBranch, target)) => Flow::CBranch { target },
Some((IrOp::Call, target)) => Flow::Call { target },
Some((IrOp::Return, _)) => Flow::Ret,
_ => Flow::Seq,
}
}
fn locate_hit(sections: &[Section], seg: &Segment, vaddr: Option<u64>) -> (Option<String>, bool) {
let Some(va) = vaddr else {
return (None, false);
};
if let Some(sec) = sections
.iter()
.find(|s| va >= s.vaddr && va.wrapping_sub(s.vaddr) < s.size)
{
return (Some(sec.name.clone()), sec.executable);
}
(None, seg.executable)
}
struct AddrSpace<'a> {
exec: Vec<&'a Segment>,
all: Vec<&'a Segment>,
}
impl<'a> AddrSpace<'a> {
fn new(bin: &'a LoadedBinary) -> Self {
Self {
exec: bin.segments.iter().filter(|s| s.executable).collect(),
all: bin.segments.iter().collect(),
}
}
fn seg_at(segs: &[&'a Segment], va: u64) -> Option<&'a Segment> {
segs.iter()
.find(|s| va >= s.vaddr && va.wrapping_sub(s.vaddr) < s.mem_size)
.copied()
}
fn code_bytes(&self, va: u64) -> Option<&'a [u8]> {
let seg = Self::seg_at(&self.exec, va)?;
let off = va.wrapping_sub(seg.vaddr) as usize;
seg.data.get(off..)
}
fn contains(&self, va: u64) -> bool {
Self::seg_at(&self.all, va).is_some()
}
fn first_exec_base(&self) -> Option<u64> {
self.exec.iter().map(|s| s.vaddr).min()
}
}
fn decode_one(arch: Arch, space: &AddrSpace, va: u64) -> Result<Option<Insn>> {
let bytes = match space.code_bytes(va) {
Some(b) if !b.is_empty() => b,
_ => return Ok(None),
};
let dis = arch.disassembler();
match arch {
Arch::X86_64 | Arch::X86_32 => {
let mut len = bytes.len().min(15);
loop {
match dis.disasm_range(&bytes[..len], va) {
Ok(mut v) if !v.is_empty() => return Ok(Some(v.remove(0))),
Ok(_) => return Ok(None),
Err(Error::Invalid(_)) if len > 1 => len -= 1,
Err(Error::Invalid(_)) => return Ok(None),
Err(e) => return Err(e),
}
}
}
Arch::Aarch64 | Arch::Arm32 => {
if bytes.len() < 4 {
return Ok(None);
}
match dis.disasm_range(&bytes[..4], va) {
Ok(mut v) if !v.is_empty() => Ok(Some(v.remove(0))),
Ok(_) => Ok(None),
Err(Error::Invalid(_)) => Ok(None),
Err(e) => Err(e),
}
}
Arch::Thumb => decode_one_thumb(&dis, bytes, va),
}
}
fn decode_one_thumb(dis: &crate::disasm::AnyDisasm, bytes: &[u8], va: u64) -> Result<Option<Insn>> {
if bytes.len() >= 4 {
match dis.disasm_range(&bytes[..4], va) {
Ok(mut v) if !v.is_empty() => return Ok(Some(v.remove(0))),
Ok(_) => return Ok(None),
Err(Error::Invalid(_)) => {}
Err(e) => return Err(e),
}
}
if bytes.len() >= 2 {
match dis.disasm_range(&bytes[..2], va) {
Ok(mut v) if !v.is_empty() => Ok(Some(v.remove(0))),
Ok(_) => Ok(None),
Err(Error::Invalid(_)) => Ok(None),
Err(e) => Err(e),
}
} else {
Ok(None)
}
}
struct Decoded {
insn: Insn,
flow: Flow,
}
fn authoritative_seeds(space: &AddrSpace, bin: &LoadedBinary) -> Vec<u64> {
let mut seeds: Vec<u64> = Vec::new();
if let Some(entry) = bin.entry
&& space.code_bytes(entry).is_some()
{
seeds.push(entry);
}
for exp in &bin.exports {
if let Some(addr) = exp.address
&& seeds.len() < MAX_SEEDS
&& space.code_bytes(addr).is_some()
{
seeds.push(addr);
}
}
if seeds.is_empty()
&& let Some(base) = space.first_exec_base()
{
seeds.push(base);
}
seeds.sort_unstable();
seeds.dedup();
seeds
}
fn prologue_seeds(arch: Arch, space: &AddrSpace, reached: &BTreeSet<u64>) -> Result<Vec<u64>> {
let mut seeds: Vec<u64> = Vec::new();
for va in prologue_scan(arch, space) {
if seeds.len() >= MAX_SEEDS {
break;
}
if !reached.contains(&va) && decode_one(arch, space, va)?.is_some() {
seeds.push(va);
}
}
Ok(seeds)
}
fn prologue_patterns(arch: Arch) -> &'static [&'static [u8]] {
match arch {
Arch::X86_64 => &[
&[0x48, 0x89, 0xE5],
&[0x48, 0x83, 0xEC],
&[0x48, 0x81, 0xEC],
&[0xF3, 0x0F, 0x1E, 0xFA],
],
Arch::X86_32 => &[&[0x89, 0xE5], &[0x83, 0xEC], &[0x81, 0xEC]],
Arch::Aarch64 | Arch::Arm32 | Arch::Thumb => &[],
}
}
fn prologue_scan(arch: Arch, space: &AddrSpace) -> Vec<u64> {
let mut out = Vec::new();
for seg in &space.exec {
if arch == Arch::Aarch64 {
let mut off = 0usize;
while off + 4 <= seg.data.len() {
let w = u32::from_le_bytes(seg.data[off..off + 4].try_into().unwrap());
if (w & 0xFFC0_7FFF) == 0xA980_7BFD {
out.push(seg.vaddr.wrapping_add(off as u64));
}
off += 4;
}
continue;
}
if arch == Arch::Arm32 {
let mut off = 0usize;
while off + 4 <= seg.data.len() {
let w = u32::from_le_bytes(seg.data[off..off + 4].try_into().unwrap());
if is_arm_push_lr(w) {
out.push(seg.vaddr.wrapping_add(off as u64));
}
off += 4;
}
continue;
}
if arch == Arch::Thumb {
let mut off = 0usize;
while off + 2 <= seg.data.len() {
let hw = u16::from_le_bytes(seg.data[off..off + 2].try_into().unwrap());
if (hw & 0xFF00) == 0xB500 && (hw & 0x0100) != 0 {
out.push(seg.vaddr.wrapping_add(off as u64));
} else if off + 4 <= seg.data.len() {
let w = u32::from_le_bytes(seg.data[off..off + 4].try_into().unwrap());
if is_arm_push_lr(w) {
out.push(seg.vaddr.wrapping_add(off as u64));
}
}
off += 2;
}
continue;
}
for pat in prologue_patterns(arch) {
for (i, win) in seg.data.windows(pat.len()).enumerate() {
if win != *pat {
continue;
}
let mut va = seg.vaddr.wrapping_add(i as u64);
if i > 0 && seg.data[i - 1] == 0x55 {
va = va.wrapping_sub(1);
}
out.push(va);
if out.len() >= MAX_SEEDS {
return out;
}
}
}
}
out.sort_unstable();
out.dedup();
out
}
fn is_arm_push_lr(word: u32) -> bool {
(word & 0x0FFF_4000) == 0x092D_4000
}
struct Walker<'a> {
arch: Arch,
space: &'a AddrSpace<'a>,
decoded: BTreeMap<u64, Decoded>,
jump_targets: BTreeSet<u64>,
fallthroughs: BTreeSet<u64>,
call_targets: BTreeSet<u64>,
}
impl<'a> Walker<'a> {
fn new(arch: Arch, space: &'a AddrSpace<'a>) -> Self {
Self {
arch,
space,
decoded: BTreeMap::new(),
jump_targets: BTreeSet::new(),
fallthroughs: BTreeSet::new(),
call_targets: BTreeSet::new(),
}
}
fn run(&mut self, seeds: Vec<u64>) -> Result<()> {
let mut stack = seeds;
while let Some(va) = stack.pop() {
if self.decoded.contains_key(&va) {
continue;
}
if self.decoded.len() >= MAX_TOTAL_INSNS {
return Err(Error::Limit(format!(
"more than {MAX_TOTAL_INSNS} instructions"
)));
}
let Some(insn) = decode_one(self.arch, self.space, va)? else {
continue;
};
let flow = classify(self.arch, &insn);
let next = va.wrapping_add(u64::from(insn.size));
match flow {
Flow::Seq => stack.push(next),
Flow::Branch { target } => {
if let Some(t) = target {
self.jump_targets.insert(t);
stack.push(t);
}
}
Flow::CBranch { target } => {
if let Some(t) = target {
self.jump_targets.insert(t);
stack.push(t);
}
self.fallthroughs.insert(next);
stack.push(next);
}
Flow::Call { target } => {
if let Some(t) = target {
self.jump_targets.insert(t);
self.call_targets.insert(t);
stack.push(t);
}
self.fallthroughs.insert(next);
stack.push(next);
}
Flow::Ret => {}
}
self.decoded.insert(va, Decoded { insn, flow });
}
Ok(())
}
}
pub fn analyze(bin: &LoadedBinary) -> Result<Analysis> {
let arch = bin.arch;
let space = AddrSpace::new(bin);
let mut walk = Walker::new(arch, &space);
let seeds1 = authoritative_seeds(&space, bin);
walk.run(seeds1.clone())?;
let round1: BTreeSet<u64> = walk.decoded.keys().copied().collect();
let seeds2 = prologue_seeds(arch, &space, &round1)?;
walk.run(seeds2.clone())?;
let Walker {
decoded,
jump_targets,
fallthroughs,
call_targets,
..
} = walk;
if decoded.is_empty() {
return Ok(Analysis {
arch,
functions: BTreeMap::new(),
callgraph: DiGraphMap::new(),
xrefs: Vec::new(),
strings: Vec::new(),
});
}
let mut entries: BTreeSet<u64> = BTreeSet::new();
for s in seeds1.iter().chain(seeds2.iter()) {
if decoded.contains_key(s) {
entries.insert(*s);
}
}
for t in &call_targets {
if decoded.contains_key(t) {
entries.insert(*t);
}
}
if entries.is_empty()
&& let Some((&first, _)) = decoded.iter().next()
{
entries.insert(first);
}
if entries.len() > MAX_FUNCTIONS {
return Err(Error::Limit(format!("more than {MAX_FUNCTIONS} functions")));
}
let entry_list: Vec<u64> = entries.iter().copied().collect();
let owner = |va: u64| -> u64 {
match entry_list.iter().rfind(|e| **e <= va) {
Some(e) => *e,
None => entry_list[0],
}
};
let mut starts: BTreeSet<u64> = BTreeSet::new();
starts.extend(entries.iter().copied());
starts.extend(jump_targets.iter().copied());
starts.extend(fallthroughs.iter().copied());
let mut blocks: BTreeMap<u64, BasicBlock> = BTreeMap::new();
let mut cur_start: Option<u64> = None;
let mut cur: Vec<Insn> = Vec::new();
let flush =
|start: &mut Option<u64>, cur: &mut Vec<Insn>, blocks: &mut BTreeMap<u64, BasicBlock>| {
if let Some(s) = start.take()
&& !cur.is_empty()
{
blocks.insert(
s,
BasicBlock {
start: s,
insns: std::mem::take(cur),
},
);
}
};
for (&va, d) in &decoded {
if starts.contains(&va) {
flush(&mut cur_start, &mut cur, &mut blocks);
cur_start = Some(va);
} else if cur_start.is_none() {
cur_start = Some(va);
}
cur.push(d.insn.clone());
if d.flow.ends_block() {
flush(&mut cur_start, &mut cur, &mut blocks);
}
}
flush(&mut cur_start, &mut cur, &mut blocks);
let mut names: BTreeMap<u64, String> = BTreeMap::new();
for exp in &bin.exports {
if let Some(addr) = exp.address {
names.entry(addr).or_insert_with(|| exp.name.clone());
}
}
let mut functions: BTreeMap<u64, Function> = BTreeMap::new();
for e in &entries {
functions.insert(
*e,
Function {
entry: *e,
name: names.get(e).cloned(),
blocks: BTreeMap::new(),
cfg: DiGraphMap::new(),
},
);
}
for (start, block) in blocks {
let o = owner(start);
if let Some(f) = functions.get_mut(&o) {
f.cfg.add_node(start);
f.blocks.insert(start, block);
}
}
functions.retain(|_, f| !f.blocks.is_empty());
let mut callgraph = DiGraphMap::<u64, ()>::new();
for e in functions.keys() {
callgraph.add_node(*e);
}
let keys: Vec<u64> = functions.keys().copied().collect();
let mut cfg_edges: Vec<(u64, u64, u64)> = Vec::new();
let mut cg_edges: Vec<(u64, u64)> = Vec::new();
for entry in keys {
let f = &functions[&entry];
for (start, block) in &f.blocks {
let last = block.insns.last().expect("blocks hold at least one insn");
let flow = classify(arch, last);
let end = block.end();
match flow {
Flow::Branch { target: Some(t) } => {
if f.blocks.contains_key(&t) {
cfg_edges.push((entry, *start, t));
} else if decoded.contains_key(&t) {
cg_edges.push((entry, owner(t)));
}
}
Flow::Branch { target: None } => {}
Flow::CBranch { target } => {
if let Some(t) = target
&& f.blocks.contains_key(&t)
{
cfg_edges.push((entry, *start, t));
}
if f.blocks.contains_key(&end) {
cfg_edges.push((entry, *start, end));
}
}
Flow::Call { target } => {
if let Some(t) = target
&& decoded.contains_key(&t)
{
cg_edges.push((entry, owner(t)));
}
if f.blocks.contains_key(&end) {
cfg_edges.push((entry, *start, end));
}
}
Flow::Ret => {}
Flow::Seq => {
if f.blocks.contains_key(&end) {
cfg_edges.push((entry, *start, end));
}
}
}
}
}
for (func, a, b) in cfg_edges {
functions
.get_mut(&func)
.expect("function retained")
.cfg
.add_edge(a, b, ());
}
for (a, b) in cg_edges {
if functions.contains_key(&b) {
callgraph.add_edge(a, b, ());
}
}
let mut xrefs: BTreeSet<(u64, XrefKind, u64)> = BTreeSet::new();
for (&va, d) in &decoded {
let target = match d.flow {
Flow::Branch { target } | Flow::CBranch { target } | Flow::Call { target } => target,
_ => None,
};
if let Some(t) = target {
xrefs.insert((va, XrefKind::Code, t));
}
if let Some(m) = d.insn.mem_target
&& space.contains(m)
{
xrefs.insert((va, XrefKind::Data, m));
}
}
let mut strings: Vec<StringHit> = Vec::new();
for seg in &space.all {
for mut h in strings::extract(
&seg.data,
strings::DEFAULT_MIN_LEN,
strings::DEFAULT_MAX_HITS,
Some(seg.vaddr),
) {
let (section, executable) = locate_hit(&bin.sections, seg, h.vaddr);
h.section = section;
h.executable = executable;
strings.push(h);
}
}
strings.sort_by_key(|h| h.vaddr.unwrap_or(u64::MAX));
let spans: Vec<(u64, u64)> = strings
.iter()
.filter_map(|h| {
h.vaddr.map(|v| {
let len = match h.encoding {
Encoding::Ascii => h.value.len() as u64,
Encoding::Utf16Le | Encoding::Utf16Be => h.value.len() as u64 * 2,
};
(v, v.wrapping_add(len))
})
})
.collect();
let data_tos: Vec<(u64, u64)> = xrefs
.iter()
.filter(|(_, k, _)| *k == XrefKind::Data)
.map(|(f, _, t)| (*f, *t))
.collect();
for (from, to) in data_tos {
for (s, e) in &spans {
if to >= *s && to < *e {
xrefs.insert((from, XrefKind::String, *s));
break;
}
}
}
let xrefs: Vec<Xref> = xrefs
.into_iter()
.map(|(from, kind, to)| Xref { from, to, kind })
.collect();
for kind in [XrefKind::Code, XrefKind::Data, XrefKind::String] {
if xrefs.iter().filter(|x| x.kind == kind).count() > MAX_XREFS_PER_KIND {
return Err(Error::Limit(format!(
"more than {MAX_XREFS_PER_KIND} {kind:?} xrefs"
)));
}
}
Ok(Analysis {
arch,
functions,
callgraph,
xrefs,
strings,
})
}
pub fn analyze_raw(bytes: &[u8], base: u64, arch: Arch, entry: Option<u64>) -> Result<Analysis> {
let bin = load_raw(bytes, RawOptions { base, arch, entry })?;
analyze(&bin)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn classify_maps_lift_ops() {
let ret = Insn {
addr: 0x1000,
size: 1,
bytes: vec![0xC3],
mnem: "ret".to_string(),
ops: Vec::new(),
branch_target: None,
mem_target: None,
};
assert_eq!(classify(Arch::X86_64, &ret), Flow::Ret);
let call = Insn {
addr: 0x1000,
size: 5,
bytes: vec![0xE8, 0, 0, 0, 0],
mnem: "call".to_string(),
ops: vec!["0x1005".to_string()],
branch_target: Some(0x1005),
mem_target: None,
};
assert_eq!(
classify(Arch::X86_64, &call),
Flow::Call {
target: Some(0x1005)
}
);
}
#[test]
fn pages_truncate_at_max_page() {
let items: Vec<u64> = (0..10).collect();
assert_eq!(page_slice(&items, 0, usize::MAX).len(), 10);
assert!(page_slice(&items, 0, 3) == [0, 1, 2]);
assert!(page_slice(&items, 20, 5).is_empty());
}
#[test]
fn block_id_is_stable() {
let b = BasicBlock {
start: 0x2007,
insns: Vec::new(),
};
assert_eq!(b.id(0x2000), "block@0x2000+0x2007");
}
}