Skip to content

Commit ede20ce

Browse files
authored
Merge pull request #156 from PhysShell/claude/mos-ownership-summary-n3q3j4
fix(extractor): oracle-driven FP hardening — 3 precision fixes + FP baseline
2 parents 5e7bb5c + 74fcbc1 commit ede20ce

21 files changed

Lines changed: 886 additions & 17 deletions

File tree

‎.github/workflows/oracle.yml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,11 @@ jobs:
260260
# Compare on product code by default: tests/benchmarks pollute the diff
261261
# (and Infer# only built the product project). include_tests keeps them.
262262
[[ "${INCLUDE_TESTS,,}" == "true" ]] || args+=(--exclude-tests)
263+
# Suppress verified false positives (triaged against real source) so the
264+
# triage queue surfaces only NEW own-only findings. Matched by name, not
265+
# line, so it survives the target drifting at HEAD. See the file header and
266+
# docs/notes/oracle-known-fps.md.
267+
[[ -f corpus/oracle-fp-baseline.txt ]] && args+=(--baseline corpus/oracle-fp-baseline.txt)
263268
[[ -f infer-out/report.sarif ]] && args+=(--infersharp infer-out/report.sarif)
264269
cq=$(find codeql-out -name '*.sarif' -type f 2>/dev/null | head -1 || true)
265270
[[ -n "$cq" ]] && args+=(--codeql "$cq")

