rustrip 0.1.14

Make stripped Rust binaries readable again: recover slice-string boundaries, demangle symbols, and reconstruct panic source locations from release/stripped ELF, PE, and Mach-O binaries.
Documentation
#!/usr/bin/env python3
"""Rustrip CLI robustness tests.

Every subprocess is executed through ``invoke``. The shared classifier rejects
panics, signals, timeouts, launch failures, malformed output, and unexpected
exit statuses rather than treating any nonzero result as an expected rejection.

Usage:
    python3 scripts/robustness_test.py [path-to-rustrip-binary]
"""

# nosec: invoke uses explicit argv lists with shell=False (the default).

import concurrent.futures
import os
import subprocess  # nosec
import sys
import tempfile
import time
import uuid
from dataclasses import dataclass
from pathlib import Path
from typing import Callable, Iterable, Optional, Sequence

from adversarial_test import classify_returncode, parse_json_array


@dataclass
class Invocation:
    """One subprocess result after common failure classification."""

    result: str
    returncode: Optional[int]
    stdout: str
    stderr: str
    detail: str = ""


exe = Path(sys.argv[1] if len(sys.argv) > 1 else "./target/release/rustrip")
results: list[tuple[str, bool]] = []


def output_text(output: object) -> str:
    """Normalize subprocess byte/text output for the common classifier."""
    if output is None:
        return ""
    if isinstance(output, bytes):
        return output.decode(errors="replace")
    return str(output)


def classify_invocation_error(error: BaseException) -> str:
    """Map launch and timeout exceptions to the same result vocabulary."""
    if isinstance(error, subprocess.TimeoutExpired):
        return "timeout"
    if isinstance(error, OSError):
        return "launch failure"
    return "invocation failure"


def invoke(
    program: Path,
    args: Sequence[str],
    *,
    input_data: Optional[bytes] = None,
    timeout: float = 60,
) -> Invocation:
    """Run an explicit argv and classify completion or process errors."""
    kwargs: dict[str, object] = {"capture_output": True, "timeout": timeout}
    if input_data is not None:
        kwargs["input"] = input_data
    try:
        completed = subprocess.run([str(program), *args], **kwargs)  # nosec
    except (OSError, subprocess.TimeoutExpired) as error:
        return Invocation(
            classify_invocation_error(error),
            None,
            output_text(getattr(error, "stdout", None)),
            output_text(getattr(error, "stderr", None)),
            str(error),
        )

    stdout = output_text(completed.stdout)
    stderr = output_text(completed.stderr)
    return Invocation(classify_returncode(completed.returncode, stderr), completed.returncode, stdout, stderr)


def invoke_rustrip(
    args: Sequence[str], *, input_data: Optional[bytes] = None, timeout: float = 60
) -> Invocation:
    """Run the target binary through the common subprocess path."""
    return invoke(exe, args, input_data=input_data, timeout=timeout)


def record(label: str, passed: bool, invocation: Optional[Invocation] = None) -> None:
    """Record a check and print classified diagnostics on failure."""
    print(f"  [{'OK ' if passed else 'FAIL'}]  {label}")
    results.append((label, passed))
    if not passed and invocation is not None:
        rc = "none" if invocation.returncode is None else str(invocation.returncode)
        print(f"     result={invocation.result} rc={rc} detail={invocation.detail}")
        print(f"     stdout: {invocation.stdout[:300]}")
        print(f"     stderr: {invocation.stderr[:300]}")


def run_case(
    label: str,
    args: Sequence[str],
    expected: Iterable[str],
    *,
    input_data: Optional[bytes] = None,
    timeout: float = 60,
    verify: Optional[Callable[[Invocation], bool]] = None,
) -> Invocation:
    """Invoke rustrip and require a classified outcome plus optional semantics."""
    invocation = invoke_rustrip(args, input_data=input_data, timeout=timeout)
    passed = invocation.result in set(expected) and (verify(invocation) if verify else True)
    record(label, passed, invocation)
    return invocation


def valid_json_output(invocation: Invocation, *, require_nonempty: bool) -> bool:
    """Require successful JSON output with optional annotation cardinality."""
    return invocation.result == "success" and parse_json_array(
        invocation.stdout, require_nonempty=require_nonempty
    ) is not None


def preflight() -> bool:
    """Require an executable that can launch and emit valid JSON for itself."""
    if not exe.is_file():
        print(f"FAIL PREFLIGHT: executable does not exist: {exe}", file=sys.stderr)
        return False
    if os.name != "nt" and not os.access(exe, os.X_OK):
        print(f"FAIL PREFLIGHT: file is not executable: {exe}", file=sys.stderr)
        return False
    invocation = invoke_rustrip([str(exe), "-f", "json"], timeout=30)
    version = invoke_rustrip(["--version"], timeout=10)
    version_passed = version.result == "success" and "rustrip" in version.stdout.lower()
    record("preflight version", version_passed, version)
    passed = version_passed and valid_json_output(invocation, require_nonempty=False)
    record("preflight self-analysis", passed, invocation)
    return passed


def nonexistent_path() -> Path:
    """Return a platform-independent path that cannot already exist."""
    return Path(tempfile.gettempdir()) / f"rustrip-missing-{uuid.uuid4().hex}.bin"


def remove_if_present(path: Path) -> None:
    """Remove a temporary file only after the case has finished using it."""
    try:
        path.unlink()
    except FileNotFoundError:
        pass


print("=== robustness ===")

if not preflight():
    sys.exit(1)

# The classifier must never turn crashes into ordinary parse rejections.
record(
    "crash marker classifier",
    classify_returncode(1, "thread 'main' panicked at src/main.rs:1") == "crash",
)

