llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-static-analyzer-1

@llvm/pr-subscribers-clang

Author: geoffreygaren

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/244754.diff


2 Files Affected:

- (modified) 
clang/lib/StaticAnalyzer/Checkers/WebKit/RetainPtrCtorAdoptChecker.cpp (+38-2) 
- (modified) 
clang/test/Analysis/Checkers/WebKit/retain-ptr-ctor-adopt-use-arc.mm (+26) 


``````````diff
diff --git 
a/clang/lib/StaticAnalyzer/Checkers/WebKit/RetainPtrCtorAdoptChecker.cpp 
b/clang/lib/StaticAnalyzer/Checkers/WebKit/RetainPtrCtorAdoptChecker.cpp
index 861ea9a6d52d3..3bf6d41ea75e4 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 b761baab30ce3..13ce049a66801 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]}}

``````````

</details>


https://github.com/llvm/llvm-project/pull/244754
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to