From 0991538519646547103781601968620556969980 Mon Sep 17 00:00:00 2001 From: Glenn Smith Date: Tue, 4 Jun 2024 13:38:34 -0400 Subject: [PATCH] PDB: Skip all the HEAD requests and just go straight to GET --- rust/examples/pdb-ng/src/lib.rs | 106 +++++++++++--------------------- 1 file changed, 35 insertions(+), 71 deletions(-) diff --git a/rust/examples/pdb-ng/src/lib.rs b/rust/examples/pdb-ng/src/lib.rs index da469b58f..954076363 100644 --- a/rust/examples/pdb-ng/src/lib.rs +++ b/rust/examples/pdb-ng/src/lib.rs @@ -24,7 +24,7 @@ use anyhow::{anyhow, Result}; use log::{debug, error, info, LevelFilter}; use pdb::PDB; -use binaryninja::binaryview::{BinaryView, BinaryViewExt}; +use binaryninja::binaryview::{BinaryView, BinaryViewBase, BinaryViewExt}; use binaryninja::debuginfo::{CustomDebugInfoParser, DebugInfo, DebugInfoParser}; use binaryninja::downloadprovider::{DownloadInstanceInputOutputCallbacks, DownloadProvider}; use binaryninja::interaction::{MessageBoxButtonResult, MessageBoxButtonSet}; @@ -238,48 +238,7 @@ fn read_from_sym_store(path: &String) -> Result<(bool, Vec)> { Ok((true, data)) } -fn sym_store_exists(path: &String) -> Result { - info!("Check file exists: {}", path); - if !path.contains("://") { - // Local file - if PathBuf::from(path).exists() { - return Ok(true); - } else { - return Ok(false); - } - } - - if !Settings::new("").get_bool("network.pdbAutoDownload", None, None) { - return Err(anyhow!("Auto download disabled")); - } - info!("HEAD: {}", path); - - // Download from remote - let dp = - DownloadProvider::try_default().map_err(|_| anyhow!("No default download provider"))?; - let mut inst = dp - .create_instance() - .map_err(|_| anyhow!("Couldn't create download instance"))?; - let result = inst - .perform_custom_request( - "HEAD", - path.clone(), - HashMap::::new(), - DownloadInstanceInputOutputCallbacks { - read: None, - write: None, - progress: None, - }, - ) - .map_err(|e| anyhow!(e.to_string()))?; - if result.status_code != 200 { - return Ok(false); - } - - Ok(true) -} - -fn search_sym_store(store_path: &String, pdb_info: &PDBInfo) -> Result> { +fn search_sym_store(store_path: &String, pdb_info: &PDBInfo) -> Result>> { // https://www.technlg.net/windows/symbol-server-path-windbg-debugging/ // For symbol servers, to identify the files path easily, Windbg uses the format // binaryname.pdb/GUID @@ -302,17 +261,17 @@ fn search_sym_store(store_path: &String, pdb_info: &PDBInfo) -> Result, debug_info: &mut DebugInfo, view: &BinaryView, progress: &Box Result<(), ()>>, check_guid: bool, did_download: bool, ) -> Result<()> { - let (_downloaded, conts) = read_from_sym_store(filename)?; let mut pdb = PDB::open(Cursor::new(&conts))?; if let Some(info) = parse_pdb_info(view) { @@ -593,11 +551,9 @@ impl CustomDebugInfoParser for PDBParser { debug_file: &BinaryView, progress: Box Result<(), ()>>, ) -> bool { - let filename = debug_file.file().filename(); - if is_pdb(debug_file) { match self.load_from_file( - &filename.to_string(), + &debug_file.read_vec(0, debug_file.len()), debug_info, view, &progress, @@ -625,9 +581,9 @@ impl CustomDebugInfoParser for PDBParser { if let Ok(stores) = stores { for store in stores { match search_sym_store(&store, &info) { - Ok(Some(path)) => { + Ok(Some(conts)) => { match self - .load_from_file(&path, debug_info, view, &progress, true, true) + .load_from_file(&conts, debug_info, view, &progress, true, true) { Ok(_) => return true, Err(e) if e.to_string() == "Cancelled" => return false, @@ -643,10 +599,16 @@ impl CustomDebugInfoParser for PDBParser { // Does the raw path just exist? if PathBuf::from(&info.path).exists() { - match self.load_from_file(&info.path, debug_info, view, &progress, true, false) { - Ok(_) => return true, + match fs::read(&info.path) { + Ok(conts) => match self + .load_from_file(&conts, debug_info, view, &progress, true, false) + { + Ok(_) => return true, + Err(e) if e.to_string() == "Cancelled" => return false, + Err(e) => debug!("Skipping, {}", e.to_string()), + }, Err(e) if e.to_string() == "Cancelled" => return false, - Err(e) => debug!("Skipping, {}", e.to_string()), + Err(e) => debug!("Could not read pdb: {}", e.to_string()), } } @@ -655,28 +617,30 @@ impl CustomDebugInfoParser for PDBParser { potential_path.pop(); potential_path.push(&info.file_name); if potential_path.exists() { - match self.load_from_file( + match fs::read( &potential_path .to_str() .expect("Potential path is a real string") .to_string(), - debug_info, - view, - &progress, - true, - false, ) { - Ok(_) => return true, + Ok(conts) => match self + .load_from_file(&conts, debug_info, view, &progress, true, false) + { + Ok(_) => return true, + Err(e) if e.to_string() == "Cancelled" => return false, + Err(e) => debug!("Skipping, {}", e.to_string()), + }, Err(e) if e.to_string() == "Cancelled" => return false, - Err(e) => debug!("Skipping, {}", e.to_string()), + Err(e) => debug!("Could not read pdb: {}", e.to_string()), } } // Check the local symbol store if let Ok(local_store_path) = active_local_cache(Some(view)) { match search_sym_store(&local_store_path, &info) { - Ok(Some(path)) => { - match self.load_from_file(&path, debug_info, view, &progress, true, false) { + Ok(Some(conts)) => { + match self.load_from_file(&conts, debug_info, view, &progress, true, false) + { Ok(_) => return true, Err(e) if e.to_string() == "Cancelled" => return false, Err(e) => debug!("Skipping, {}", e.to_string()), @@ -696,8 +660,8 @@ impl CustomDebugInfoParser for PDBParser { for server in server_list.iter() { match search_sym_store(&server.to_string(), &info) { - Ok(Some(path)) => { - match self.load_from_file(&path, debug_info, view, &progress, true, true) { + Ok(Some(conts)) => { + match self.load_from_file(&conts, debug_info, view, &progress, true, true) { Ok(_) => return true, Err(e) if e.to_string() == "Cancelled" => return false, Err(e) => debug!("Skipping, {}", e.to_string()),