# Use missing and sleeping Python programs to verify the exact common launch
# and timeout paths; rustrip invocations below use the same invoke() function.
missing_program = nonexistent_path()
missing_invocation = invoke(missing_program, ["--version"], timeout=1)
record("missing binary is a launch failure", missing_invocation.result == "launch failure", missing_invocation)

timeout_invocation = invoke(
    Path(sys.executable), ["-c", "import time; time.sleep(1)"], timeout=0.01
)
record("timeout is classified", timeout_invocation.result == "timeout", timeout_invocation)

self_bytes = exe.read_bytes()
run_case(
    "stdin valid binary",
    ["-", "-f", "json"],
    {"success"},
    input_data=self_bytes,
    timeout=30,
    verify=lambda invocation: valid_json_output(invocation, require_nonempty=False),
)
run_case(
    "stdin junk",
    ["-", "-f", "json"],
    {"parser rejection"},
    input_data=b"junkjunkjunk",
    timeout=20,
)

run_case(
    "--help exit 0",
    ["--help"],
    {"success"},
    timeout=10,
    verify=lambda invocation: "Make stripped Rust binaries readable again"
    in invocation.stdout + invocation.stderr
    and "Usage:" in invocation.stdout + invocation.stderr,
)
run_case(
    "--version",
    ["--version"],
    {"success"},
    timeout=10,
    verify=lambda invocation: "rustrip" in invocation.stdout,
)
run_case("-h short-help", ["-h"], {"success"}, timeout=10)

run_case("missing file", [str(nonexistent_path()), "-f", "json"], {"input rejection"})
run_case(
    "--format garbage argument rejection",
    [str(exe), "-f", "garbage"],
    {"argument rejection"},
)
run_case("no PATH arg", [], {"argument rejection"})

for size, description in ((0, "0-byte"), (1, "1-byte")):
    with tempfile.NamedTemporaryFile(suffix=".bin", delete=False) as handle:
        handle.write(b"\x00" * size)
        path = Path(handle.name)
    try:
        run_case(f"{description} file", [str(path), "-f", "json"], {"parser rejection"})
    finally:
        remove_if_present(path)

with tempfile.NamedTemporaryFile(suffix=".bin", delete=False) as handle:
    handle.write(os.urandom(100 * 1024 * 1024))
    large_path = Path(handle.name)
try:
    started = time.monotonic()
    invocation = run_case(
        "100 MiB random input",
        [str(large_path), "-f", "json"],
        {"input rejection"},
        timeout=60,
    )
    elapsed = time.monotonic() - started
    record(
        f"100 MiB rejection is prompt ({elapsed:.2f}s)",
        invocation.result == "input rejection" and elapsed < 30,
        invocation,
    )
finally:
    remove_if_present(large_path)

with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
    parallel = list(
        executor.map(
            lambda _index: invoke_rustrip([str(exe), "-f", "json"], timeout=60),
            range(4),
        )
    )
record(
    "4 parallel reads", all(valid_json_output(invocation, require_nonempty=False) for invocation in parallel)
)

run_case(
    "stdout when -o omitted",
    [str(exe), "-f", "json"],
    {"success"},
    timeout=30,
    verify=lambda invocation: valid_json_output(invocation, require_nonempty=False),
)

for output_format in ("json", "ghidra", "binja", "text", "cli", "py-ghidra", "binary-ninja", "bn"):
    output = Path(tempfile.gettempdir()) / f"rustrip-{output_format}-{uuid.uuid4().hex}.out"
    try:
        invocation = run_case(
            f"format '{output_format}'",
            [str(exe), "-f", output_format, "-o", str(output)],
            {"success"},
        )
        record(
            f"format '{output_format}' writes output",
            invocation.result == "success"
            and not invocation.stdout
            and output.is_file()
            and output.stat().st_size > 0,
            invocation,
        )
    finally:
        remove_if_present(output)

run_case("max-string-len 99999", [str(exe), "-f", "table", "--max-string-len", "99999"], {"success"})
run_case(
    "max-string-len -1 argument rejection",
    [str(exe), "-f", "table", "--max-string-len", "-1"],
    {"argument rejection"},
)

with tempfile.NamedTemporaryFile(suffix=".bin", delete=False) as handle:
    handle.write(b"\x7fELF")
    invalid_path = Path(handle.name)
invalid_output = Path(tempfile.gettempdir()) / f"rustrip-invalid-{uuid.uuid4().hex}.json"
try:
    invalid_output.unlink(missing_ok=True)
    invalid_invocation = run_case(
        "invalid input with -o",
        [str(invalid_path), "-f", "json", "-o", str(invalid_output)],
        {"parser rejection"},
    )
    record(
        "invalid input does not create output",
        invalid_invocation.result == "parser rejection" and not invalid_output.exists(),
        invalid_invocation,
    )
finally:
    remove_if_present(invalid_path)
    remove_if_present(invalid_output)

for flag in ("--no-strings", "--no-symbols", "--no-panics"):
    run_case(flag, [str(exe), "-f", "json", flag], {"success"})
run_case(
    "all 3 selective flags",
    [str(exe), "-f", "json", "--no-strings", "--no-symbols", "--no-panics"],
    {"success"},
)

print()
ok_count = sum(1 for _, passed in results if passed)
failed = [label for label, passed in results if not passed]
print(f"=== {ok_count} / {len(results)} passed; {len(failed)} failures ===")
for label in failed:
    print(f"  FAIL: {label}")
sys.exit(0 if not failed else 1)