Skip to content

Check the type of Objective-C++ instance variables in WebKit member variable checkers. #127570

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 1 commit into from
Feb 19, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -61,6 +61,11 @@ class RawPtrRefMemberChecker
Checker->visitRecordDecl(RD);
return true;
}

bool VisitObjCContainerDecl(const ObjCContainerDecl *CD) override {
Checker->visitObjCDecl(CD);
return true;
}
};

LocalVisitor visitor(this);
@@ -87,6 +92,31 @@ class RawPtrRefMemberChecker
}
}

void visitObjCDecl(const ObjCContainerDecl *CD) const {
if (auto *ID = dyn_cast<ObjCInterfaceDecl>(CD)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is ID always guaranteed to be non-null?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think so. ObjCContainerDecl could be ObjCCategoryDecl or ObjCProtocolDecl as well.
Or are you saying CD could be null?

Copy link
Contributor

@t-rasmud t-rasmud Feb 19, 2025

Choose a reason for hiding this comment

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

Ah ok. I was wondering if we should check for nullness before dereferencing ID in the next line ID->ivars()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh, but this if statement does that already, right? as in, this if will only evaluate to true if ID wasn't nullptr so I think we have a null check here already in that regard.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah yes, of course! I wasn't reading right.

for (auto *Ivar : ID->ivars())
visitIvarDecl(CD, Ivar);
return;
}
if (auto *ID = dyn_cast<ObjCImplementationDecl>(CD)) {
for (auto *Ivar : ID->ivars())
visitIvarDecl(CD, Ivar);
return;
}
}

void visitIvarDecl(const ObjCContainerDecl *CD,
const ObjCIvarDecl *Ivar) const {
const Type *IvarType = Ivar->getType().getTypePtrOrNull();
if (!IvarType)
return;
if (auto *IvarCXXRD = IvarType->getPointeeCXXRecordDecl()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can IvarCXXRD be null?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think so. If IvarType isn't a pointer to a C++ type, getPointeeCXXRecordDecl can return nullptr. e.g. Objective-C interface type.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But again, we're doing a nullptr check here with if statement so I think we're good :)

std::optional<bool> IsCompatible = isPtrCompatible(IvarCXXRD);
if (IsCompatible && *IsCompatible)
reportBug(Ivar, IvarType, IvarCXXRD, CD);
}
}

bool shouldSkipDecl(const RecordDecl *RD) const {
if (!RD->isThisDeclarationADefinition())
return true;
@@ -121,17 +151,21 @@ class RawPtrRefMemberChecker
return false;
}

void reportBug(const FieldDecl *Member, const Type *MemberType,
template <typename DeclType, typename ParentDeclType>
void reportBug(const DeclType *Member, const Type *MemberType,
const CXXRecordDecl *MemberCXXRD,
const RecordDecl *ClassCXXRD) const {
const ParentDeclType *ClassCXXRD) const {
assert(Member);
assert(MemberType);
assert(MemberCXXRD);

SmallString<100> Buf;
llvm::raw_svector_ostream Os(Buf);

Os << "Member variable ";
if (isa<ObjCContainerDecl>(ClassCXXRD))
Os << "Instance variable ";
else
Os << "Member variable ";
printQuotedName(Os, Member);
Os << " in ";
printQuotedQualifiedName(Os, ClassCXXRD);
35 changes: 35 additions & 0 deletions clang/test/Analysis/Checkers/WebKit/unchecked-members-objc.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.webkit.NoUncheckedPtrMemberChecker -verify %s

#include "mock-types.h"

__attribute__((objc_root_class))
@interface NSObject
+ (instancetype) alloc;
- (instancetype) init;
- (instancetype)retain;
- (void)release;
@end

void doSomeWork();

@interface SomeObjC : NSObject {
CheckedObj* _unchecked1;
// expected-warning@-1{{Instance variable '_unchecked1' in 'SomeObjC' is a raw pointer to CheckedPtr capable type 'CheckedObj'}}
CheckedPtr<CheckedObj> _counted1;
[[clang::suppress]] CheckedObj* _unchecked2;
}
- (void)doWork;
@end

@implementation SomeObjC {
CheckedObj* _unchecked3;
// expected-warning@-1{{Instance variable '_unchecked3' in 'SomeObjC' is a raw pointer to CheckedPtr capable type 'CheckedObj'}}
CheckedPtr<CheckedObj> _counted2;
[[clang::suppress]] CheckedObj* _unchecked4;
}

- (void)doWork {
doSomeWork();
}

@end
35 changes: 35 additions & 0 deletions clang/test/Analysis/Checkers/WebKit/uncounted-members-objc.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// RUN: %clang_analyze_cc1 -analyzer-checker=webkit.NoUncountedMemberChecker -verify %s

#include "mock-types.h"

__attribute__((objc_root_class))
@interface NSObject
+ (instancetype) alloc;
- (instancetype) init;
- (instancetype)retain;
- (void)release;
@end

void doSomeWork();

@interface SomeObjC : NSObject {
RefCountable* _uncounted1;
// expected-warning@-1{{Instance variable '_uncounted1' in 'SomeObjC' is a raw pointer to ref-countable type 'RefCountable'}}
RefPtr<RefCountable> _counted1;
[[clang::suppress]] RefCountable* _uncounted2;
}
- (void)doWork;
@end

@implementation SomeObjC {
RefCountable* _uncounted3;
// expected-warning@-1{{Instance variable '_uncounted3' in 'SomeObjC' is a raw pointer to ref-countable type 'RefCountable'}}
RefPtr<RefCountable> _counted2;
[[clang::suppress]] RefCountable* _uncounted4;
}

- (void)doWork {
doSomeWork();
}

@end