Repository navigation
fix(ownership): reset the caller's slot when a callee releases *pp and resets it - #289
Draft
SizzleUnrlsd wants to merge 2 commits into
Draft
SizzleUnrlsd wants to merge 2 commits into
SizzleUnrlsd wants to merge 2 commits into
Conversation
…d resets it A callee that releases the pointer it receives by address and then resets it to NULL was summarised only as releasing *pp. At the call site the caller's variable kept naming the released resource, so a later release or a guarded use was reported (#278). The ownership summary now lists, per exit kind, the pointees the function rewrites on every exit, and with what: null, a value the analysis does not follow, or either. It is read off one more solve in which the function's writes to its pointees are strong updates: a pointee is one cell of the caller's memory, named by an argument and a constant offset. That solve runs only when the function writes one of its pointees strongly. A call rewrites the caller's slot with it after the transformers, as a strong update, like an out-param the callee certainly wrote. On an unwind edge without an exceptional summary, the rewrite is only added. The released resource keeps its state: another copy of the pointer still names it, and releasing or using that copy is still reported. The summary format changes, so the schema and the key of the summary cache move to v7: summaries cached by an earlier version are not read.
A new strict fixture covers #278. After a callee that releases *pp and then resets it, releasing or using the caller's variable is not reported: a second call, a free, a use under a null test, a reset to 0, a reset through a wrapper, and a struct field. The released object stays released for every other copy of the pointer: freeing or using the copy the callee did not reset is still reported, and so are the double release and the use after release when the callee does not reset *pp. A callee that resets *pp without releasing it leaks the only pointer, and that report is expected too. The incomplete inter-procedural warnings on the reset copies are unchanged. The strict count admits no other report. check_release_and_null_cross_tu runs the same kinds of calls with the callees in another file, in memory and over three runs sharing a summary cache. The third run must read every summary back from the cache (0 module analyses in the --timing report), so the new summary field is checked after a round trip through the cache files. check_ownership_wrapper_metadata expects the cache schema resource-summary-cache-v7. A unit test of the ownership engine covers the new summary field and its use at a call: what the callee leaves in *pp on every exit, and, in the caller, the reset of the variable while another copy keeps the released resource.
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.
Fixes #278.
Problem
A callee that releases the pointer it receives by address and then resets it to
NULLwassummarised only as "releases
*pp". At the call site, the caller's variable kept naming thereleased resource, so a later release or a guarded use was reported:
The issue's other two functions, a second
release_and_null(&p)andif (p) p[0] = 1;, werereported the same way. With the callee in another file, the second call and the
freewerereported too.
Cause
The ownership summary of a function says what happens to the resources its arguments point to
(
pointeeParamsfor*pp), not what*ppcontains when the function returns. In the callee,*ppis not strongly updated either: after*pp = NULL, the function's own state still lets*pphold the object it was passed. In the caller,phas its address taken, so its own writesare weak too. The engine's verdict on the later release was
Unknown, and the release countersdecided: two releases for one acquisition, so a double release.
Change
OwnershipEngine.cpp,computeSummary): a new field,ExitTransformer::overwrittenPointees, lists, per exit kind, the pointees the functionrewrites on every exit, and with what: null, a value the analysis does not follow, or either.
updates. A pointee is one cell of the caller's memory, named by an argument and a constant
offset; a pointee reached through an intermediate pointer (
viaPointerSlot) is left out,since it may name another cell.
existing solves do not change, nor do the transformers they produce.
applyEvent): after the transformers, the caller's slot is replaced with that value,as a strong update, like an out-param the callee certainly wrote.
another copy of the pointer still names it, and releasing or using that copy is still
reported.
ResourceFactCollector.cpp): the field is mapped to the caller's slots with thesame resolution as
pointeeParamsandoutArgs. An incomplete summary contributes nothing.AnalyzerApp.cpp): the field is written, and required when a file is read.The schema moves to
resource-summary-cache-v7and the key tocross-tu-resource-summary-v7,so a file written by an earlier version is neither looked up nor accepted. Cross-TU runs write
this cache by default (
.cache/resource-lifetime), so this matters without any option.sameSummarycompares the new field, so the summary fixpoint sees it change.The second commit adds the tests. It changes only
run_test.pyand files undertest/.Evidence
Everything in this section ran locally, on one macOS arm64 machine (Apple clang 17, LLVM 20.1.2),
with a Release build in the CI's configuration. "Before" is
588e800, the base of this branch."After" is the fix commit
141b12e. The test commit is2fa5519.Inputs written for the issue
Seven small input files were each analyzed with both binaries and the generic resource model:
0, a reset through a wrapper, a struct field*ppwithout resetting itq; the originalpis then freed or usedpp;q, copied frompbefore the call, is then freed or usedqchar** pp = &p*pp = other_buffer()*pp = NULLwithout a release, thenfree(p)delete p; p = nullptr;in a callee takingint*&With
--verbose, the engine's verdict on the release that follows the reset goes fromUnknownto
Checked(the slot holds only null). Releases through another copy are judged as before.Across files and through the summary cache
With the callees in one file and the callers in another (
--resource-cross-tu), the two falsepositives of the issue are gone, and the callee without a reset and the copy are still reported.
Over a summary cache directory shared by both binaries:
588e800writes 4 files with the v6 schema;0 module analysesin the--timingreport), with the same diagnostics.
Fixtures
Every fixture under
test/in2fa5519(526.c/.cc/.cpp/.llfiles, 90 of them with aresource model) was analyzed one at a time with both binaries, with the models and stack limit
its directives name. Exit codes and diagnostics are identical on all of them but one: the 523
fixtures
588e800already had are unchanged, so none of them covered this pattern. Three sourcesunder
test/unit/produce no report with either binary, because they do not compile on their own.The one file that differs is the new fixture
release-and-null-out-param.c: 12 diagnosticsbefore, 7 after. Its six false positives are gone, and the leak of the lost pointer appears. Over
all fixtures,
ResourceLifetime.DoubleReleasegoes from 46 to 41,UseAfterReleasefrom 28 to 27and
MissingReleasefrom 25 to 26. The other resource lifetime rules are unchanged.Both unit test binaries pass on
141b12e:stack_usage_analyzer_unit_testswith 396 checks andownership_engine_unit_testswith 155. They pass again on2fa5519, with 396 and 166 checks.The new tests
test/resource-lifetime/release-and-null-out-param.c:0, a wrapper,a struct field).
*pp, and when the other copy of the pointer is freed or used.explicitly.
588e800reports them too.check_release_and_null_cross_tu, inrun_test.py, withcross-tu-release-and-null-def.candcross-tu-release-and-null-use.c:three times sharing a summary cache, and expects exactly the two double releases that stay.
module analyses from the
--timingreport and fails unless it is 0 there, and above 0 onthe first run. The new field is therefore checked after a round trip through the cache files.
588e800too. Its IR comes from thecompile cache, and the module hash in the summary key then differs from the one of a fresh
compile. This PR does not change that.
check_ownership_wrapper_metadata: it expectsresource-summary-cache-v7, the one-linechange the v5 to v6 move also made (
3f26bff).testPointeeOverwritesinownership_engine_unit_tests.cpp(11 checks):reached through an intermediate pointer, a reset on one path only;
pis reset whileqstill names the released object, sofree(p)is judgedCheckedandfree(q)Detected; a call that may stop before the reset only adds null.The tests, as committed in
2fa5519, ran with each binary. The fixture ran throughrun_test.py's owncheck_file, without and with the CI's--compile-ir-cache-dir, with the sameresult in both modes:
release-and-null-out-param.ccheck_release_and_null_cross_tucheck_ownership_wrapper_metadataownership_engine_unit_tests588e800's engine, this branch's headers)pointer and the strict count, in the default and Z3 passes. The cross-TU check fails exactly on
twice,freedandchecked.the variable with the release): the fixture fails (22/28, without the double release through
originaland the use after release throughsource), the cross-TU check fails, and the unittest fails 3 checks.
each run rebuilds the summaries, with the right diagnostics. The cross-TU check fails on its
cache condition alone (4 module analyses on the third run). Without that condition, it passes.
After each mutant, the source was restored and the rebuilt binary is identical to the fix binary.
Full suite
The full suite ran with the CI's configuration:
run_test.py --jobs=8 --analyzer-timeout=120, where--jobsismin(8, CPUs)as in the CI;CORETRACE_RUN_TEST_EXTRA_ANALYZER_ARGS=--compile-ir-cache-dir=.cache/compile-ir;On
2fa5519, all 2945 tests passed (2945/2945) in 49 s. The same run on the fix commit alone,141b12e, gave 2915/2916. Its one failure wascheck_ownership_wrapper_metadataexpecting thev6 schema, which the test commit updates.
Limits
deletein a callee: a callee that runsdelete ptestspagainst null first, andits summary keeps the object owned on that edge. This already gives a leak warning in the
caller on
588e800, for a callee taking the pointer by value or by reference, with no resetinvolved. In
destroy_then_delete(the callee runsdelete p; p = nullptr;, then the callerruns
delete p), the caller's seconddeletecounted as a release and hid that warning. Nowpis null after the call, and the warning appears instead of the two double releases. Thatis a separate issue, not addressed here.
nothing, or an unknown value. The release counters decide as before, and the double release is
still reported.
char** pp = &p: the incomplete inter-procedural warning stays.q = p; release_and_null(&p); free(q);): therelease of
qis still reported as on588e800, but as an incomplete inter-proceduralwarning rather than a double release, because
qinherits the unknown value ofp.strongly. Not measured beyond totals: analyzing the 526 fixtures with their models took 30 s
with each binary, a single run each, so the figures carry unquantified noise. No speed change
is claimed.
only, not on external code bases.
GCC 13.3, AppleClang 16 and AppleClang 21 were covered by the CI only (see the CI results
below).
Interaction with #286, #287 and #288
This PR shares
run_test.pywith #286 andsrc/analysis/ownership/ResourceFactCollector.cppwith #288. Git merges
2fa5519with each of their heads (fdaca3c,1914585and6d0affd)without a conflict. These combinations have not been built or tested.
CI results
The CI ran on
2fa5519(Build run).All 14 checks pass. "Attach binaries to GitHub Release" is skipped, because it only runs for
tags.
run_test.pypasses 2945/2945 on each build job. On each of them,release-and-null-out-param.cpasses its 28 checks, andcheck_release_and_null_cross_tuandcheck_ownership_wrapper_metadatapass:run_test.pyjobsrun_test.pyUnit tests: both unit test binaries pass on each build job, with 396 and 166 checks. The
166 include the 11 new ones.
Self-analysis (ubuntu-24.04,
--fail-on error): passes, with no error on its 55 files, andthe SARIF upload succeeds.
Code scanning: no new finding. The PR's merge ref and
main(588e800) both have 65 openalerts, and they are the same except for one renumbering:
UninitializedLocalReadwarning ontransformerindecodeExitTransformer(
AnalyzerApp.cpp) is open onmainas GlobalReadBeforeWrite: static globals with the same name in two files share one cross-TU entry #166. On the merge ref, it is reported as #298.out.pointeeParams[path] = transformer;. It is 12 lines lower because this PR adds lines tothe encoder above it.
The other alerts in the files this PR changes were already open on
main. Some of them moveddown with the inserted lines.
Other workflows: these all pass:
format-check,license-complianceandCheck conventional commits;Concurrency analysis;Test Consumer Project,Test Docker ImageandTest GitHub Action.