Repository navigation
fix(resource-lifetime): decide DoubleRelease with the ownership engine - #283
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ResourceLifetime.DoubleReleaseno longer comes from counters alone. The ownership engine,which reports
UseAfterReleasesince #270, judges every release from its stabilised states, andits 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):
if/elseor aswitch, after an earlyreturn, at agotocleanup label, or in a
catchthat returns.cpp-exception-paths.cppandrelease-all-paths-no-leak.chad opted out of the strict count because of it;release(h); h = NULL; release(h);, acceptance case 1 ofDoubleRelease and UseAfterRelease: derive them from the ownership engine #104), also through a wrapper, and a null pointer freed in a function that allocates nothing;
(
alias-keeps-old-resource-no-leak.c).Now reported as potential double releases, where only the "incomplete" warning, or nothing, was
reported:
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;
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 onevery 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
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.
release, Checked emits nothing, Detected one
DoubleRelease(their own text when they hadone), and Unknown what they emitted before. The engine reports a Detected release alone where
the counters keep no local handle for it.
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);) andrelease_one(p, p, c)stay Unknown.(DoubleRelease: a release repeated by a loop is not reported #275).
not depend on an unrelated allocation.
--verboseprints each verdict ([DR-VERDICT] file:line:column function effect verdict) andthe work of the extra solve per function (
[DR-COST]).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 arelease 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,MissingReleaseandReferenceLostkeep the collector's facts. This is atransition until untracked values get an explicit representation for every rule (#281).
Measured cost:
with many releases (not in the repository), 2.07 times the engine's solving;
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): main1fadba2against 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:
summary cache still recomputes some rounds.
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 %.
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.
with a filled cache, and 1.40 GB against 1.43 GB while compiling them.
Limits kept
address leaves the function (Ownership engine: a NULL reset joined with a path where the resource is still live #276).
release_one(p, p, c), and the"released without a matching acquire" check, kept as a fallback (DoubleRelease fallback counters: successive values and exclusive releases are conflated #277).
*ppto NULL (Ownership summaries: a callee that releases *pp and then resets it to NULL #278).delete p; p = nullptr;in C++ (Resource lifetime: false leak warnings after delete p; p = nullptr; in C++ #282).double-release-delete-reset.cpppins only the absenceof a double release there.
Tests
release_both/release_one, copies of one release, an incomplete solve), the resource averdict names, and four cases for
withUnknownAtEntry.release (exclusive aliases, delete-reset).
check_double_release_cross_turuns wrappers fromanother file in memory, over three runs sharing a summary cache, with Z3, and the caller alone.
accusation inside a loop, verdicts read off the collector's facts, judged releases left out of
the counts, one verdict per instruction.
[DR-VERDICT]trace was compared with a verdict predicted per release on thefixtures and on extra probe files: no mismatch.
Verification
run_test.py --jobs=4on Linux (Dockerct217-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.