From 9f36aa67cef4e8f2c72bcd35aa886d6e03a5fc63 Mon Sep 17 00:00:00 2001 From: nathanbzrr Date: Tue, 24 Nov 2026 00:48:55 -0300 Subject: [PATCH] Census: deduplicate PE imports without hint (cherry picked from commit a3f89eda547690efa8542b8b993b9de894f05bb1) --- apps/victory/src/census.rs | 245 +++++++++++++++++++++++++++++++++---- 1 file changed, 221 insertions(+), 24 deletions(-) diff --git a/apps/victory/src/census.rs b/apps/victory/src/census.rs index 59ec2b7..1314efb 100644 --- a/apps/victory/src/census.rs +++ b/apps/victory/src/census.rs @@ -9,9 +9,24 @@ use victory_hle::{HleResolver, HostPolicy}; use victory_loader::Provenance; use victory_pe::{ImageInfo, ImportSymbol}; +#[derive(PartialEq, Eq, PartialOrd, Ord)] +enum FunctionKey { + Name(String), + Ordinal(u16), +} + +impl From<&ImportSymbol> for FunctionKey { + fn from(symbol: &ImportSymbol) -> Self { + match symbol { + ImportSymbol::Name { name, .. } => Self::Name(name.clone()), + ImportSymbol::Ordinal(ordinal) => Self::Ordinal(*ordinal), + } + } +} + #[derive(Default)] struct DllStats { - functions: BTreeSet, + functions: BTreeSet, hle: usize, host: usize, missing: usize, @@ -58,29 +73,7 @@ pub fn run(paths: Vec, strict: bool, list_missing: bool) -> Result<()> } }; images += 1; - for module in info.imports.iter().chain(&info.delay_imports) { - let key = module.dll.to_ascii_lowercase(); - let stats = dlls.entry(key.clone()).or_default(); - stats.images.insert(path.to_path_buf()); - for e in &module.entries { - let class = resolver.classify(&module.dll, &e.symbol); - if class.is_none() { - missing - .entry((key.clone(), e.symbol.to_string())) - .or_default() - .insert(path.to_path_buf()); - } - // Count each dll!function once across all images. - if stats.functions.insert(e.symbol.clone()) { - match class { - Some(Provenance::Hle) => stats.hle += 1, - Some(Provenance::Host) => stats.host += 1, - Some(Provenance::Title) => unreachable!("census maps no title DLLs"), - None => stats.missing += 1, - } - } - } - } + tally_image(path, &info, &resolver, &mut dlls, &mut missing); } } @@ -129,3 +122,207 @@ fn looks_like_pe(path: &Path) -> bool { .is_ok() && &magic == b"MZ" } + +fn tally_image( + path: &Path, + info: &ImageInfo, + resolver: &HleResolver, + dlls: &mut BTreeMap, + missing: &mut BTreeMap<(String, String), BTreeSet>, +) { + for module in info.imports.iter().chain(&info.delay_imports) { + let key = module.dll.to_ascii_lowercase(); + let stats = dlls.entry(key.clone()).or_default(); + stats.images.insert(path.to_path_buf()); + for e in &module.entries { + let class = resolver.classify(&module.dll, &e.symbol); + if class.is_none() { + missing + .entry((key.clone(), e.symbol.to_string())) + .or_default() + .insert(path.to_path_buf()); + } + // Count each dll!function once across all images. + if stats.functions.insert(FunctionKey::from(&e.symbol)) { + match class { + Some(Provenance::Hle) => stats.hle += 1, + Some(Provenance::Host) => stats.host += 1, + Some(Provenance::Title) => unreachable!("census maps no title DLLs"), + None => stats.missing += 1, + } + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use victory_pe::{ImportEntry, ImportModule}; + + fn entry(symbol: ImportSymbol) -> ImportEntry { + ImportEntry { + symbol, + iat_slot_rva: 0, + } + } + + fn named(hint: u16, name: &str) -> ImportEntry { + entry(ImportSymbol::Name { + hint, + name: name.into(), + }) + } + + fn ordinal(ord: u16) -> ImportEntry { + entry(ImportSymbol::Ordinal(ord)) + } + + fn synthetic_image(imports: Vec, delay_imports: Vec) -> ImageInfo { + ImageInfo { + machine: 0, + characteristics: 0, + timestamp: 0, + subsystem: 0, + dll_characteristics: 0, + image_base: 0, + size_of_image: 0, + entry_rva: 0, + stack_reserve: 0, + stack_commit: 0, + sections: Vec::new(), + imports, + delay_imports, + exports: Vec::new(), + tls: None, + has_relocations: false, + has_exception_table: false, + has_load_config: false, + codeview: None, + } + } + + #[test] + fn deduplicates_imports_ignoring_hint_across_images_and_delay_imports() { + let resolver = HleResolver::new(HostPolicy::none()); + let mut dlls: BTreeMap = BTreeMap::new(); + let mut missing: BTreeMap<(String, String), BTreeSet> = BTreeMap::new(); + + let image_a = synthetic_image( + vec![ImportModule { + dll: "XGameRuntime.dll".into(), + entries: vec![ + named(1, "XGameRuntimeInitialize"), + named(10, "SyntheticMissingApi"), + ordinal(42), + ], + }], + vec![ImportModule { + dll: "XGameRuntime.dll".into(), + entries: vec![ + named(2, "XGameRuntimeInitialize"), + named(20, "SyntheticMissingApi"), + ], + }], + ); + let image_b = synthetic_image( + vec![ImportModule { + dll: "xgameruntime.dll".into(), + entries: vec![ + named(99, "XGameRuntimeInitialize"), + named(200, "SyntheticMissingApi"), + ordinal(42), + ], + }], + vec![ImportModule { + dll: "XGameRuntime.dll".into(), + entries: vec![named(300, "SyntheticMissingApi"), ordinal(42)], + }], + ); + + tally_image( + Path::new("image_a.exe"), + &image_a, + &resolver, + &mut dlls, + &mut missing, + ); + tally_image( + Path::new("image_b.dll"), + &image_b, + &resolver, + &mut dlls, + &mut missing, + ); + + assert_eq!(dlls.len(), 1); + let stats = &dlls["xgameruntime.dll"]; + assert_eq!(stats.functions.len(), 3); + assert_eq!(stats.hle, 1); + assert_eq!(stats.host, 0); + assert_eq!(stats.missing, 2); + assert_eq!(stats.images.len(), 2); + assert_eq!( + dlls.values().map(|s| s.missing).sum::(), + missing.len() + ); + assert_eq!( + missing[&("xgameruntime.dll".into(), "SyntheticMissingApi".into())].len(), + 2 + ); + assert_eq!(missing[&("xgameruntime.dll".into(), "#42".into())].len(), 2); + } + + #[test] + fn counts_distinct_symbols_and_dlls_separately() { + let resolver = HleResolver::new(HostPolicy::none()); + let mut dlls: BTreeMap = BTreeMap::new(); + let mut missing: BTreeMap<(String, String), BTreeSet> = BTreeMap::new(); + + let image = synthetic_image( + vec![ + ImportModule { + dll: "XGameRuntime.dll".into(), + entries: vec![ + named(0, "XGameRuntimeInitialize"), + named(0, "SyntheticMissingA"), + named(0, "SyntheticMissingB"), + ordinal(7), + ordinal(8), + ], + }, + ImportModule { + dll: "SyntheticExtra.dll".into(), + entries: vec![named(0, "SyntheticMissingA"), ordinal(7)], + }, + ], + Vec::new(), + ); + + tally_image( + Path::new("single.exe"), + &image, + &resolver, + &mut dlls, + &mut missing, + ); + + assert_eq!(dlls.len(), 2); + let xgr = &dlls["xgameruntime.dll"]; + assert_eq!(xgr.functions.len(), 5); + assert_eq!(xgr.hle, 1); + assert_eq!(xgr.missing, 4); + assert_eq!(xgr.images.len(), 1); + + let extra = &dlls["syntheticextra.dll"]; + assert_eq!(extra.functions.len(), 2); + assert_eq!(extra.hle, 0); + assert_eq!(extra.missing, 2); + assert_eq!(extra.images.len(), 1); + + assert_eq!( + dlls.values().map(|s| s.missing).sum::(), + missing.len() + ); + } +}