Skip to content

Commit 52a562e

Browse files
committed
D5.3: tighten BCL factory match + propagate freshness through wrappers (Codex #127)
Two Codex P2s on the BCL fresh-factory table: - PRECISION: the match was a loose last-two-segments suffix, so an external `MyCompany.File.OpenRead` returning a plain value would be treated as a BCL factory and fabricate a false OWN001/OWN002. Now matches ONLY the bare `File.Method` or the fully-qualified `System.IO.File.Method` identity. Also flipped `_callee_returns_fresh` so a first-party summary is authoritative and OVERRIDES the table (a callee whose body we can see is never given a fabricated `fresh`). - RECALL: a first-party wrapper `Make(){ return File.OpenRead(p) }` recorded a `forward` to the external factory, which the solver degraded to `unknown`, so a dropped `Make()` leaked invisibly. `_infer_return_skeleton` now classifies such a return as `fresh`, so the caller is charged the leak. Tests: non-System.IO look-alike rejected, first-party override, wrapper-fresh recall. ownir 177/177; ruff + mypy clean. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KkpSWNx7ARLpQeAs13kkyA
1 parent 76d29af commit 52a562e

3 files changed

Lines changed: 70 additions & 18 deletions

File tree

‎docs/notes/d5-ownership-transfer.md‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -341,11 +341,18 @@ escape-without-transfer and all `unknown`/`may` lower to **silence** in the defa
341341
returns_fresh` path the first-party T1 inference uses (now the single source of truth for
342342
the leak pre-scan, branch-hoist safety, and lowering), so a leaked `var s =
343343
File.OpenRead(p)` surfaces as OWN001 *at the factory call* — invisible before (no body to
344-
infer from; see `corpus-benchmark.md`). Keyed by `Type.Method` (matches a namespace-
345-
qualified callee on its last two segments). Pure factories only — overload-ambiguous
346-
*wrappers* that adopt an arg (`new StreamReader(stream)`) are excluded (sink/T4). Tests in
347-
`test_ownir.py` (leak / disposed-clean / use-after-dispose / namespace-qualified / a
348-
non-disposable `File.ReadAllText` correctly making no claim).
344+
infer from; see `corpus-benchmark.md`). Matched conservatively (Codex): ONLY the bare
345+
`File.Method` or the fully-qualified `System.IO.File.Method` — a same-named factory in
346+
another namespace (`MyCompany.File.OpenRead`) is **not** a match, so we never fabricate
347+
ownership for a look-alike. A **first-party summary overrides** the table (`_callee_
348+
returns_fresh` trusts a known body over Tier B), and a first-party **wrapper** that
349+
returns a factory result (`Make(){ return File.OpenRead(p) }`) is itself `fresh`, so a
350+
dropped `Make()` leaks too (the return skeleton propagates BCL freshness instead of
351+
forwarding to the external, unsummarizable callee). Pure factories only — overload-
352+
ambiguous *wrappers* that adopt an arg (`new StreamReader(stream)`) are excluded (sink/T4).
353+
Tests in `test_ownir.py` (leak / disposed-clean / use-after-dispose / namespace-qualified /
354+
non-System.IO look-alike rejected / first-party override / wrapper-fresh recall / a
355+
non-disposable `File.ReadAllText` making no claim).
349356
- **Sink side — `leaveOpen` breadth (remaining, extractor-side).** The documented
350357
consume/borrow table (`StreamReader`/`StreamWriter`/`CryptoStream`/… by the `leaveOpen`
351358
bool literal) rides the existing `$consume`/`$borrow` channel (D5.1b); its breadth is a

‎ownlang/ownir.py‎

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,12 @@ def _infer_return_skeleton(nodes: Any, param_names: set[str]) -> ReturnSkeleton:
10411041
(v,) = tuple(returned)
10421042
callee = call_results.get(v)
10431043
if callee and v not in param_names and v not in acquired:
1044+
if _is_bcl_fresh_factory(callee):
1045+
# a thin wrapper returning a BCL factory's result is itself `fresh` — the
1046+
# caller owns it (Codex). Without this the return is a `forward` to an
1047+
# external (bodyless) callee, which the solver degrades to `unknown`, so a
1048+
# dropped `Make()` whose body is `return File.OpenRead(p)` leaks invisibly.
1049+
return ReturnSkeleton("fresh")
10441050
return ReturnSkeleton("forward", callee=callee)
10451051
return ReturnSkeleton() # not provably owned -> no claim
10461052

@@ -1109,26 +1115,29 @@ def _infer_return_skeleton(nodes: Any, param_names: set[str]) -> ReturnSkeleton:
11091115
"File.OpenRead", "File.OpenText", "File.OpenWrite",
11101116
"File.Open", "File.Create", "File.CreateText", "File.AppendText",
11111117
})
1118+
# the fully-qualified `System.IO.File.*` identities — accepted alongside the bare forms.
1119+
_BCL_FRESH_FQNS = frozenset("System.IO." + e for e in _BCL_FRESH_FACTORIES)
11121120

11131121

11141122
def _is_bcl_fresh_factory(callee: str) -> bool:
1115-
"""True if `callee` names a curated BCL factory whose return the caller owns. Matches
1116-
the bare `Type.Method` or any namespace-qualified form of it (its last two segments)."""
1117-
if not callee:
1118-
return False
1119-
return (callee in _BCL_FRESH_FACTORIES
1120-
or ".".join(callee.split(".")[-2:]) in _BCL_FRESH_FACTORIES)
1123+
"""True if `callee` names a curated BCL factory whose return the caller owns. Accepts
1124+
ONLY the bare `Type.Method` (`File.OpenRead`) or the fully-qualified `System.IO.File.*`
1125+
identity — a same-named type in another namespace (`MyCompany.File.OpenRead`) is NOT a
1126+
match. Precision-first: we never fabricate ownership for a non-BCL look-alike (Codex)."""
1127+
return bool(callee) and (callee in _BCL_FRESH_FACTORIES or callee in _BCL_FRESH_FQNS)
11211128

11221129

11231130
def _callee_returns_fresh(callee: str, mos: dict[str, Any] | None) -> bool:
1124-
"""Whether a `call` to `callee` yields a fresh owned result the caller must release —
1125-
a first-party summary that returns `fresh` (Tier A) OR a curated BCL factory (Tier B).
1126-
The single source of truth shared by the leak pre-scan, the branch-hoist safety walk,
1127-
and the flow lowering, so all three agree on what counts as an acquire."""
1128-
if _is_bcl_fresh_factory(callee):
1129-
return True
1131+
"""Whether a `call` to `callee` yields a fresh owned result the caller must release.
1132+
A first-party summary is AUTHORITATIVE — if one exists we trust its `returns`, so a
1133+
same-named first-party `File.OpenRead` (Tier A) overrides the BCL table (Tier B) and is
1134+
never given a fabricated `fresh` (Codex). Only a callee we have no body for falls back to
1135+
the curated BCL factory table. The single source of truth shared by the leak pre-scan,
1136+
the branch-hoist safety walk, and the flow lowering, so all three agree."""
11301137
summ = mos.get(callee) if (mos is not None and callee) else None
1131-
return summ is not None and getattr(summ, "returns", None) == "fresh"
1138+
if summ is not None:
1139+
return getattr(summ, "returns", None) == "fresh"
1140+
return _is_bcl_fresh_factory(callee)
11321141

11331142

11341143
def _param_signals(pname: str, nodes: Any) -> tuple[bool, bool, bool]:

‎tests/test_ownir.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,6 +1551,42 @@ def _bcl(body: list) -> list:
15511551
if _bcl([{"op": "call", "callee": "File.ReadAllText", "args": ["p"],
15521552
"result": "t", "line": 3}]):
15531553
fails.append("Tier B: a non-disposable BCL method must not be treated as a factory")
1554+
checks += 1
1555+
# PRECISION (Codex): a same-named factory in ANOTHER namespace is NOT System.IO.File, so
1556+
# the match must not be a loose suffix — only bare `File.X` and `System.IO.File.X` count.
1557+
# A `MyCompany.File.OpenRead` returning a plain value must NOT fabricate a false OWN001.
1558+
if _bcl([{"op": "call", "callee": "MyCompany.File.OpenRead", "args": ["p"],
1559+
"result": "s", "line": 5}]):
1560+
fails.append("Tier B precision: a non-System.IO `*.File.OpenRead` must NOT match")
1561+
checks += 1
1562+
# OVERRIDE (Codex): a first-party summary is authoritative — a first-party `File.OpenRead`
1563+
# that returns its parameter is NOT fresh, so a caller dropping its result is clean; the
1564+
# table must not fabricate ownership for a callee whose body we can see.
1565+
ov_fp = check_facts({"module": "M", "functions": [
1566+
{"name": "File.OpenRead", "file": "B.cs", "params": [{"name": "x", "line": 1}],
1567+
"body": [{"op": "return", "var": "x", "line": 2}]},
1568+
{"name": "Caller", "file": "B.cs", "body": [
1569+
{"op": "acquire", "var": "a", "line": 10},
1570+
{"op": "call", "callee": "File.OpenRead", "args": ["a"],
1571+
"result": "r", "line": 11},
1572+
{"op": "release", "var": "a", "line": 12}]}]})
1573+
if ov_fp:
1574+
fails.append(f"Tier B: a first-party summary must override the BCL table, "
1575+
f"got {[(x.component, x.code) for x in ov_fp]}")
1576+
checks += 1
1577+
# RECALL (Codex): a first-party wrapper that returns a BCL factory result is itself fresh,
1578+
# so a caller dropping `Make()` leaks OWN001 — the return skeleton propagates BCL freshness
1579+
# rather than degrading to a `forward` to the external factory (-> unknown -> invisible).
1580+
wrap = [(x.component, x.line, x.code) for x in check_facts({"module": "M", "functions": [
1581+
{"name": "Make", "file": "B.cs", "body": [
1582+
{"op": "call", "callee": "File.OpenRead", "args": ["p"],
1583+
"result": "s", "line": 2},
1584+
{"op": "return", "var": "s", "line": 3}]},
1585+
{"name": "Caller2", "file": "B.cs", "body": [
1586+
{"op": "call", "callee": "Make", "args": [], "result": "r", "line": 10}]}]})]
1587+
if wrap != [("Caller2", 10, "OWN001")]:
1588+
fails.append(f"Tier B: a wrapper returning a BCL factory result must be fresh "
1589+
f"(caller leak OWN001@10), got {wrap}")
15541590
# OVERWRITE kills the prior binding (CodeRabbit): `acquire x; x = Unknown(); release x`
15551591
# — the call's result reuses an owned local and the call is dropped (unknown callee),
15561592
# so the ORIGINAL x leaks (its reference is lost), not read as clean. The release after

0 commit comments

Comments
 (0)