Skip to content

Commit adff62e

Browse files
fix(extract): give each Go init() its own QN
Go allows any number of init() functions per package — even several in one file — and all of them run at start-up. On the flat QN (proj.pkg.init) they all collided and the graph upsert kept ONE node per package, silently dropping the rest: measured on a real Go repo, an event-source package registering each decoder in its own file's init() collapsed 21 init functions into one node (29 in source, 7 in the graph), erasing the whole registration pattern. Disambiguate with the DeusData#495 cfg-twin pattern: fold the file basename and line into the QN (proj.pkg.init#a.go:L5). Calling init explicitly is illegal in Go, so nothing ever joins on the plain QN; the call-scope side (compute_func_qn) mirrors the exact formula so init-body calls keep their source attribution instead of degrading to the calls_find_source File fallback. Reproduce-first: pipeline_go_multi_init_nodes_survive is RED without the def-side change (node count 1, expected 2) and GREEN with it; extract_go_multiple_init_disambiguated pins two same-file inits to distinct suffixed QNs. Fixes DeusData#1910 Signed-off-by: Ilya Brykau <ilya.brykau@orca.security>
1 parent dbba757 commit adff62e

6 files changed

Lines changed: 188 additions & 0 deletions

File tree

‎internal/cbm/extract_defs.c‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3739,6 +3739,25 @@ static void extract_func_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec
37393739
}
37403740
}
37413741

3742+
/* Go allows any number of init() functions per package — even several in
3743+
* one file — and they all run at start-up. On the flat QN they all
3744+
* collided and the graph upsert kept ONE node per package, silently
3745+
* dropping the rest (#1910). Disambiguate with the #495 cfg-twin pattern:
3746+
* fold the file basename and the init's per-file ordinal into the QN.
3747+
* The ordinal (not the line) keeps the QN stable when code is edited
3748+
* above the function — the QN is node identity, and a line suffix would
3749+
* churn nodes on every unrelated insertion. Nothing ever joins on the
3750+
* plain QN — calling init explicitly is illegal in Go — and the
3751+
* call-scope side (compute_func_qn in extract_unified.c) computes the
3752+
* same cbm_go_init_ordinal so init-body calls keep their attribution. */
3753+
if (ctx->language == CBM_LANG_GO && strcmp(def.label, "Function") == 0 &&
3754+
strcmp(name, "init") == 0) {
3755+
const char *base = strrchr(ctx->rel_path, '/');
3756+
base = base ? base + 1 : ctx->rel_path;
3757+
def.qualified_name = cbm_arena_sprintf(a, "%s#%s:%d", def.qualified_name, base,
3758+
cbm_go_init_ordinal(node, ctx->source));
3759+
}
3760+
37423761
// C++/CUDA: out-of-line method definition (`Foo::bar` in a .cc/.cpp). The
37433762
// class body in the header is declaration-only, so without this the
37443763
// definition is recorded as a free Function. Promote it to a Method whose QN

‎internal/cbm/extract_unified.c‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,22 @@ static const char *compute_func_qn(CBMExtractCtx *ctx, TSNode node, const CBMLan
918918
}
919919
}
920920

