diff --git a/internal/cbm/vendored/grammars/MANIFEST.md b/internal/cbm/vendored/grammars/MANIFEST.md index 2d828f958..dc8c469b9 100644 --- a/internal/cbm/vendored/grammars/MANIFEST.md +++ b/internal/cbm/vendored/grammars/MANIFEST.md @@ -126,6 +126,7 @@ row instead. | vbnet | `vbnet/scanner.c`, `scan_xml_literal` (patch file: `tools/tree-sitter-vbnet/patches/xml-literal-tag-syntax.patch`) | inside an element tag, outside a quoted value, accept only name characters, blanks, `=`, quotes, `/>`, `>` and `<%=`; in element text, require `<` to start a tag, closing tag, comment, PI or embedded expression, and `&` to start a reference; otherwise the scan fails | **Quadratic lexing at attribute lines.** CodeGraph's scanner is offered a `<` where a declaration or member starts. There it opens a VB attribute block (` _`), never an XML literal, but the scan took `start_tag);` before `state->start_tag = NULL;` | **Leak (LeakSanitizer, Linux x64; 0e61867d9).** tree-sitter calls deserialize repeatedly during a parse, and each call dropped the previous `start_tag` allocation; freeing it first is safe because create() NULL-initializes it and destroy() frees and NULLs it. Upstream fixed the same leak after our pin in `ee2f84f9b857` (2026-04-24, merged via #361 on 2026-09-19; it also moves the free out of serialize and checks the allocation): drop this row on a refresh past it. `tools/tree-sitter-sql/src/scanner.c` is this patched file | | swift | `swift/scanner.c`, `OP_SYMBOL_SUPPRESSOR` + `eat_operators` | `1UL <<` / `1 <<` → `1ULL <<` | UBSan: `1 << suppressor` shifts an `int` by up to `TOKEN_COUNT` bits, undefined once the index reaches 31, while the mask it feeds is `uint64_t`. `1UL << FAKE_TRY_BANG` is the same defect on Windows, where `unsigned long` is 32 bits and `FAKE_TRY_BANG` is 32; the CLANGARM64 leg runs UBSan in trap mode, so there it is an illegal instruction rather than a log line. Upstream already carries both changes: `fb63a7004f07` (2026-04-06, upstream #558) for `eat_operators`, `6ab8d1d74ebd` (2026-08-10) for the `OP_SYMBOL_SUPPRESSOR` entry. Our pin `8abb3e8b3325` (2026-03-20) predates both, so this is a backport rather than a local invention — a re-vendor past 2026-08-10 should delete this row, not re-apply it | +| cobol | `cobol/scanner.c`, scan, `LINE_PREFIX_COMMENT` loop and `COMMENT_ENTRY` branch (#2998, #3000) | the column-prefix loop also stops at `lookahead == 0`; `COMMENT_ENTRY` is not emitted when `start_with_word()` consumed nothing | **Livelock at EOF.** `advance()` is a no-op at end of input, so a file reaching EOF at column 5 or less (a `.cob` file shorter than 6 bytes, or the empty line after a final newline in the right parser state) spun the scanner forever and the index worker until its quiet timeout. The `COMMENT_ENTRY` guard is defensive (zero-length external token); with the current tables it cannot fire. In-house grammar (see "Authored in-house"): there is no upstream to re-vendor from, the row records the change for the vendoring audit (`repro_issue2998_*` in `tests/repro/repro_extraction.c`) | ## Vendored from verified upstream diff --git a/internal/cbm/vendored/grammars/cobol/scanner.c b/internal/cbm/vendored/grammars/cobol/scanner.c index b0fd35277..263e7bb6e 100644 --- a/internal/cbm/vendored/grammars/cobol/scanner.c +++ b/internal/cbm/vendored/grammars/cobol/scanner.c @@ -31,9 +31,14 @@ char* any_content_keyword[] = { "procedure division", }; -static bool start_with_word( TSLexer *lexer, char *words[], int number_of_words) { +// consumed_any reports whether this call advanced the lexer at all, so the +// caller can refuse to emit a zero-length token (tree-sitter livelock). +static bool start_with_word(TSLexer *lexer, char *words[], int number_of_words, + bool *consumed_any) { + *consumed_any = false; while(lexer->lookahead == ' ' || lexer->lookahead == '\t') { lexer->advance(lexer, true); + *consumed_any = true; } char *keyword_pointer[number_of_words]; @@ -60,6 +65,7 @@ static bool start_with_word( TSLexer *lexer, char *words[], int number_of_words) if(all_match_failed) { for(; lexer->get_column(lexer) < 71 && lexer->lookahead != '\n' && lexer->lookahead != 0; lexer->advance(lexer, true)) { + *consumed_any = true; } return false; } @@ -83,6 +89,7 @@ static bool start_with_word( TSLexer *lexer, char *words[], int number_of_words) // next character lexer->advance(lexer, true); + *consumed_any = true; } return false; @@ -106,7 +113,10 @@ bool tree_sitter_COBOL_external_scanner_scan(void *payload, TSLexer *lexer, } if(valid_symbols[LINE_PREFIX_COMMENT] && lexer->get_column(lexer) <= 5) { - while(lexer->get_column(lexer) <= 5) { + // EOF guard: tree-sitter's advance() is a no-op at EOF (the column + // never increments), so without checking lookahead this loop spins + // forever when the input ends at column <= 5. + while(lexer->get_column(lexer) <= 5 && lexer->lookahead != 0) { lexer->advance(lexer, true); } lexer->result_symbol = LINE_PREFIX_COMMENT; @@ -143,7 +153,13 @@ bool tree_sitter_COBOL_external_scanner_scan(void *payload, TSLexer *lexer, } if(valid_symbols[COMMENT_ENTRY]) { - if(!start_with_word(lexer, any_content_keyword, number_of_comment_entry_keywords)) { + bool consumed_any = false; + if(!start_with_word(lexer, any_content_keyword, number_of_comment_entry_keywords, &consumed_any)) { + // Zero-length guard: emitting a token without consuming input + // livelocks the parser. Bail out and let tree-sitter recover. + if(!consumed_any) { + return false; + } lexer->mark_end(lexer); lexer->result_symbol = COMMENT_ENTRY; return true; diff --git a/scripts/vendored-checksums.txt b/scripts/vendored-checksums.txt index 5ad66849d..86a09c06b 100644 --- a/scripts/vendored-checksums.txt +++ b/scripts/vendored-checksums.txt @@ -5,7 +5,7 @@ c5cfb43042b6b72045f4ba997834d0a7786d2793d91680868b5815b39f14fc78 internal/cbm/v b29c1c9fb7cc82f58c84b376df1297d6e2737a1d655fd356db0859e3c29c2fea internal/cbm/vendored/common/tree_sitter/alloc.h 31e60a1bff6f715afacce03b5b70efe42b58371b4f9595dd4af52a577ff9608c internal/cbm/vendored/common/tree_sitter/array.h 180b893c8734778fd32f372dfbc27bd6ad1cd2221f26150b31256ff6716320d2 internal/cbm/vendored/common/tree_sitter/parser.h -48ebc96beae7e671b0b38f9d2e26f2888732843fcbdcf0ac2e73930edf20a7d0 internal/cbm/vendored/grammars/MANIFEST.md +4f8164ae42590e751bdbd96f419ee3d8e8e78b1478190fcbaa1f1d15a9c572b5 internal/cbm/vendored/grammars/MANIFEST.md ad8425038de519f8c4e3e9339feebf99dfad8a6002dcf79227d91402780a32cc internal/cbm/vendored/grammars/ada/LICENSE 02805ec13939b749c891567be36bf024b09034e04c80683a1ae667272458d549 internal/cbm/vendored/grammars/ada/parser.c 115a75d000bef9c70c4de7dfbb7f2a80b90cbb14ba265056dc7abb1ef3b9b6db internal/cbm/vendored/grammars/ada/tree_sitter/alloc.h @@ -134,7 +134,7 @@ daf550976dc40608917b1f16b5c8b21773111bdd2a7fcc23c5e5fec3737055f5 internal/cbm/v c4b9482069f61a2a26a590181baa34f08eb6dcc57a138def578e6f9720c35ea5 internal/cbm/vendored/grammars/cmake/tree_sitter/parser.h 1f58f9911dc5e3bcb96de28bb28e7b6bb7eb323952d29569c5d7214a152146bb internal/cbm/vendored/grammars/cobol/LICENSE d40a31d4854a2d82977c74d33c3ee9c004d9e63743176ebc84159a1734db7b2b internal/cbm/vendored/grammars/cobol/parser.c -0e146beb0331e4f95e2fb815e263c649f2bc404b35dd1b19eb125cbd4ed95df8 internal/cbm/vendored/grammars/cobol/scanner.c +6958b5867131d6ed02964da9a2a7b5e68db461253c6b122d90c5307055ef3d8c internal/cbm/vendored/grammars/cobol/scanner.c 8e819abdeba5866bf1aaf6e7b97522241c903d94a18395c6a75ed02984270ee8 internal/cbm/vendored/grammars/cobol/tree_sitter/parser.h 00bb7f417416e8233ae465209e72eaa6162d53b061add7d9bed0238552a6fa0c internal/cbm/vendored/grammars/commonlisp/LICENSE 27082ecef6f3fe6c09aaf2488b2003f031a165d23b15179ad6c2b52bf47b05ff internal/cbm/vendored/grammars/commonlisp/parser.c diff --git a/tests/repro/repro_extraction.c b/tests/repro/repro_extraction.c index 99db6954d..8afd67ca7 100644 --- a/tests/repro/repro_extraction.c +++ b/tests/repro/repro_extraction.c @@ -8,9 +8,17 @@ * Cluster (TIER A, in-process via cbm_extract_file): * #554 — C++ out-of-line method CALLS source = Module, not enclosing Method * (more added per wave: #495 #521 #382 #408 #523 #56 #333) + * #2998 — COBOL external scanner livelock at EOF (hang, not a wrong edge) */ #include "test_framework.h" #include "cbm.h" +#include +#if !defined(_WIN32) +#include +#include +#include +#include +#endif /* Convenience: extract, return result (caller frees). Mirrors test_extraction.c. */ static CBMFileResult *rx(const char *src, CBMLanguage lang, const char *proj, const char *path) { @@ -87,7 +95,98 @@ TEST(repro_issue554_cpp_out_of_line_method_class_qualified) { PASS(); } +/* ─────────────────────────────────────────────────────────────────── + * #2998 — COBOL external scanner livelocks the parser at EOF. + * + * Root cause: the vendored COBOL scanner's LINE_PREFIX_COMMENT loop advanced + * while get_column() <= 5 without checking lookahead == 0. tree-sitter's + * advance() is a no-op at EOF (the column never increments), so any input that + * reaches EOF at column <= 5 — e.g. a .cob file shorter than 6 bytes, or the + * virtual empty line after a trailing "\n" in the right parser state — spun + * the scanner forever at 100% CPU. A second hazard emitted a zero-length + * COMMENT_ENTRY token when start_with_word() bailed at "\n"/EOF without + * consuming, another livelock class. + * + * Expected: extraction RETURNS (the hang itself is the failure mode; the + * fork + CPU-time-cap harness below turns a regression back into a RED test + * instead of a hung CI job), and the canonical valid-program fixture still + * extracts PROGRAM-ID. Actual (buggy): never returns; last worker-log line + * "pass.start pass=definitions". + * ─────────────────────────────────────────────────────────────────── */ +#if !defined(_WIN32) +/* CPU seconds the child may spend. Parsing these few bytes costs microseconds + * even under sanitizers; the livelock spends CPU without bound. */ +enum { REPRO2998_CPU_SECONDS = 5 }; + +/* Parse src in a forked child whose CPU time is capped (RLIMIT_CPU), and wait + * for it with one blocking waitpid. A livelock burns CPU and is stopped by + * SIGXCPU; a busy host only delays the child without spending its CPU time, so + * the verdict does not depend on wall-clock time and nothing polls or sleeps. + * Returns the child's exit code (0 = extraction returned), 128 + the signal + * that stopped it (SIGXCPU/SIGKILL = the livelock reproduced), -2 on fork + * failure, -3 if the child could not be waited for. */ +static int repro2998_parse_cpu_capped(const char *src, CBMLanguage lang, const char *path) { + fflush(NULL); + pid_t pid = fork(); + if (pid < 0) + return -2; + if (pid == 0) { + struct rlimit no_core = {0, 0}; + struct rlimit cpu = {REPRO2998_CPU_SECONDS, REPRO2998_CPU_SECONDS + 1}; + (void)setrlimit(RLIMIT_CORE, &no_core); + (void)setrlimit(RLIMIT_CPU, &cpu); + CBMFileResult *r = cbm_extract_file(src, (int)strlen(src), lang, "t", path, 0, NULL, NULL); + if (r) + cbm_free_result(r); + _exit(0); /* reaching _exit at all is what the bug took away */ + } + int status = 0; + pid_t got; + do { + got = waitpid(pid, &status, 0); + } while (got < 0 && errno == EINTR); + if (got != pid) + return -3; + return WIFEXITED(status) ? WEXITSTATUS(status) : 128 + WTERMSIG(status); +} + +TEST(repro_issue2998_cobol_scanner_eof_livelock) { + /* EOF lands inside the LINE_PREFIX_COMMENT column scan: shorter than + * column 6, or a trailing newline leaving the lexer at column 0 of the + * virtual empty last line. */ + ASSERT_EQ(repro2998_parse_cpu_capped("X", CBM_LANG_COBOL, "m.cob"), 0); + ASSERT_EQ(repro2998_parse_cpu_capped("*<<", CBM_LANG_COBOL, "m.cob"), 0); + ASSERT_EQ(repro2998_parse_cpu_capped("X\n", CBM_LANG_COBOL, "m.cob"), 0); + ASSERT_EQ(repro2998_parse_cpu_capped("display 1.\n", CBM_LANG_COBOL, "m.cob"), 0); + PASS(); +} + +TEST(repro_issue2998_cobol_valid_program_still_extracts) { + /* The EOF/zero-length guards must not break the happy path: the canonical + * fixed-format fixture (same as the cobol row in grammar_cases.h) still + * yields its PROGRAM-ID definition. */ + CBMFileResult *r = rx(" IDENTIFICATION DIVISION.\n" + " PROGRAM-ID. FOO.\n" + " PROCEDURE DIVISION.\n" + " MAIN-PARA.\n" + " STOP RUN.\n", + CBM_LANG_COBOL, "t", "a.cob"); + ASSERT_NOT_NULL(r); + int saw_foo = 0; + for (int i = 0; r && i < r->defs.count; i++) + if (r->defs.items[i].name && strcmp(r->defs.items[i].name, "FOO") == 0) + saw_foo = 1; + cbm_free_result(r); + ASSERT_TRUE(saw_foo); + PASS(); +} +#endif /* !_WIN32 */ + /* ── Suite ──────────────────────────────────────────────────────── */ SUITE(repro_extraction) { RUN_TEST(repro_issue554_cpp_out_of_line_method_class_qualified); +#if !defined(_WIN32) + RUN_TEST(repro_issue2998_cobol_scanner_eof_livelock); + RUN_TEST(repro_issue2998_cobol_valid_program_still_extracts); +#endif }