-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[Webkit Checkers] Treat const member variables as a safe origin #115594
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
rniwa
merged 5 commits into
llvm:main
from
rniwa:treat-const-member-as-safe-in-webkit-checkers
Nov 15, 2024
+404
−22
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
28ee832
[Webkit Checkers] Treat const member variables as a safe origin
rniwa 8bda965
Add the support for treating const unique_ptr as a safe pointer origin.
rniwa 4b7233a
Fix formatting.
rniwa 4efb59c
Fix formatting 2.
rniwa b74c55a
Use more early return in isPtrOfType and add a test case for UniqueRef
rniwa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
clang/test/Analysis/Checkers/WebKit/call-args-checked-const-member.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.webkit.UncheckedCallArgsChecker -verify %s | ||
|
||
#include "mock-types.h" | ||
|
||
namespace call_args_const_checkedptr_member { | ||
|
||
class Foo { | ||
public: | ||
Foo(); | ||
void bar(); | ||
|
||
private: | ||
const CheckedPtr<CheckedObj> m_obj1; | ||
CheckedPtr<CheckedObj> m_obj2; | ||
}; | ||
|
||
void Foo::bar() { | ||
m_obj1->method(); | ||
m_obj2->method(); | ||
// expected-warning@-1{{Call argument for 'this' parameter is unchecked and unsafe}} | ||
} | ||
|
||
} // namespace call_args_const_checkedptr_member | ||
|
||
namespace call_args_const_checkedref_member { | ||
|
||
class Foo { | ||
public: | ||
Foo(); | ||
void bar(); | ||
|
||
private: | ||
const CheckedRef<CheckedObj> m_obj1; | ||
CheckedRef<CheckedObj> m_obj2; | ||
}; | ||
|
||
void Foo::bar() { | ||
m_obj1->method(); | ||
m_obj2->method(); | ||
// expected-warning@-1{{Call argument for 'this' parameter is unchecked and unsafe}} | ||
} | ||
|
||
} // namespace call_args_const_checkedref_member | ||
|
||
namespace call_args_const_unique_ptr { | ||
|
||
class Foo { | ||
public: | ||
Foo(); | ||
void bar(); | ||
|
||
private: | ||
const std::unique_ptr<CheckedObj> m_obj1; | ||
std::unique_ptr<CheckedObj> m_obj2; | ||
}; | ||
|
||
void Foo::bar() { | ||
m_obj1->method(); | ||
m_obj2->method(); | ||
// expected-warning@-1{{Call argument for 'this' parameter is unchecked and unsafe}} | ||
} | ||
|
||
} // namespace call_args_const_unique_ptr |
86 changes: 86 additions & 0 deletions
86
clang/test/Analysis/Checkers/WebKit/call-args-counted-const-member.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.webkit.UncountedCallArgsChecker -verify %s | ||
|
||
#include "mock-types.h" | ||
|
||
namespace std { | ||
} | ||
|
||
namespace call_args_const_refptr_member { | ||
|
||
class Foo { | ||
public: | ||
Foo(); | ||
void bar(); | ||
|
||
private: | ||
const RefPtr<RefCountable> m_obj1; | ||
RefPtr<RefCountable> m_obj2; | ||
}; | ||
|
||
void Foo::bar() { | ||
m_obj1->method(); | ||
m_obj2->method(); | ||
// expected-warning@-1{{Call argument for 'this' parameter is uncounted and unsafe}} | ||
} | ||
|
||
} // namespace call_args_const_refptr_member | ||
|
||
namespace call_args_const_ref_member { | ||
|
||
class Foo { | ||
public: | ||
Foo(); | ||
void bar(); | ||
|
||
private: | ||
const Ref<RefCountable> m_obj1; | ||
Ref<RefCountable> m_obj2; | ||
}; | ||
|
||
void Foo::bar() { | ||
m_obj1->method(); | ||
m_obj2->method(); | ||
// expected-warning@-1{{Call argument for 'this' parameter is uncounted and unsafe}} | ||
} | ||
|
||
} // namespace call_args_const_ref_member | ||
|
||
namespace call_args_const_unique_ptr { | ||
|
||
class Foo { | ||
public: | ||
Foo(); | ||
void bar(); | ||
|
||
private: | ||
const std::unique_ptr<RefCountable> m_obj1; | ||
std::unique_ptr<RefCountable> m_obj2; | ||
}; | ||
|
||
void Foo::bar() { | ||
m_obj1->method(); | ||
m_obj2->method(); | ||
// expected-warning@-1{{Call argument for 'this' parameter is uncounted and unsafe}} | ||
} | ||
|
||
} // namespace call_args_const_unique_ptr | ||
rniwa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
namespace call_args_const_unique_ref { | ||
|
||
class Foo { | ||
public: | ||
Foo(); | ||
void bar(); | ||
|
||
private: | ||
const UniqueRef<RefCountable> m_obj1; | ||
UniqueRef<RefCountable> m_obj2; | ||
}; | ||
|
||
void Foo::bar() { | ||
m_obj1->method(); | ||
m_obj2->method(); | ||
// expected-warning@-1{{Call argument for 'this' parameter is uncounted and unsafe}} | ||
} | ||
|
||
} // namespace call_args_const_unique_ref |
72 changes: 72 additions & 0 deletions
72
clang/test/Analysis/Checkers/WebKit/local-vars-checked-const-member.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.webkit.UncheckedLocalVarsChecker -verify %s | ||
|
||
#include "mock-types.h" | ||
#include "mock-system-header.h" | ||
|
||
void someFunction(); | ||
|
||
namespace local_vars_const_checkedptr_member { | ||
|
||
class Foo { | ||
public: | ||
Foo(); | ||
void bar(); | ||
|
||
private: | ||
const CheckedPtr<CheckedObj> m_obj1; | ||
CheckedPtr<CheckedObj> m_obj2; | ||
}; | ||
|
||
void Foo::bar() { | ||
auto* obj1 = m_obj1.get(); | ||
obj1->method(); | ||
auto* obj2 = m_obj2.get(); | ||
// expected-warning@-1{{Local variable 'obj2' is unchecked and unsafe [alpha.webkit.UncheckedLocalVarsChecker]}} | ||
obj2->method(); | ||
} | ||
|
||
} // namespace local_vars_const_checkedptr_member | ||
|
||
namespace local_vars_const_checkedref_member { | ||
|
||
class Foo { | ||
public: | ||
Foo(); | ||
void bar(); | ||
|
||
private: | ||
const CheckedRef<CheckedObj> m_obj1; | ||
CheckedRef<CheckedObj> m_obj2; | ||
}; | ||
|
||
void Foo::bar() { | ||
auto& obj1 = m_obj1.get(); | ||
obj1.method(); | ||
auto& obj2 = m_obj2.get(); | ||
// expected-warning@-1{{Local variable 'obj2' is unchecked and unsafe [alpha.webkit.UncheckedLocalVarsChecker]}} | ||
obj2.method(); | ||
} | ||
|
||
} // namespace local_vars_const_ref_member | ||
|
||
namespace call_args_const_unique_ptr { | ||
|
||
class Foo { | ||
public: | ||
Foo(); | ||
void bar(); | ||
|
||
private: | ||
const std::unique_ptr<CheckedObj> m_obj1; | ||
std::unique_ptr<CheckedObj> m_obj2; | ||
}; | ||
|
||
void Foo::bar() { | ||
auto* obj1 = m_obj1.get(); | ||
obj1->method(); | ||
auto* obj2 = m_obj2.get(); | ||
// expected-warning@-1{{Local variable 'obj2' is unchecked and unsafe [alpha.webkit.UncheckedLocalVarsChecker]}} | ||
obj2->method(); | ||
} | ||
|
||
} // namespace call_args_const_unique_ptr |
72 changes: 72 additions & 0 deletions
72
clang/test/Analysis/Checkers/WebKit/local-vars-counted-const-member.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.webkit.UncountedLocalVarsChecker -verify %s | ||
|
||
#include "mock-types.h" | ||
#include "mock-system-header.h" | ||
|
||
void someFunction(); | ||
|
||
namespace local_vars_const_refptr_member { | ||
|
||
class Foo { | ||
public: | ||
Foo(); | ||
void bar(); | ||
|
||
private: | ||
const RefPtr<RefCountable> m_obj1; | ||
RefPtr<RefCountable> m_obj2; | ||
}; | ||
|
||
void Foo::bar() { | ||
auto* obj1 = m_obj1.get(); | ||
obj1->method(); | ||
auto* obj2 = m_obj2.get(); | ||
// expected-warning@-1{{Local variable 'obj2' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}} | ||
obj2->method(); | ||
} | ||
|
||
} // namespace local_vars_const_refptr_member | ||
|
||
namespace local_vars_const_ref_member { | ||
|
||
class Foo { | ||
public: | ||
Foo(); | ||
void bar(); | ||
|
||
private: | ||
const Ref<RefCountable> m_obj1; | ||
Ref<RefCountable> m_obj2; | ||
}; | ||
|
||
void Foo::bar() { | ||
auto& obj1 = m_obj1.get(); | ||
obj1.method(); | ||
auto& obj2 = m_obj2.get(); | ||
// expected-warning@-1{{Local variable 'obj2' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}} | ||
obj2.method(); | ||
} | ||
|
||
} // namespace local_vars_const_ref_member | ||
|
||
namespace call_args_const_unique_ptr { | ||
|
||
class Foo { | ||
public: | ||
Foo(); | ||
void bar(); | ||
|
||
private: | ||
const std::unique_ptr<RefCountable> m_obj1; | ||
std::unique_ptr<RefCountable> m_obj2; | ||
}; | ||
|
||
void Foo::bar() { | ||
auto* obj1 = m_obj1.get(); | ||
obj1->method(); | ||
auto* obj2 = m_obj2.get(); | ||
// expected-warning@-1{{Local variable 'obj2' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}} | ||
obj2->method(); | ||
} | ||
|
||
} // namespace call_args_const_unique_ptr |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of curiosity: Is this (
UniqueRef
andLazyUniqueRef
) the exhaustive list of additional Webkit defined pointer owner types(I'm assuming)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There also
BlockPtr
to retain a Obj-C block andRetainPtr
for retaining Obj-C objects. For now, static analyzer don't recognize those pointer types.