Skip to content

Commit 9cb3cab

Browse files
authored
Merge pull request DeusData#779 from DeusData/feat/lsp-walk-depth-caps
fix(lsp): cap resolve-walker recursion depth in py/go/php/kotlin (Stage 2/B1)
2 parents 6c44fc1 + 4aa53e3 commit 9cb3cab

10 files changed

Lines changed: 210 additions & 8 deletions

File tree

‎internal/cbm/lsp/go_lsp.c‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,22 @@
66
#include <stdlib.h>
77

88
// Forward declarations
9-
static void resolve_calls_in_node(GoLSPContext* ctx, TSNode node);
9+
static void resolve_calls_in_node_inner(GoLSPContext* ctx, TSNode node);
10+
11+
/* Depth-guarded entry for the AST call-resolution walk. The walk recurses once
12+
* per nesting level; a deeply-nested or cyclic file can overflow the native
13+
* stack (SIGSEGV) and take down the whole index. Past the cap the subtree is
14+
* skipped — its calls stay unresolved, which is graceful degradation, not a
15+
* crash. The cap is CBM_LSP_MAX_WALK_DEPTH, env-overridable via the same name.
16+
* The walk_depth-- runs after the inner returns, so early returns in the body
17+
* never leak the counter. */
18+
static void resolve_calls_in_node(GoLSPContext* ctx, TSNode node) {
19+
if (ctx->walk_depth >= cbm_lsp_max_walk_depth())
20+
return;
21+
ctx->walk_depth++;
22+
resolve_calls_in_node_inner(ctx, node);
23+
ctx->walk_depth--;
24+
}
1025
static void emit_resolved_call(GoLSPContext* ctx, const char* callee_qn, const char* strategy, float confidence);
1126
static const CBMType* go_lookup_field(GoLSPContext* ctx, const char* type_qn, const char* field_name, int depth);
1227
static void extract_type_params_from_ast(CBMArena* arena, CBMTypeRegistry* reg,
@@ -1108,7 +1123,7 @@ static void emit_unresolved_call(GoLSPContext* ctx, const char* expr_text, const
11081123

11091124
// --- Walk call expressions and resolve them ---
11101125

1111-
static void resolve_calls_in_node(GoLSPContext* ctx, TSNode node) {
1126+
static void resolve_calls_in_node_inner(GoLSPContext* ctx, TSNode node) {
11121127
if (ts_node_is_null(node)) return;
11131128
const char* kind = ts_node_type(node);
11141129

‎internal/cbm/lsp/go_lsp.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ typedef struct {
2626
// Output: resolved calls accumulate here
2727
CBMResolvedCallArray* resolved_calls;
2828

29+
// AST-walk recursion depth for resolve_calls_in_node (guards stack overflow
30+
// on deeply-nested/cyclic files; see cbm_lsp_max_walk_depth). Zero via memset.
31+
int walk_depth;
32+
2933
// Debug mode (CBM_LSP_DEBUG env)
3034
bool debug;
3135
} GoLSPContext;

‎internal/cbm/lsp/kotlin_lsp.c‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,22 @@
6262

6363
/* ── forward declarations ─────────────────────────────────────────── */
6464

65-
static void kt_resolve_calls_in_node(KotlinLSPContext *ctx, TSNode node);
65+
static void kt_resolve_calls_in_node_inner(KotlinLSPContext *ctx, TSNode node);
66+
67+
/* Depth-guarded entry for the AST call-resolution walk. The walk recurses once
68+
* per nesting level; a deeply-nested or cyclic file can overflow the native
69+
* stack (SIGSEGV) and take down the whole index. Past the cap the subtree is
70+
* skipped — its calls stay unresolved, which is graceful degradation, not a
71+
* crash. The cap is CBM_LSP_MAX_WALK_DEPTH, env-overridable via the same name.
72+
* The walk_depth-- runs after the inner returns, so early returns in the body
73+
* never leak the counter. */
74+
static void kt_resolve_calls_in_node(KotlinLSPContext *ctx, TSNode node) {
75+
if (ctx->walk_depth >= cbm_lsp_max_walk_depth())
76+
return;
77+
ctx->walk_depth++;
78+
kt_resolve_calls_in_node_inner(ctx, node);
79+
ctx->walk_depth--;
80+
}
6681
static void kt_process_class_decl(KotlinLSPContext *ctx, TSNode node);
6782
static void kt_process_object_decl(KotlinLSPContext *ctx, TSNode node, bool is_companion,
6883
const char *outer_class_qn);
@@ -3174,7 +3189,7 @@ static void kt_process_statement(KotlinLSPContext *ctx, TSNode stmt) {
31743189
/* Generic walker that fires call resolution on every call_expression in
31753190
* a subtree, *without* descending into nested function/class bodies (those
31763191
* are processed separately with their own scope). */
3177-
static void kt_resolve_calls_in_node(KotlinLSPContext *ctx, TSNode node) {
3192+
static void kt_resolve_calls_in_node_inner(KotlinLSPContext *ctx, TSNode node) {
31783193
if (ts_node_is_null(node)) {
31793194
return;
31803195
}

‎internal/cbm/lsp/kotlin_lsp.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ typedef struct KotlinLSPContext {
110110
/* Recursion guard for kotlin_eval_expr_type. */
111111
int eval_depth;
112112

113+
/* AST-walk recursion depth for kt_resolve_calls_in_node (guards stack
114+
* overflow on deeply-nested/cyclic files; see cbm_lsp_max_walk_depth).
115+
* Zero via memset. */
116+
int walk_depth;
117+
113118
/* Debug mode (CBM_LSP_DEBUG env). */
114119
bool debug;
115120
} KotlinLSPContext;

‎internal/cbm/lsp/php_lsp.c‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,22 @@
3232
extern const TSLanguage *tree_sitter_php_only(void);
3333

3434
/* Forward decls */
35-
static void php_resolve_calls_in_node(PHPLSPContext *ctx, TSNode node);
35+
static void php_resolve_calls_in_node_inner(PHPLSPContext *ctx, TSNode node);
36+
37+
/* Depth-guarded entry for the AST call-resolution walk. The walk recurses once
38+
* per nesting level; a deeply-nested or cyclic file can overflow the native
39+
* stack (SIGSEGV) and take down the whole index. Past the cap the subtree is
40+
* skipped — its calls stay unresolved, which is graceful degradation, not a
41+
* crash. The cap is CBM_LSP_MAX_WALK_DEPTH, env-overridable via the same name.
42+
* The walk_depth-- runs after the inner returns, so early returns in the body
43+
* never leak the counter. */
44+
static void php_resolve_calls_in_node(PHPLSPContext *ctx, TSNode node) {
45+
if (ctx->walk_depth >= cbm_lsp_max_walk_depth())
46+
return;
47+
ctx->walk_depth++;
48+
php_resolve_calls_in_node_inner(ctx, node);
49+
ctx->walk_depth--;
50+
}
3651
static void process_function_like(PHPLSPContext *ctx, TSNode node);
3752
static void process_class_decl(PHPLSPContext *ctx, TSNode node);
3853
static const CBMType *php_substitute_template(CBMArena *arena, const CBMType *t,
@@ -2110,7 +2125,7 @@ static void process_if_statement(PHPLSPContext *ctx, TSNode node) {
21102125
}
21112126

21122127
/* Walk a subtree, binding scope and resolving calls. */
2113-
static void php_resolve_calls_in_node(PHPLSPContext *ctx, TSNode node) {
2128+
static void php_resolve_calls_in_node_inner(PHPLSPContext *ctx, TSNode node) {
21142129
if (ts_node_is_null(node))
21152130
return;
21162131
const char *kind = ts_node_type(node);

‎internal/cbm/lsp/php_lsp.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ typedef struct {
5050
/* Recursion guard for php_eval_expr_type. */
5151
int eval_depth;
5252

53+
/* AST-walk recursion depth for php_resolve_calls_in_node (guards stack
54+
* overflow on deeply-nested/cyclic files; see cbm_lsp_max_walk_depth).
55+
* Zero via memset. */
56+
int walk_depth;
57+
5358
/* Debug mode (CBM_LSP_DEBUG env). */
5459
bool debug;
5560
} PHPLSPContext;

‎internal/cbm/lsp/py_lsp.c‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,22 @@
2424
#include "py_builtins.c"
2525

2626
// Forward decls
27-
static void py_resolve_calls_in(PyLSPContext *ctx, TSNode node);
27+
static void py_resolve_calls_in_inner(PyLSPContext *ctx, TSNode node);
28+
29+
/* Depth-guarded entry for the AST call-resolution walk. The walk recurses once
30+
* per nesting level; a deeply-nested or cyclic file can overflow the native
31+
* stack (SIGSEGV) and take down the whole index. Past the cap the subtree is
32+
* skipped — its calls stay unresolved, which is graceful degradation, not a
33+
* crash. The cap is CBM_LSP_MAX_WALK_DEPTH, env-overridable via the same name.
34+
* The walk_depth-- runs after the inner returns, so early returns in the body
35+
* never leak the counter. */
36+
static void py_resolve_calls_in(PyLSPContext *ctx, TSNode node) {
37+
if (ctx->walk_depth >= cbm_lsp_max_walk_depth())
38+
return;
39+
ctx->walk_depth++;
40+
py_resolve_calls_in_inner(ctx, node);
41+
ctx->walk_depth--;
42+
}
2843
static const CBMType *py_eval_expr_type(PyLSPContext *ctx, TSNode node);
2944
static void py_process_statement(PyLSPContext *ctx, TSNode node);
3045
static const CBMRegisteredFunc *py_lookup_attribute(PyLSPContext *ctx, const char *type_qn,
@@ -2199,7 +2214,7 @@ static void py_emit_dunder_call(PyLSPContext *ctx, const CBMType *recv, const ch
21992214
}
22002215
}
22012216

2202-
static void py_resolve_calls_in(PyLSPContext *ctx, TSNode node) {
2217+
static void py_resolve_calls_in_inner(PyLSPContext *ctx, TSNode node) {
22032218
if (!ctx || ts_node_is_null(node))
22042219
return;
22052220
const char *k = ts_node_type(node);

‎internal/cbm/lsp/py_lsp.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ typedef struct {
6969
int dict_literal_count;
7070
int dict_literal_cap;
7171

72+
// AST-walk recursion depth for py_resolve_calls_in (guards stack overflow on
73+
// deeply-nested/cyclic files; see cbm_lsp_max_walk_depth). Zero via memset.
74+
int walk_depth;
75+
7276
// Debug mode (CBM_LSP_DEBUG env, shared across all language LSPs).
7377
bool debug;
7478
} PyLSPContext;

‎internal/cbm/lsp/scope.h‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "type_rep.h"
55
#include "../arena.h"
6+
#include <stdlib.h> /* getenv, atoi (cbm_lsp_max_walk_depth) */
67

78
typedef struct {
89
const char* name;
@@ -28,6 +29,30 @@ typedef struct CBMScope {
2829
// rather than recursing — guards against pathological hierarchies.
2930
#define CBM_LSP_MAX_LOOKUP_DEPTH 16
3031

32+
// Recursion cap for the per-language "resolve calls in AST node" walkers. These
33+
// recurse once per AST nesting level; a deeply-nested or cyclic file can drive
34+
// them into a native stack overflow (SIGSEGV) that takes down the whole index.
35+
// Past this cap the wrapper skips the subtree — those calls stay unresolved,
36+
// which is graceful degradation, not a crash. 512 is far deeper than any
37+
// hand-written source nests; override for pathological/generated repos via the
38+
// CBM_LSP_MAX_WALK_DEPTH env var (positive integer).
39+
#define CBM_LSP_MAX_WALK_DEPTH 512
40+
41+
// Resolved walk-depth cap: env override (CBM_LSP_MAX_WALK_DEPTH, if a positive
42+
// integer) else CBM_LSP_MAX_WALK_DEPTH. Read once and cached — the walkers call
43+
// this per node, so it must not hit getenv on the hot path. The cache is a
44+
// benign idempotent race under multi-threaded indexing (every thread computes
45+
// the same value).
46+
static inline int cbm_lsp_max_walk_depth(void) {
47+
static int cached = -1;
48+
if (cached < 0) {
49+
const char* e = getenv("CBM_LSP_MAX_WALK_DEPTH");
50+
int v = (e && *e) ? atoi(e) : 0;
51+
cached = (v > 0) ? v : CBM_LSP_MAX_WALK_DEPTH;
52+
}
53+
return cached;
54+
}
55+
3156
CBMScope* cbm_scope_push(CBMArena* a, CBMScope* current);
3257
CBMScope* cbm_scope_pop(CBMScope* scope);
3358
void cbm_scope_bind(CBMScope* scope, const char* name, const CBMType* type);

‎tests/test_stack_overflow.c‎

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,101 @@ TEST(lsp_ts_cyclic_types_no_crash) {
516516
PASS();
517517
}
518518

519+
/* ─── Deeply-nested calls drive the per-language LSP resolve walkers into
520+
* per-nesting-level native recursion. Unguarded, these SIGSEGV and take down
521+
* the whole index. Each walker now has a walk_depth cap (CBM_LSP_MAX_WALK_DEPTH,
522+
* env-overridable) that skips the too-deep subtree — graceful degradation.
523+
* These mirror lsp_java_deep_nesting_no_crash: same fixture shape, one per
524+
* previously-unguarded walker (py_resolve_calls_in, resolve_calls_in_node[go],
525+
* php_resolve_calls_in_node, kt_resolve_calls_in_node). RED proof: run the
526+
* suite with CBM_LSP_MAX_WALK_DEPTH set huge (disabling only these caps) and
527+
* each of these four SIGSEGVs — proving the guard, not the fixture, is what
528+
* keeps them green. ─── */
529+
530+
TEST(lsp_python_deep_nesting_no_crash) {
531+
/* py_resolve_calls_in recurses per nesting level; see the Java analog. */
532+
const int DEPTH = 30000;
533+
size_t sz = (size_t)DEPTH * 3 + 256;
534+
char *src = malloc(sz);
535+
ASSERT_NOT_NULL(src);
536+
char *p = src;
537+
p += snprintf(p, sz, "def f(a):\n return a\ndef g():\n return ");
538+
for (int i = 0; i < DEPTH; i++) {
539+
*p++ = 'f';
540+
*p++ = '(';
541+
}
542+
*p++ = '1';
543+
memset(p, ')', DEPTH);
544+
p += DEPTH;
545+
snprintf(p, sz - (size_t)(p - src), "\n");
546+
ASSERT_FALSE(so_extract_crashes(src, CBM_LANG_PYTHON, "deep.py"));
547+
free(src);
548+
PASS();
549+
}
550+
551+
TEST(lsp_go_deep_nesting_no_crash) {
552+
/* resolve_calls_in_node recurses per nesting level; see the Java analog. */
553+
const int DEPTH = 30000;
554+
size_t sz = (size_t)DEPTH * 3 + 256;
555+
char *src = malloc(sz);
556+
ASSERT_NOT_NULL(src);
557+
char *p = src;
558+
p += snprintf(p, sz, "package p\nfunc f(a int) int { return a }\nfunc g() int { return ");
559+
for (int i = 0; i < DEPTH; i++) {
560+
*p++ = 'f';
561+
*p++ = '(';
562+
}
563+
*p++ = '1';
564+
memset(p, ')', DEPTH);
565+
p += DEPTH;
566+
snprintf(p, sz - (size_t)(p - src), " }\n");
567+
ASSERT_FALSE(so_extract_crashes(src, CBM_LANG_GO, "deep.go"));
568+
free(src);
569+
PASS();
570+
}
571+
572+
TEST(lsp_php_deep_nesting_no_crash) {
573+
/* php_resolve_calls_in_node recurses per nesting level; Java analog. */
574+
const int DEPTH = 30000;
575+
size_t sz = (size_t)DEPTH * 3 + 256;
576+
char *src = malloc(sz);
577+
ASSERT_NOT_NULL(src);
578+
char *p = src;
579+
p += snprintf(p, sz, "<?php\nfunction f($a) { return $a; }\nfunction g() { return ");
580+
for (int i = 0; i < DEPTH; i++) {
581+
*p++ = 'f';
582+
*p++ = '(';
583+
}
584+
*p++ = '1';
585+
memset(p, ')', DEPTH);
586+
p += DEPTH;
587+
snprintf(p, sz - (size_t)(p - src), "; }\n");
588+
ASSERT_FALSE(so_extract_crashes(src, CBM_LANG_PHP, "deep.php"));
589+
free(src);
590+
PASS();
591+
}
592+
593+
TEST(lsp_kotlin_deep_nesting_no_crash) {
594+
/* kt_resolve_calls_in_node recurses per nesting level; Java analog. */
595+
const int DEPTH = 30000;
596+
size_t sz = (size_t)DEPTH * 3 + 256;
597+
char *src = malloc(sz);
598+
ASSERT_NOT_NULL(src);
599+
char *p = src;
600+
p += snprintf(p, sz, "fun f(a: Int): Int { return a }\nfun g(): Int { return ");
601+
for (int i = 0; i < DEPTH; i++) {
602+
*p++ = 'f';
603+
*p++ = '(';
604+
}
605+
*p++ = '1';
606+
memset(p, ')', DEPTH);
607+
p += DEPTH;
608+
snprintf(p, sz - (size_t)(p - src), " }\n");
609+
ASSERT_FALSE(so_extract_crashes(src, CBM_LANG_KOTLIN, "deep.kt"));
610+
free(src);
611+
PASS();
612+
}
613+
519614
/* ═══════════════════════════════════════════════════════════════════
520615
* Suite registration
521616
* ═══════════════════════════════════════════════════════════════════ */
@@ -529,6 +624,10 @@ SUITE(stack_overflow) {
529624
RUN_TEST(lsp_java_lambda_args_exceed_params_no_crash);
530625
RUN_TEST(lsp_cpp_deep_expression_no_crash);
531626
RUN_TEST(lsp_ts_cyclic_types_no_crash);
627+
RUN_TEST(lsp_python_deep_nesting_no_crash);
628+
RUN_TEST(lsp_go_deep_nesting_no_crash);
629+
RUN_TEST(lsp_php_deep_nesting_no_crash);
630+
RUN_TEST(lsp_kotlin_deep_nesting_no_crash);
532631

533632
RUN_TEST(js_calls_exceed_512);
534633
RUN_TEST(python_calls_exceed_512);

0 commit comments

Comments
 (0)