Skip to content

[WebKit Checkers] Treat const Objective-C ivar as a safe origin #126353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 11, 2025

Conversation

rniwa
Copy link
Contributor

@rniwa rniwa commented Feb 8, 2025

Like const C++ member variables, treat const Ref, RefPtr, CheckedRef, CheckedPtr Objective-C ivars as a safe pointer origin in WebKit checkers.

Like const C++ member variables, treat const Ref, RefPtr, CheckedRef, CheckedPtr
Objective-C ivars as a safe pointer origin in WebKit checkers.
@rniwa rniwa requested a review from haoNoQ February 8, 2025 07:23
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:static analyzer labels Feb 8, 2025
@rniwa rniwa requested a review from t-rasmud February 8, 2025 07:23
@llvmbot
Copy link
Member

llvmbot commented Feb 8, 2025

@llvm/pr-subscribers-clang-static-analyzer-1

@llvm/pr-subscribers-clang

Author: Ryosuke Niwa (rniwa)

Changes

Like const C++ member variables, treat const Ref, RefPtr, CheckedRef, CheckedPtr Objective-C ivars as a safe pointer origin in WebKit checkers.


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

2 Files Affected:

  • (modified) clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp (+6-3)
  • (modified) clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.mm (+8-1)
diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp
index abf5d3ec193a41a..029ca0d2385232d 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp
@@ -163,10 +163,13 @@ bool isConstOwnerPtrMemberExpr(const clang::Expr *E) {
     if (OCE->getOperator() == OO_Star && OCE->getNumArgs() == 1)
       E = OCE->getArg(0);
   }
-  auto *ME = dyn_cast<MemberExpr>(E);
-  if (!ME)
+  const ValueDecl *D = nullptr;
+  if (auto *ME = dyn_cast<MemberExpr>(E))
+    D = ME->getMemberDecl();
+  else if (auto *IVR = dyn_cast<ObjCIvarRefExpr>(E))
+    D = IVR->getDecl();
+  else
     return false;
-  auto *D = ME->getMemberDecl();
   if (!D)
     return false;
   auto T = D->getType();
diff --git a/clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.mm b/clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.mm
index 9ad1880e9d11883..ccac2e6439444ae 100644
--- a/clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.mm
+++ b/clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.mm
@@ -5,7 +5,10 @@
 #import "mock-system-header.h"
 #import "../../Inputs/system-header-simulator-for-objc-dealloc.h"
 
-@interface Foo : NSObject
+@interface Foo : NSObject {
+  const Ref<RefCountable> _obj1;
+  const RefPtr<RefCountable> _obj2;
+}
 
 @property (nonatomic, readonly) RefPtr<RefCountable> countable;
 
@@ -17,6 +20,9 @@ @implementation Foo
 
 - (void)execute {
   self._protectedRefCountable->method();
+  _obj1->method();
+  _obj1.get().method();
+  (*_obj2).method();
 }
 
 - (RefPtr<RefCountable>)_protectedRefCountable {
@@ -30,6 +36,7 @@ - (void)execute {
   void ref() const;
   void deref() const;
   Ref<RefCountedObject> copy() const;
+  void method();
 };
 
 @interface WrapperObj : NSObject

Copy link
Contributor

@t-rasmud t-rasmud left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@rniwa
Copy link
Contributor Author

rniwa commented Feb 11, 2025

Thank you for the review!

@rniwa rniwa merged commit 71478ec into llvm:main Feb 11, 2025
8 checks passed
@rniwa rniwa deleted the treat-const-objc-member-as-safe branch February 11, 2025 18:00
rniwa added a commit to rniwa/llvm-project that referenced this pull request Feb 11, 2025
…#126353)

Like const C++ member variables, treat const Ref, RefPtr, CheckedRef,
CheckedPtr Objective-C ivars as a safe pointer origin in WebKit
checkers.
Icohedron pushed a commit to Icohedron/llvm-project that referenced this pull request Feb 11, 2025

Verified

This commit was signed with the committer’s verified signature.
Icohedron Deric C.
…#126353)

Like const C++ member variables, treat const Ref, RefPtr, CheckedRef,
CheckedPtr Objective-C ivars as a safe pointer origin in WebKit
checkers.
flovent pushed a commit to flovent/llvm-project that referenced this pull request Feb 13, 2025
…#126353)

Like const C++ member variables, treat const Ref, RefPtr, CheckedRef,
CheckedPtr Objective-C ivars as a safe pointer origin in WebKit
checkers.
joaosaffran pushed a commit to joaosaffran/llvm-project that referenced this pull request Feb 14, 2025
…#126353)

Like const C++ member variables, treat const Ref, RefPtr, CheckedRef,
CheckedPtr Objective-C ivars as a safe pointer origin in WebKit
checkers.
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Feb 24, 2025
…#126353)

Like const C++ member variables, treat const Ref, RefPtr, CheckedRef,
CheckedPtr Objective-C ivars as a safe pointer origin in WebKit
checkers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:static analyzer clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants