Skip to content

Commit 77bfc07

Browse files
committed
feat(p1a): curate XmlReader/XmlWriter/JsonDocument as owned factories
P1a stdlib contract pack (producer side): extend the curated owned-returning factory recognition with three high-value, low-FP BCL families whose result the caller must dispose — `XmlReader.Create`, `XmlWriter.Create` (System.Xml) and `JsonDocument.Parse` (System.Text.Json, which pools memory and is a common real leak when dropped). Same producer-half contract as File.Open* / crypto Create*: a `var doc = JsonDocument.Parse(json)` that drops `doc` now surfaces OWN001 at the factory call, and use/double-dispose of the result is OWN002/OWN003. Both synced halves are updated in lockstep: - extractor `IsOwningFactory` (Program.cs) — gates whether to EMIT the factory call fact, by resolved symbol (static + result implements IDisposable + the type/namespace), so a Task-returning `JsonDocument.ParseAsync` is excluded; - bridge `_BCL_FRESH_BY_NS` (ownir.py) — recognises the callee name as `fresh`. Bridge half is covered by new tests (leak → OWN001 for each, bare and namespace-qualified; a disposed result stays clean): ownir 206/206, ownership 44/44, mypy --strict, ruff all green locally. NOTE: this environment has no dotnet, so the extractor (C#) half is not built locally — it is verified by CI (golden C# compiles & runs, C# leak extractor, and the real-C# corpus benchmark for recall/specificity). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019846YSZ35c7CdkWQ1qX5gm
1 parent 8b878de commit 77bfc07

3 files changed

Lines changed: 42 additions & 1 deletion

File tree

‎frontend/roslyn/OwnSharp.Extractor/Program.cs‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2260,9 +2260,15 @@ void Visit(ExpressionSyntax e)
22602260
// IncrementalHash.CreateHash(), ... (guarded by static + Create-prefixed + the RESULT
22612261
// implementing IDisposable + the crypto namespace, so an instance `CreateEncryptor()` or
22622262
// a non-IDisposable `CreateFromName()` is never mistaken for one).
2263+
// * P1a stdlib pack: System.Xml `XmlReader.Create`/`XmlWriter.Create` -> a fresh owned
2264+
// reader/writer; System.Text.Json `JsonDocument.Parse` -> a JsonDocument that pools memory
2265+
// and must be disposed. Gated on the RESULT implementing IDisposable, so a Task-returning
2266+
// `JsonDocument.ParseAsync` is excluded.
22632267
// Curated + symbol-resolved, so a borrowed/cached disposable handed back by some other API is
22642268
// never mistaken for an owned acquire (precision over recall — the set grows only as
2265-
// ownership is certain).
2269+
// ownership is certain). Kept in lockstep with the bridge table `_BCL_FRESH_BY_NS`
2270+
// (ownlang/ownir.py): the extractor decides whether to EMIT the factory call fact, the bridge
2271+
// recognises the callee name as `fresh`.
22662272
static bool IsOwningFactory(ExpressionSyntax? e, SemanticModel model)
22672273
{
22682274
if (e is not InvocationExpressionSyntax i
@@ -2278,6 +2284,14 @@ static bool IsOwningFactory(ExpressionSyntax? e, SemanticModel model)
22782284
&& ImplementsIDisposable(sym.ReturnType)
22792285
&& IsInNamespace(sym.ContainingType, "System", "Security", "Cryptography"))
22802286
return true;
2287+
if (sym.IsStatic && ImplementsIDisposable(sym.ReturnType)
2288+
&& ((sym.Name == "Create"
2289+
&& sym.ContainingType is { Name: "XmlReader" or "XmlWriter" } xt
2290+
&& IsInNamespace(xt, "System", "Xml"))
2291+
|| (sym.Name == "Parse"
2292+
&& sym.ContainingType is { Name: "JsonDocument" } jt
2293+
&& IsInNamespace(jt, "System", "Text", "Json"))))
2294+
return true;
22812295
return false;
22822296
}
22832297

‎ownlang/ownir.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,17 @@ def _infer_return_skeleton(nodes: Any, param_names: set[str],
11851185
"SHA1.Create", "SHA256.Create", "SHA384.Create", "SHA512.Create", "MD5.Create",
11861186
"Aes.Create", "RSA.Create", "ECDsa.Create",
11871187
),
1188+
# P1a (stdlib contract pack): more well-known static factories whose result the caller
1189+
# OWNS and must dispose. `XmlReader`/`XmlWriter.Create` return a fresh reader/writer; a
1190+
# `JsonDocument` from `Parse` pools memory and must be disposed (a `var doc =
1191+
# JsonDocument.Parse(json)` that drops `doc` is a common real leak). Kept in lockstep with
1192+
# the extractor's `IsOwningFactory` (frontend/roslyn/.../Program.cs).
1193+
"System.Xml": (
1194+
"XmlReader.Create", "XmlWriter.Create",
1195+
),
1196+
"System.Text.Json": (
1197+
"JsonDocument.Parse",
1198+
),
11881199
}
11891200
_BCL_FRESH_FACTORIES = frozenset(e for es in _BCL_FRESH_BY_NS.values() for e in es)
11901201
# the fully-qualified identities (each under its real namespace), accepted beside the bare forms.

‎tests/test_ownir.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,6 +1691,22 @@ def _bcl(body: list) -> list:
16911691
if _bcl([{"op": "call", "callee": "global::MyCompany.File.OpenRead", "args": ["p"],
16921692
"result": "s", "line": 4}]):
16931693
fails.append("Tier B precision: `global::`-qualified non-System.IO must NOT match")
1694+
# P1a (stdlib pack): more curated owned-returning factories. A dropped XmlReader/XmlWriter/
1695+
# JsonDocument result leaks at the factory call (OWN001), the same producer-side contract as
1696+
# File.Open* — both the bare `Type.Method` and the namespace-qualified identity resolve.
1697+
for fresh_callee, ln in (("XmlReader.Create", 5), ("XmlWriter.Create", 6),
1698+
("JsonDocument.Parse", 7),
1699+
("System.Text.Json.JsonDocument.Parse", 8)):
1700+
checks += 1
1701+
leak = [(x.code, x.line) for x in _bcl(
1702+
[{"op": "call", "callee": fresh_callee, "args": ["a"], "result": "s", "line": ln}])]
1703+
if leak != [("OWN001", ln)]:
1704+
fails.append(f"P1a: a leaked `{fresh_callee}` result must be OWN001@{ln}, got {leak}")
1705+
# disposing the P1a factory result is clean (no false leak), proving it is a real acquire.
1706+
checks += 1
1707+
if _bcl([{"op": "call", "callee": "XmlReader.Create", "args": ["a"], "result": "s", "line": 5},
1708+
{"op": "release", "var": "s", "line": 6}]):
1709+
fails.append("P1a: a disposed XmlReader.Create result must be clean (silent)")
16941710
checks += 1
16951711
# OVERRIDE (Codex): a first-party summary is authoritative — a first-party `File.OpenRead`
16961712
# that returns its parameter is NOT fresh, so a caller dropping its result is clean; the

0 commit comments

Comments
 (0)