Skip to content

fix(ownership): reset the caller's slot when a callee releases *pp and resets it - #289

Draft
SizzleUnrlsd wants to merge 2 commits into
mainfrom
278-summary-reset-after-release
Draft

SizzleUnrlsd wants to merge 2 commits into
mainfrom
278-summary-reset-after-release

Conversation

@SizzleUnrlsd

@SizzleUnrlsd SizzleUnrlsd commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #278.

Problem

A callee that releases the pointer it receives by address and then resets it to NULL was
summarised only as "releases *pp". At the call site, the caller's variable kept naming the
released resource, so a later release or a guarded use was reported:

static void release_and_null(char** pp)
{
    free(*pp);
    *pp = NULL;
}

void then_free(void)
{
    char* p = malloc(8);
    release_and_null(&p);
    free(p); /* reported: potential double release */
}

The issue's other two functions, a second release_and_null(&p) and if (p) p[0] = 1;, were
reported the same way. With the callee in another file, the second call and the free were
reported too.

Cause

The ownership summary of a function says what happens to the resources its arguments point to
(pointeeParams for *pp), not what *pp contains when the function returns. In the callee,
*pp is not strongly updated either: after *pp = NULL, the function's own state still lets
*pp hold the object it was passed. In the caller, p has its address taken, so its own writes
are weak too. The engine's verdict on the later release was Unknown, and the release counters
decided: two releases for one acquisition, so a double release.

