Skip to content

Commit da76194

Browse files
fix(extract): receiver-qualify Go method QNs
A Go method's QN was the flat package form (proj.pkg.method) — the receiver was ignored, so every same-name method in a package collided on one QN and the graph upsert kept exactly one node. Measured on a real Go repo: 20 Process/Name methods across 15 files kept 2 nodes; 9 different Task() methods fused into one chimera node carrying all nine bodies' call edges; 19 structs pointed DEFINES_METHOD at a single shared method node; a _test.go mock Close outranked the production Close in the dedupe tie-break. The upsert's own comment calls kind-disambiguated QNs 'the real cure'. Qualify the QN with the receiver type (proj.pkg.Recv.method), the same shape as Go interface members and the C++ out-of-line method path right below it in extract_func_def: - extract_defs.c: def.qualified_name = parent_class + name whenever the receiver type resolves; go_receiver_type_name becomes the shared cbm_go_receiver_type_name (exported via helpers.h) so both sides of the contract use one formula. - extract_unified.c (compute_func_qn): mirror branch for method_declaration, so method-body calls keep exact source attribution instead of degrading to File-node fallback (calls_find_source). - Consumers already agree: pxc_build_lsp_def passes the def QN and parent_class (receiver_type) verbatim into the Go LSP registries, and check_go_class_implements explicitly supports class-qualified method QNs (its path (b)). Side effect: resolve_same_module's exact module.name hash no longer matches concrete methods, which kills the conf-0.9 false edges where an interface-typed call bound to an unrelated same-package method. Fixes DeusData#1909 Signed-off-by: Ilya Brykau <ilya.brykau@orca.security>
1 parent 50ee111 commit da76194

7 files changed

Lines changed: 156 additions & 15 deletions

File tree

