87 lines
2.5 KiB
Python
87 lines
2.5 KiB
Python
"""Shared re/rvadb.json loading: typed function rows and database discovery.
|
|
|
|
Every tool under tools/wave/ reads a program's function database through this
|
|
module instead of re-parsing JSON inline, so the row shape (as written by
|
|
tools/ghidra/export_rvadb.py) is defined once.
|
|
"""
|
|
# /// script
|
|
# requires-python = ">=3.11"
|
|
# dependencies = []
|
|
# ///
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
from typing import Final, NewType
|
|
|
|
Rva = NewType("Rva", str)
|
|
|
|
REPO_ROOT: Final[Path] = Path(__file__).resolve().parents[2]
|
|
STATUS_VALUES: Final[frozenset[str]] = frozenset({"orig", "stub", "reimpl", "verified"})
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class FunctionRow:
|
|
rva: Rva
|
|
name: str
|
|
status: str
|
|
owning_module_guess: str
|
|
calling_convention: str
|
|
signature: str
|
|
callees: tuple[Rva, ...]
|
|
callers: tuple[Rva, ...]
|
|
globals_touched: tuple[Rva, ...]
|
|
|
|
def __post_init__(self) -> None:
|
|
if self.status not in STATUS_VALUES:
|
|
raise ValueError(f"{self.rva}: unknown status {self.status!r}")
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class ProgramDatabase:
|
|
path: Path
|
|
program: str
|
|
function_count: int
|
|
functions: tuple[FunctionRow, ...]
|
|
|
|
def verified_count(self) -> int:
|
|
return sum(1 for row in self.functions if row.status == "verified")
|
|
|
|
def by_rva(self) -> dict[Rva, FunctionRow]:
|
|
return {row.rva: row for row in self.functions}
|
|
|
|
|
|
def load_database(path: Path) -> ProgramDatabase:
|
|
with path.open(encoding="utf-8") as source:
|
|
raw = json.load(source)
|
|
functions = tuple(
|
|
FunctionRow(
|
|
rva=Rva(row["rva"]),
|
|
name=row["name"],
|
|
status=row["status"],
|
|
owning_module_guess=row["owning_module_guess"],
|
|
calling_convention=row["calling_convention"],
|
|
signature=row["signature"],
|
|
callees=tuple(Rva(c) for c in row["callees"]),
|
|
callers=tuple(Rva(c) for c in row["callers"]),
|
|
globals_touched=tuple(Rva(g) for g in row["globals_touched"]),
|
|
)
|
|
for row in raw["functions"]
|
|
)
|
|
return ProgramDatabase(
|
|
path=path,
|
|
program=raw["program"],
|
|
function_count=raw["function_count"],
|
|
functions=functions,
|
|
)
|
|
|
|
|
|
def discover_databases(root: Path = REPO_ROOT) -> tuple[Path, ...]:
|
|
re_dir = root / "re"
|
|
primary = re_dir / "rvadb.json"
|
|
found = {primary} if primary.is_file() else set()
|
|
if re_dir.is_dir():
|
|
found.update(re_dir.rglob("*_rvadb.json"))
|
|
return tuple(sorted(found))
|