From f442c3dfba5dca9dcf144ef71f1c52b6b3bd1cf1 Mon Sep 17 00:00:00 2001 From: TheGeogeo <57634077+TheGeogeo@users.noreply.github.com> Date: Tue, 1 Dec 2026 01:49:41 +0100 Subject: [PATCH] scripts: red_green.py tells a crashed compiler from a build error (TEST-RED-GREEN-COMPILER-CRASH) A verification's only trial said "red does not build" while rustc had crashed (0xC0000005). A side whose cargo output shows rustc or a build script ending with a Windows crash status, an internal compiler error or a failed allocation now builds again once, and the report says so; a second crash gives the verdict `compiler crashed` (exit 1), not `red does not build`. A test binary that crashes stays a test result. --- docs/testing.md | 7 +++-- scripts/red_green.py | 41 +++++++++++++++++++++--- tests/test_red_green.py | 69 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 6 deletions(-) diff --git a/docs/testing.md b/docs/testing.md index 635c700a65..c7b91a4a76 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -150,8 +150,11 @@ python -I scripts/red_green.py target dir takes several GB; delete the folder (and run `git worktree prune`) when done. - It prints the tests that go from red to green, those that pass on red and fail on green, those that fail on both (failing on `main` too), and those found on one side only. The verdict is one of: `red then green` (exit 0), - `no red`, `red does not build`, `green fails`, `red not constructible`, `green not constructible`, `no tests ran` - (exit 1). `--json` prints all of it. + `no red`, `red does not build`, `green fails`, `red not constructible`, `green not constructible`, `no tests ran`, + `compiler crashed` (exit 1). `--json` prints all of it. +- A crashed compiler is not a build error: rustc or a build script ending with a Windows crash status (such as + 0xC0000005, an access violation), an internal compiler error, or a failed allocation. That side builds again once, + and the report says so; a second crash gives `compiler crashed`. Run it again later, or on another machine. - `red does not build` is the case the fork-CI steps above call "does not build without the pull request's new file": the test needs the new code, which does not show that it catches the bug. - `--crate ` adds a crate, `--no-cargo` or `--no-python` leaves one kind out, `--base ` tests on another diff --git a/scripts/red_green.py b/scripts/red_green.py index 1af2fd1677..7d8305f453 100644 --- a/scripts/red_green.py +++ b/scripts/red_green.py @@ -20,7 +20,8 @@ toolchain applies) and the changed `tests/**/test*.py` files with unittest, on both sides, in one worktree and one cargo target dir under --work-dir (default `/red-green`), red first. It keeps both between runs: the second run on the same crates builds only what changed. C++ `runtime/` tests are not run here (docs/testing.md has -the fork-CI way). +the fork-CI way). A side whose compiler crashed (a Windows crash status of rustc or a build script, an internal +compiler error, a failed allocation) builds again once; a second crash is the verdict `compiler crashed`. Exit 0: at least one test went from red to green and no test that passes on red fails on green. 1: any other verdict. 2: a usage or git error. @@ -272,7 +273,16 @@ def crates_of(tree: Path, paths: list[str]) -> list[str]: return names -def cargo_tests(tree: Path, crates: list[str], cargo: str, target: Path) -> dict: +# A crashed compiler, not an error in the code: rustc or a build script ending with a Windows crash status +# (0xC0000005 access violation, 0xC00000FD stack overflow, 0xC0000409 abort, ...), an internal compiler error, or a +# failed allocation. Such a side runs again once (a crash of a test binary stays a test result). +COMPILER_CRASH = re.compile( + r"process didn't exit successfully: `[^`]*(?:rustc|build-script-\w+)[^`]*` \(exit (?:code|status): " + r"(0x[cC][0-9a-fA-F]{7}(?:, \w+)?)\)" + r"|(internal compiler error)|(the compiler unexpectedly panicked)|(memory allocation of \d+ bytes failed)") + + +def cargo_once(tree: Path, crates: list[str], cargo: str, target: Path) -> dict: cmd = [sys.executable, cargo] if cargo.endswith(".py") else [cargo] cmd += ["test", "--locked", "--no-fail-fast"] + [a for c in crates for a in ("-p", c)] env = dict(os.environ, CARGO_TARGET_DIR=str(target)) @@ -290,7 +300,21 @@ def cargo_tests(tree: Path, crates: list[str], cargo: str, target: Path) -> dict elif error is None and CARGO_ERROR.match(line): error = line.strip() no_build = r.returncode != 0 and not tests - return {"tests": tests, "no_build": (error or f"cargo exit {r.returncode}") if no_build else None} + crash = COMPILER_CRASH.search(out) if no_build else None + return {"tests": tests, "no_build": (error or f"cargo exit {r.returncode}") if no_build and not crash else None, + "crash": next(g for g in crash.groups() if g) if crash else None} + + +def cargo_tests(tree: Path, crates: list[str], cargo: str, target: Path) -> dict: + first = cargo_once(tree, crates, cargo, target) + if not first["crash"]: + return {"tests": first["tests"], "no_build": first["no_build"]} + print(f"red_green: the compiler crashed ({first['crash']}), running again", file=sys.stderr) + again = cargo_once(tree, crates, cargo, target) + result = {"tests": again["tests"], "no_build": again["no_build"], "rerun_after": first["crash"]} + if again["crash"]: + result["compiler_crashed"] = again["crash"] + return result PY_TEST = re.compile(r"^(\w+) \(([\w.]+)\)(?:\n[^\n]*?)? \.\.\. (ok|FAIL|ERROR|skipped|expected failure|unexpected success)", @@ -318,12 +342,14 @@ def python_tests(tree: Path, files: list[str]) -> dict: def side(tree: Path, crates: list[str], py_files: list[str], cargo: str, target: Path) -> dict: - result = {"tests": {}, "no_build": []} + result = {"tests": {}, "no_build": [], "compiler_crashed": None, "rerun_after": None} for part in ([cargo_tests(tree, crates, cargo, target)] if crates else []) + \ ([python_tests(tree, py_files)] if py_files else []): result["tests"].update(part["tests"]) if part["no_build"]: result["no_build"].append(part["no_build"]) + for key in ("compiler_crashed", "rerun_after"): + result[key] = result[key] or part.get(key) return result @@ -355,6 +381,9 @@ def compare(red: dict | None, green: dict | None) -> dict: def verdict(red_why: str | None, green_why: str | None, red: dict | None, green: dict | None, rows: dict) -> str: if green_why: return "green not constructible" + # Twice in a row on a side: the build cannot tell anything; not a code error, run it again later. + if any(s and s.get("compiler_crashed") for s in (red, green)): + return "compiler crashed" green_failed = rows["regressions"] or any(r.endswith("(failed)") for r in rows["only_on_green"]) if green and green["no_build"] or green_failed: return "green fails" @@ -379,6 +408,10 @@ def report(r: dict) -> str: s = r[name] if s and s["no_build"]: out.append(f"{name} does not build: " + "; ".join(s["no_build"])) + if s and s.get("compiler_crashed"): + out.append(f"{name}: the compiler crashed twice ({s['compiler_crashed']})") + elif s and s.get("rerun_after"): + out.append(f"{name}: the compiler crashed once ({s['rerun_after']}), the second build is the one shown") rows = r["rows"] labels = {"red_to_green": "red then green", "regressions": "pass on red, FAIL on green", "fail_on_both": "fail on both (failing on the base too?)", "only_on_green": "only on green", diff --git a/tests/test_red_green.py b/tests/test_red_green.py index 4d20e5c902..5f4b895298 100644 --- a/tests/test_red_green.py +++ b/tests/test_red_green.py @@ -47,7 +47,32 @@ ''' +# A cargo whose rustc crashes like an access violation on Windows: always, or on its first call only (a marker file +# beside it), then the stub above. +CRASH_STUB = r''' +import os, runpy, sys +here = os.path.dirname(os.path.abspath(__file__)) +mark = os.path.join(here, "crashed") +if __MODE__ == "always" or not os.path.exists(mark): + open(mark, "w").close() + print(" Compiling foo v0.1.0") + print("error: could not compile `foo` (lib test)") + print("") + print("Caused by:") + print(" process didn't exit successfully: `rustc --crate-name foo --edition=2021 src/lib.rs --test` " + "(exit code: 0xc0000005, STATUS_ACCESS_VIOLATION)") + sys.exit(101) +runpy.run_path(os.path.join(here, "cargo_stub.py"), run_name="__main__") +''' + + class Repo: + def crash_stub(self, mode): + path = os.path.join(self.tmp.name, f"cargo_crash_{mode}.py") + with open(path, "w", encoding="utf-8") as f: + f.write(CRASH_STUB.replace("__MODE__", repr(mode))) + return path + def __init__(self, test): self.tmp = tempfile.TemporaryDirectory(prefix="red-green-test-", ignore_cleanup_errors=True) test.addCleanup(self.tmp.cleanup) @@ -211,6 +236,50 @@ def test_a_test_of_a_new_tool_does_not_import_on_red(self): self.assertEqual(code, 1) +class CompilerCrash(unittest.TestCase): + def fixed_repo(self): + repo = crate_repo(self) + repo.commit("fix", {"d3d12/crates/foo/src/lib.rs": LIB.replace(" 1\n", " 2\n").replace( + "fn value_is_one() {\n assert_eq!(super::value(), 1);", + "fn value_is_two() {\n assert_eq!(super::value(), 2);")}) + return repo + + def test_a_compiler_that_crashes_once_is_run_again(self): + repo = self.fixed_repo() + code, r, err = repo.run("--cargo", repo.crash_stub("once")) + self.assertEqual((code, r["verdict"]), (0, "red then green"), r) + self.assertEqual(r["red"]["rerun_after"], "0xc0000005, STATUS_ACCESS_VIOLATION") + self.assertIsNone(r["red"]["compiler_crashed"]) + self.assertIn("the compiler crashed (0xc0000005, STATUS_ACCESS_VIOLATION), running again", err) + + def test_a_compiler_that_crashes_twice_is_not_a_build_error(self): + repo = self.fixed_repo() + code, r, _ = repo.run("--cargo", repo.crash_stub("always")) + self.assertEqual((code, r["verdict"]), (1, "compiler crashed"), r) + self.assertEqual(r["red"]["compiler_crashed"], "0xc0000005, STATUS_ACCESS_VIOLATION") + self.assertEqual(r["red"]["no_build"], []) + self.assertIn("red: the compiler crashed twice", rg.report(r)) + + def test_what_counts_as_a_crashed_compiler(self): + crashed = [ + " process didn't exit successfully: `C:\\x\\rustc.exe --crate-name a` (exit code: 0xc00000fd, STATUS_STACK_OVERFLOW)", + " process didn't exit successfully: `C:\\t\\build\\a-1\\build-script-build` (exit code: 0xc0000409)", + "error: internal compiler error: compiler/rustc_middle/src/ty/mod.rs:1: oops", + "note: the compiler unexpectedly panicked. this is a bug.", + "memory allocation of 1048576 bytes failed", + ] + for line in crashed: + self.assertIsNotNone(rg.COMPILER_CRASH.search(line), line) + not_crashed = [ + # A test binary that crashes is a test result, and an ordinary failure is a build error. + " process didn't exit successfully: `C:\\t\\debug\\deps\\foo-0123456789abcdef.exe` (exit code: 0xc0000005, STATUS_ACCESS_VIOLATION)", + " process didn't exit successfully: `C:\\x\\rustc.exe --crate-name a` (exit code: 1)", + "error[E0425]: cannot find value `x` in this scope", + ] + for line in not_crashed: + self.assertIsNone(rg.COMPILER_CRASH.search(line), line) + + class Hunks(unittest.TestCase): def diff(self, old, new): with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp: