Skip to content

fix(ownership): drop the null edge of delete p when p is written more than once - #288

Draft
SizzleUnrlsd wants to merge 2 commits into
mainfrom
282-delete-reset-null-edge
Draft

SizzleUnrlsd wants to merge 2 commits into
mainfrom
282-delete-reset-null-edge

Conversation

@SizzleUnrlsd

Copy link
Copy Markdown
Contributor

Fixes #282.

Problem

Writing a variable again after deleting it produced false leak warnings in C++:

void delete_then_reset()
{
    int* reset = new int(1);
    delete reset;
    reset = nullptr; // the allocation was reported as possibly leaked
}

clang tests the pointer against null before delete p. The ownership facts drop the edge where
that test is true when LLVM proves the pointer non-null, which it does for the result of a
throwing new. In the analyzed IR, the tested value is reloaded from the variable's stack slot,
and the lookup of the value stored there required the slot to have a single store in the whole
function. After delete p; p = nullptr;, or when the variable is reused for a second allocation,
the slot has two stores. The null edge stayed, and the leak check reported the allocation as not
released on it.

Change

infeasibleNullCheckSuccessor (src/analysis/ownership/ResourceFactCollector.cpp) now resolves
the reload through its MemorySSA clobber, from FunctionFacts::clobberingAccess, as
tryGetConstFromValue does since #262. That clobber is the store whose value the load reads,
whatever other stores write the slot elsewhere. Only a simple store of the loaded type to the same
pointer is accepted. A merge of stores from several paths, a call or the function entry gives
nothing, and the null edge stays.

  • A copy such as q = p also goes through a slot, so the lookup follows the chain of reloads, up
    to 16 levels.
  • A value LLVM cannot prove non-null keeps both edges, such as the result of malloc or of
    new (std::nothrow).
  • FunctionFacts (BasicAA and MemorySSA) is built only at the first null test of a reload in a
    function. Facts are collected sequentially within a module, and each module has its own
    LLVMContext, so this adds no state shared between threads.
  • The signature of collectOwnershipFacts does not change.

There is no name heuristic and no size cutoff.

The second commit adds two fixtures. It also restores the strict count of
double-release-delete-reset.cpp, which waived it because of this issue.

Evidence

Everything below ran 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.

Inputs written for the issue

21 small inputs were each analyzed with both binaries and the generic resource model:

Group Inputs Before After
The issue's four functions 4 4 false leak warnings on 3 functions; delete_once has none none
Variants: delete[], a class with a destructor, a loop, a null test written by hand, a copy alias = owner 5 4 false leak warnings; the class with a destructor has none none
True leaks, after an overwrite or through an exception 7 each true leak, plus 4 false warnings of the same kind each true leak, at the same line; same text except one site, see below
Allocations that may return null (malloc, new (std::nothrow)) 5 malloc: one true leak; nothrow: one true leak, plus 3 warnings that come from the model (see Limits) identical to before
  • True leaks kept:
    • a new over a pointer still owned ("is overwritten");
    • a delete on one branch only, then an overwrite ("may be overwritten");
    • a second allocation never released ("is not released");
    • a reset to nullptr without a delete;
    • a delete of a pointer already reset to nullptr.
  • malloc: when the first of two allocations returns NULL, an early return leaks the second
    one. That true leak exists only on the null edge of malloc, and it is still reported.
  • One site changes its reason, not its status:
    • The engine keeps one reason per acquisition site. The input has two new and a null test
      written by hand.
    • The first allocation was reported as "may be overwritten while still owned". It is now
      reported as "may leave the function without being released", which is the true leak when the
      second new throws.
    • With -fno-exceptions, the fix reports nothing on that input, where 588e800 reported two
      warnings.

Fixtures

Every fixture under test/ (523 .c/.cc/.cpp/.ll files, 89 of them with a resource model)
was analyzed one at a time with both binaries. Each got the models and stack limit its directives
name. Exit codes and diagnostics are identical on 522 of them. 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 test/resource-lifetime/double-release-delete-reset.cpp. Its false
warning "potential resource leak: 'CppHeap' acquired in handle 'resetp' may be overwritten while
still owned" is gone, and that is the warning its comment attributed to this issue. Over all
fixtures, ResourceLifetime.MissingRelease goes from 25 to 24. The other resource lifetime rules
are unchanged.

Both stack_usage_analyzer_unit_tests and ownership_engine_unit_tests pass.

