Skip to content

Commit ee9d136

Browse files
authored
Merge branch 'main' into fix/elixir-extraction-defects
2 parents 4d21c3b + 66d9c2b commit ee9d136

10 files changed

Lines changed: 434 additions & 80 deletions

File tree

‎internal/cbm/extract_calls.c‎

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ static const char *strip_quotes(CBMArena *a, const char *text) {
7979

8080
// Callee suffixes for IRIS Python interop string-dispatch. Kept at file scope
8181
// (not inside the function) to satisfy cppcheck variableScope.
82-
static const char *s_py_dispatch_suffixes[] = {".classMethodValue", ".classMethodVoid",
83-
".classMethodBoolean", ".classMethodObject", NULL};
82+
static const char *s_py_dispatch_suffixes[] = {".classMethodValue", ".classMethodVoid",
83+
".classMethodBoolean", ".classMethodObject",
84+
".invokeClassMethod", NULL};
8485

8586
// Per-language callee-suffix dispatch table — returns a NULL-terminated list of
8687
// method-name suffixes whose calls should be resolved by extracting class+method
@@ -2115,6 +2116,49 @@ static const char *extract_nth_string_arg(CBMExtractCtx *ctx, TSNode args, uint3
21152116
return NULL;
21162117
}
21172118

2119+
// IRIS embedded Python names the target class in the receiver:
2120+
// iris.cls("Pkg.Class").Method(...) calls ClassMethod Pkg.Class.Method.
2121+
// Returns "Pkg.Class.Method", or NULL unless the receiver is iris.cls() with
2122+
// a string-literal first argument. A non-literal class names nothing, so it
2123+
// gets no class-qualified callee rather than a guessed one.
2124+
static const char *python_iris_cls_callee(CBMExtractCtx *ctx, TSNode call_node) {
2125+
TSNode fn = ts_node_child_by_field_name(call_node, TS_FIELD("function"));
2126+
if (ts_node_is_null(fn) || strcmp(ts_node_type(fn), "attribute") != 0) {
2127+
return NULL;
2128+
}
2129+
TSNode recv = ts_node_child_by_field_name(fn, TS_FIELD("object"));
2130+
TSNode meth = ts_node_child_by_field_name(fn, TS_FIELD("attribute"));
2131+
if (ts_node_is_null(recv) || ts_node_is_null(meth) || strcmp(ts_node_type(recv), "call") != 0) {
2132+
return NULL;
2133+
}
2134+
TSNode recv_fn = ts_node_child_by_field_name(recv, TS_FIELD("function"));
2135+
TSNode recv_args = ts_node_child_by_field_name(recv, TS_FIELD("arguments"));
2136+
if (ts_node_is_null(recv_fn) || ts_node_is_null(recv_args) ||
2137+
ts_node_named_child_count(recv_args) == 0) {
2138+
return NULL;
2139+
}
2140+
const char *recv_text = cbm_node_text(ctx->arena, recv_fn, ctx->source);
2141+
if (!recv_text || strcmp(recv_text, "iris.cls") != 0) {
2142+
return NULL;
2143+
}
2144+
TSNode cls_arg = ts_node_named_child(recv_args, 0);
2145+
if (!is_string_like(ts_node_type(cls_arg))) {
2146+
return NULL;
2147+
}
2148+
// Plain quoted literals only: an f-string or other prefixed string is a
2149+
// "string" node too, but its text is not a class name.
2150+
char *cls_text = cbm_node_text(ctx->arena, cls_arg, ctx->source);
2151+
if (!cls_text || (cls_text[0] != '"' && cls_text[0] != '\'')) {
2152+
return NULL;
2153+
}
2154+
const char *cls = strip_and_validate_string_arg(ctx->arena, cls_text);
2155+
const char *mth = cbm_node_text(ctx->arena, meth, ctx->source);
2156+
if (!cls || !cls[0] || !mth || !mth[0]) {
2157+
return NULL;
2158+
}
2159+
return cbm_arena_sprintf(ctx->arena, "%s.%s", cls, mth);
2160+
}
2161+
21182162
// --- Unified handler: called once per node by the cursor walk ---
21192163

21202164
// Process a keyword argument (keyword_argument or pair node).
@@ -3963,6 +4007,16 @@ CBMInvocationDescriptor handle_calls(CBMExtractCtx *ctx, TSNode node, const CBML
39634007
}
39644008
}
39654009

