|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Exercise the real production Make rules with tiny C fixtures. CodeQL starts |
| 3 | +# tracing after cbm-third-party, so every affected object must compile again, |
| 4 | +# while unrelated objects must remain reused. No dry-run/timestamp assertions. |
| 5 | +set -euo pipefail |
| 6 | +ROOT="${1:-$(cd "$(dirname "$0")/.." && pwd)}" |
| 7 | +python3 - "$ROOT" "$BASH" <<'PY' |
| 8 | +import os |
| 9 | +import pathlib |
| 10 | +import re |
| 11 | +import subprocess |
| 12 | +import sys |
| 13 | +import tempfile |
| 14 | +
|
| 15 | +root, bash = pathlib.Path(sys.argv[1]), sys.argv[2] |
| 16 | +failures = [] |
| 17 | +with tempfile.TemporaryDirectory(prefix="cbm-codeql-retrace-") as directory: |
| 18 | + work = pathlib.Path(directory) |
| 19 | +
|
| 20 | + def write(path, text): |
| 21 | + dest = work / path |
| 22 | + dest.parent.mkdir(parents=True, exist_ok=True) |
| 23 | + dest.write_text(text, encoding="utf-8") |
| 24 | +
|
| 25 | + for path in ("Makefile.cbm", "scripts/ci/codeql-retrace-vendored.sh", |
| 26 | + "scripts/ci/select-lanes.sh", ".github/codeql/codeql-config.yml"): |
| 27 | + write(path, (root / path).read_text(encoding="utf-8")) |
| 28 | +
|
| 29 | + # Nested and aliased includes deliberately escape the Makefile's shallow |
| 30 | + # wildcard dependencies. The compiler really compiles these translation units. |
| 31 | + write("vendored/mimalloc/src/static.c", |
| 32 | + '#include "mimalloc/internal.h"\n#include "prim/osx/prim.c"\n') |
| 33 | + write("vendored/mimalloc/include/mimalloc/internal.h", "#define MI_VALUE 7\n") |
| 34 | + write("vendored/mimalloc/src/prim/osx/prim.c", '#include "../unix/prim.c"\n') |
| 35 | + write("vendored/mimalloc/src/prim/unix/prim.c", "int mi_value(void) { return MI_VALUE; }\n") |
| 36 | + write("internal/cbm/vendored/common/scanner.h", "#define SHARED_VALUE 3\n") |
| 37 | + for language in ("go", "json"): |
| 38 | + write(f"internal/cbm/grammar_{language}.c", |
| 39 | + f'#include "vendored/grammars/{language}/parser.c"\n') |
| 40 | + write(f"internal/cbm/vendored/grammars/{language}/parser.c", |
| 41 | + '#include "nested/value.h"\n#include "../../common/scanner.h"\n' |
| 42 | + f'int grammar_{language}(void) {{ return VALUE + SHARED_VALUE; }}\n') |
| 43 | + write(f"internal/cbm/vendored/grammars/{language}/nested/value.h", "#define VALUE 2\n") |
| 44 | + write("internal/cbm/vendored/grammars/shared/shared.h", "/* future shared grammar helper */\n") |
| 45 | + write("internal/cbm/ts_runtime.c", '#include "vendored/ts_runtime/src/lib.c"\n') |
| 46 | + write("internal/cbm/vendored/ts_runtime/src/lib.c", "int runtime_value(void) { return 1; }\n") |
| 47 | + for path, symbol in (("vendored/sqlite3/sqlite3.c", "sqlite_value"), |
| 48 | + ("vendored/tre/tre_all.c", "tre_value"), |
| 49 | + ("internal/cbm/vendored/lz4/lz4.c", "lz4_value"), |
| 50 | + ("internal/cbm/vendored/lz4/lz4hc.c", "lz4hc_value"), |
| 51 | + ("internal/cbm/vendored/zstd/zstd.c", "zstd_value")): |
| 52 | + write(path, f"int {symbol}(void) {{ return 1; }}\n") |
| 53 | + write("internal/cbm/vendored/lz4/lz4.h", "/* shared LZ4 header */\n") |
| 54 | + write("vendored/sqlite3/sqlite3.h", "/* sqlite header */\n") |
| 55 | + write("internal/cbm/vendored/zstd/zstd.h", "/* zstd header */\n") |
| 56 | + write("vendored/nomic/code_vectors_blob.S", ".text\n") |
| 57 | + write("vendored/nomic/code_vectors.bin", "") |
| 58 | + write("src/main.c", "int main(void) { return 0; }\n") |
| 59 | +
|
| 60 | + def run(args): |
| 61 | + proc = subprocess.run(args, cwd=work, capture_output=True, text=True) |
| 62 | + if proc.returncode: |
| 63 | + raise RuntimeError(f"{args}: exit {proc.returncode}\n{proc.stdout}\n{proc.stderr}") |
| 64 | + return proc.stdout |
| 65 | +
|
| 66 | + make = ["make", "--no-print-directory", "-f", "Makefile.cbm", "cbm-third-party", |
| 67 | + "BUILD_DIR=build/c", "CC=" + os.environ.get("CC", "cc")] |
| 68 | +
|
| 69 | + def compiled(output): |
| 70 | + return set(re.findall(r" -c -o build/c/([^\s]+\.o)(?:\s|$)", output)) |
| 71 | +
|
| 72 | + prebuilt = compiled(run(make)) |
| 73 | + expected = {"prod_mimalloc.o", "prod_sqlite3.o", "prod_grammar_go.o", |
| 74 | + "prod_grammar_json.o", "prod_ts_runtime.o", "prod_lz4.o", |
| 75 | + "prod_lz4hc.o", "prod_zstd.o", "unixcoder_blob.o"} |
| 76 | + # TRE is a Windows-only production object. All other objects are mandatory. |
| 77 | + if prebuilt not in (expected, expected | {"prod_tre.o"}): |
| 78 | + raise RuntimeError(f"incomplete prebuild: {sorted(prebuilt)}") |
| 79 | + if compiled(run(make)): |
| 80 | + raise RuntimeError("unchanged prebuild is not reused") |
| 81 | +
|
| 82 | + cases = [ |
| 83 | + ("nested allocator header", ["vendored/mimalloc/include/mimalloc/internal.h"], {"prod_mimalloc.o"}), |
| 84 | + ("aliased allocator source", ["vendored/mimalloc/src/prim/unix/prim.c"], {"prod_mimalloc.o"}), |
| 85 | + ("nested grammar header", ["internal/cbm/vendored/grammars/go/nested/value.h"], {"prod_grammar_go.o"}), |
| 86 | + ("grammar wrapper", ["internal/cbm/grammar_go.c"], {"prod_grammar_go.o"}), |
| 87 | + ("runtime wrapper", ["internal/cbm/ts_runtime.c"], {"prod_ts_runtime.o"}), |
| 88 | + ("runtime source", ["internal/cbm/vendored/ts_runtime/src/lib.c"], {"prod_ts_runtime.o"}), |
| 89 | + ("shared grammar header", ["internal/cbm/vendored/common/scanner.h"], {"prod_grammar_go.o", "prod_grammar_json.o"}), |
| 90 | + ("unknown grammar family", ["internal/cbm/vendored/grammars/shared/shared.h"], {"prod_grammar_go.o", "prod_grammar_json.o"}), |
| 91 | + ("LZ4 header", ["internal/cbm/vendored/lz4/lz4.h"], {"prod_lz4.o", "prod_lz4hc.o"}), |
| 92 | + ("SQLite header", ["vendored/sqlite3/sqlite3.h"], {"prod_sqlite3.o"}), |
| 93 | + ("zstd header", ["internal/cbm/vendored/zstd/zstd.h"], {"prod_zstd.o"}), |
| 94 | + ("first-party only", ["src/main.c"], set()), |
| 95 | + ("duplicate changes", ["internal/cbm/grammar_go.c", "internal/cbm/grammar_go.c"], {"prod_grammar_go.o"}), |
| 96 | + ] |
| 97 | + for label, paths, expected_objects in cases: |
| 98 | + write("changed.txt", "\n".join(paths) + "\n") |
| 99 | + run([bash, "scripts/ci/codeql-retrace-vendored.sh", "--changed", "changed.txt", "retrace.txt"]) |
| 100 | + retrace = (work / "retrace.txt").read_text(encoding="utf-8").splitlines() |
| 101 | + actual = compiled(run(make + [arg for path in retrace for arg in ("-W", path)])) |
| 102 | + if actual != expected_objects: |
| 103 | + failures.append(f"{label}: expected {sorted(expected_objects)}, compiled {sorted(actual)}") |
| 104 | + else: |
| 105 | + print(f"PASS {label}: {len(actual)}/{len(prebuilt)} third-party objects retraced") |
| 106 | +if failures: |
| 107 | + print("CODEQL RETRACE CONTRACT VIOLATED:\n " + "\n ".join(failures)) |
| 108 | + sys.exit(1) |
| 109 | +print("CodeQL retrace contract OK (real compiler + production Make rules)") |
| 110 | +PY |
0 commit comments