921+
/* Go init(): the def extractor folds the file basename and the init's
922+
* per-file ordinal into the QN so every init in a package survives the
923+
* upsert (#1910, the #495 cfg-twin pattern; ordinal, not line, so the QN
924+
* is stable under edits above the function). Mirror the exact formula
925+
* here, or init-body calls fall back to File-node attribution
926+
* (calls_find_source). */
927+
if (ctx->language == CBM_LANG_GO && strcmp(name, "init") == 0 &&
928+
strcmp(ts_node_type(node), "function_declaration") == 0) {
929+
const char *base_qn = cbm_fqn_compute_source_lang(ctx->arena, ctx->project, ctx->rel_path,
930+
name, ctx->language);
931+
const char *base = strrchr(ctx->rel_path, '/');
932+
base = base ? base + 1 : ctx->rel_path;
933+
return cbm_arena_sprintf(ctx->arena, "%s#%s:%d", base_qn, base,
934+
cbm_go_init_ordinal(node, ctx->source));
935+
}
936+
921937
/* Nix: a binding's own attrpath contributes scope (`a.b.fn = …`), and the def
922938
* extractor bakes it into the def QN. Compose it identically here — otherwise
923939
* an in-body call sources to a QN one or more segments short of the def, and

‎internal/cbm/helpers.c‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1734,6 +1734,42 @@ int cbm_classify_string(const char *str, int len) {
17341734
* canonical parameter shape of server-side route paths
17351735
* (`/things/${id}/x` -> "/things/{}/x"). Returns NULL when the node yields
17361736
* no text or exceeds the route-sized buffer. */
1737+
/* 1-based source-order ordinal of a top-level Go `init` function_declaration
1738+
* among the file's `init`s. Both the definition extractor and compute_func_qn
1739+
* fold it into the QN (`pkg.init#file.go:N`, #1910), so it must be computed
1740+
* from the tree on both sides with this one formula. Unlike a line number,
1741+
* the ordinal is stable under edits above the function — QNs are node
1742+
* identity, and a line-based suffix would churn nodes on every unrelated
1743+
* insertion (the #495 cfg suffix and the GoogleTest derivation are both
1744+
* edit-stable for the same reason). */
1745+
int cbm_go_init_ordinal(TSNode fn_node, const char *source) {
1746+
TSNode root = fn_node;
1747+
for (TSNode p = ts_node_parent(root); !ts_node_is_null(p); p = ts_node_parent(p)) {
1748+
root = p;
1749+
}
1750+
int ordinal = 0;
1751+
uint32_t n = ts_node_named_child_count(root);
1752+
for (uint32_t i = 0; i < n; i++) {
1753+
TSNode child = ts_node_named_child(root, i);
1754+
if (strcmp(ts_node_type(child), "function_declaration") != 0) {
1755+
continue;
1756+
}
1757+
TSNode name = ts_node_child_by_field_name(child, TS_FIELD("name"));
1758+
if (ts_node_is_null(name)) {
1759+
continue;
1760+
}
1761+
uint32_t start = ts_node_start_byte(name);
1762+
if (ts_node_end_byte(name) - start != 4 || memcmp(source + start, "init", 4) != 0) {
1763+
continue;
1764+
}
1765+
ordinal++;
1766+
if (ts_node_eq(child, fn_node)) {
1767+
return ordinal;
1768+
}
1769+
}
1770+
return ordinal;
1771+
}
1772+
17371773
const char *cbm_template_string_text(CBMArena *a, TSNode node, const char *source) {
17381774
enum { TPL_BUF = 512 };
17391775
char buf[TPL_BUF];

‎internal/cbm/helpers.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ bool cbm_is_loop_node_type(const char *kind);
197197
// Is this a module-level node? (not nested inside function/class body)
198198
bool cbm_is_module_level(TSNode node, CBMLanguage lang);
199199

200+
/* 1-based source-order ordinal of a Go `init` among its file's top-level
201+
* `init` function_declarations (QN disambiguation, #1910). */
202+
int cbm_go_init_ordinal(TSNode fn_node, const char *source);
203+
200204
// Same check, but the node's PARENT is supplied directly — avoids the
201205
// O(n) ts_node_parent rescan. Use at call sites iterating a known
202206
// parent's children (the common case). `parent` is the parent of the

‎tests/test_extraction.c‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4899,6 +4899,68 @@ TEST(extract_go_method_receiver_qualified_qn) {
48994899
PASS();
49004900
}
49014901

4902+
TEST(extract_go_multiple_init_disambiguated) {
4903+
/* Go allows several init() per package (even per file); each gets a
4904+
* file+ordinal-suffixed QN (#495 cfg-twin pattern) so none is lost to the
4905+
* same-QN upsert. The ordinal — unlike a line number — must not move when
4906+
* code is inserted above the function: the QN is node identity, and a
4907+
* line-based suffix would churn nodes on every unrelated edit. */
4908+
CBMFileResult *r = extract("package m\n"
4909+
"func init() { a() }\n"
4910+
"func init() { b() }\n"
4911+
"func a() {}\n"
4912+
"func b() {}\n",
4913+
CBM_LANG_GO, "t", "x.go");
4914+
ASSERT_NOT_NULL(r);
4915+
ASSERT_FALSE(r->has_error);
4916+
const char *first = NULL;
4917+
const char *second = NULL;
4918+
for (int i = 0; i < r->defs.count; i++) {
4919+
const CBMDefinition *d = &r->defs.items[i];
4920+
if (!d->name || strcmp(d->name, "init") != 0) {
4921+
continue;
4922+
}
4923+
if (!first) {
4924+
first = d->qualified_name;
4925+
} else {
4926+
second = d->qualified_name;
4927+
}
4928+
}
4929+
ASSERT_NOT_NULL(first);
4930+
ASSERT_NOT_NULL(second);
4931+
ASSERT_STR_EQ(first, "t.init#x.go:1");
4932+
ASSERT_STR_EQ(second, "t.init#x.go:2");
4933+
4934+
/* Stability: an insertion ABOVE both inits must not change either QN. */
4935+
CBMFileResult *r2 = extract("package m\n"
4936+
"import \"fmt\"\n"
4937+
"var shifted = fmt.Sprint(\"pad\")\n"
4938+
"func init() { a() }\n"
4939+
"func init() { b() }\n"
4940+
"func a() {}\n"
4941+
"func b() {}\n",
4942+
CBM_LANG_GO, "t", "x.go");
4943+
ASSERT_NOT_NULL(r2);
4944+
ASSERT_FALSE(r2->has_error);
4945+
int seen = 0;
4946+
for (int i = 0; i < r2->defs.count; i++) {
4947+
const CBMDefinition *d = &r2->defs.items[i];
4948+
if (!d->name || strcmp(d->name, "init") != 0) {
4949+
continue;
4950+
}
4951+
seen++;
4952+
if (seen == 1) {
4953+
ASSERT_STR_EQ(d->qualified_name, "t.init#x.go:1");
4954+
} else {
4955+
ASSERT_STR_EQ(d->qualified_name, "t.init#x.go:2");
4956+
}
4957+
}
4958+
ASSERT_EQ(seen, 2);
4959+
cbm_free_result(r2);
4960+
cbm_free_result(r);
4961+
PASS();
4962+
}
4963+
49024964
/* TS/JS/TSX receiver-aware flag (#592/#606; same intent as the Perl flag above).
49034965
* A member call x.foo() with a non-this/super receiver is flagged is_method so
49044966
* the resolver can suppress a weak short-name match (`re.test()` must not bind a
@@ -6580,6 +6642,7 @@ SUITE(extraction) {
65806642
RUN_TEST(extract_python_member_call_flags_is_method);
65816643
RUN_TEST(extract_go_selector_call_flags_is_method);
65826644
RUN_TEST(extract_go_method_receiver_qualified_qn);
6645+
RUN_TEST(extract_go_multiple_init_disambiguated);
65836646
RUN_TEST(extract_ts_member_call_flags_is_method);
65846647
RUN_TEST(extract_ts_this_super_receiver_not_flagged);
65856648
RUN_TEST(extract_js_member_call_flags_is_method);

‎tests/test_pipeline.c‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4837,6 +4837,55 @@ TEST(pipeline_go_receiver_suppresses_weak_method_edge) {
48374837
PASS();
48384838
}
48394839

4840+
TEST(pipeline_go_multi_init_nodes_survive) {
4841+
char tmp[256];
4842+
snprintf(tmp, sizeof(tmp), "/tmp/cbm_go_init_XXXXXX");
4843+
if (!cbm_mkdtemp(tmp)) {
4844+
FAIL("tmpdir");
4845+
}
4846+
4847+
/* Two files, one package, one init() each — Go runs both at start-up.
4848+
* Pre-fix both collapsed onto one QN and the upsert kept one node
4849+
* (reproduce-first: RED asserts node count == 2). */
4850+
write_temp_file(tmp, "go.mod", "module example.com/reg\n\ngo 1.22\n");
4851+
write_temp_file(tmp, "reg/a.go",
4852+
"package reg\n"
4853+
"\n"
4854+
"var handlers = map[string]func(){}\n"
4855+
"\n"
4856+
"func init() { handlers[\"a\"] = handleA }\n"
4857+
"\n"
4858+
"func handleA() {}\n");
4859+
write_temp_file(tmp, "reg/b.go",
4860+
"package reg\n"
4861+
"\n"
4862+
"func init() { handlers[\"b\"] = handleB }\n"
4863+
"\n"
4864+
"func handleB() {}\n");
4865+
4866+
char db_path[512];
4867+
snprintf(db_path, sizeof(db_path), "%s/go_init.db", tmp);
4868+
cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
4869+
ASSERT_NOT_NULL(p);
4870+
ASSERT_EQ(cbm_pipeline_run(p), 0);
4871+
const char *project = cbm_pipeline_project_name(p);
4872+
4873+
cbm_store_t *s = cbm_store_open_path(db_path);
4874+
ASSERT_NOT_NULL(s);
4875+
4876+
/* Both init functions survive as distinct nodes. */
4877+
cbm_node_t *inits = NULL;
4878+
int ic = 0;
4879+
cbm_store_find_nodes_by_name(s, project, "init", &inits, &ic);
4880+
ASSERT_EQ(ic, 2);
4881+
cbm_store_free_nodes(inits, ic);
4882+
4883+
cbm_store_close(s);
4884+
cbm_pipeline_free(p);
4885+
th_rmtree(tmp);
4886+
PASS();
4887+
}
4888+
48404889
/* Fixture for the #1928 cross-language reference-guard probes (sequential and
48414890
* parallel twins). pad_files > 0 adds filler files to push the index over the
48424891
* parallel-pipeline threshold, since USAGE/WRITES/READS have one resolver per
@@ -13261,6 +13310,7 @@ SUITE(pipeline) {
1326113310
RUN_TEST(pipeline_python_receiver_suppresses_weak_method_edge);
1326213311
RUN_TEST(pipeline_go_method_caller_keeps_lsp_join);
1326313312
RUN_TEST(pipeline_go_receiver_suppresses_weak_method_edge);
13313+
RUN_TEST(pipeline_go_multi_init_nodes_survive);
1326413314
RUN_TEST(pipeline_go_rw_usage_never_cross_into_c);
1326513315
RUN_TEST(pipeline_go_rw_usage_never_cross_into_c_parallel);
1326613316
RUN_TEST(pipeline_go_bare_ref_never_binds_field);

0 commit comments

Comments
 (0)