‎corpus/oracle-fp-baseline.txt‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Cross-tool oracle — verified false-positive baseline.
2+
#
3+
# Each line is one own-only finding that was triaged against the target's REAL
4+
# source (at the run commit) and confirmed NOT to be a leak. The comparator
5+
# (scripts/oracle_compare.py --baseline) moves these out of "Own.NET only" into a
6+
# separate "Known FP (baselined)" section, so a re-run's triage queue shows only
7+
# genuinely-new findings. Full per-entry rationale: docs/notes/oracle-known-fps.md.
8+
#
9+
# Format (|-delimited, '#' starts a full-line comment, blank lines ignored):
10+
# <owner/repo> | <file-basename> | <OWN code> | <message-substring> | <reason>
11+
#
12+
# The match key is (repo, basename, OWN code, message-substring) — deliberately
13+
# NOT the line number: re-runs clone the target at its current HEAD and the lines
14+
# drift, but the field/event/local name in the message is stable. Keep substrings
15+
# specific enough to bind to exactly the intended finding.
16+
#
17+
# This file suppresses ONLY confirmed false positives. True findings stay visible
18+
# even when benign — e.g. serilog BatchingSink._shutdownSignal (a CTS never
19+
# Dispose()d) and NLog's XmlParser._xmlSource / FileTarget._reusable*Stream
20+
# (fields genuinely never disposed, but wrapping managed-memory-only resources).
21+
# Those are NOT baselined; they are real catches the oracle's leak query misses.
22+
23+
# --- NLog/NLog : all WaitForDispose timer FPs fixed at the source ----------------
24+
# All five NLog timers disposed through the `WaitForDispose(this Timer)` sink are now
25+
# cleared in the extractor — no baseline entries remain. Four (AsyncTaskTarget
26+
# _taskTimeoutTimer/_lazyWriterTimer, AsyncTargetWrapper _lazyWriterTimer,
27+
# BufferingTargetWrapper _flushTimer) by CallReleasesReceiver (direct / simple-alias
28+
# receiver), and TimeoutContinuation _timeoutTimer by RefExchangeNulledField (its
29+
# receiver is the `Interlocked.Exchange(ref _timeoutTimer, null)` result, now bound to
30+
# the field). The three remaining NLog own-only findings (XmlParser _xmlSource,
31+
# FileTarget _reusable*Stream) are genuine undisposed fields, kept visible.
32+
33+
# --- protobuf-net/protobuf-net --------------------------------------------------
34+
protobuf-net/protobuf-net | ProtoWriter.BufferWriter.cs | OWN001 | _nullWriter | intentional null-object kept attached for pooled reuse; Dispose() documents "don't cascade dispose to the null one"
35+
protobuf-net/protobuf-net | ProtoTranscoder.cs | OWN001 | 'sync' | non-product (protobuf 'assorted' sample/extension tree); NetTranscoder is a long-lived singleton holding one ReaderWriterLockSlim for app lifetime
36+
# CommandLineOptions XsltMessageEncountered was here (self-cycle: source `XsltOptions` is
37+
# a get-only property over a constructed XsltArgumentList field, handler captures `this`).
38+
# Now fixed at the source by PropertyReturnsOwnedMember — confirmed cleared on a live
39+
# protobuf oracle run (own-only 0, the finding absent from own-only and baselined) — deleted.
40+
protobuf-net/protobuf-net | Page.xaml.cs | OWN001 | local 'timer' | non-product (assorted/ Silverlight sample); the timer is disposed by an enclosing `using (timer) { ... }` the extractor missed
41+
42+
# --- JamesNK/Newtonsoft.Json ----------------------------------------------------
43+
JamesNK/Newtonsoft.Json | TraceJsonReader.cs | OWN001 | _textWriter | no-op dispose: a JsonTextWriter over an in-memory StringWriter/StringBuilder holds no unmanaged resource, so leaving it undisposed releases nothing
44+
JamesNK/Newtonsoft.Json | JsonSerializer.cs | OWN001 | serializer.Error | intra-call self-subscription: the serializer is freshly created from the same JsonSerializerSettings whose .Error handler it subscribes; source and handler are co-lifetimed
45+
46+
# --- JoshClose/CsvHelper --------------------------------------------------------
47+
JoshClose/CsvHelper | ConsoleHost.cs | OWN014 | AppDomain.CurrentDomain.ProcessExit | non-product (docs-src/ doc-generator) + process-lived subscriber: ConsoleHost itself lives for the whole process, so promoting it to a process-lived event source changes nothing
48+
JoshClose/CsvHelper | ConsoleHost.cs | OWN014 | Console.CancelKeyPress | non-product (docs-src/ doc-generator) + process-lived subscriber: same — a process-lived host subscribing to a process-lived event source is not a lifetime leak
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Threading;
3+
4+
// The "detach and dispose" teardown: stop + dispose the timer through the sink.
5+
static class TimerSink
6+
{
7+
public static void WaitForDispose(this Timer timer, TimeSpan timeout)
8+
{
9+
timer.Change(Timeout.Infinite, Timeout.Infinite);
10+
timer.Dispose();
11+
}
12+
}
13+
14+
// FIX: the owned Timer field is released through the canonical atomic teardown —
15+
// `Interlocked.Exchange(ref _timer, null)` hands back the live timer and nulls the
16+
// field, then the sink disposes it. Recognising this needs two hops: the local
17+
// `current` is bound to `_timer` because Exchange returns the field's owned object
18+
// (RefExchangeNulledField), and `current?.WaitForDispose(...)` is a release because
19+
// the sink disposes its receiver (CallReleasesReceiver). Together → no leak, silent.
20+
sealed class Continuation : IDisposable
21+
{
22+
Timer? _timer;
23+
24+
public Continuation() => _timer = new Timer(_ => { }, null, 0, 1000);
25+
26+
public void Dispose() => StopTimer();
27+
28+
void StopTimer()
29+
{
30+
var current = Interlocked.Exchange(ref _timer, null);
31+
current?.WaitForDispose(TimeSpan.Zero);
32+
}
33+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Threading;
3+
4+
// The "detach and dispose" teardown mined on NLog's TimeoutContinuation: the owned
5+
// Timer field is meant to be released via `Interlocked.Exchange(ref _timer, null)`
6+
// (which hands back the live timer and nulls the field) followed by the
7+
// `WaitForDispose(this Timer)` sink. Here the teardown is missing entirely.
8+
static class TimerSink
9+
{
10+
public static void WaitForDispose(this Timer timer, TimeSpan timeout)
11+
{
12+
timer.Change(Timeout.Infinite, Timeout.Infinite);
13+
timer.Dispose();
14+
}
15+
}
16+
17+
// BUG: the owned Timer field is constructed and never released on any path → OWN001.
18+
sealed class Continuation : IDisposable
19+
{
20+
Timer? _timer;
21+
22+
public Continuation() => _timer = new Timer(_ => { }, null, 0, 1000);
23+
24+
public void Dispose()
25+
{
26+
// nothing — _timer is leaked
27+
}
28+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// OwnLang model of NLog's TimeoutContinuation teardown (src/NLog/Internal/
2+
// TimeoutContinuation.cs, StopTimer). The owned Timer field is released by the atomic
3+
// detach-and-dispose idiom: `Interlocked.Exchange(ref _timeoutTimer, null)` hands back
4+
// the live timer and nulls the field, then `WaitForDispose(this Timer)` stops and
5+
// disposes it. before.cs omits the teardown — the generic OWN001 owned-field leak,
6+
// modelled here as an acquire with no `release`. after.cs releases it via the idiom,
7+
// which the extractor recognises by binding the exchange result to the field
8+
// (RefExchangeNulledField) and following the sink's dispose effect (CallReleasesReceiver).
9+
module Corpus
10+
resource Timer {
11+
acquire create
12+
release dispose
13+
kind "disposable"
14+
emit_type "Timer"
15+
emit_acquire "new Timer({args})"
16+
emit_release "Interlocked.Exchange(ref {0}, null)?.WaitForDispose()"
17+
}
18+
fn Continuation(callback: int) {
19+
let timer = acquire Timer(callback); // _timer = new Timer(_ => { }, null, 0, 1000)
20+
// no `release timer;` — the timer is never detached or disposed (OWN001)
21+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
OWN001
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# field-dispose-via-exchange
2+
3+
The atomic **detach-and-dispose** teardown for an owned `IDisposable` field, mined on
4+
NLog's `TimeoutContinuation.StopTimer` (`src/NLog/Internal/TimeoutContinuation.cs`):
5+
6+
```csharp
7+
var current = Interlocked.Exchange(ref _timeoutTimer, null);
8+
current?.WaitForDispose(TimeSpan.Zero);
9+
```
10+
11+
`Interlocked.Exchange(ref _field, null)` atomically nulls the field and **returns the
12+
object it used to own**, so the local `current` aliases the field's just-detached
13+
disposable; the `WaitForDispose(this Timer)` sink then stops and disposes it.
14+
15+
- **before.cs** — the `Timer` field is constructed and never released → `OWN001`.
16+
- **after.cs** — released via the exchange + sink → **clean**.
17+
18+
## Recognition rule
19+
20+
Two hops, each reusing existing machinery:
21+
22+
1. **`RefExchangeNulledField`** binds the local `current` to `_timer`, because
23+
`Interlocked.Exchange(ref _timer, null)` returns the field's owned object. Restricted
24+
to a `null`/`default` replacement — the unambiguous teardown; an exchange that installs
25+
a *new* non-null value re-arms the field with a fresh object the syntactic scan can't
26+
follow, so crediting the field there could hide a real leak (declined, precision-first).
27+
The bound alias joins the same `aliasToField` map as a plain `var x = _field;`.
28+
2. **`CallReleasesReceiver`** recognises `current?.WaitForDispose(...)` as a release,
29+
because the first-party extension sink disposes its receiver (proved via `ConsumesParam`).
30+
31+
Together they clear `TimeoutContinuation`, the last NLog timer that the
32+
[`field-dispose-via-helper`](../field-dispose-via-helper/notes.md) fix did not reach
33+
(its receiver was the exchange result, not a tracked field alias). With this, all five
34+
NLog `WaitForDispose` timer false positives are fixed at the source.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Threading;
3+
4+
// A "drain and dispose" sink: stops the timer, then disposes it. Because its
5+
// receiver parameter is disposed in the body, a `_timer.WaitForDispose(...)` call
6+
// releases the field — the extractor proves this by inspecting the sink's body
7+
// (ConsumesParam on the reduced extension method's receiver), not by name.
8+
static class TimerSink
9+
{
10+
public static void WaitForDispose(this Timer timer, TimeSpan timeout)
11+
{
12+
timer.Change(Timeout.Infinite, Timeout.Infinite);
13+
timer.Dispose();
14+
}
15+
}
16+
17+
// FIX: the owned Timer field is released through the sink on Dispose. No literal
18+
// `_timer.Dispose()` appears, so recognising this requires following the sink's
19+
// dispose effect — once we do, there is no leak and the case is silent.
20+
sealed class Worker : IDisposable
21+
{
22+
readonly Timer _timer;
23+
24+
public Worker() => _timer = new Timer(_ => { }, null, 0, 1000);
25+
26+
public void Dispose() => _timer.WaitForDispose(TimeSpan.Zero);
27+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Threading;
3+
4+
// A "drain and dispose" sink: the canonical shape mined on NLog, where a Timer
5+
// field is released not by a literal `_timer.Dispose()` but by a custom extension
6+
// method that stops the timer and then disposes it (NLog's
7+
// `WaitForDispose(this Timer, TimeSpan)` in Common/AsyncHelpers.cs).
8+
static class TimerSink
9+
{
10+
public static void WaitForDispose(this Timer timer, TimeSpan timeout)
11+
{
12+
timer.Change(Timeout.Infinite, Timeout.Infinite);
13+
timer.Dispose();
14+
}
15+
}
16+
17+
// BUG: the owned Timer field is constructed but never released on any path —
18+
// neither a literal `.Dispose()` nor the sink. A genuine OWN001 owned-field leak.
19+
sealed class Worker : IDisposable
20+
{
21+
readonly Timer _timer;
22+
23+
public Worker() => _timer = new Timer(_ => { }, null, 0, 1000);
24+
25+
public void Dispose()
26+
{
27+
// nothing — _timer is leaked
28+
}
29+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// OwnLang model of the "drain and dispose" field-release pattern mined on NLog
2+
// (src/NLog/Targets/AsyncTaskTarget.cs and siblings). A Timer field is owned by the
3+
// component; the real fix releases it not with a literal `_timer.Dispose()` but
4+
// through a custom extension method `WaitForDispose(this Timer, TimeSpan)` that
5+
// stops the timer and then disposes it (Common/AsyncHelpers.cs). The before.cs
6+
// leaks the timer (no release on any path) — the generic OWN001 owned-field leak
7+
// modelled here as an acquire with no `release`. The after.cs releases it via the
8+
// sink, which the extractor now recognises by inspecting the sink's body
9+
// (ConsumesParam on its receiver parameter); see notes.md for that recognition rule
10+
// and which NLog variants it does and does not reach.
11+
module Corpus
12+
resource Timer {
13+
acquire create
14+
release dispose
15+
kind "disposable"
16+
emit_type "Timer"
17+
emit_acquire "new Timer({args})"
18+
emit_release "{0}.WaitForDispose()"
19+
}
20+
fn Worker(callback: int) {
21+
let timer = acquire Timer(callback); // _timer = new Timer(_ => { }, null, 0, 1000)
22+
// no `release timer;` — the timer is never stopped or disposed (OWN001)
23+
}

0 commit comments

Comments
 (0)