Skip to content

Commit f95fe55

Browse files
committed
perf(ts): charge the type budget on expression-eval entries
ts_eval_expr_type was depth-capped but work-unbounded: crafted expressions (the TS test suite's repeated object spreads) stay under the depth cap while fanning out. Charge the same per-file budget the type-text parser uses, at 16 units per entry (an eval entry does ~two orders of magnitude more work than a text-parse unit), degrading to UNKNOWN on exhaustion. Honest status: this hardens the documented budget design, but the known 3.6 KB spread-bomb baseline file still measures ~11 s in-corpus — its entry path into the evaluator apparently runs unarmed and is recorded as an open lead (zero budget warnings observed). Suites green (ts_lsp, extraction, complexity). Refs DeusData#1669. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent a412642 commit f95fe55

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

‎internal/cbm/lsp/ts_lsp.c‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1980,6 +1980,27 @@ const CBMType *ts_eval_expr_type(TSLSPContext *ctx, TSNode node) {
19801980
return cbm_type_unknown();
19811981
if (ctx->eval_depth > TS_LSP_MAX_EVAL_DEPTH)
19821982
return cbm_type_unknown();
1983+
/* Depth alone does not bound WORK: crafted expressions (e.g. the TS test
1984+
* suite's repeated object spreads) stay under the depth cap while fanning
1985+
* out exponentially — one 3.6 KB baseline file measured 11 s here. Charge
1986+
* the same per-file budget the type-text parser uses and degrade to
1987+
* UNKNOWN on exhaustion, exactly like that path. */
1988+
if (g_ts_type_budget >= 0) {
1989+
/* An eval entry does ~two orders of magnitude more work than one
1990+
* text-parse unit (allocations, lookups, recursion bookkeeping), so it
1991+
* charges accordingly — otherwise the default per-file budget still
1992+
* permits ~12 s of evaluation on a crafted file. Normal files run
1993+
* hundreds-to-thousands of entries, far under any budget. */
1994+
g_ts_type_budget -= 16;
1995+
if (g_ts_type_budget < 0) {
1996+
if (!g_ts_type_budget_warned) {
1997+
g_ts_type_budget_warned = true;
1998+
fprintf(stderr, " [tslsp] expression-eval budget exhausted; degrading to "
1999+
"unknown\n");
2000+
}
2001+
return cbm_type_unknown();
2002+
}
2003+
}
19832004
ctx->eval_depth++;
19842005

19852006
const CBMType *result = cbm_type_unknown();

0 commit comments

Comments
 (0)