The new tests

  • New fixture test/resource-lifetime/new-delete-reassigned-variable.cpp:
    • Six functions must have no leak warning: the issue's four, a copy deleted through the alias,
      and a null test written by hand on the result of new.
    • Five true leaks after an overwrite must stay reported, with their text and position.
    • Three negative expectations pin false warnings that 588e800 also reported in the functions
      with a true leak:
      • on the second allocation of lost and of maybe;
      • on the first allocation of reused, which is deleted before the variable is reused.
    • The strict diagnostic count, on by default, admits no other report.
  • New fixture test/resource-lifetime/malloc-null-check-reassigned-variable.c:
    • It expects the early-return leak of the second allocation when malloc returns NULL, and no
      warning on the first allocation, nor on if (p) free(p); p = NULL;.
    • It passes with both binaries, because it guards the null edge of malloc. A mutant that
      drops every null edge, without any proof that the pointer is non-null, fails it (4/8).
  • double-release-delete-reset.cpp: the comment about this issue and
    strict-diagnostic-count: false are removed, and a negative expectation on the leak of
    resetp is added.

The three fixtures ran through run_test.py's own check_file, with each binary. Each ran without
and with the CI's --compile-ir-cache-dir, and the result was the same in both modes:

Fixture Before the fix After the fix
new-delete-reassigned-variable.cpp fails (12/30) passes (30/30)
malloc-null-check-reassigned-variable.c passes (8/8) passes (8/8)
double-release-delete-reset.cpp, updated fails (2/6) passes (6/6)
double-release-delete-reset.cpp, as on main passes (2/2) passes (2/2)

Before the fix, the C++ fixture fails exactly on eight negative expectations and on the strict
count (14 warnings instead of 5), in both passes. Both binaries report the five true leaks.

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.

All 2958 tests passed (2958/2958) in 48 s. The same run on the fix commit alone gave 2916/2916.

Limits

  • new (std::nothrow): the generic model acquires the result of operator new
    unconditionally, although the nothrow form may return nullptr. Leak warnings on its null
    path remain, already with a single store to the variable. This PR does not change them: that is
    a model issue, separate from Resource lifetime: false leak warnings after delete p; p = nullptr; in C++ #282.
  • One reason per site: removing a false reason can bring out the true reason of a site that
    is already reported, as with the exception above.
  • Coverage: diagnostics were compared on the repository's fixtures and the 21 inputs above
    only, not on external code bases.
  • Platforms and compilers: everything above ran on macOS arm64 with Apple clang 17. Linux and
    the other compilers are covered by the CI only.
  • Cost: not measured beyond totals. Analyzing the 523 fixtures took 31 s before and 30 s
    after, a single run each, so the figures carry unquantified noise. No speed change is claimed.

Interaction with #286 and #287

This PR shares no file with #286 or #287, and git merges it with each of them without a
conflict. These combinations have not been built or tested.

CI

Pending when this description was written.

… than once

clang tests the pointer against null before `delete p`. The ownership
facts drop that edge when LLVM proves the pointer non-null, which it
does for the result of a throwing new. The tested value is reloaded from
the variable's slot, and the lookup of the value stored there required
the slot to have a single store. With `delete p; p = nullptr;`, or a
variable reused for a second allocation, the null edge stayed, and the
leak check reported the allocation as not released on it (#282).

The reload now resolves through its MemorySSA clobber, from
FunctionFacts: the store whose value it reads, whatever other stores
write the slot elsewhere. A copy such as `q = p` is followed the same
way. A value LLVM cannot prove non-null, such as the result of malloc or
of new (std::nothrow), keeps both edges.
…r overwrites

Two new fixtures cover #282. No leak is reported when the variable that
held the result of new is written again after its delete: reset to
nullptr, deleted again, reused for a second allocation, deleted through
a copy, or first tested against null by hand. A leak after an overwrite
stays reported: a new over a pointer still owned, a delete on one branch
only, a second allocation never released, a reset to nullptr without a
delete, and a delete after that reset. The strict count admits no other
report.

In C, a null test of the result of malloc keeps both edges: when the
first allocation returns NULL, the early return leaks the second one,
and that report stays.

double-release-delete-reset.cpp no longer waives the strict count: the
leak it tolerated was #282. A negative expectation pins its absence.
@SizzleUnrlsd SizzleUnrlsd added bug Something isn't working FP labels Nov 18, 2026
@SizzleUnrlsd SizzleUnrlsd self-assigned this Nov 18, 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.

Resource lifetime: false leak warnings after delete p; p = nullptr; in C++

1 participant