Skip to content

Commit ac46c17

Browse files
fix(pipeline): bind field_type_hint by owning QN segment, not substring
try_field_type_hint accepted any candidate whose qualified name merely CONTAINED the hinted type name. A single-letter receiver (f.Close(), hint "F") matched almost every same-named candidate - the method name itself included - and rebound a call whose resolution had already failed to an arbitrary project symbol at confidence 0.85, presented as the one weak strategy the Go selector guard deliberately trusts. Require the candidate's owning dot-segment - the segment immediately before the method - to EQUAL the hinted type (or its I-prefixed interface form). The intended variable-named-after-its-type case keeps resolving (lrp_go_s8_field_type_hint stays green); the fabricated bindings lose their only anchor and fall back to the weak short-name strategies the guards already handle. Measured on a real ~1150-file Go repo (with the DeusData#1906/DeusData#1909/DeusData#1910 fixes applied): field_type_hint 1484 -> 356 CALLS edges, the single-character-receiver class 376 -> 0, Go->C/C++ targets 34 -> 0, and no other strategy absorbed the removed edges. 63 edges moved to the interface that declares the method instead of an arbitrary implementation. Fixes DeusData#1927 Signed-off-by: Ilya Brykau <ilya.brykau@orca.security>
1 parent adff62e commit ac46c17

2 files changed

Lines changed: 145 additions & 1 deletion

File tree

