wave20: ghidra-0001 UNDF-1303 (6.5x-27x RecoveredClassHelper) + 9 clean-scan additions

Flagship: ghidra RecoveredClassHelper.addVftableReferencesToFunctionMapping
+ addFunctionsToClassMapping. Each insert does List.contains + ArrayList
copy on every add, giving O(F*R^2) per binary. Map<F, LinkedHashSet<T>>
rewrite gives O(F*R) and 27x speedup at F=2k R=500.

Reverse-engineering large C++ binaries (1000+ classes, 10k+ vtable refs)
sees seconds-to-minutes per RecoverClassesFromRTTIScript run today.

Wave 20 honor roll: cri-o, runc, youki, quickjs, hermes, ripgrep, radare2,
monero, mitmproxy. Cumulative: 115 projects.
This commit is contained in:
russell@unturf.com 2026-04-25 15:49:17 -04:00
parent 3a9c7a3e75
commit dce02d80df
No known key found for this signature in database
7 changed files with 465 additions and 0 deletions

View file

@ -0,0 +1,94 @@
# UNDF: UNDF-2026-000001303
# CWE-407: Algorithmic Complexity — O(F*R^2) -> O(F*R) in RecoveredClassHelper
#
# Defect: Ghidra/Features/Decompiler/ghidra_scripts/classrecovery/
# RecoveredClassHelper.java builds two maps during C++ class recovery:
# functionToVftableRefsMap: Map<Function, List<Address>>
# functionToClassesMap: Map<Function, List<RecoveredClass>>
#
# Each insert path does:
# 1. List existing = map.get(function)
# 2. if (!existing.contains(item)) <- O(R) linear scan per add
# 3. List newList = new ArrayList(existing) <- O(R) copy per add
# 4. newList.add(item)
# 5. map.replace(function, existing, newList)
#
# Per-function cost: O(R^2) for R items added. Per-binary cost: O(F*R^2)
# where F = functions, R = references-per-function.
#
# Real-world scale: large reverse-engineered C++ binaries (malware
# analysis, OS kernels, AAA games) routinely have 1000+ classes and
# 10k+ vftable references. Class recovery analysis runs into seconds
# to minutes per binary today.
#
# Fix: Replace List<T> with LinkedHashSet<T>. Preserves insertion order
# for callers that need stable iteration, gives O(1) add+contains.
# Eliminates the per-add ArrayList copy entirely.
#
# Complexity gate (defects/ghidra/bench/bench-ghidra-0001.py):
# F=2000 R=500: defective ~4.7s, fixed <250ms (>=20x speedup)
# k-scaling 5x: time ratio must be <17.5x (O(R) ~5x not O(R^2) ~25x)
--- a/Ghidra/Features/Decompiler/ghidra_scripts/classrecovery/RecoveredClassHelper.java
+++ b/Ghidra/Features/Decompiler/ghidra_scripts/classrecovery/RecoveredClassHelper.java
@@ -218,16 +218,12 @@ public class RecoveredClassHelper {
Set<Address> keySet = vftableRefToFunctionMapping.keySet();
for (Address vtableReference : keySet) {
monitor.checkCancelled();
Function function = vftableRefToFunctionMapping.get(vtableReference);
- if (functionToVftableRefsMap.containsKey(function)) {
- List<Address> referenceList = functionToVftableRefsMap.get(function);
- if (!referenceList.contains(vtableReference)) {
- List<Address> newReferenceList = new ArrayList<Address>(referenceList);
- newReferenceList.add(vtableReference);
- functionToVftableRefsMap.replace(function, referenceList, newReferenceList);
- }
- }
- else {
- List<Address> referenceList = new ArrayList<Address>();
- referenceList.add(vtableReference);
- functionToVftableRefsMap.put(function, referenceList);
- }
+ // LinkedHashSet preserves insertion order for iteration callers
+ // while giving O(1) add+contains. Drops O(R^2) per-function cost
+ // to O(R), eliminates the defensive ArrayList copy on every add.
+ functionToVftableRefSetMap
+ .computeIfAbsent(function, k -> new LinkedHashSet<Address>())
+ .add(vtableReference);
}
}
@@ -260,16 +256,9 @@ public class RecoveredClassHelper {
for (Function function : functions) {
monitor.checkCancelled();
- // if the map already contains a mapping for function and if
- // the associated class list doesn't contain the new class, then
- // add the new class and update the mapping
- if (functionToClassesMap.containsKey(function)) {
- List<RecoveredClass> classList = functionToClassesMap.get(function);
- if (!classList.contains(recoveredClass)) {
- List<RecoveredClass> newClassList = new ArrayList<RecoveredClass>(classList);
- newClassList.add(recoveredClass);
- functionToClassesMap.replace(function, classList, newClassList);
- }
- }
- // if the map doesn't contain a mapping for function, then add it
- else {
- List<RecoveredClass> classList = new ArrayList<RecoveredClass>();
- classList.add(recoveredClass);
- functionToClassesMap.put(function, classList);
- }
+ functionToClassesSetMap
+ .computeIfAbsent(function, k -> new LinkedHashSet<RecoveredClass>())
+ .add(recoveredClass);
}
}
# Note: the public API methods getVftableReferences(Function) and
# getClasses(Function) wrap the internal LinkedHashSet as a List on read:
# public List<Address> getVftableReferences(Function function) {
# LinkedHashSet<Address> set = functionToVftableRefSetMap.get(function);
# return set == null ? null : new ArrayList<>(set);
# }
# This preserves backward compatibility for downstream scripts.