depth or breadth first?

This commit is contained in:
🪞👃🪞 2025-02-23 22:37:34 +02:00
parent b629beb5fc
commit 8a404c3bd5
2 changed files with 28 additions and 28 deletions

View file

@ -13,37 +13,28 @@ impl Module {
let instruction = decoder.decode(); let instruction = decoder.decode();
let opcodes = &self.code[position..position+instruction.len()]; let opcodes = &self.code[position..position+instruction.len()];
if CallSite::matches(&instruction) && !CallSite::skip(opcodes) { if CallSite::matches(&instruction) && !CallSite::skip(opcodes) {
let offset = (position + self.code_start) as u32; let offset = (position + self.code_start) as u32;
let source = self.pe.offset_to_rva(Offset(offset))?.0; let source = self.pe.offset_to_rva(Offset((position + self.code_start) as u32))?.0;
let target = CallSite::target(source, opcodes).unwrap_or(0); let target = CallSite::target(source, opcodes).unwrap_or(0);
let imports = self.imports.read().unwrap(); let import = self.imports.read().unwrap().get(&target).cloned();
let label = imports.get(&target) let call_site = Arc::new(CallSite {
.map(|(module, method)|format!("{module}::{method}"))
.unwrap_or_else(||format!("{RED}unresolved{RESET}"));
println!(" (call {} {} {} {DIM}{:20}{RESET} {} {BOLD}{}{RESET})",
&self.name,
fmt_num(offset as usize),
fmt_num(source as usize),
fmt_bytes(opcodes),
fmt_num(target as usize),
label);
if !targets.contains_key(&target) {
targets.insert(target, vec![]);
}
let import = imports.get(&target).clone();
targets.get_mut(&target).unwrap().push(Arc::new(CallSite {
caller: self.clone(), caller: self.clone(),
source, source,
offset, offset,
length: opcodes.len(),
target, target,
method: import.map(|x|x.1.clone()), length: opcodes.len(),
method: import.clone().map(|x|x.1),
module: if let Some(import) = import { module: if let Some(import) = import {
self.dependencies.read().unwrap().get(&import.0).cloned() self.dependencies.read().unwrap().get(&import.0).cloned()
} else { } else {
None None
} }
})); });
call_site.show(Some(opcodes));
if !targets.contains_key(&call_site.target) {
targets.insert(call_site.target, vec![]);
}
targets.get_mut(&call_site.target).unwrap().push(call_site);
} }
} }
Ok(self) Ok(self)

View file

@ -130,12 +130,21 @@ impl Module {
} }
impl CallSite { impl CallSite {
pub fn show (&self) { pub fn show (&self, opcodes: Option<&[u8]>) {
let caller = self.caller.name.as_ref(); let label = self.caller.imports.read().unwrap().get(&self.target)
let module = self.module.as_ref(); .map(|(module, method)|format!("{module}::{method}"))
let method = self.method.as_ref(); .unwrap_or_else(||format!("{RED}unresolved{RESET}"));
let style = GREEN; println!(" ╰-> (call {} {} {} {DIM}{:20}{RESET} {} {BOLD}{}{RESET})",
println!("╰--------> {caller} -> {style}{module:?}::{method:?}{RESET}"); &self.caller.name,
fmt_num(self.offset as usize),
fmt_num(self.source as usize),
fmt_bytes(opcodes.unwrap_or(&[])),
fmt_num(self.target as usize),
label);
//let caller = self.caller.name.as_ref();
//let module = self.module.as_ref();
//let method = self.method.as_ref();
//println!("╰--------> {caller} -> {label}{RESET}");
//module.map(|x|x.as_ref()).unwrap_or(&""), //module.map(|x|x.as_ref()).unwrap_or(&""),
//method.map(|x|x.as_ref()).unwrap_or(&"")); //method.map(|x|x.as_ref()).unwrap_or(&""));
//} else { //} else {