|
| 1 | +use binaryninja::binary_view::{BinaryView, BinaryViewExt}; |
| 2 | +use binaryninja::debuginfo::*; |
| 3 | +use binaryninja::headless::Session; |
| 4 | +use binaryninja::types::{MemberAccess, MemberScope, StructureBuilder, Type, TypeBuilder}; |
| 5 | +use std::path::PathBuf; |
| 6 | +use std::sync::atomic::{AtomicBool, Ordering}; |
| 7 | + |
| 8 | +static TEST_PARSER_ENABLED: AtomicBool = AtomicBool::new(true); |
| 9 | + |
| 10 | +struct TestDebugInfoParser; |
| 11 | + |
| 12 | +impl CustomDebugInfoParser for TestDebugInfoParser { |
| 13 | + fn is_valid(&self, _view: &BinaryView) -> bool { |
| 14 | + TEST_PARSER_ENABLED.load(Ordering::SeqCst) |
| 15 | + } |
| 16 | + |
| 17 | + fn parse_info( |
| 18 | + &self, |
| 19 | + debug_info: &mut DebugInfo, |
| 20 | + _view: &BinaryView, |
| 21 | + _debug_file: &BinaryView, |
| 22 | + _progress: Box<dyn Fn(usize, usize) -> Result<(), ()>>, |
| 23 | + ) -> bool { |
| 24 | + let test_type = TypeBuilder::int(4, true).finalize(); |
| 25 | + let test_struct = StructureBuilder::new() |
| 26 | + .append( |
| 27 | + &test_type, |
| 28 | + "myfield", |
| 29 | + MemberAccess::PublicAccess, |
| 30 | + MemberScope::NoScope, |
| 31 | + ) |
| 32 | + .finalize(); |
| 33 | + let new_type = TypeBuilder::structure(&test_struct).finalize(); |
| 34 | + debug_info.add_type("test_dbg", &new_type, &[]); |
| 35 | + |
| 36 | + let func_type = Type::function(&test_type, vec![], true); |
| 37 | + |
| 38 | + let test_func = DebugFunctionInfo::new( |
| 39 | + None, |
| 40 | + None, |
| 41 | + Some("test_func".to_string()), |
| 42 | + Some(func_type), |
| 43 | + Some(0x3b440), |
| 44 | + None, |
| 45 | + vec![], |
| 46 | + vec![], |
| 47 | + ); |
| 48 | + debug_info.add_function(test_func); |
| 49 | + true |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +#[test] |
| 54 | +fn test_debug_info() { |
| 55 | + let _session = Session::new().expect("Failed to initialize session"); |
| 56 | + let out_dir = env!("OUT_DIR").parse::<PathBuf>().unwrap(); |
| 57 | + |
| 58 | + // Register test parser |
| 59 | + DebugInfoParser::register("test", TestDebugInfoParser); |
| 60 | + |
| 61 | + // Make sure it exists. |
| 62 | + let _parser = DebugInfoParser::from_name("test").expect("Debug info test parser exists"); |
| 63 | + |
| 64 | + { |
| 65 | + let view = binaryninja::load(out_dir.join("atox.obj")).expect("Failed to create view"); |
| 66 | + view.type_by_name("test_dbg") |
| 67 | + .expect("Debug info test type exists"); |
| 68 | + |
| 69 | + let func = view |
| 70 | + .function_at(&view.default_platform().unwrap(), 0x3b440) |
| 71 | + .expect("Debug info test function exists"); |
| 72 | + assert_eq!(func.symbol().raw_name().to_string(), "test_func"); |
| 73 | + view.file().close(); |
| 74 | + } |
| 75 | + |
| 76 | + // Disable the parser for other tests, so we don't introduce any unwanted behavior. |
| 77 | + TEST_PARSER_ENABLED.store(false, Ordering::SeqCst); |
| 78 | +} |
0 commit comments