Change

  • Summary (OwnershipEngine.cpp, computeSummary): a new field,
    ExitTransformer::overwrittenPointees, 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; a pointee reached through an intermediate pointer (viaPointerSlot) is left out,
      since it may name another cell.
    • That solve runs only when the function writes one of its pointees strongly. The four
      existing solves do not change, nor do the transformers they produce.
  • Call (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.
    • Only the slot changes. The released resource keeps the state the transformers give it:
      another copy of the pointer still names it, and releasing or using that copy is still
      reported.
    • On an unwind edge without an exceptional summary, the value is only added.
  • Collector (ResourceFactCollector.cpp): the field is mapped to the caller's slots with the
    same resolution as pointeeParams and outArgs. An incomplete summary contributes nothing.
  • Summary cache (AnalyzerApp.cpp): the field is written, and required when a file is read.
    The schema moves to resource-summary-cache-v7 and the key to cross-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.
  • Equality: sameSummary compares the new field, so the summary fixpoint sees it change.

The second commit adds the tests. It changes only run_test.py and files under test/.

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 is 2fa5519.

Inputs written for the issue

Seven small input files were each analyzed with both binaries and the generic resource model:

Input Before After
The issue's three functions 2 double releases, 1 use after release none
Variants: a reset to 0, a reset through a wrapper, a struct field 3 double releases none
A callee that releases *pp without resetting it a double release and a use after release unchanged
The callee resets q; the original p is then freed or used a double release and a use after release on p unchanged
The callee resets p; q, copied from p before the call, is then freed or used an incomplete inter-procedural warning and a use after release on q unchanged
A call through a variable char** pp = &p an incomplete inter-procedural warning unchanged
A reset on one path only, or *pp = other_buffer() a double release each unchanged
*pp = NULL without a release, then free(p) nothing a leak warning, which is true: the only pointer is lost
C++: delete p; p = nullptr; in a callee taking int*& 2 double releases, 1 leak warning 2 leak warnings, see Limits

With --verbose, the engine's verdict on the release that follows the reset goes from Unknown
to 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 false
positives 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:

  • 588e800 writes 4 files with the v6 schema;
  • the fix does not read them: it writes 4 v7 files, with the same diagnostics as in memory;
  • a third run reads every summary back from the cache (0 module analyses in the --timing
    report), with the same diagnostics.

Fixtures

Every fixture under test/ in 2fa5519 (526 .c/.cc/.cpp/.ll files, 90 of them with a
resource 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 588e800 already had are unchanged, so none of them covered this pattern. Three sources
under 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 diagnostics
before, 7 after. Its six false positives are gone, and the leak of the lost pointer appears. Over
all fixtures, ResourceLifetime.DoubleRelease goes from 46 to 41, UseAfterRelease from 28 to 27
and MissingRelease from 25 to 26. The other resource lifetime rules are unchanged.

Both unit test binaries pass on 141b12e: stack_usage_analyzer_unit_tests with 396 checks and
ownership_engine_unit_tests with 155. They pass again on 2fa5519, with 396 and 166 checks.

The new tests

  • New fixture test/resource-lifetime/release-and-null-out-param.c:
    • No diagnostic on the issue's three functions or on the variants (a reset to 0, a wrapper,
      a struct field).
    • Still reported: the double release and the use after release when the callee does not reset
      *pp, and when the other copy of the pointer is freed or used.
    • The incomplete inter-procedural warnings on the copies the callee resets are expected
      explicitly. 588e800 reports them too.
    • The leak of a pointer that a callee resets without releasing it is expected.
    • The strict diagnostic count, on by default, admits no other report.
  • New check check_release_and_null_cross_tu, in run_test.py, with
    cross-tu-release-and-null-def.c and cross-tu-release-and-null-use.c:
    • It runs the same kinds of calls with the callees in the other file, once in memory, then
      three times sharing a summary cache, and expects exactly the two double releases that stay.
    • The third run must read every summary back from the cache: the check reads the number of
      module analyses from the --timing report and fails unless it is 0 there, and above 0 on
      the first run. The new field is therefore checked after a round trip through the cache files.
    • The second run may still rebuild the summaries, on 588e800 too. Its IR comes from the
      compile 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 expects resource-summary-cache-v7, the one-line
    change the v5 to v6 move also made (3f26bff).
  • New unit test testPointeeOverwrites in ownership_engine_unit_tests.cpp (11 checks):
    • the summary: a reset to null, an unknown value, a release alone, a weak write, a pointee
      reached through an intermediate pointer, a reset on one path only;
    • the call: p is reset while q still names the released object, so free(p) is judged
      Checked and free(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 through
run_test.py's own check_file, without and with the CI's --compile-ir-cache-dir, with the same
result in both modes:

Test Before the fix After the fix
release-and-null-out-param.c fails (12/28) passes (28/28)
check_release_and_null_cross_tu fails passes
check_ownership_wrapper_metadata fails passes
ownership_engine_unit_tests 4 failures (588e800's engine, this branch's headers) 166/166
  • Before the fix: the fixture fails on its 6 negative expectations, the leak of the lost
    pointer and the strict count, in the default and Z3 passes. The cross-TU check fails exactly on
    twice, freed and checked.
  • A mutant where the reset also makes every copy forget the object (confusing the reset of
    the variable with the release): the fixture fails (22/28, without the double release through
    original and the use after release through source), the cross-TU check fails, and the unit
    test fails 3 checks.
  • A mutant whose cache files lose the new field: every file is then rejected when read, and
    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 --jobs is min(8, CPUs) as in the CI;
  • CORETRACE_RUN_TEST_EXTRA_ANALYZER_ARGS=--compile-ir-cache-dir=.cache/compile-ir;
  • empty caches at the start.

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 was check_ownership_wrapper_metadata expecting the
v6 schema, which the test commit updates.

Limits

  • C++ delete in a callee: a callee that runs delete p tests p against null first, and
    its 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 reset
    involved. In destroy_then_delete (the callee runs delete p; p = nullptr;, then the caller
    runs delete p), the caller's second delete counted as a release and hid that warning. Now
    p is null after the call, and the warning appears instead of the two double releases. That
    is a separate issue, not addressed here.
  • A reset on one path only, or to a value the analysis does not follow: the summary records
    nothing, or an unknown value. The release counters decide as before, and the double release is
    still reported.
  • A call through a variable char** pp = &p: the incomplete inter-procedural warning stays.
  • A copy of the variable passed by address (q = p; release_and_null(&p); free(q);): the
    release of q is still reported as on 588e800, but as an incomplete inter-procedural
    warning rather than a double release, because q inherits the unknown value of p.
  • Cost: one more solve per summary, only for a function that writes one of its pointees
    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.
  • Coverage: diagnostics were compared on the repository's fixtures and the inputs above
    only, not on external code bases.
  • Platforms and compilers: everything above ran on macOS arm64 with Apple clang 17. Linux,
    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.py with #286 and src/analysis/ownership/ResourceFactCollector.cpp
with #288. Git merges 2fa5519 with each of their heads (fdaca3c, 1914585 and 6d0affd)
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.py passes 2945/2945 on each build job. On each of them,
release-and-null-out-param.c passes its 28 checks, and check_release_and_null_cross_tu and
check_ownership_wrapper_metadata pass:

Job Compiler run_test.py jobs run_test.py
ubuntu-24.04 GCC 13.3.0 4 2945/2945
macos-latest AppleClang 21.0.0 3 2945/2945
macos-15 AppleClang 16.0.0 3 2945/2945
  • Unit 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, and
    the SARIF upload succeeds.

  • Code scanning: no new finding. The PR's merge ref and main (588e800) both have 65 open
    alerts, and they are the same except for one renumbering:

    The other alerts in the files this PR changes were already open on main. Some of them moved
    down with the inserted lines.

  • Other workflows: these all pass:

    • format-check, license-compliance and Check conventional commits;
    • Concurrency analysis;
    • both Linux release binaries (amd64 and arm64);
    • Test Consumer Project, Test Docker Image and Test GitHub Action.

…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.
@SizzleUnrlsd SizzleUnrlsd added bug Something isn't working FP labels Nov 29, 2026
@SizzleUnrlsd SizzleUnrlsd self-assigned this Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working FP

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ownership summaries: a callee that releases *pp and then resets it to NULL

1 participant