‎internal/cbm/extract_defs.c‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3521,7 +3521,7 @@ static void set_def_complexity(CBMDefinition *def, TSNode body, const CBMLangSpe
35213521
* Walks to the parameter_declaration's `type` field, unwrapping pointer_type
35223522
* and generic_type, and returns the type_identifier text (e.g. "OrderService").
35233523
* Returns NULL if no type_identifier is found. */
3524-
static char *go_receiver_type_name(CBMArena *a, TSNode recv, const char *source) {
3524+
char *cbm_go_receiver_type_name(CBMArena *a, TSNode recv, const char *source) {
35253525
uint32_t nc = ts_node_child_count(recv);
35263526
for (uint32_t i = 0; i < nc; i++) {
35273527
TSNode child = ts_node_child(recv, i);
@@ -3733,12 +3733,22 @@ static void extract_func_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec
37333733
* (and downstream Go IMPLEMENTS/OVERRIDE) link the method to its owning
37343734
* struct/type node. The parent QN must match the type's node QN, which
37353735
* is computed the same way (cbm_fqn_compute on the type name). */
3736-
char *recv_type = go_receiver_type_name(a, recv, ctx->source);
3736+
char *recv_type = cbm_go_receiver_type_name(a, recv, ctx->source);
37373737
if (recv_type && recv_type[0]) {
37383738
/* Must match the Go type node QN (directory-based module) so the
37393739
* DEFINES_METHOD edge links the method to its owning type. */
37403740
def.parent_class = cbm_fqn_compute_source_lang(a, ctx->project, ctx->rel_path,
37413741
recv_type, ctx->language);
3742+
/* Receiver-qualify the method QN (proj.pkg.Recv.method) — same
3743+
* shape as the C++ out-of-line path below and Go interface
3744+
* members. With the flat proj.pkg.method QN every same-name
3745+
* method in a package collided in the graph upsert: one body
3746+
* survived and the twins' call edges accreted onto it. The
3747+
* call-scope side (compute_func_qn in extract_unified.c) mirrors
3748+
* this formula, and go_lsp consumers read the def QN and
3749+
* parent_class (receiver_type) verbatim, so resolution joins
3750+
* stay exact. */
3751+
def.qualified_name = cbm_arena_sprintf(a, "%s.%s", def.parent_class, name);
37423752
}
37433753
}
37443754

‎internal/cbm/extract_unified.c‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,25 @@ static const char *compute_func_qn(CBMExtractCtx *ctx, TSNode node, const CBMLan
10871087
}
10881088
}
10891089

1090+
/* Go method `func (s *Storage) Close() {...}`: the def extractor records
1091+
* this as Method "proj.pkg.Storage.Close" (receiver-qualified, mirroring
1092+
* the C++ out-of-line rule above). The call-scope QN must match — a bare
1093+
* "proj.pkg.Close" names a node that no longer exists, so every call
1094+
* inside the method body would fall back to File-node attribution
1095+
* (calls_find_source). Same ONE-formula contract as the def side:
1096+
* cbm_go_receiver_type_name + cbm_fqn_compute_source_lang. */
1097+
if (ctx->language == CBM_LANG_GO && strcmp(ts_node_type(node), "method_declaration") == 0) {
1098+
TSNode recv = ts_node_child_by_field_name(node, TS_FIELD("receiver"));
1099+
if (!ts_node_is_null(recv)) {
1100+
char *recv_type = cbm_go_receiver_type_name(ctx->arena, recv, ctx->source);
1101+
if (recv_type && recv_type[0]) {
1102+
const char *type_qn = cbm_fqn_compute_source_lang(
1103+
ctx->arena, ctx->project, ctx->rel_path, recv_type, ctx->language);
1104+
return cbm_arena_sprintf(ctx->arena, "%s.%s", type_qn, name);
1105+
}
1106+
}
1107+
}
1108+
10901109
/* Nix: a binding's own attrpath contributes scope (`a.b.fn = …`), and the def
10911110
* extractor bakes it into the def QN. Compose it identically here — otherwise
10921111
* an in-body call sources to a QN one or more segments short of the def, and

‎internal/cbm/helpers.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ TSNode cbm_resolve_func_name(TSNode node, CBMLanguage lang);
119119
// def extractor — drift dropped the class qualifier from in-body calls (#554/#621).
120120
char *cbm_cpp_out_of_line_parent_class(CBMArena *a, TSNode node, const char *source);
121121

122+
/* Go: resolve a method_declaration's receiver parameter_list down to the bare
123+
* receiver type_identifier (unwrapping pointer_type / generic_type). Shared by
124+
* def extraction (extract_defs.c) and call-scope attribution
125+
* (extract_unified.c) so the receiver-qualified method QN has ONE formula. */
126+
char *cbm_go_receiver_type_name(CBMArena *a, TSNode recv, const char *source);
127+
122128
// Find a child node by kind string.
123129
TSNode cbm_find_child_by_kind(TSNode parent, const char *kind);
124130

‎internal/cbm/lsp/go_lsp.c‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1861,15 +1861,24 @@ static void process_function(GoLSPContext* ctx, TSNode func_node) {
18611861
char* func_name = lsp_node_text(ctx, name_node);
18621862
if (!func_name || !func_name[0]) return;
18631863

1864-
// Enclosing-function QN must be the BARE package.Func form (no receiver
1865-
// type segment). The textual call events (extract_unified.c) source calls
1866-
// as package_qn.func_name — methods included — and the defs pass creates
1867-
// the graph Method node under the same QN, so any other form breaks the
1868-
// caller-QN join in cbm_pipeline_find_lsp_resolution and the LSP-resolved
1869-
// call silently falls back to the registry short-name resolver. The
1870-
// receiver type still reaches the registry via the def's parent_class /
1871-
// method->receiver_type; it just does not appear in the caller QN.
1864+
// Enclosing-function QN must be EXACTLY the QN the textual call events
1865+
// (extract_unified.c compute_func_qn) and the defs pass produce, or the
1866+
// caller-QN join in cbm_pipeline_find_lsp_resolution breaks and the
1867+
// LSP-resolved call silently falls back to the registry short-name
1868+
// resolver. Since #1909 receiver-qualified Go method QNs, that shared
1869+
// form is package_qn.ReceiverType.method for methods (the ONE-formula
1870+
// contract: cbm_go_receiver_type_name) and package_qn.func for functions.
18721871
ctx->enclosing_func_qn = cbm_arena_sprintf(ctx->arena, "%s.%s", ctx->package_qn, func_name);
1872+
if (strcmp(ts_node_type(func_node), "method_declaration") == 0) {
1873+
TSNode recv = ts_node_child_by_field_name(func_node, "receiver", 8);
1874+
if (!ts_node_is_null(recv)) {
1875+
char *recv_type = cbm_go_receiver_type_name(ctx->arena, recv, ctx->source);
1876+
if (recv_type && recv_type[0]) {
1877+
ctx->enclosing_func_qn = cbm_arena_sprintf(ctx->arena, "%s.%s.%s", ctx->package_qn,
1878+
recv_type, func_name);
1879+
}
1880+
}
1881+
}
18731882

18741883
// Push function scope
18751884
CBMScope* saved_scope = ctx->current_scope;

‎tests/test_extraction.c‎

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4715,12 +4715,12 @@ TEST(extract_go_no_filename_in_module_qn) {
47154715
ASSERT_NOT_NULL(conn);
47164716
ASSERT_STR_EQ(conn->qualified_name, "proj.myapp.db.Conn");
47174717

4718-
/* Go method nodes keep a FLAT QN (module + name) with a separate
4719-
* parent_class link to the receiver type — the QN must carry the
4718+
/* Go method nodes carry a receiver-qualified QN (module + receiver type +
4719+
* name) plus the parent_class link — the QN must carry the
47204720
* directory-based module and NOT the `.conn.` filename segment. */
47214721
const CBMDefinition *query = find_def_by_name(r, "Query");
47224722
ASSERT_NOT_NULL(query);
4723-
ASSERT_STR_EQ(query->qualified_name, "proj.myapp.db.Query");
4723+
ASSERT_STR_EQ(query->qualified_name, "proj.myapp.db.Conn.Query");
47244724
ASSERT_EQ(strstr(query->qualified_name, ".conn."), NULL);
47254725
/* The method's parent_class must match the type node QN (for DEFINES_METHOD). */
47264726
ASSERT_NOT_NULL(query->parent_class);
@@ -5501,6 +5501,49 @@ TEST(extract_python_bare_call_flag_is_depth_independent) {
55015501
PASS();
55025502
}
55035503

5504+
TEST(extract_go_method_receiver_qualified_qn) {
5505+
/* A Go method QN carries the receiver type (proj.pkg.Recv.method), same
5506+
* shape as C++ out-of-line methods and Go interface members — so two
5507+
* same-name methods on different receivers no longer collide in the
5508+
* graph upsert. Free functions keep the flat package QN. */
5509+
CBMFileResult *r = extract("package m\n"
5510+
"type Storage struct{}\n"
5511+
"type Cache struct{}\n"
5512+
"func (s *Storage) Close() {}\n"
5513+
"func (c Cache) Close() {}\n"
5514+
"func Shutdown() {}\n",
5515+
CBM_LANG_GO, "t", "x.go");
5516+
ASSERT_NOT_NULL(r);
5517+
ASSERT_FALSE(r->has_error);
5518+
bool saw_storage = false;
5519+
bool saw_cache = false;
5520+
bool saw_free = false;
5521+
for (int i = 0; i < r->defs.count; i++) {
5522+
const CBMDefinition *d = &r->defs.items[i];
5523+
if (!d->name || !d->qualified_name) {
5524+
continue;
5525+
}
5526+
if (strcmp(d->name, "Close") == 0 && strcmp(d->qualified_name, "t.Storage.Close") == 0) {
5527+
ASSERT_STR_EQ(d->label, "Method");
5528+
ASSERT_STR_EQ(d->parent_class, "t.Storage");
5529+
saw_storage = true;
5530+
}
5531+
if (strcmp(d->name, "Close") == 0 && strcmp(d->qualified_name, "t.Cache.Close") == 0) {
5532+
ASSERT_STR_EQ(d->parent_class, "t.Cache");
5533+
saw_cache = true;
5534+
}
5535+
if (strcmp(d->name, "Shutdown") == 0) {
5536+
ASSERT_STR_EQ(d->qualified_name, "t.Shutdown");
5537+
saw_free = true;
5538+
}
5539+
}
5540+
ASSERT_TRUE(saw_storage);
5541+
ASSERT_TRUE(saw_cache);
5542+
ASSERT_TRUE(saw_free);
5543+
cbm_free_result(r);
5544+
PASS();
5545+
}
5546+
55045547
/* TS/JS/TSX receiver-aware flag (#592/#606; same intent as the Perl flag above).
55055548
* A member call x.foo() with a non-this/super receiver is flagged is_method so
55065549
* the resolver can suppress a weak short-name match (`re.test()` must not bind a
@@ -7541,6 +7584,7 @@ SUITE(extraction) {
75417584
RUN_TEST(extract_python_bare_call_flags_locally_bound_callee);
75427585
RUN_TEST(extract_python_bare_call_flag_is_depth_independent);
75437586
RUN_TEST(extract_go_selector_call_flags_is_method);
7587+
RUN_TEST(extract_go_method_receiver_qualified_qn);
75447588
RUN_TEST(extract_ts_member_call_flags_is_method);
75457589
RUN_TEST(extract_ts_this_super_receiver_not_flagged);
75467590
RUN_TEST(extract_js_member_call_flags_is_method);

‎tests/test_parallel.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3444,8 +3444,9 @@ TEST(parallel_go_cross_package_field_chain_resolves) {
34443444
cbm_gbuf_t *gbuf = run_go_field_chain_sequential("go_field_fold", tmpdir, files, 2);
34453445
ASSERT_NOT_NULL(gbuf);
34463446

3447-
const cbm_gbuf_edge_t *edge =
3448-
find_call_edge_to_target_fragment(gbuf, "handler.PlaceOrder", ".service.PlaceOrder");
3447+
/* Since #1909, Go method QNs are receiver-qualified on both endpoints. */
3448+
const cbm_gbuf_edge_t *edge = find_call_edge_to_target_fragment(
3449+
gbuf, "OrderHandler.PlaceOrder", ".service.OrderService.PlaceOrder");
34493450
const bool found = edge != NULL;
34503451
const bool dispatch = edge && edge->properties_json &&
34513452
strstr(edge->properties_json, "\"strategy\":\"lsp_type_dispatch\"");

‎tests/test_pipeline.c‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5467,6 +5467,57 @@ TEST(pipeline_python_receiver_suppresses_weak_method_edge) {
54675467
PASS();
54685468
}
54695469

5470+
TEST(pipeline_go_method_caller_keeps_lsp_join) {
5471+
/* #1909: receiver-qualifying method QNs moves the def/textual-call QN to
5472+
* package.Type.method — go_lsp's enclosing-function QN must move with it
5473+
* (the ONE-formula contract), or every LSP resolution sourced from inside
5474+
* a method body loses its caller join and the edge dies. The callee name
5475+
* collides across receivers so no short-name fallback can mask the loss. */
5476+
char tmp[256];
5477+
snprintf(tmp, sizeof(tmp), "/tmp/cbm_go_mcall_XXXXXX");
5478+
if (!cbm_mkdtemp(tmp)) {
5479+
FAIL("tmpdir");
5480+
}
5481+
write_temp_file(tmp, "go.mod", "module example.com/fxmcall\n\ngo 1.22\n");
5482+
write_temp_file(tmp, "svc/repo.go",
5483+
"package svc\n"
5484+
"\n"
5485+
"type Repo struct {\n"
5486+
"\tn int\n"
5487+
"}\n"
5488+
"\n"
5489+
"func (r *Repo) Install() int {\n"
5490+
"\treturn r.helper()\n"
5491+
"}\n");
5492+
write_temp_file(tmp, "svc/helper.go",
5493+
"package svc\n"
5494+
"\n"
5495+
"func (r *Repo) helper() int {\n"
5496+
"\treturn r.n\n"
5497+
"}\n");
5498+
write_temp_file(tmp, "other/other.go",
5499+
"package other\n"
5500+
"\n"
5501+
"type Decoy struct{}\n"
5502+
"\n"
5503+
"func (d *Decoy) helper() int { return 2 }\n");
5504+
5505+
char db_path[512];
5506+
snprintf(db_path, sizeof(db_path), "%s/go_mcall.db", tmp);
5507+
cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
5508+
ASSERT_NOT_NULL(p);
5509+
ASSERT_EQ(cbm_pipeline_run(p), 0);
5510+
const char *project = cbm_pipeline_project_name(p);
5511+
5512+
cbm_store_t *s = cbm_store_open_path(db_path);
5513+
ASSERT_NOT_NULL(s);
5514+
ASSERT_TRUE(cross_file_call_exists(s, project, "Install", "helper"));
5515+
cbm_store_close(s);
5516+
cbm_pipeline_free(p);
5517+
th_rmtree(tmp);
5518+
PASS();
5519+
}
5520+
54705521
TEST(pipeline_go_receiver_suppresses_weak_method_edge) {
54715522
char tmp[256];
54725523
snprintf(tmp, sizeof(tmp), "/tmp/cbm_go_recv_XXXXXX");
@@ -14687,6 +14738,7 @@ SUITE(pipeline) {
1468714738
#endif
1468814739
RUN_TEST(pipeline_tsjs_receiver_suppresses_weak_method_edge);
1468914740
RUN_TEST(pipeline_python_receiver_suppresses_weak_method_edge);
14741+
RUN_TEST(pipeline_go_method_caller_keeps_lsp_join);
1469014742
RUN_TEST(pipeline_go_receiver_suppresses_weak_method_edge);
1469114743
RUN_TEST(pipeline_go_rw_usage_never_cross_into_c);
1469214744
RUN_TEST(pipeline_go_rw_usage_never_cross_into_c_parallel);

0 commit comments

Comments
 (0)