From 44ee999bba0c3ddb311f1e96734e43c1cb37380a Mon Sep 17 00:00:00 2001 From: Hugo Date: Wed, 18 Nov 2026 09:48:46 +0100 Subject: [PATCH 1/2] fix(ownership): drop the null edge of delete p when p is written more 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. --- .../ownership/ResourceFactCollector.cpp | 57 +++++++++++++------ 1 file changed, 39 insertions(+), 18 deletions(-) diff --git a/src/analysis/ownership/ResourceFactCollector.cpp b/src/analysis/ownership/ResourceFactCollector.cpp index 3fa7bf5..6614e85 100644 --- a/src/analysis/ownership/ResourceFactCollector.cpp +++ b/src/analysis/ownership/ResourceFactCollector.cpp @@ -3,10 +3,12 @@ #include "../ResourceLifetimeInternal.hpp" #include "analysis/AnalyzerUtils.hpp" +#include "analysis/FunctionFacts.hpp" #include "analysis/IRValueUtils.hpp" #include #include +#include #include #include #include @@ -32,6 +34,10 @@ namespace ctrace::stack::analysis::ownership using lifetime_detail::StorageKey; using lifetime_detail::StorageScope; + /// Bounds the walk through reloads; a -O0 copy chain such as `q = p` needs one level + /// per copy. + constexpr unsigned kMaxReloadDepth = 16; + class Collector { public: @@ -115,26 +121,17 @@ namespace ctrace::stack::analysis::ownership if (!llvm::isa(other)) return nullptr; } - // -O0 reloads the pointer from its slot before the test. + // -O0 reloads the pointer from its slot before the test, and a copy such as + // `q = p` goes through slots too: follow each reload to the value written by the + // store it reads, whatever other stores write the slot elsewhere. const llvm::Value* origin = ptr; - if (const auto* load = llvm::dyn_cast(ptr)) + for (unsigned depth = 0; depth < kMaxReloadDepth; ++depth) { - if (const auto* slot = llvm::dyn_cast( - load->getPointerOperand()->stripPointerCasts())) - { - const llvm::StoreInst* unique = nullptr; - for (const llvm::User* u : slot->users()) - { - if (const auto* st = llvm::dyn_cast(u)) - { - if (unique) - return nullptr; - unique = st; - } - } - if (unique) - origin = unique->getValueOperand(); - } + const auto* load = llvm::dyn_cast(origin); + const llvm::StoreInst* store = load ? storeReadBy(*load) : nullptr; + if (!store) + break; + origin = store->getValueOperand(); } if (!llvm::isKnownNonZero(origin, DL_)) return nullptr; @@ -142,6 +139,29 @@ namespace ctrace::stack::analysis::ownership return br->getSuccessor(icmp->getPredicate() == llvm::CmpInst::ICMP_EQ ? 0 : 1); } + /// The store whose value `load` reads: its MemorySSA clobber, when that clobber + /// stores the loaded type to the loaded pointer. Null for any other clobber, such + /// as a merge of stores from several paths, a call or the function entry. + const llvm::StoreInst* storeReadBy(const llvm::LoadInst& load) + { + if (!load.isSimple()) + return nullptr; + if (!facts_) + facts_.emplace(const_cast(F_)); + const auto* def = + llvm::dyn_cast_or_null(facts_->clobberingAccess(load)); + const auto* store = + def ? llvm::dyn_cast_or_null(def->getMemoryInst()) : nullptr; + if (!store || !store->isSimple() || + store->getPointerOperand()->stripPointerCasts() != + load.getPointerOperand()->stripPointerCasts() || + store->getValueOperand()->getType() != load.getType()) + { + return nullptr; + } + return store; + } + /// Several source `return`s are usually merged into one `ret` block that only /// reloads a return slot. Its events are moved onto every incoming edge so each /// path is judged with its own state instead of the join (spec ยง6: the @@ -1181,6 +1201,7 @@ namespace ctrace::stack::analysis::ownership llvm::DenseMap valueIds_; llvm::DenseMap loadedFrom_; // the slot read std::map exceptionalSummaryEvents_; + std::optional facts_; // built on the first null test of a reload std::optional unknownTarget_; LocationId returnLocation_ = 0; bool mayUnwind_ = false; From 6d0affdb4f1bbef2ebcd2a882b8fa6f5c637efc2 Mon Sep 17 00:00:00 2001 From: Hugo Date: Wed, 18 Nov 2026 21:11:22 +0100 Subject: [PATCH 2/2] test(ownership): variables written again after delete, and leaks after 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. --- .../double-release-delete-reset.cpp | 7 +- .../malloc-null-check-reassigned-variable.c | 30 +++++ .../new-delete-reassigned-variable.cpp | 112 ++++++++++++++++++ 3 files changed, 144 insertions(+), 5 deletions(-) create mode 100644 test/resource-lifetime/malloc-null-check-reassigned-variable.c create mode 100644 test/resource-lifetime/new-delete-reassigned-variable.cpp diff --git a/test/resource-lifetime/double-release-delete-reset.cpp b/test/resource-lifetime/double-release-delete-reset.cpp index 000a16e..84c0ac2 100644 --- a/test/resource-lifetime/double-release-delete-reset.cpp +++ b/test/resource-lifetime/double-release-delete-reset.cpp @@ -1,10 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 // resource-model: models/resource-lifetime/generic.txt -// Deleting a pointer reset to nullptr deletes nothing: no double release. The function also gets -// a false resource-leak warning ("may be overwritten while still owned"): the null check clang -// emits around `delete` is taken as feasible once the variable is written twice. That warning is -// tracked in #282 and deliberately not pinned here, hence no strict count. -// strict-diagnostic-count: false +// Deleting a pointer reset to nullptr deletes nothing: no double release, and no leak (#282). void delete_reset_delete() { @@ -15,3 +11,4 @@ void delete_reset_delete() } // not contains: potential double release: 'CppHeap' handle 'resetp' +// not contains: potential resource leak: 'CppHeap' acquired in handle 'resetp' diff --git a/test/resource-lifetime/malloc-null-check-reassigned-variable.c b/test/resource-lifetime/malloc-null-check-reassigned-variable.c new file mode 100644 index 0000000..aa19c88 --- /dev/null +++ b/test/resource-lifetime/malloc-null-check-reassigned-variable.c @@ -0,0 +1,30 @@ +// SPDX-License-Identifier: Apache-2.0 +// resource-model: models/resource-lifetime/generic.txt +// #282: malloc may return NULL, so a null test of its result keeps both edges, also when the +// variable is written again. +#include + +void checked_free_then_reset(void) +{ + int* checked = malloc(sizeof *checked); + if (checked) + free(checked); + checked = NULL; +} + +void early_return_then_reset(void) +{ + int* first = malloc(sizeof *first); + int* second = malloc(sizeof *second); + if (first == NULL) + return; + free(first); + first = NULL; + free(second); +} + +// not contains: potential resource leak: 'HeapAlloc' acquired in handle 'checked' +// not contains: potential resource leak: 'HeapAlloc' acquired in handle 'first' + +// at line 18, column 19 +// [ !!Warn ] potential resource leak: 'HeapAlloc' acquired in handle 'second' may leave the function without being released diff --git a/test/resource-lifetime/new-delete-reassigned-variable.cpp b/test/resource-lifetime/new-delete-reassigned-variable.cpp new file mode 100644 index 0000000..859ebe8 --- /dev/null +++ b/test/resource-lifetime/new-delete-reassigned-variable.cpp @@ -0,0 +1,112 @@ +// SPDX-License-Identifier: Apache-2.0 +// resource-model: models/resource-lifetime/generic.txt +// #282: clang tests the pointer against null before `delete p`. When `p` holds the result of a +// throwing new, the null edge is never taken, also when the variable is written again, as in +// `delete p; p = nullptr;`. A leak after an overwrite stays reported. + +void delete_once() +{ + int* once = new int(1); + delete once; +} + +void delete_then_reset() +{ + int* reset = new int(1); + delete reset; + reset = nullptr; +} + +void delete_reset_delete() +{ + int* again = new int(1); + delete again; + again = nullptr; + delete again; +} + +void delete_then_reuse_slot() +{ + int* slot = new int(1); + delete slot; + slot = new int(2); + delete slot; +} + +void delete_copy_then_reset() +{ + int* owner = new int(1); + int* alias = owner; + delete alias; + alias = nullptr; +} + +void user_check_then_reset() +{ + int* checked = new int(1); + if (checked == nullptr) + return; + delete checked; + checked = nullptr; +} + +void overwrite_without_delete() +{ + int* lost = new int(1); + lost = new int(2); + delete lost; +} + +void delete_on_one_branch(bool release) +{ + int* maybe = new int(1); + if (release) + delete maybe; + maybe = new int(2); + delete maybe; +} + +void reuse_without_second_delete() +{ + int* reused = new int(1); + delete reused; + reused = new int(2); +} + +void reset_without_delete() +{ + int* dropped = new int(1); + dropped = nullptr; +} + +void reset_then_delete() +{ + int* late = new int(1); + late = nullptr; + delete late; +} + +// not contains: potential resource leak: 'CppHeap' acquired in handle 'once' +// not contains: potential resource leak: 'CppHeap' acquired in handle 'reset' +// not contains: potential resource leak: 'CppHeap' acquired in handle 'again' +// not contains: potential resource leak: 'CppHeap' acquired in handle 'slot' +// not contains: potential resource leak: 'CppHeap' acquired in handle 'owner' +// not contains: potential resource leak: 'CppHeap' acquired in handle 'checked' +// not contains: potential resource leak: 'CppHeap' acquired in handle 'lost' may leave the function without being released +// not contains: potential resource leak: 'CppHeap' acquired in handle 'maybe' may leave the function without being released +// not contains: potential resource leak: 'CppHeap' acquired in handle 'reused' may be overwritten while still owned + +// at line 55, column 17 +// [ !!Warn ] potential resource leak: 'CppHeap' acquired in handle 'lost' is overwritten while still owned + +// at line 62, column 18 +// [ !!Warn ] potential resource leak: 'CppHeap' acquired in handle 'maybe' may be overwritten while still owned + +// at line 73, column 14 +// [ !!Warn ] potential resource leak: 'CppHeap' acquired in handle 'reused' is not released in this function + +// at line 78, column 20 +// [ !!Warn ] potential resource leak: 'CppHeap' acquired in handle 'dropped' is overwritten while still owned + +// at line 84, column 17 +// [ !!Warn ] potential resource leak: 'CppHeap' acquired in handle 'late' is overwritten while still owned