decompile-verify / demo /examples.py
ejschwartz's picture
Add three Faultless completeness examples
52a9362
Raw History Blame Contribute Delete
4.42 kB
"""Combine shared core examples with saved frontend completeness pairs."""
from pathlib import Path
import sys
import json
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "core"))
from examples.ui import GHIDRA_EXAMPLE_NAMES, ghidra_metadata
from examples import ui as core_examples
from demo import faultless
ORIGINAL_EXAMPLE_NAMES = core_examples.ORIGINAL_EXAMPLE_NAMES + faultless.NAMES
ORIGINAL_EXAMPLE_LABELS = core_examples.ORIGINAL_EXAMPLE_LABELS + faultless.LABELS
EXAMPLE_NAMES = ORIGINAL_EXAMPLE_NAMES + GHIDRA_EXAMPLE_NAMES
EXAMPLE_LABELS = ORIGINAL_EXAMPLE_LABELS + tuple(
core_examples.EXAMPLE_LABELS[core_examples.EXAMPLE_NAMES.index(name)]
for name in GHIDRA_EXAMPLE_NAMES)
def example_metadata(name):
if name in faultless.NAMES:
return faultless.metadata(name)
metadata = core_examples.example_metadata(name)
if name == "faultless-linked-list":
metadata["expected_verdict"] = "mismatch"
return metadata
def reference_sources():
return tuple(example_metadata(name)["reference"] for name in EXAMPLE_NAMES)
def expected_result(name, compiler="gcc"):
"""Recorded result for the bundled candidate and compiler, before user edits."""
metadata = example_metadata(name)
if metadata.get("expected_error"):
return "error"
return metadata.get("compiler_verdicts", {}).get(compiler,
metadata.get("expected_verdict")) or "inconclusive"
def prepare_examples(directory, *, include_ghidra=True):
rows = dict(zip(core_examples.EXAMPLE_NAMES, core_examples.prepare_examples(directory)))
rows.update(zip(faultless.NAMES, faultless.prepare_examples(directory)))
names = EXAMPLE_NAMES if include_ghidra else tuple(
name for name in EXAMPLE_NAMES if name not in GHIDRA_EXAMPLE_NAMES)
selected = [rows[name] for name in names]
(Path(directory) / "examples.json").write_text(json.dumps(selected, indent=2) + "\n")
return selected
# UI categories use stable catalog names; preparation retains its original order.
EXAMPLE_GROUPS = (
("Basics", ("identical", "rotate-vs-shift", "population-count",
"unsigned-maximum", "byte-swap", "sign-extension")),
("Calls and memory", ("hello-goodbye", "hello-world", "malloc", "format-string",
"transient-global", "tail-call", "void-memory")),
("Types and ABI", ("wide-integer", "float-identity", "signed-zero", "nan-payload",
"struct-fields", "struct-indirect", "struct-padding", "pointer-identity")),
("Real binaries", ("factor-gcd", "factor-gcd-euclidean", "factor-gcd-even")),
("Related work", core_examples.FAULTLESS_NAMES + core_examples.RESEARCH_NAMES + faultless.NAMES),
("Ghidra exports", GHIDRA_EXAMPLE_NAMES),
)
EXAMPLE_GROUP_DESCRIPTIONS = {
"Basics": "Small arithmetic and bitwise examples, including matching implementations and deliberate bugs.",
"Calls and memory": "Examples involving function calls, allocation, strings, globals, and memory writes.",
"Types and ABI": "Examples exploring integer widths, floating-point values, structs, and pointer types.",
"Real binaries": (
"Compare the GCD function in the bundled [GNU coreutils `factor` binary]"
"(https://www.gnu.org/software/coreutils/manual/html_node/factor-invocation.html) "
"with matching and deliberately incorrect C implementations."
),
"Related work": (
"Paper examples and artifact cases from [Faultless](https://arxiv.org/abs/2609.34089), "
"[D-Helix](https://www.usenix.org/system/files/usenixsecurity24-zou.pdf), "
"[DiscScope](https://doi.org/10.1145/3708821.3733877), and "
"[Decompile-Diverge](https://arxiv.org/abs/2609.05370), "
"covering pointer and memory behavior, decompilation errors, and compilation failures."
),
"Ghidra exports": (
"Saved C exports from Ghidra 12.1.3 using "
"[GhidraFunctionCPPExporter](https://github.com/edmcman/GhidraFunctionCPPExporter). "
"Examples include agreement, mismatches, and compilation or type errors."
),
}
def example_groups(names):
"""Return nonempty categories with indices into the selected catalog."""
return tuple((label, tuple(index for index, name in enumerate(names) if name in members))
for label, members in EXAMPLE_GROUPS if any(name in members for name in names))