Skip to content

armgit / kong

Public Imported from GitHub
0

The world's first agentic reverse engineer.

implement work queue v1 -needs tests

Amruth6eb247c4fc2f
diff --git a/kong/agent/queue.py b/kong/agent/queue.py
new file mode 100644
index 0000000..c98b492
--- /dev/null
+++ b/kong/agent/queue.py
@@ -0,0 +1,168 @@
+"""Work queue with call-graph-aware ordering for bottom-up analysis."""
+
+from __future__ import annotations
+
+from collections import defaultdict, deque
+from dataclasses import dataclass, field
+
+from kong.ghidra.types import FunctionClassification, FunctionInfo
+
+
+@dataclass
+class WorkItem:
+ function: FunctionInfo
+ depth: int = 0 # call graph depth (0 = leaf)
+ callers: list[int] = field(default_factory=list)
+ callees: list[int] = field(default_factory=list)
+ priority: int = 0 # lower = processed first
+
+ @property
+ def address(self) -> int:
+ return self.function.address
+
+
+class WorkQueue:
+ """Priority queue ordered bottom-up by call graph depth.
+
+ Leaf functions (no callees) are processed first, then their callers,
+ etc. Within the same depth, smaller functions come first.
+ """
+
+ def __init__(self) -> None:
+ self._items: list[WorkItem] = []
+ self._index: int = 0
+ self._by_address: dict[int, WorkItem] = {}
+
+ def build(
+ self,
+ functions: list[FunctionInfo],
+ callers_map: dict[int, list[int]],
+ callees_map: dict[int, list[int]],
+ skip_classifications: set[FunctionClassification] | None = None,
+ ) -> None:
+ """Build the work queue from functions and call graph edges.
+
+ Args:
+ functions: All functions in the binary.
+ callers_map: addr -> list of caller addrs.
+ callees_map: addr -> list of callee addrs.
+ skip_classifications: Function types to exclude (e.g., IMPORTED, THUNK).
+ """
+ if skip_classifications is None:
+ skip_classifications = {
+ FunctionClassification.IMPORTED,
+ FunctionClassification.THUNK,
+ }
+
+ analyzable = {
+ f.address: f
+ for f in functions
+ if f.classification not in skip_classifications
+ }
+
+ depths = self._compute_depths(analyzable, callees_map)
+
+ items = []
+ for addr, func in analyzable.items():
+ depth = depths.get(addr, 0)
+ item = WorkItem(
+ function=func,
+ depth=depth,
+ callers=callers_map.get(addr, []),
+ callees=callees_map.get(addr, []),
+ # Priority: depth first (bottom-up), then size (smaller first)
+ priority=depth * 1_000_000 + func.size,
+ )
+ items.append(item)
+ self._by_address[addr] = item
+
+ # lowest first = leaves + small functions first
+ items.sort(key=lambda w: w.priority)
+ self._items = items
+ self._index = 0
+
+ def _compute_depths(
+ self,
+ analyzable: dict[int, FunctionInfo],
+ callees_map: dict[int, list[int]],
+ ) -> dict[int, int]:
+ """Compute call graph depth for each function via BFS from leaves."""
+ addr_set = set(analyzable.keys())
+
+ # Build adjacency: caller -> callees (only within analyzable set)
+ children: dict[int, list[int]] = defaultdict(list)
+ parent_count: dict[int, int] = defaultdict(int)
+
+ for addr in addr_set:
+ for callee in callees_map.get(addr, []):
+ if callee in addr_set:
+ children[addr].append(callee)
+ parent_count[callee] += 1
+
+ leaves = deque(addr for addr in addr_set if not children[addr])
+
+ depths: dict[int, int] = {}
+ for leaf in leaves:
+ depths[leaf] = 0
+
+ # BFS upward: callers get depth = max(callee depths) + 1
+ # Build reverse adjacency for upward traversal
+ reverse: dict[int, list[int]] = defaultdict(list)
+ for addr in addr_set:
+ for callee in children[addr]:
+ reverse[callee].append(addr)
+
+ visited = set(leaves)
+ queue = deque(leaves)
+
+ while queue:
+ addr = queue.popleft()
+ for caller in reverse.get(addr, []):
+ new_depth = depths[addr] + 1
+ if caller not in depths or new_depth > depths[caller]:
+ depths[caller] = new_depth
+ if caller not in visited:
+ visited.add(caller)
+ queue.append(caller)
+
+ for addr in addr_set:
+ if addr not in depths:
+ depths[addr] = 0
+
+ return depths
+
+ def __len__(self) -> int:
+ return len(self._items) - self._index
+
+ def __bool__(self) -> bool:
+ return self._index < len(self._items)
+
+ @property
+ def total(self) -> int:
+ return len(self._items)
+
+ @property
+ def completed(self) -> int:
+ return self._index
+
+ @property
+ def remaining(self) -> int:
+ return len(self)
+
+ def next(self) -> WorkItem | None:
+ if self._index >= len(self._items):
+ return None
+ item = self._items[self._index]
+ self._index += 1
+ return item
+
+ def peek(self) -> WorkItem | None:
+ if self._index >= len(self._items):
+ return None
+ return self._items[self._index]
+
+ def get_by_address(self, addr: int) -> WorkItem | None:
+ return self._by_address.get(addr)
+
+ def all_items(self) -> list[WorkItem]:
+ return list(self._items)
diff --git a/kong/agent/supervisor.py b/kong/agent/supervisor.py
index a4e6b9b..5e5ddd0 100644
--- a/kong/agent/supervisor.py
+++ b/kong/agent/supervisor.py
@@ -10,6 +10,7 @@ import time
from dataclasses import dataclass
from kong.agent.events import Event, EventCallback, EventType, Phase
+from kong.agent.queue import WorkQueue
from kong.config import KongConfig
from kong.ghidra.client import GhidraClient
from kong.ghidra.types import BinaryInfo, FunctionInfo
@@ -92,6 +93,7 @@ class Supervisor:
) -> None:
self.client = client
self.config = config
+ self.queue = WorkQueue()
self.stats = AnalysisStats()
self.results: dict[int, FunctionResult] = {} # Mapping of address to FunctionResult
self.binary_info: BinaryInfo | None = None # initially None until set in _run_triage
@@ -188,7 +190,18 @@ class Supervisor:
data={"matched": self.stats.signature_matches},
))
- # TODO: setup queuing system
+ self.queue.build(self.functions, callers_map, callees_map)
+ self.stats.total_functions = self.queue.total
+
+ self._emit(Event(
+ type=EventType.TRIAGE_QUEUE_BUILT,
+ phase=Phase.TRIAGE,
+ message=(
+ f"Work queue built: {self.queue.total} functions to analyze "
+ f"(bottom-up order)."
+ ),
+ data={"queue_size": self.queue.total},
+ ))
self._emit(Event(
type=EventType.PHASE_COMPLETE,
@@ -208,7 +221,63 @@ class Supervisor:
message="Starting bottom-up analysis...",
))
- # TODO: analyze functions bottom-up via queue system
+ while self.queue:
+ if self._paused:
+ time.sleep(0.1)
+ continue
+
+ item = self.queue.next()
+ if item is None:
+ break
+
+ func = item.function
+ self._emit(Event(
+ type=EventType.FUNCTION_START,
+ phase=Phase.ANALYSIS,
+ message=f"Analyzing {func.name} ({func.address_hex})...",
+ data={
+ "address": func.address,
+ "name": func.name,
+ "size": func.size,
+ "depth": item.depth,
+ "progress": f"{self.queue.completed}/{self.queue.total}",
+ },
+ ))
+
+ try:
+ result = self._analyze_function(item)
+ except Exception as e:
+ result = FunctionResult(
+ address=func.address,
+ original_name=func.name,
+ error=str(e),
+ )
+ self._emit(Event(
+ type=EventType.FUNCTION_ERROR,
+ phase=Phase.ANALYSIS,
+ message=f"Error analyzing {func.name}: {e}",
+ data={"address": func.address, "error": str(e)},
+ ))
+
+ self.results[func.address] = result
+ self.stats.record_result(result)
+
+ if not result.error and not result.skipped:
+ self._emit(Event(
+ type=EventType.FUNCTION_COMPLETE,
+ phase=Phase.ANALYSIS,
+ message=(
+ f"{func.address_hex} → {result.name} "
+ f"(confidence: {result.confidence}%)"
+ ),
+ data={
+ "address": func.address,
+ "original_name": result.original_name,
+ "name": result.name,
+ "confidence": result.confidence,
+ "classification": result.classification,
+ },
+ ))
self._emit(Event(
type=EventType.PHASE_COMPLETE,