Skip to content

fix(resource-lifetime): decide DoubleRelease with the ownership engine - #283

Merged
SizzleUnrlsd merged 8 commits into
mainfrom
feat/ownership-double-release
Nov 16, 2026
Merged

SizzleUnrlsd merged 8 commits into
mainfrom
feat/ownership-double-release

Conversation

@SizzleUnrlsd

Copy link
Copy Markdown
Contributor

Summary

ResourceLifetime.DoubleRelease no longer comes from counters alone. The ownership engine,
which reports UseAfterRelease since #270, judges every release from its stabilised states, and
its verdict decides what is reported at that release. The counters stay where the engine has no
verdict.

Part of #104.

What changes

No longer reported (false positives):

  • a release on each branch of an if/else or a switch, after an early return, at a goto
    cleanup label, or in a catch that returns. cpp-exception-paths.cpp and
    release-all-paths-no-leak.c had opted out of the strict count because of it;
  • a release after a reset to NULL (release(h); h = NULL; release(h);, acceptance case 1 of
    DoubleRelease and UseAfterRelease: derive them from the ownership engine #104), also through a wrapper, and a null pointer freed in a function that allocates nothing;
  • the "inter-procedural resource analysis incomplete" warning on a copy the engine follows
    (alias-keeps-old-resource-no-leak.c).

Now reported as potential double releases, where only the "incomplete" warning, or nothing, was
reported:

  • a second release through an alias, q = p; free(p); free(q); (comment of 2026-11-11 on DoubleRelease and UseAfterRelease: derive them from the ownership engine #104),
    including a copy kept in a local struct field, and an alias passed to a release wrapper defined
    in the same file or another one;
  • a pointer whose possible allocations were all released before, or that holds a released
    allocation on one path and null or an unknown pointer on another.

The double releases the counters already reported stay, with the same text, including a second
call to a release wrapper and release_both(p, p) when the callee releases both parameters on
every normal exit.

On the 79 fixtures with a resource model (149 diagnostics on main): 3 removed, the false
positives above; none added.

How it works

  • One verdict per release: per released argument or pointee, not per instruction.
    Detected when the location holds one resource that may be released, or several
    possibilities each released on every path where it exists; Checked when it holds null only,
    or resources only owned and certain, and nothing unknown; Unknown otherwise.
  • The counters are filtered, not changed. They count and deduplicate as before. At a judged
    release, Checked emits nothing, Detected one DoubleRelease (their own text when they had
    one), and Unknown what they emitted before. The engine reports a Detected release alone where
    the counters keep no local handle for it.
  • Several releases in one call are judged on the state before the call: argument order is
    not execution order. One alert only when both parameters hold exactly the same single resource
    and the callee releases both on every normal exit. Exclusive aliases
    (if (c) { x = a; y = b; } else { x = b; y = a; } release_both(x, y);) and
    release_one(p, p, c) stay Unknown.
  • Loops: a release inside a CFG cycle is never Detected; the counters keep deciding there
    (DoubleRelease: a release repeated by a loop is not reported #275).
  • Functions that release without acquiring are solved too, so freeing a null pointer does
    not depend on an unrelated allocation.
  • --verbose prints each verdict ([DR-VERDICT] file:line:column function effect verdict) and
    the work of the extra solve per function ([DR-COST]).
  • Message, severity and rule id are unchanged. No cache format change.

The extra solve and its cost

The fact collector leaves a location empty when it holds a value the engine does not follow, and
a join forgets the empty path: h = NULL; if (c) h = unknown_ptr(); free(h); would look like a
release of null only, and the counters' warning would be dropped. So the verdicts are read off a
second solve of the same facts in which those locations start unknown (withUnknownAtEntry).
UseAfterRelease, MissingRelease and ReferenceLost keep the collector's facts. This is a
transition until untracked values get an explicit representation for every rule (#281).

Measured cost:

  • engine work: 1052 block visits added to 987 on the resource fixtures and extra probe files
    with many releases (not in the repository), 2.07 times the engine's solving;
  • wall time and memory: no notable slowdown observed on the measured scenarios, with one
    reservation on the heaviest case with an empty cache (below). Controlled comparison on a remote
    Linux machine (AMD Ryzen 5 5600H, Docker ct217-linux-amd64:llvm20, 4 CPUs): main 1fadba2
    against this branch, both built with the same configuration, on the same inputs and options,
    the two versions alternated (A B, then B A). Median wall time [min–max], 5 runs per version
    unless noted:
Inputs Cache main this branch Difference
79 fixtures, one process each empty 4.68 s [4.35–5.72] 4.51 s [4.12–5.01] −3.7 %
filled 2.81 s [2.76–2.85] 2.78 s [2.69–3.55] −1.1 %
3 extra probe files, one process each empty 0.187 s [0.167–0.202] 0.188 s [0.158–0.205] +0.5 %
filled 0.128 s [0.106–0.129] 0.123 s [0.111–0.137] −3.5 %
3 of the analyzer's own sources, cross-TU empty 43.5 s [40.7–57.5] 42.5 s [41.1–51.9] −2.2 %
filled 16.26 s [16.14–16.29] 16.26 s [16.19–16.42] 0.0 %
10 of the analyzer's own sources, cross-TU empty, 3 runs 122.3 s [91.4–136.8] 143.3 s [89.9–144.9] inconclusive, see below
filled 35.70 s [35.66–35.95] 35.72 s [35.63–35.99] +0.1 %
  • For the cross-TU inputs, "filled" means after two unmeasured runs: the second run of a
    summary cache still recomputes some rounds.
  • Reservation: the 10 sources with an empty cache, the heaviest case, allow no conclusion.
    The machine slowed down and then sped up during these runs, whatever the version. In
    execution order: main 136.8 s, branch 143.3 s; branch 144.9 s, main 122.3 s; main 91.4 s,
    branch 89.9 s. The ResourceLifetime step, which includes the extra solve, took 21.3 to
    21.6 s in the first three runs and 13.4 s in the last three, on both versions. Compilation
    dominates these runs, and this change does not touch it. With three runs per version and
    this drift, a small slowdown in that case is not ruled out; the same sources with a filled
    cache differ by +0.1 %.
  • Compilation and analysis: in the fixture runs, loading and compiling took 1.68 s on main and
    1.69 s on the branch, the rest 2.99 s and 2.82 s. Modules of a cross-TU run compile in
    parallel, so the runs with a filled cache measure the analysis alone. There, the
    ResourceLifetime step, which includes the extra solve, took 13.19 s on main and 13.20 s on the
    branch for the 10 sources.
  • Peak memory of the largest process: within 2 %, for example 487 MB on both for the 10 sources
    with a filled cache, and 1.40 GB against 1.43 GB while compiling them.

Limits kept

Tests

  • Engine unit tests: the verdict rules (including both allocations released, exclusive aliases,
    release_both / release_one, copies of one release, an incomplete solve), the resource a
    verdict names, and four cases for withUnknownAtEntry.
  • Ten new fixtures: eight with a strict count, and two that pin only the absence of a double
    release (exclusive aliases, delete-reset). check_double_release_cross_tu runs wrappers from
    another file in memory, over three runs sharing a summary cache, with Z3, and the caller alone.
  • Three fixtures get their strict count back, with negative assertions.
  • Mutation checks, each caught by a test: the pair rule without the same-target condition, an
    accusation inside a loop, verdicts read off the collector's facts, judged releases left out of
    the counts, one verdict per instruction.
  • The engine's [DR-VERDICT] trace was compared with a verdict predicted per release on the
    fixtures and on extra probe files: no mismatch.

Verification

  • Each commit builds and passes the unit tests; commit 4 leaves every diagnostic unchanged.
  • run_test.py --jobs=4 on Linux (Docker ct217-linux-amd64:llvm20, LLVM 20.1.8, Z3):
    2915/2915 on the tree of these commits (the CHANGELOG wording was refined afterwards); ctest
    2/2.

judgeReleases reads, at every Release event and every parameter of a
Call that releases an owned resource without uncertainty, what the
released location may hold just before it. It returns one verdict per
instruction and location.

Detected: the location holds one resource that may be released (the
filter of releasedUses), or several possibilities, each released on
every path where it exists. Checked: it holds null only, or resources
that are only owned and certain, and nothing unknown. Unknown: anything
else, and everything when the solve is incomplete.

The parameters of one call are judged on the state before the call:
argument order is not execution order. When two of them hold exactly
the same single resource and the callee releases both on every normal
exit, the later one is Detected. When their targets only overlap, or a
release is only possible, a Checked verdict becomes Unknown. Copies of
one release on the edges of an invoke must agree.

Nothing uses the verdicts yet.
The fact collector leaves a location empty when it holds a value the
engine does not follow: the result of an unknown call, an argument,
untracked memory, a slot not written yet. A join then forgets the empty
path, so `h = NULL; if (c) h = unknown_ptr(); free(h);` looks like a
release of null only.

withUnknownAtEntry writes "unknown" at entry into every storage
location and every SSA value that no event writes. The release verdicts
are meant to be read off these facts; UseAfterRelease, MissingRelease
and ReferenceLost keep solving the collector's facts. The second solve
is a transition until untracked values get an explicit representation
for every rule (#281).
…s from

CollectedFunction gains releaseEffects. For every release the engine
may judge, it records the event, the released location, the argument
path it comes from (a model rule's argument, a summarised parameter
passed by value, or a summarised pointee) and the slot the released
pointer was read from, to name it. A call whose summary is incomplete
records none.
The ownership engine now runs once per function before the counters,
and also in a function that releases without acquiring anything, so
that freeing a null pointer does not depend on an unrelated allocation.
Its verdicts are mapped to the call and argument they belong to, read
off withUnknownAtEntry, and a release inside a CFG cycle is never
Detected: the engine cannot tell a loop that repeats a release from a
guard that runs it once (#275).

`--verbose` prints each verdict as `[DR-VERDICT] file:line:column
function effect verdict`, and the work of the extra solve as
`[DR-COST]`. Diagnostics do not change.
…lease

The DoubleRelease counters still count and deduplicate as before. At a
release with a verdict, the verdict decides what they emit there:
Checked emits nothing, Detected emits one DoubleRelease (their own text
when they had one), and Unknown emits what they emitted before. A
Detected release for which the counters keep no local handle is
reported by the engine alone.

Releases on mutually exclusive paths, after a reset to NULL, or of
another allocation held by an alias no longer give a DoubleRelease or
IncompleteInterproc warning. A second release through an alias, also
one passed to a release wrapper, and a release of a pointer whose
possible allocations were all released before, are now potential
double releases. The "released without a matching acquire" check stays
as the fallback where the engine has no verdict.

Part of #104.
… engine

Eight fixtures with a strict count pin double releases through aliases,
several possible allocations, wrappers, unknown calls, loops and C++
catches, and their absence on exclusive paths, after a reset to NULL
and for two releases of distinct allocations in one call.

Two fixtures only pin the absence of a double release, without a
strict count. In double-release-exclusive-aliases.c, x and y may each
hold a or b but never the same one; the fallback warnings it still
gets are tracked in #277. In double-release-delete-reset.cpp, deleting
a pointer reset to nullptr deletes nothing; its false ReferenceLost is
tracked in #282.

check_double_release_cross_tu runs wrappers defined in another file in
memory, over three runs sharing a summary cache, with Z3, and then the
caller alone, where the wrappers are unknown.
…ixtures

release-all-paths-no-leak.c, cpp-exception-paths.cpp and
alias-keeps-old-resource-no-leak.c opted out of the strict count
because of a false DoubleRelease or IncompleteInterproc warning. Both
are gone: the files get their strict count back, with negative
assertions on the warnings they used to get.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants