Repository navigation
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
Draft
SizzleUnrlsd wants to merge 2 commits into
SizzleUnrlsd wants to merge 2 commits into
Conversation
… 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.
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 #282.
Problem
Writing a variable again after deleting it produced false leak warnings in C++:
clang tests the pointer against null before
delete p. The ownership facts drop the edge wherethat 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 resolvesthe reload through its MemorySSA clobber, from
FunctionFacts::clobberingAccess, astryGetConstFromValuedoes 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.
q = palso goes through a slot, so the lookup follows the chain of reloads, upto 16 levels.
mallocor ofnew (std::nothrow).FunctionFacts(BasicAA and MemorySSA) is built only at the first null test of a reload in afunction. Facts are collected sequentially within a module, and each module has its own
LLVMContext, so this adds no state shared between threads.collectOwnershipFactsdoes 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:
delete_oncehas nonedelete[], a class with a destructor, a loop, a null test written by hand, a copyalias = ownermalloc,new (std::nothrow))malloc: one true leak;nothrow: one true leak, plus 3 warnings that come from the model (see Limits)newover a pointer still owned ("is overwritten");deleteon one branch only, then an overwrite ("may be overwritten");nullptrwithout adelete;deleteof a pointer already reset tonullptr.malloc: when the first of two allocations returns NULL, an early return leaks the secondone. That true leak exists only on the null edge of
malloc, and it is still reported.newand a null testwritten by hand.
reported as "may leave the function without being released", which is the true leak when the
second
newthrows.-fno-exceptions, the fix reports nothing on that input, where588e800reported twowarnings.
Fixtures
Every fixture under
test/(523.c/.cc/.cpp/.llfiles, 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 falsewarning "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.MissingReleasegoes from 25 to 24. The other resource lifetime rulesare unchanged.
Both
stack_usage_analyzer_unit_testsandownership_engine_unit_testspass.The new tests
test/resource-lifetime/new-delete-reassigned-variable.cpp:and a null test written by hand on the result of
new.588e800also reported in the functionswith a true leak:
lostand ofmaybe;reused, which is deleted before the variable is reused.test/resource-lifetime/malloc-null-check-reassigned-variable.c:mallocreturns NULL, and nowarning on the first allocation, nor on
if (p) free(p); p = NULL;.malloc. A mutant thatdrops 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 andstrict-diagnostic-count: falseare removed, and a negative expectation on the leak ofresetpis added.The three fixtures ran through
run_test.py's owncheck_file, with each binary. Each ran withoutand with the CI's
--compile-ir-cache-dir, and the result was the same in both modes:new-delete-reassigned-variable.cppmalloc-null-check-reassigned-variable.cdouble-release-delete-reset.cpp, updateddouble-release-delete-reset.cpp, as onmainBefore 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--jobsismin(8, CPUs)as in the CI;CORETRACE_RUN_TEST_EXTRA_ANALYZER_ARGS=--compile-ir-cache-dir=.cache/compile-ir;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 ofoperator newunconditionally, although the
nothrowform may returnnullptr. Leak warnings on its nullpath 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.
is already reported, as with the exception above.
only, not on external code bases.
the other compilers are covered by the CI only.
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.