4010+
// #1260: iris.cls("Pkg.X").M() names its class, so the qualified
4011+
// callee replaces the bare one. Keeping both would let the bare
4012+
// name weak-match an unrelated same-named method.
4013+
if (ctx->language == CBM_LANG_PYTHON) {
4014+
const char *iris_callee = python_iris_cls_callee(ctx, node);
4015+
if (iris_callee) {
4016+
call.callee_name = iris_callee;
4017+
call.is_method = false;
4018+
}
4019+
}
39664020
cbm_calls_push(&ctx->result->calls, ctx->arena, call);
39674021
invocation = describe_emitted_primary_call(node, &callee);
39684022

‎internal/cbm/extract_defs.c‎

Lines changed: 61 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
#include "helpers.h"
44
#include "lang_specs.h"
55
#include "foundation/constants.h"
6-
#include "foundation/platform.h" // safe_realloc (frees old on failure)
7-
#include "foundation/log.h" // cbm_log_warn
6+
#include "foundation/log.h" // cbm_log_error
7+
#include "foundation/mem_core.h" // cbm_realloc/cbm_free -- walk_defs stack
88
#include "extract_node_stack.h"
99
#include "simhash/minhash.h"
1010
#include "semantic/ast_profile.h"
1111
#include "tree_sitter/api.h" // TSNode, ts_node_*
12-
#include <stdint.h> // uint32_t
12+
#include <limits.h> // INT_MAX
13+
#include <stdint.h> // uint32_t, SIZE_MAX
1314
#include <stdio.h> // snprintf (ObjectScript storage/trigger sidecars)
14-
#include <stdlib.h> // getenv, atoi
15+
#include <stdlib.h> // malloc/free (child-collection scratch)
1516
#include <string.h>
1617
#include <ctype.h>
1718

