https://github.com/geoffreygaren created https://github.com/llvm/llvm-project/pull/244754
Under ARC, RetainSummaryManager reports ns_returns_retained results and alloc/new/copy family results as not owned, because ARC balances them. This makes `f() ns_returns_retained; adoptNS(f());` look like a use-after-free when it's actually the intended behavior. Treat ns_returns_retained calls and methods, and alloc/new/copy/mutableCopy family messages as +1 in the ARC-mode adopt and RetainPtr constructor checks, matching the existing handling of alloc/init. >From 5d28f2eafbcd00035a00f3fbbe05cfe02c9cbe13 Mon Sep 17 00:00:00 2001 From: Geoff Garen <[email protected]> Date: Fri, 20 Nov 2026 13:35:46 -0800 Subject: [PATCH] [alpha.webkit.RetainPtrCtorAdoptChecker] Analysis gets ns_returns_retained wrong when ARC is enabled Under ARC, RetainSummaryManager reports ns_returns_retained results and alloc/new/copy family results as not owned, because ARC balances them. This makes `f() ns_returns_retained; adoptNS(f());` look like a use-after-free when it's actually the intended behavior. Treat ns_returns_retained calls and methods, and alloc/new/copy/mutableCopy family messages as +1 in the ARC-mode adopt and RetainPtr constructor checks, matching the existing handling of alloc/init. --- .../WebKit/RetainPtrCtorAdoptChecker.cpp | 40 ++++++++++++++++++- .../WebKit/retain-ptr-ctor-adopt-use-arc.mm | 26 ++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/RetainPtrCtorAdoptChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/RetainPtrCtorAdoptChecker.cpp index 861ea9a6d52d3e..3bf6d41ea75e45 100644 --- a/clang/lib/StaticAnalyzer/Checkers/WebKit/RetainPtrCtorAdoptChecker.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/RetainPtrCtorAdoptChecker.cpp @@ -182,7 +182,7 @@ class RetainPtrCtorAdoptChecker return; } if (Result == IsOwnedResult::Owned || Result == IsOwnedResult::Skip || - isNullPtr(Arg)) { + isNullPtr(Arg) || (RTC.isARCEnabled() && isRetainedWithoutARC(Arg))) { CreateOrCopyFnCall.insert(Arg); return; } @@ -336,7 +336,8 @@ class RetainPtrCtorAdoptChecker Result = IsOwnedResult::NotOwned; if (Result == IsOwnedResult::Owned) reportLeak(Name, CE, DeclWithIssue); - else if (RTC.isARCEnabled() && isAllocInit(Arg)) + else if (RTC.isARCEnabled() && + (isAllocInit(Arg) || isRetainedWithoutARC(Arg))) reportLeak(Name, CE, DeclWithIssue, "when ARC is disabled"); else if (isCreateOrCopy(Arg)) reportLeak(Name, CE, DeclWithIssue); @@ -464,6 +465,41 @@ class RetainPtrCtorAdoptChecker CalleeName.find("Copy") != std::string::npos; } + // Under ARC, RetainSummaryManager reports ns_returns_retained and + // alloc/new/copy family results as not owned because ARC balances them, but + // the "when ARC is disabled" diagnostics ask about non-ARC semantics. + bool isRetainedWithoutARC(const Expr *E) const { + if (auto *CE = dyn_cast<CallExpr>(E)) { + // ARC records ns_returns_retained in the callee's function type, which + // also covers calls through function pointers. + QualType T = CE->getCallee()->getType(); + if (auto *PT = T->getAs<PointerType>()) + T = PT->getPointeeType(); + auto *FT = T->getAs<FunctionType>(); + return FT && FT->getExtInfo().getProducesResult(); + } + auto *Msg = dyn_cast<ObjCMessageExpr>(E); + if (!Msg) + return false; + auto *Method = Msg->getMethodDecl(); + if (!Method || Method->hasAttr<NSReturnsNotRetainedAttr>()) + return false; + // Sema adds an implicit ns_returns_retained to init methods; isAllocInit + // handles those. + if (auto *Attr = Method->getAttr<NSReturnsRetainedAttr>(); + Attr && !Attr->isImplicit()) + return true; + switch (Msg->getMethodFamily()) { + case OMF_alloc: + case OMF_new: + case OMF_copy: + case OMF_mutableCopy: + return true; + default: + return false; + } + } + enum class IsOwnedResult { Unknown, Skip, Owned, NotOwned }; IsOwnedResult isOwned(const Expr *E) const { while (1) { diff --git a/clang/test/Analysis/Checkers/WebKit/retain-ptr-ctor-adopt-use-arc.mm b/clang/test/Analysis/Checkers/WebKit/retain-ptr-ctor-adopt-use-arc.mm index b761baab30ce30..13ce049a66801b 100644 --- a/clang/test/Analysis/Checkers/WebKit/retain-ptr-ctor-adopt-use-arc.mm +++ b/clang/test/Analysis/Checkers/WebKit/retain-ptr-ctor-adopt-use-arc.mm @@ -21,6 +21,32 @@ void basic_correct() { CFMutableArrayRef provide_cf(); +NS_RETURNS_RETAINED SomeObj *provide_retained_ns(); +NS_RETURNS_RETAINED dispatch_queue_t provide_retained_queue(); + +@interface SomeObj (ReturnsRetained) ++ (SomeObj *)makeRetained NS_RETURNS_RETAINED; ++ (SomeObj *)newSomeObj; ++ (SomeObj *)newNotRetained __attribute__((ns_returns_not_retained)); +@end + +void returns_retained_good() { + auto ns1 = adoptNS(provide_retained_ns()); + auto ns2 = adoptNS([SomeObj makeRetained]); + auto ns3 = adoptNS([SomeObj newSomeObj]); + auto os1 = adoptOSObject(provide_retained_queue()); + auto os2 = adoptOSObject(dispatch_queue_create("q", nullptr)); +} + +void returns_retained_wrong() { + RetainPtr<SomeObj> ns1 = provide_retained_ns(); + // expected-warning@-1{{Incorrect use of RetainPtr constructor; the argument is +1 and results in a memory leak when ARC is disabled [alpha.webkit.RetainPtrCtorAdoptChecker]}} + RetainPtr<SomeObj> ns2 = [SomeObj newSomeObj]; + // expected-warning@-1{{Incorrect use of RetainPtr constructor; the argument is +1 and results in a memory leak when ARC is disabled [alpha.webkit.RetainPtrCtorAdoptChecker]}} + auto ns3 = adoptNS([SomeObj newNotRetained]); + // expected-warning@-1{{Incorrect use of adoptNS; the argument is +0 and results in an use-after-free when ARC is disabled [alpha.webkit.RetainPtrCtorAdoptChecker]}} +} + void basic_wrong() { RetainPtr<SomeObj> ns1 = [[SomeObj alloc] init]; // expected-warning@-1{{Incorrect use of RetainPtr constructor; the argument is +1 and results in a memory leak when ARC is disabled [alpha.webkit.RetainPtrCtorAdoptChecker]}} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
