rustrip 0.1.13

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#!/usr/bin/env python3
"""Adversarial-input regression test for rustrip.

Usage:
    bash scripts/adversarial_test.sh [path-to-rustrip-binary]
    bash scripts/adversarial_test.sh --self-test

The supplied executable must first launch, report its version, and analyze
itself as a valid JSON array. Adversarial cases then accept only exit 0 or the documented parser
rejection exit 1; panics, signals, Windows exception statuses, timeouts,
launch failures, malformed JSON, and other exit codes fail the test.
"""

# nosec: subprocess.run always receives a fixed argv list with shell=False.

import json
import os
import re
import shutil
import subprocess  # nosec
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,  # STATUS_FATAL_APP_EXIT
    0x80000003,  # STATUS_BREAKPOINT
    0xC0000005,  # STATUS_ACCESS_VIOLATION
    0xC000001D,  # STATUS_ILLEGAL_INSTRUCTION
    0xC0000094,  # STATUS_INTEGER_DIVIDE_BY_ZERO
    0xC0000096,  # STATUS_PRIVILEGED_INSTRUCTION
    0xC00000FD,  # STATUS_STACK_OVERFLOW
    0xC0000135,  # STATUS_DLL_NOT_FOUND
    0xC0000139,  # STATUS_ENTRYPOINT_NOT_FOUND
    0xC0000142,  # STATUS_DLL_INIT_FAILED
    0xC0000374,  # STATUS_HEAP_CORRUPTION
    0xC0000409,  # STATUS_STACK_BUFFER_OVERRUN
}
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:
    """Return whether stderr contains a known panic or fatal-crash marker."""
    lowered = stderr.lower()
    return any(marker in lowered for marker in PANIC_MARKERS)


def is_crash_returncode(returncode: int) -> bool:
    """Recognize Unix signals and common signed/unsigned Windows statuses."""
    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]:
    """Extract the first cause from the stable anyhow loading-binary layout."""
    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:
    """Recognize only documented malformed parser failures from `Binary::parse`."""
    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:
    """Recognize only documented input-opening and input-size failures."""
    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:
    """Recognize only the CLI diagnostics this harness deliberately invokes."""
    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:
    """Classify a completed invocation without treating arbitrary errors as rejection."""
    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]:
    """Parse stdout as an array, optionally requiring at least one annotation."""
    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:
    """Pure helper used by preflight and `--self-test`."""
    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:
    """Accept only the case-specific process outcome requested by a test."""
    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:
    """Prove common false-green process results cannot pass the harness."""
    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]:
    """Invoke rustrip and return classification, return code, stdout, stderr."""
    try:
        completed = subprocess.run(  # nosec
            [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:
    """Require a launchable rustrip that emits version and valid JSON."""
    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(  # nosec
            [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(  # nosec
            [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:
    """Persist a failing random input outside the temporary suite directory."""
    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:
    """Write and execute one case, returning whether it met the strict outcome."""
    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:
    """Run preflight, fixed adversarial cases, and randomized stress cases."""
    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())