From 26626e683d41438ff26748fe7357e1f548f4415d Mon Sep 17 00:00:00 2001 From: Rubens Brandao Date: Fri, 20 Sep 2024 16:37:29 -0300 Subject: [PATCH 1/3] update idb_import to add function types --- rust/examples/idb_import/src/lib.rs | 159 ++++++++++++++++++---------- 1 file changed, 106 insertions(+), 53 deletions(-) diff --git a/rust/examples/idb_import/src/lib.rs b/rust/examples/idb_import/src/lib.rs index 85644b0c3..1810bde8d 100644 --- a/rust/examples/idb_import/src/lib.rs +++ b/rust/examples/idb_import/src/lib.rs @@ -875,7 +875,7 @@ fn parse_til_section_info( fn parse_id0_section_info( debug_info: &mut DebugInfo, bv: &BinaryView, - _debug_file: &BinaryView, + debug_file: &BinaryView, id0: ID0Section, progress: impl Fn(usize, usize) -> Result<(), ()>, ) -> Result<()> { @@ -887,73 +887,126 @@ fn parse_id0_section_info( idb_rs::id0::FunctionsAndComments::Function(_) => {} idb_rs::id0::FunctionsAndComments::RepeatableComment { address, value } | idb_rs::id0::FunctionsAndComments::Comment { address, value } => { - let funcs_at = bv.functions_at(address); - if !funcs_at.is_empty() { - for function in &funcs_at { - function.set_comment(value); - } - } else { - // No functions directly at the address we can annotate containing functions instead. - for function in &bv.functions_containing(address) { - function.set_comment_at(address, value); - } + for function in &bv.functions_containing(address) { + function.set_comment_at(address, value); } } idb_rs::id0::FunctionsAndComments::Unknown { .. } => {} } } - #[derive(Debug, Default)] - struct ID0Function { - address: Option, - name: Option, - symbol: Option, - } - let mut functions: HashMap = HashMap::new(); - for entry_point in id0.entry_points()? { - // TODO check for duplication - match entry_point? { - idb_rs::id0::EntryPoint::Name => {} - idb_rs::id0::EntryPoint::Unknown { .. } => {} - // TODO take ordinal in consideration if the order of the functions is important - idb_rs::id0::EntryPoint::Ordinal { .. } => {} - idb_rs::id0::EntryPoint::Function { key, address } => { - let fun = functions.entry(key).or_default(); - let _ = fun.address.insert(address); - } - idb_rs::id0::EntryPoint::ForwardedSymbol { key, symbol } => { - let fun = functions.entry(key).or_default(); - let _ = fun.symbol.insert(symbol.to_string()); - } - idb_rs::id0::EntryPoint::FunctionName { key, name } => { - let fun = functions.entry(key).or_default(); - let _ = fun.name.insert(name.to_string()); - } - } - } - let total = functions.len(); - for (i, function) in functions.into_values().enumerate() { + let entry_points = id0.entry_points()?; + let total = entry_points.len(); + for (i, entry_point) in entry_points.into_iter().enumerate() { if progress(i, total).is_err() { warn!("Aborted while adding the functions"); break; } - let name = function.name.clone(); - if !debug_info.add_function(DebugFunctionInfo::new( - None, - None, - function.name.clone(), - None, - function.address, - None, - vec![], - vec![], - )) { - error!("Unable to add the function {name:?}") + // TODO handle entry_point.forwarded type currently on the til section + //if let Some(forwarded) = entry_point.forwarded { + // todo!() + //} + match entry_point.entry_type { + None => { + // TODO add label without type + } + Some(ty @ TILType::Function(_)) => { + let ty = translate_ephemeral_type( + &mut *debug_info, + debug_file, + &ty, + entry_point.address, + ); + if !debug_info.add_function(DebugFunctionInfo::new( + None, + None, + Some(entry_point.name), + ty, + Some(entry_point.address), + None, + vec![], + vec![], + )) { + error!("Unable to add the function at {:#x}", entry_point.address) + } + } + Some(ty) => { + let ty = translate_ephemeral_type( + &mut *debug_info, + debug_file, + &ty, + entry_point.address, + ); + if let Some(ty) = ty { + if !debug_info.add_data_variable( + entry_point.address, + &ty, + Some(entry_point.name), + &[], + ) { + error!("Unable to add the type at {:#x}", entry_point.address) + } + } + todo!(); + } } } Ok(()) } +fn translate_ephemeral_type( + debug_info: &mut DebugInfo, + debug_file: &BinaryView, + ty: &TILType, + address: u64, +) -> Option> { + // in case we need to translate types + let translator = TranslateIDBTypes { + debug_info: &mut *debug_info, + _debug_file: debug_file, + arch: debug_file.default_arch().unwrap(/* TODO */), + progress: |_, _| Ok(()), + // TODO it's unclear what to do here + til: &TILSection { + format: 12, + title: String::new(), + description: String::new(), + id: 0, + cm: 0, + def_align: 1, + symbols: vec![], + type_ordinal_numbers: None, + types: vec![], + size_i: 4.try_into().unwrap(), + size_b: 1.try_into().unwrap(), + sizes: None, + size_long_double: None, + macros: None, + is_universal: false, + }, + types: vec![], + types_by_ord: HashMap::new(), + types_by_name: HashMap::new(), + }; + + match translator.translate_type(ty) { + TranslateTypeResult::Translated(result) => Some(result), + TranslateTypeResult::PartiallyTranslated(_, None) | TranslateTypeResult::NotYet => { + error!("Unable to translate the type at {:#x}", address); + None + } + TranslateTypeResult::PartiallyTranslated(_, Some(bn_type_error)) + | TranslateTypeResult::Error(bn_type_error) => { + error!( + "Unable to translate the type at {:#x}: {bn_type_error}", + address + ); + None + } + } +} + +#[allow(non_snake_case)] #[no_mangle] pub extern "C" fn CorePluginInit() -> bool { let _logger = logger::init(LevelFilter::Error); From cf9ad52ed0a5728061d7596adf85a08ad1f405a7 Mon Sep 17 00:00:00 2001 From: Rubens Brandao Date: Mon, 23 Sep 2024 11:04:20 -0300 Subject: [PATCH 2/3] update idb_import --- rust/examples/idb_import/src/lib.rs | 66 ++++++++++++++--------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/rust/examples/idb_import/src/lib.rs b/rust/examples/idb_import/src/lib.rs index 1810bde8d..bb285a30c 100644 --- a/rust/examples/idb_import/src/lib.rs +++ b/rust/examples/idb_import/src/lib.rs @@ -906,48 +906,48 @@ fn parse_id0_section_info( //if let Some(forwarded) = entry_point.forwarded { // todo!() //} - match entry_point.entry_type { - None => { + match (entry_point.entry_type, entry_point.forwarded) { + (None, _) => { // TODO add label without type } - Some(ty @ TILType::Function(_)) => { - let ty = translate_ephemeral_type( + // TODO handle forwarded types/functions + (Some(_ty), Some(_forw)) => {} + // regular type + (Some(ty), None) => { + let bnty = translate_ephemeral_type( &mut *debug_info, debug_file, &ty, entry_point.address, ); - if !debug_info.add_function(DebugFunctionInfo::new( - None, - None, - Some(entry_point.name), - ty, - Some(entry_point.address), - None, - vec![], - vec![], - )) { - error!("Unable to add the function at {:#x}", entry_point.address) - } - } - Some(ty) => { - let ty = translate_ephemeral_type( - &mut *debug_info, - debug_file, - &ty, - entry_point.address, - ); - if let Some(ty) = ty { - if !debug_info.add_data_variable( - entry_point.address, - &ty, - Some(entry_point.name), - &[], - ) { - error!("Unable to add the type at {:#x}", entry_point.address) + match (bnty, ty) { + // TODO handle types that can't be translated + (None, _) => {} + (Some(bnty), TILType::Function(_)) => { + if !debug_info.add_function(DebugFunctionInfo::new( + None, + None, + Some(entry_point.name), + Some(bnty), + Some(entry_point.address), + None, + vec![], + vec![], + )) { + error!("Unable to add the function at {:#x}", entry_point.address) + } + } + (Some(bnty), _) => { + if !debug_info.add_data_variable( + entry_point.address, + &bnty, + Some(entry_point.name), + &[], + ) { + error!("Unable to add the type at {:#x}", entry_point.address) + } } } - todo!(); } } } From 65e254bc13a5abee5a96f38a0cdfa48a98938aa0 Mon Sep 17 00:00:00 2001 From: Rubens Brandao Date: Mon, 23 Sep 2024 12:18:55 -0300 Subject: [PATCH 3/3] update idb_import idb-rs version --- rust/Cargo.lock | 2 +- rust/examples/idb_import/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/rust/Cargo.lock b/rust/Cargo.lock index ff76050b4..7227377c1 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -480,7 +480,7 @@ dependencies = [ [[package]] name = "idb-rs" version = "0.1.0" -source = "git+https://github.com/Vector35/idb-rs?rev=fec57b6#fec57b66ae3692b934f32ca1f6fcc6416ee3a522" +source = "git+https://github.com/Vector35/idb-rs?rev=88e33ae#88e33ae12a4099dbf3663eb4ec1cfe6a75f7253b" dependencies = [ "anyhow", "bincode", diff --git a/rust/examples/idb_import/Cargo.toml b/rust/examples/idb_import/Cargo.toml index 855c1af59..fdf7af7ed 100644 --- a/rust/examples/idb_import/Cargo.toml +++ b/rust/examples/idb_import/Cargo.toml @@ -10,5 +10,5 @@ crate-type = ["cdylib"] [dependencies] anyhow = "1.0.86" binaryninja = { path = "../../" } -idb-rs = { git = "https://github.com/Vector35/idb-rs", rev = "fec57b6" } +idb-rs = { git = "https://github.com/Vector35/idb-rs", rev = "88e33ae" } log = "0.4.20"