import json
import os
import re
import shutil
import subprocess import sys
import tempfile
from pathlib import Path
from typing import Optional, Tuple
NORMAL_REJECTION = 1
PANIC_MARKERS = (
"panicked at",
"fatal runtime error",
"segmentation fault",
"access violation",
"illegal instruction",
"stack overflow",
"abort trap",
)
WINDOWS_EXCEPTION_CODES = {
0x40000015, 0x80000003, 0xC0000005, 0xC000001D, 0xC0000094, 0xC0000096, 0xC00000FD, 0xC0000135, 0xC0000139, 0xC0000142, 0xC0000374, 0xC0000409, }
MALFORMED_PARSER_CAUSE_PREFIXES = (
"ELF ",
"ELF:",
"PE ",
"PE:",
"Mach-O ",
"Mach-O:",
"goblin: failed to parse object",
)
MALFORMED_PARSER_CAUSES = {
"unsupported object kind (archive)",
"unsupported object kind (COFF)",
"unsupported object kind (likely archive)",
"Mach-O fat archives are not supported",
}
def has_crash_marker(stderr: str) -> bool:
lowered = stderr.lower()
return any(marker in lowered for marker in PANIC_MARKERS)
def is_crash_returncode(returncode: int) -> bool:
if returncode < 0:
return True
normalized = returncode & 0xFFFF_FFFF
return normalized in WINDOWS_EXCEPTION_CODES or normalized >= 0xC0000000
def loading_binary_cause(stderr: str) -> Optional[str]:
header = "Error: loading binary\n\nCaused by:\n"
if not stderr.startswith(header):
return None
for line in stderr[len(header) :].splitlines():
cause = line.strip()
if cause:
return re.sub(r"^\d+:\s*", "", cause)
return None
def is_parser_rejection(stderr: str) -> bool:
first_line = stderr.splitlines()[0] if stderr else ""
if re.fullmatch(r"Error: binary too small: \d+ bytes", first_line):
return True
cause = loading_binary_cause(stderr)
return cause in MALFORMED_PARSER_CAUSES or (
cause is not None and cause.startswith(MALFORMED_PARSER_CAUSE_PREFIXES)
)
def is_input_rejection(stderr: str) -> bool:
first_line = stderr.splitlines()[0] if stderr else ""
return (stderr.startswith("Error: opening ") and "\n\nCaused by:" in stderr) or bool(
re.fullmatch(
r"Error: input .+ exceeds the maximum supported size of \d+ bytes",
first_line,
)
)
def is_argument_rejection(stderr: str) -> bool:
first_line = stderr.splitlines()[0] if stderr else ""
missing_path = (
first_line == "error: the following required arguments were not provided:"
and "<PATH>" in stderr
and "\n\nUsage:" in stderr
)
invalid_format = (
first_line.startswith("error: invalid value ")
and " for '--format <FORMAT>': unknown output format " in first_line
)
invalid_max_string_len = (
first_line.startswith("error: invalid value ")
and " for '--max-string-len <MAX_STRING_LEN>':" in first_line
)
unexpected_negative_max_string_len = (
first_line == "error: unexpected argument '-1' found"
and "to pass '-1' as a value" in stderr
and "\n\nUsage:" in stderr
)
return (
missing_path
or invalid_format
or invalid_max_string_len
or unexpected_negative_max_string_len
)
def classify_returncode(returncode: int, stderr: str = "") -> str:
if has_crash_marker(stderr) or is_crash_returncode(returncode):
return "crash"
if returncode == 0:
return "success"
if returncode == NORMAL_REJECTION and is_parser_rejection(stderr):
return "parser rejection"
if returncode == NORMAL_REJECTION and is_input_rejection(stderr):
return "input rejection"
if returncode == 2 and is_argument_rejection(stderr):
return "argument rejection"
return "abnormal exit"
def parse_json_array(stdout: str, *, require_nonempty: bool) -> Optional[list]:
try:
value = json.loads(stdout)
except (json.JSONDecodeError, TypeError):
return None
if not isinstance(value, list) or (require_nonempty and not value):
return None
return value
def preflight_result_is_valid(returncode: int, stdout: str, stderr: str) -> bool:
return (
classify_returncode(returncode, stderr) == "success"
and parse_json_array(stdout, require_nonempty=False) is not None
)
def expected_accepts(expected: str, result: str) -> bool:
if expected == "ok":
return result == "success"
if expected == "parser":
return result == "parser rejection"
if expected == "either":
return result in {"success", "parser rejection"}
raise ValueError(f"unknown expected outcome: {expected}")
def run_self_tests() -> None:
nonempty = '[{"vaddr":"0x1"}]'
always_exit_one = subprocess.CompletedProcess(["fake"], 1, nonempty, "")
unix_crash = subprocess.CompletedProcess(["fake"], -11, nonempty, "")
windows_crash = subprocess.CompletedProcess(["fake"], 0xC0000005, nonempty, "")
wrapped_internal_error = (
"Error: loading binary\n\nCaused by:\n internal invariant failed\n"
)
checks = (
*(
not preflight_result_is_valid(
completed.returncode, completed.stdout, completed.stderr
)
for completed in (always_exit_one, unix_crash, windows_crash)
),
classify_returncode(1, "Error: binary too small: 3 bytes\n") == "parser rejection",
classify_returncode(1, "Error: loading binary\n\nCaused by:\n ELF header: truncated input\n")
== "parser rejection",
classify_returncode(
1,
"Error: loading binary\n\nCaused by:\n"
" 0: goblin: failed to parse object\n"
" 1: Malformed entity\n",
)
== "parser rejection",
classify_returncode(
1,
"Error: loading binary\n\nCaused by:\n"
" unsupported object kind (archive)\n",
)
== "parser rejection",
classify_returncode(1, "Error: opening missing.bin\n\nCaused by:\n not found")
== "input rejection",
classify_returncode(1, wrapped_internal_error) == "abnormal exit",
not expected_accepts("parser", classify_returncode(1, wrapped_internal_error)),
not expected_accepts("parser", "crash"),
not expected_accepts("either", "crash"),
not expected_accepts("parser", "abnormal exit"),
classify_returncode(
2,
"error: invalid value 'garbage' for '--format <FORMAT>': "
"unknown output format 'garbage'\n",
)
== "argument rejection",
classify_returncode(
2,
"error: the following required arguments were not provided:\n"
" <PATH>\n\nUsage: rustrip <PATH>\n",
)
== "argument rejection",
classify_returncode(
2,
"error: unexpected argument '-1' found\n\n"
"tip: to pass '-1' as a value, use '-- -1'\n\n"
"Usage: rustrip.exe [OPTIONS] <PATH>\n",
)
== "argument rejection",
expected_accepts("parser", "parser rejection"),
expected_accepts("either", "success"),
preflight_result_is_valid(0, nonempty, ""),
)
if not all(checks):
raise RuntimeError("adversarial helper self-test failed")
print("adversarial helper self-tests passed")
def invoke(exe: Path, target: Path, timeout: int) -> Tuple[str, Optional[int], str, str]:
try:
completed = subprocess.run( [str(exe), str(target), "-f", "json"],
capture_output=True,
text=True,
timeout=timeout,
)
except subprocess.TimeoutExpired as error:
stdout = error.stdout.decode(errors="replace") if isinstance(error.stdout, bytes) else error.stdout
stderr = error.stderr.decode(errors="replace") if isinstance(error.stderr, bytes) else error.stderr
return "timeout", None, stdout or "", stderr or ""
except OSError as error:
return "launch failure", None, "", str(error)
result = classify_returncode(completed.returncode, completed.stderr or "")
if result == "success" and parse_json_array(completed.stdout, require_nonempty=False) is None:
result = "invalid json"
return result, completed.returncode, completed.stdout, completed.stderr
def preflight(exe: Path) -> bool:
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
try:
version = subprocess.run( [str(exe), "--version"],
capture_output=True,
text=True,
timeout=10,
)
except subprocess.TimeoutExpired:
print("FAIL PREFLIGHT: --version timed out", file=sys.stderr)
return False
except OSError as error:
print(f"FAIL PREFLIGHT: could not launch --version: {error}", file=sys.stderr)
return False
if version.returncode != 0 or "rustrip" not in (version.stdout or "").lower():
print(
f"FAIL PREFLIGHT: --version failed rc={version.returncode}",
file=sys.stderr,
)
if version.stderr:
print(f"stderr: {version.stderr[:500]}", file=sys.stderr)
return False
try:
completed = subprocess.run( [str(exe), str(exe), "-f", "json"],
capture_output=True,
text=True,
timeout=30,
)
except subprocess.TimeoutExpired:
print("FAIL PREFLIGHT: self-analysis timed out", file=sys.stderr)
return False
except OSError as error:
print(f"FAIL PREFLIGHT: could not launch executable: {error}", file=sys.stderr)
return False
if not preflight_result_is_valid(
completed.returncode, completed.stdout, completed.stderr or ""
):
result = classify_returncode(completed.returncode, completed.stderr or "")
print(
"FAIL PREFLIGHT: self-analysis must exit 0 and emit a valid "
f"JSON array; got {result}, rc={completed.returncode}",
file=sys.stderr,
)
if completed.stderr:
print(f"stderr: {completed.stderr[:500]}", file=sys.stderr)
return False
return True
def retain_random_failure(desc: str, data: bytes) -> None:
import hashlib
digest = hashlib.sha256(data).hexdigest()
retained_dir = Path.cwd() / "adversarial-failures"
retained_path = retained_dir / f"{digest}.bin"
try:
retained_dir.mkdir(exist_ok=True)
if not retained_path.exists():
retained_path.write_bytes(data)
except OSError as error:
print(
f" could not retain random input sha256={digest}: {error}",
file=sys.stderr,
)
return
print(
f" retained random input for {desc}: {retained_path.resolve()} sha256={digest}",
file=sys.stderr,
)
def report_case(
desc: str,
expected: str,
data: bytes,
exe: Path,
tmp: Path,
*,
retain_on_failure: bool,
) -> bool:
path = tmp / "input.bin"
path.write_bytes(data)
result, returncode, _stdout, stderr = invoke(exe, path, 20)
passed = expected_accepts(expected, result)
status = "PASS" if passed else "FAIL"
rc_text = "none" if returncode is None else str(returncode)
print(f" {status:<5} {desc:<30} size={len(data):<8} rc={rc_text} ({result})")
if not passed:
if stderr:
print(f" stderr: {stderr[:500]}", file=sys.stderr)
if retain_on_failure:
retain_random_failure(desc, data)
return passed
def run_suite(exe: Path) -> int:
if not preflight(exe):
return 1
tmp = Path(tempfile.mkdtemp(prefix="rustrip-adv-"))
failures = 0
total = 0
try:
print(f"tmp: {tmp}\n", file=sys.stderr)
cases = [
("0 bytes", "parser", b"", False),
("1 byte", "parser", b"\x7f", False),
("2 bytes", "parser", b"AB", False),
("3 bytes", "parser", b"ABC", False),
("4 bytes ELF?", "parser", b"\x7fELF", False),
("4 bytes PE?", "parser", b"MZ\x90\x00", False),
("7 bytes ELF extend", "parser", b"\x7fELF\xff\xff", False),
("8 bytes PE extend", "parser", b"MZ\x90\x00\xff\xff", False),
("0xff x 16", "parser", b"\xff" * 16, False),
("0xff x 4096", "parser", b"\xff" * 4096, False),
("0x00 x 4096", "parser", b"\x00" * 4096, False),
("ASCII like Rust", "either", b'fn main() { println!("hi"); }\n', False),
("Random 1KB", "either", os.urandom(1024), True),
("Random 64KB", "either", os.urandom(65536), True),
("Random 1MB", "either", os.urandom(1024 * 1024), True),
("JPEG-like SOI + garbage", "either", b"\xff\xd8\xff\xe0" + b"\x00" * 4096, False),
("AR archive", "parser", b"!<arch>\x0a" + b"x" * 4096, False),
("gzip-ish", "either", b"\x1f\x8b\x08\x00" + b"\x00" * 8192, False),
]
for desc, expected, data, retain_on_failure in cases:
total += 1
if not report_case(
desc,
expected,
data,
exe,
tmp,
retain_on_failure=retain_on_failure,
):
failures += 1
print("\nstress: 200 random inputs (success or parser rejection only)...\n", file=sys.stderr)
for index in range(200):
size = (index * 17 + 1) % 8192
path = tmp / "stress.bin"
data = os.urandom(size)
path.write_bytes(data)
total += 1
result, returncode, _stdout, stderr = invoke(exe, path, 15)
if expected_accepts("either", result):
continue
rc_text = "none" if returncode is None else str(returncode)
print(
f" FAIL stress{index} size={size} rc={rc_text} ({result})",
file=sys.stderr,
)
if stderr:
print(f" stderr: {stderr[:500]}", file=sys.stderr)
retain_random_failure(f"stress{index}", data)
failures += 1
break
finally:
shutil.rmtree(tmp, ignore_errors=True)
print(f"\n=== {total} cases; {failures} failures ===")
return 0 if failures == 0 else 1
def main() -> int:
if len(sys.argv) == 2 and sys.argv[1] == "--self-test":
run_self_tests()
return 0
exe_arg = sys.argv[1] if len(sys.argv) > 1 else "./target/release/rustrip"
return run_suite(Path(exe_arg).expanduser().resolve())
if __name__ == "__main__":
sys.exit(main())