@@ -7344,74 +7345,75 @@ typedef struct {
73447345
* single ~160 KB C-stack frame. That overflowed small thread stacks (the
73457346
* pre-2026-03 Windows 1 MB main thread) on the definitions pass, and its
73467347
* `top < 4096` push guards SILENTLY DROPPED every top-level definition past
7347-
* 4096. Use a growable heap stack instead: a tiny initial footprint that doubles
7348-
* on demand, bounded by a generous, env-configurable ceiling that WARNs (once)
7349-
* rather than dropping — so a file with thousands of top-level defs is fully
7350-
* extracted, and a pathological one degrades to a warned, bounded skip instead
7351-
* of an OOM or a stack overflow. */
7348+
* 4096. It became a growable stack bounded by an 8M-frame ceiling
7349+
* (env CBM_WALK_DEFS_MAX) that still stopped pushing once reached — and since
7350+
* children are pushed last-to-first, a file wider than the ceiling lost its
7351+
* FIRST definitions. No work cap may decide graph content, so there is no
7352+
* ceiling: the stack doubles on demand, and only an allocation failure stops
7353+
* the walk. That failure is loud — logged at error level and reported as a
7354+
* per-file extract error (result->has_error) — never a silent partial file. */
73527355
typedef struct {
73537356
walk_defs_frame_t *data;
73547357
int top;
73557358
int cap;
7356-
const char *path; // for the WARN when the ceiling is hit (may be NULL)
7357-
bool warned;
7359+
const char *path; // for the error log on allocation failure (may be NULL)
7360+
bool failed; // growth failed: pending frames dropped, walk drains
73587361
/* The per-file traversal scratch (ctx->scratch) when there is one: frames
73597362
* then come from memory the thread reuses file after file, where a malloc
73607363
* of 256 frames per file was 28 k allocations and 255 MB never written on
73617364
* the Go corpus (waste sanitizer, 2026-09-17). Growth copies into a
73627365
* doubled buffer and abandons the old one to the arena, like TSNodeStack.
7363-
* NULL: the heap, freed by the walk. */
7366+
* NULL: the memory core (class EXTRACT), released by the walk. */
73647367
CBMArena *arena;
73657368
} wd_stack_t;
73667369

7367-
// Generous safety ceiling (frames), env-overridable via CBM_WALK_DEFS_MAX.
7368-
// Realistic files never approach this; it only bounds a pathological/adversarial
7369-
// file so extraction degrades to a warned skip rather than unbounded memory.
7370-
static int wd_stack_max(void) {
7371-
const char *e = getenv("CBM_WALK_DEFS_MAX");
7372-
if (e) {
7373-
int v = atoi(e);
7374-
if (v > 0) {
7375-
return v;
7376-
}
7377-
}
7378-
return 8 * 1024 * 1024; // 8M frames (~320 MB) default
7379-
}
7380-
7381-
static void wd_push(wd_stack_t *s, TSNode node, const char *enclosing_qn) {
7382-
if (s->top >= s->cap) {
7383-
int ncap = s->cap ? s->cap * 2 : 256;
7384-
if (ncap > wd_stack_max()) {
7385-
if (!s->warned) {
7386-
char lim[24];
7387-
snprintf(lim, sizeof(lim), "%d", wd_stack_max());
7388-
cbm_log_warn("extract.walk_defs_capped", "limit", lim, "path",
7389-
s->path ? s->path : "");
7390-
s->warned = true;
7391-
}
7392-
return; // bounded: stop growing (warned, not silent)
7393-
}
7394-
walk_defs_frame_t *nd = NULL;
7370+
enum { WD_STACK_INITIAL = 256 };
7371+
7372+
/* Double `s` (or give it its first WD_STACK_INITIAL frames). Returns false,
7373+
* after logging, when the doubled size is not representable or cannot be
7374+
* allocated; the old buffer is left intact so the walk can still release it. */
7375+
static bool wd_grow(wd_stack_t *s) {
7376+
walk_defs_frame_t *nd = NULL;
7377+
int ncap = WD_STACK_INITIAL;
7378+
bool fits = true;
7379+
if (s->cap > 0) {
7380+
fits = s->cap <= INT_MAX / 2;
7381+
ncap = fits ? s->cap * 2 : s->cap;
7382+
}
7383+
fits = fits && (size_t)ncap <= SIZE_MAX / sizeof(walk_defs_frame_t);
7384+
if (fits) {
7385+
size_t bytes = (size_t)ncap * sizeof(walk_defs_frame_t);
73957386
if (s->arena) {
7396-
nd = (walk_defs_frame_t *)cbm_arena_alloc(s->arena,
7397-
(size_t)ncap * sizeof(walk_defs_frame_t));
7387+
nd = (walk_defs_frame_t *)cbm_arena_alloc(s->arena, bytes);
73987388
if (nd && s->top > 0) {
73997389
memcpy(nd, s->data, (size_t)s->top * sizeof(walk_defs_frame_t));
74007390
}
74017391
} else {
7402-
nd = safe_realloc(s->data, (size_t)ncap * sizeof(walk_defs_frame_t));
7403-
}
7404-
if (!nd) {
7405-
/* OOM — safe_realloc already freed the old buffer. Bail cleanly: drop
7406-
* pending frames so the walk_defs loop drains and exits without a NULL
7407-
* deref; extraction keeps whatever was already emitted. */
7408-
s->data = NULL;
7409-
s->cap = 0;
7410-
s->top = 0;
7411-
return;
7392+
nd = (walk_defs_frame_t *)cbm_realloc(CBM_MEM_CLASS_EXTRACT, s->data, bytes);
74127393
}
7413-
s->data = nd;
7414-
s->cap = ncap;
7394+
}
7395+
if (!nd) {
7396+
char pending[24];
7397+
snprintf(pending, sizeof(pending), "%d", s->top);
7398+
cbm_log_error("extract.walk_stack_alloc_failed", "walker", "walk_defs", "pending", pending,
7399+
"path", s->path ? s->path : "");
7400+
return false;
7401+
}
7402+
s->data = nd;
7403+
s->cap = ncap;
7404+
return true;
7405+
}
7406+
7407+
static void wd_push(wd_stack_t *s, TSNode node, const char *enclosing_qn) {
7408+
if (s->failed) {
7409+
return;
7410+
}
7411+
if (s->top >= s->cap && !wd_grow(s)) {
7412+
/* Drop every pending frame so the walk_defs loop drains and exits;
7413+
* walk_defs then marks the file's result as an extract error. */
7414+
s->failed = true;
7415+
s->top = 0;
7416+
return;
74157417
}
74167418
s->data[s->top++] = (walk_defs_frame_t){node, enclosing_qn};
74177419
}
@@ -8219,7 +8221,11 @@ static void walk_defs(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec *spec,
82198221
wd_push_children_reverse(&s, node, frame.enclosing_class_qn);
82208222
}
82218223
if (!s.arena) {
8222-
free(s.data);
8224+
cbm_free(CBM_MEM_CLASS_EXTRACT, s.data);
8225+
}
8226+
if (s.failed) {
8227+
ctx->result->has_error = true;
8228+
ctx->result->error_msg = "definitions walk: stack allocation failed";
82238229
}
82248230
}
82258231

‎scripts/memory-core-baseline.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
internal/cbm/ac.c 20
77
internal/cbm/arena.c 3
88
internal/cbm/cbm.c 13
9-
internal/cbm/extract_defs.c 5
9+
internal/cbm/extract_defs.c 4
1010
internal/cbm/helpers.c 3
1111
internal/cbm/lsp/c_lsp.c 4
1212
internal/cbm/lsp/perl_lsp.c 21

‎src/foundation/compat.c‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,10 +285,11 @@ int cbm_mkstemp(char *tmpl) {
285285
return CBM_NOT_FOUND;
286286
}
287287
static const wchar_t hex[] = L"0123456789abcdef";
288+
int saved_errno = EEXIST;
288289
for (int attempt = 0; attempt < 128; attempt++) {
289290
unsigned int random_bits = 0;
290291
if (!cbm_secure_random(&random_bits, sizeof(random_bits))) {
291-
errno = EIO;
292+
saved_errno = EIO;
292293
break;
293294
}
294295
for (int digit = 0; digit < 6; digit++) {
@@ -299,10 +300,11 @@ int cbm_mkstemp(char *tmpl) {
299300
if (!expanded || !wide_open || strlen(expanded) >= sizeof(buf)) {
300301
free(expanded);
301302
free(wide_open);
302-
errno = ENAMETOOLONG;
303+
saved_errno = ENAMETOOLONG;
303304
break;
304305
}
305306
int fd = _wopen(wide_open, _O_CREAT | _O_EXCL | _O_RDWR | _O_BINARY, _S_IREAD | _S_IWRITE);
307+
saved_errno = errno;
306308
free(wide_open);
307309
if (fd >= 0) {
308310
strcpy(tmpl, expanded);
@@ -311,11 +313,12 @@ int cbm_mkstemp(char *tmpl) {
311313
return fd;
312314
}
313315
free(expanded);
314-
if (errno != EEXIST) {
316+
if (saved_errno != EEXIST) {
315317
break;
316318
}
317319
}
318320
free(wide_template);
321+
errno = saved_errno;
319322
return CBM_NOT_FOUND;
320323
}
321324
#endif

‎src/watcher/watcher.c‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,3 +1653,15 @@ int cbm_watcher_run(cbm_watcher_t *w, int base_interval_ms) {
16531653
cbm_log_info("watcher.stop");
16541654
return 0;
16551655
}
1656+
1657+
#if defined(CBM_ENABLE_TEST_SEAMS) && CBM_ENABLE_TEST_SEAMS
1658+
int cbm_watcher_test_pending_free_count(cbm_watcher_t *w) {
1659+
if (!w) {
1660+
return -1;
1661+
}
1662+
cbm_mutex_lock(&w->projects_lock);
1663+
int count = w->pending_free_count;
1664+
cbm_mutex_unlock(&w->projects_lock);
1665+
return count;
1666+
}
1667+
#endif

‎src/watcher/watcher.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ int cbm_watcher_watch_count(cbm_watcher_t *w);
110110
* without first giving the counter atomic accessors. */
111111
int cbm_watcher_index_failure_count(cbm_watcher_t *w, const char *project_name);
112112

113+
#if defined(CBM_ENABLE_TEST_SEAMS) && CBM_ENABLE_TEST_SEAMS
114+
/* Test seam: number of unwatched/replaced project states parked on the
115+
* deferred-free list, read under projects_lock. The drain in poll_once is
116+
* otherwise unobservable — cbm_watcher_free frees the same list, so a poll
117+
* that stopped draining would neither crash nor leak. Test builds only. */
118+
int cbm_watcher_test_pending_free_count(cbm_watcher_t *w);
119+
#endif
120+
113121
/* Return the adaptive poll interval (ms) for a given file count. */
114122
int cbm_watcher_poll_interval_ms(int file_count);
115123

‎tests/test_cli.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,8 +1653,9 @@ static _Noreturn void cli_scope_host_child(const cli_scope_fixture_t *fixture, i
16531653
(void)write(ready_fd, &ready, 1);
16541654
close(ready_fd);
16551655
bool drained = false;
1656-
uint64_t deadline = cbm_now_ms() + 120000U;
1657-
while (ready_ok && cbm_now_ms() < deadline) {
1656+
/* The parent owns the release pipe. If it exits, poll observes POLLHUP;
1657+
* a fixed lifetime can stop a healthy host during a slow binary install. */
1658+
while (ready_ok) {
16581659
if (cbm_daemon_runtime_service_wait_exited(service, 50U)) {
16591660
drained = true;
16601661
break;

0 commit comments

Comments
 (0)