‎src/pipeline/pass_parallel.c‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2092,6 +2092,29 @@ static const cbm_gbuf_node_t *find_source_node(const cbm_gbuf_t *gbuf, const cha
20922092
/* Field type hint resolution for obj.Method() with multiple candidates.
20932093
* Strips C# field prefixes (_ / m_), capitalizes to get type name, and
20942094
* checks if TypeName.Method or ITypeName.Method exists among candidates. */
2095+
/* #1927: the hint may only bind a candidate whose OWNING dot-segment — the
2096+
* segment immediately before the method name — EQUALS the hinted type. The
2097+
* previous raw strstr() accepted the hinted name anywhere in the QN, so a
2098+
* single-letter receiver (`f.Close()` → hint "F") matched nearly every
2099+
* candidate ("F" ⊂ "…FileStore.Close", the method name included) and rebound
2100+
* a call whose resolution had already failed to an arbitrary project symbol
2101+
* at PP_FIELD_HINT_CONF. Segment equality keeps the intended
2102+
* variable-named-after-its-type case (`repo.Find()` → `Repo.Find`,
2103+
* lrp_go_s8_field_type_hint) and picks the candidate whose owner actually
2104+
* carries the name instead of whichever QN contains the letters. */
2105+
static bool fth_owner_segment_is(const char *candidate_qn, const char *type_name) {
2106+
const char *last_dot = strrchr(candidate_qn, '.');
2107+
if (!last_dot || last_dot == candidate_qn) {
2108+
return false;
2109+
}
2110+
const char *seg_start = last_dot;
2111+
while (seg_start > candidate_qn && seg_start[-1] != '.') {
2112+
seg_start--;
2113+
}
2114+
size_t seg_len = (size_t)(last_dot - seg_start);
2115+
return seg_len > 0 && strncmp(seg_start, type_name, seg_len) == 0 && type_name[seg_len] == '\0';
2116+
}
2117+
20952118
static void try_field_type_hint(resolve_ctx_t *rc, cbm_resolution_t *res, const char *callee_name,
20962119
int64_t source_id) {
20972120
if (!res->qualified_name || res->candidate_count <= SKIP_ONE) {
@@ -2131,7 +2154,8 @@ static void try_field_type_hint(resolve_ctx_t *rc, cbm_resolution_t *res, const
21312154
int cand_count = 0;
21322155
cbm_registry_find_by_name(rc->registry, method, &cands, &cand_count);
21332156
for (int ci = 0; ci < cand_count; ci++) {
2134-
if (strstr(cands[ci], type_name) || strstr(cands[ci], iface_name)) {
2157+
if (fth_owner_segment_is(cands[ci], type_name) ||
2158+
fth_owner_segment_is(cands[ci], iface_name)) {
21352159
const cbm_gbuf_node_t *better = cbm_gbuf_find_by_qn(rc->main_gbuf, cands[ci]);
21362160
if (better && better->id != source_id) {
21372161
res->qualified_name = cands[ci];

‎tests/test_pipeline.c‎

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4886,6 +4886,125 @@ TEST(pipeline_go_multi_init_nodes_survive) {
48864886
PASS();
48874887
}
48884888

4889+
TEST(pipeline_go_parallel_field_hint_requires_owner_segment) {
4890+
/* #1927: try_field_type_hint (parallel resolver only) matched the hinted
4891+
* type name as a raw SUBSTRING of the candidate QN. A single-letter
4892+
* receiver — `f.Close()` on an *os.File — hints "F", and "F" is a
4893+
* substring of "…FileStore.Close", so a stdlib call was rebound to an
4894+
* arbitrary project method at PP_FIELD_HINT_CONF and presented as
4895+
* field_type_hint (the one weak strategy the Go selector guard trusts).
4896+
* The hint must fire only when the candidate's OWNING segment — the
4897+
* dot-segment immediately before the method — EQUALS the hinted type;
4898+
* otherwise the resolution stays a weak short-name match and the Go
4899+
* guard (#1906) drops it. >= 50 files forces pass_parallel.c, the only
4900+
* path that runs try_field_type_hint(). */
4901+
char tmp[256];
4902+
snprintf(tmp, sizeof(tmp), "/tmp/cbm_go_fth_XXXXXX");
4903+
if (!cbm_mkdtemp(tmp)) {
4904+
FAIL("tmpdir");
4905+
}
4906+
4907+
write_temp_file(tmp, "go.mod", "module example.com/fxhint\n\ngo 1.22\n");
4908+
/* Two project methods named Close → the registry resolves f.Close() as an
4909+
* ambiguous short-name match (candidate_count 2), the shape the hint
4910+
* upgrades. FileStore's QN carries the capital F the buggy substring
4911+
* latches onto; Conn is the second candidate. */
4912+
write_temp_file(tmp, "filestore/filestore.go",
4913+
"package filestore\n"
4914+
"\n"
4915+
"type FileStore struct{ open bool }\n"
4916+
"\n"
4917+
"func (fs *FileStore) Close() {\n"
4918+
"\tfs.open = false\n"
4919+
"}\n");
4920+
write_temp_file(tmp, "conn/conn.go",
4921+
"package conn\n"
4922+
"\n"
4923+
"type Conn struct{ live bool }\n"
4924+
"\n"
4925+
"func (cn *Conn) Close() {\n"
4926+
"\tcn.live = false\n"
4927+
"}\n");
4928+
/* Two project methods named Find → same ambiguity, but here the receiver
4929+
* variable IS named after its type (`finder`), the case the hint exists
4930+
* for: the owning segment of Finder.Find equals the hinted "Finder". */
4931+
write_temp_file(tmp, "finder/finder.go",
4932+
"package finder\n"
4933+
"\n"
4934+
"type Finder struct{ n int }\n"
4935+
"\n"
4936+
"func NewFinder() *Finder { return &Finder{n: 1} }\n"
4937+
"\n"
4938+
"func (fi *Finder) Find(id int) int {\n"
4939+
"\treturn fi.n + id\n"
4940+
"}\n");
4941+
write_temp_file(tmp, "locator/locator.go",
4942+
"package locator\n"
4943+
"\n"
4944+
"type Locator struct{ n int }\n"
4945+
"\n"
4946+
"func (lo *Locator) Find(id int) int {\n"
4947+
"\treturn lo.n - id\n"
4948+
"}\n");
4949+
write_temp_file(tmp, "app/app.go",
4950+
"package app\n"
4951+
"\n"
4952+
"import (\n"
4953+
"\t\"os\"\n"
4954+
"\n"
4955+
"\txf \"example.com/fxhint/finder\"\n"
4956+
")\n"
4957+
"\n"
4958+
"func Lookup(path string) int64 {\n"
4959+
"\tf, err := os.Open(path)\n"
4960+
"\tif err != nil {\n"
4961+
"\t\treturn 0\n"
4962+
"\t}\n"
4963+
"\tdefer f.Close()\n"
4964+
"\tst, err := f.Stat()\n"
4965+
"\tif err != nil {\n"
4966+
"\t\treturn 0\n"
4967+
"\t}\n"
4968+
"\treturn st.Size()\n"
4969+
"}\n"
4970+
"\n"
4971+
"func UseFinder(id int) int {\n"
4972+
"\tfinder := xf.NewFinder()\n"
4973+
"\treturn finder.Find(id)\n"
4974+
"}\n");
4975+
for (int i = 0; i < 52; i++) {
4976+
char name[64];
4977+
char body[128];
4978+
snprintf(name, sizeof(name), "pad/filler%d.go", i);
4979+
snprintf(body, sizeof(body), "package pad\n\nfunc filler%d() int { return %d }\n", i, i);
4980+
write_temp_file(tmp, name, body);
4981+
}
4982+
4983+
char db_path[512];
4984+
snprintf(db_path, sizeof(db_path), "%s/go_fth.db", tmp);
4985+
cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
4986+
ASSERT_NOT_NULL(p);
4987+
ASSERT_EQ(cbm_pipeline_run(p), 0);
4988+
const char *project = cbm_pipeline_project_name(p);
4989+
4990+
cbm_store_t *s = cbm_store_open_path(db_path);
4991+
ASSERT_NOT_NULL(s);
4992+
4993+
/* (1) Reproduce-first: RED before the fix. The substring hint rebinds the
4994+
* stdlib f.Close() to FileStore.Close ("F" ⊂ QN) at high confidence and
4995+
* the Go guard keeps it. With segment equality the upgrade is refused,
4996+
* the match stays suffix_match, and the Go guard drops it. */
4997+
ASSERT_FALSE(cross_file_call_exists(s, project, "Lookup", "Close"));
4998+
/* (2) The intended hint case survives: `finder` names its type, so the
4999+
* owning segment of Finder.Find equals the hint. */
5000+
ASSERT_TRUE(cross_file_call_exists(s, project, "UseFinder", "Find"));
5001+
5002+
cbm_store_close(s);
5003+
cbm_pipeline_free(p);
5004+
th_rmtree(tmp);
5005+
PASS();
5006+
}
5007+
48895008
/* Fixture for the #1928 cross-language reference-guard probes (sequential and
48905009
* parallel twins). pad_files > 0 adds filler files to push the index over the
48915010
* parallel-pipeline threshold, since USAGE/WRITES/READS have one resolver per
@@ -13310,6 +13429,7 @@ SUITE(pipeline) {
1331013429
RUN_TEST(pipeline_python_receiver_suppresses_weak_method_edge);
1331113430
RUN_TEST(pipeline_go_method_caller_keeps_lsp_join);
1331213431
RUN_TEST(pipeline_go_receiver_suppresses_weak_method_edge);
13432+
RUN_TEST(pipeline_go_parallel_field_hint_requires_owner_segment);
1331313433
RUN_TEST(pipeline_go_multi_init_nodes_survive);
1331413434
RUN_TEST(pipeline_go_rw_usage_never_cross_into_c);
1331513435
RUN_TEST(pipeline_go_rw_usage_never_cross_into_c_parallel);

0 commit comments

Comments
 (0)