Skip to content

Merge Webkit checker fixes #10038

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 49 commits into from
Feb 25, 2025
Merged
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
52b29a7
[webkit.RefCntblBaseVirtualDtor] Make ThreadSafeRefCounted not genera…
rniwa Sep 11, 2024
705c658
[WebKit Static Analyzer] Treat WTFReportBacktrace as a trivial functi…
rniwa Sep 11, 2024
2fa4ba1
[WebKit Checkers] Allow "singleton" suffix to be camelCased. (#108257)
rniwa Sep 11, 2024
c7f0961
[alpha.webkit.UncountedCallArgsChecker] Allow protector functions in …
rniwa Sep 11, 2024
bd4a088
[alpha.webkit.UncountedCallArgsChecker] Add support for Objective-C++…
rniwa Sep 18, 2024
69bbcce
[webkit.RefCntblBaseVirtualDtor] ThreadSafeRefCounted still generates…
rniwa Sep 18, 2024
f5d4535
[alpha.webkit.UncountedCallArgsChecker] Use canonical type (#109393)
rniwa Sep 27, 2024
be1eac1
WebKit Checkers should set DeclWithIssue. (#109389)
rniwa Sep 27, 2024
f6c5b46
[alpha.webkit.NoUncheckedPtrMemberChecker] Introduce member variable …
rniwa Sep 27, 2024
d3f17ae
[alpha.webkit.UncountedCallArgsChecker] Skip std::forward in tryToFin…
rniwa Oct 10, 2024
f91b736
[alpha.webkit.UncountedCallArgsChecker] Add the support for trivial C…
rniwa Oct 10, 2024
cca5259
isUncountedPtr should take QualType as an argument. (#110213)
rniwa Oct 10, 2024
bafe96a
[alpha.webkit.UncountedLocalVarsChecker] Recursive functions are erro…
rniwa Oct 17, 2024
1e106d8
[WebKit Checkers] Allow a guardian CheckedPtr/CheckedRef (#110222)
rniwa Oct 25, 2024
73a4f81
[alpha.webkit.UncountedLocalVarsChecker] Warn the use of a raw pointe…
rniwa Oct 30, 2024
a7141f2
Introduce a new WebKit checker for a unchecked local variable (#113708)
rniwa Nov 1, 2024
71be385
Update clang static analyzers per rename of member functions in CanMa…
rniwa Nov 5, 2024
d8d6d11
Introduce a new WebKit checker for a unchecked call arguments (#11370…
rniwa Nov 7, 2024
cf6b7bd
[webkit.UncountedLambdaCapturesChecker] Ignore trivial functions and …
rniwa Nov 12, 2024
2942942
[WebKit Checkers] Make TrivialFunctionAnalysis recognize std::array::…
t-rasmud Oct 24, 2024
f4a2542
[WebKit checkers] Treat ref() and incrementCheckedPtrCount() as trivi…
rniwa Nov 14, 2024
e980007
[Webkit Checkers] Treat const member variables as a safe origin (#115…
rniwa Nov 15, 2024
1e61f30
[webkit.UncountedLambdaCapturesChecker] Fix debug assertion failure. …
rniwa Nov 21, 2024
ca7ae4e
[webkit.UncountedLambdaCapturesChecker] Ignore lambda invocation with…
rniwa Nov 23, 2024
cb4a48d
[ASTMatchers] AST matcher support for ObjC pointers (#117021)
t-rasmud Dec 3, 2024
7599261
[Webkit Checkers] Introduce a Webkit checker for memory unsafe casts …
t-rasmud Dec 5, 2024
a06f0bd
UncountedLocalVarsChecker and UncheckedLocalVarsChecker should recogn…
rniwa Dec 10, 2024
0dc0498
Fix a bug that CXXConstructExpr wasn't recognized by tryToFindPtrOrig…
rniwa Dec 12, 2024
17cea60
[WebKit checkers] Recognize ensureFoo functions (#119681)
rniwa Dec 13, 2024
f0a358e
[webkit.UncountedLambdaCapturesChecker] Add a fallback for checking l…
rniwa Dec 15, 2024
946e837
[WebKit checkers] Recognize adoptRef as a safe function (#120629)
rniwa Dec 20, 2024
f55b40e
[webkit.UncountedLambdaCapturesChecker] Detect protectedThis pattern.…
rniwa Dec 20, 2024
37fde4b
[webkit.UncountedLambdaCapturesChecker] Fix a nullptr deference. (#12…
rniwa Dec 20, 2024
623e1b4
[Static analysis] Encodes a filename before inserting it into a URL. …
rniwa Jan 17, 2025
03431c0
[webkit.UncountedLambdaCapturesChecker] Fix a bug that the checker di…
rniwa Feb 5, 2025
338625d
[webkit.UncountedLambdaCapturesChecker] Fix a regression that [[noesc…
rniwa Feb 7, 2025
69d9462
[WebKit checkers] Treat an implicit value initialization as trivial (…
rniwa Feb 11, 2025
38cf7a2
[WebKit Checkers] Treat const Objective-C ivar as a safe origin (#126…
rniwa Feb 11, 2025
ec65ecf
[WebKit Checkers] Allow operator T&() in a const member function (#12…
rniwa Feb 11, 2025
8881a46
[NFC] [clang] fix unused variable warning (#126796)
fmayer Feb 11, 2025
7a17516
[NFC] [clang] Use isa instead of dyn_cast
fmayer Feb 11, 2025
d109d96
[WebKit Checkers] Treat offsetof as a trivial expression. (#126996)
rniwa Feb 14, 2025
69edf7f
[webkit.UncountedLambdaCapturesChecker] Support [[clang::noescape]] o…
rniwa Feb 14, 2025
d99a480
[alpha.webkit.UncountedCallArgsChecker] Allow ArrayInitLoopExpr and O…
rniwa Feb 14, 2025
3926bc2
[webkit.UncountedLambdaCapturesChecker] Recognize nested protectedThi…
rniwa Feb 15, 2025
5e36c55
[WebKit Checkers] Treat attributes as trivial (#127289)
rniwa Feb 15, 2025
09f00b8
[webkit.UncountedLambdaCapturesChecker] Fix a crash in declProtectsTh…
rniwa Feb 15, 2025
6adc720
center-endpath-html.patch
rniwa Feb 19, 2025
f811649
Check the type of Objective-C++ instance variables in WebKit member v…
rniwa Feb 19, 2025
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
4 changes: 4 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
@@ -1367,6 +1367,10 @@ AST Matchers
- Fixed captureVars assertion failure if not capturesVariables. (#GH76425)
- ``forCallable`` now properly preserves binding on successful match. (#GH89657)

- Ensure ``hasType`` and ``hasDeclaration`` match Objective-C interface declarations.

- Ensure ``pointee`` matches Objective-C pointer types.

clang-format
------------

99 changes: 94 additions & 5 deletions clang/docs/analyzer/checkers.rst
Original file line number Diff line number Diff line change
@@ -3433,6 +3433,52 @@ Check for non-determinism caused by sorting of pointers.
alpha.WebKit
^^^^^^^^^^^^

.. _alpha-webkit-NoUncheckedPtrMemberChecker:

alpha.webkit.MemoryUnsafeCastChecker
""""""""""""""""""""""""""""""""""""""
Check for all casts from a base type to its derived type as these might be memory-unsafe.

Example:

.. code-block:: cpp

class Base { };
class Derived : public Base { };

void f(Base* base) {
Derived* derived = static_cast<Derived*>(base); // ERROR
}

For all cast operations (C-style casts, static_cast, reinterpret_cast, dynamic_cast), if the source type a `Base*` and the destination type is `Derived*`, where `Derived` inherits from `Base`, the static analyzer should signal an error.

This applies to:

- C structs, C++ structs and classes, and Objective-C classes and protocols.
- Pointers and references.
- Inside template instantiations and macro expansions that are visible to the compiler.

For types like this, instead of using built in casts, the programmer will use helper functions that internally perform the appropriate type check and disable static analysis.

alpha.webkit.NoUncheckedPtrMemberChecker
""""""""""""""""""""""""""""""""""""""""
Raw pointers and references to an object which supports CheckedPtr or CheckedRef can't be used as class members. Only CheckedPtr, CheckedRef, RefPtr, or Ref are allowed.

.. code-block:: cpp

struct CheckableObj {
void incrementCheckedPtrCount() {}
void decrementCheckedPtrCount() {}
};

struct Foo {
CheckableObj* ptr; // warn
CheckableObj& ptr; // warn
// ...
};

See `WebKit Guidelines for Safer C++ Programming <https://github.com/WebKit/WebKit/wiki/Safer-CPP-Guidelines>`_ for details.

.. _alpha-webkit-UncountedCallArgsChecker:

alpha.webkit.UncountedCallArgsChecker
@@ -3522,6 +3568,12 @@ We also define a set of safe transformations which if passed a safe value as an
- casts
- unary operators like ``&`` or ``*``

alpha.webkit.UncheckedCallArgsChecker
"""""""""""""""""""""""""""""""""""""
The goal of this rule is to make sure that lifetime of any dynamically allocated CheckedPtr capable object passed as a call argument keeps its memory region past the end of the call. This applies to call to any function, method, lambda, function pointer or functor. CheckedPtr capable objects aren't supposed to be allocated on stack so we check arguments for parameters of raw pointers and references to unchecked types.

The rules of when to use and not to use CheckedPtr / CheckedRef are same as alpha.webkit.UncountedCallArgsChecker for ref-counted objects.

alpha.webkit.UncountedLocalVarsChecker
""""""""""""""""""""""""""""""""""""""
The goal of this rule is to make sure that any uncounted local variable is backed by a ref-counted object with lifetime that is strictly larger than the scope of the uncounted local variable. To be on the safe side we require the scope of an uncounted variable to be embedded in the scope of ref-counted object that backs it.
@@ -3546,7 +3598,7 @@ These are examples of cases that we consider safe:
RefCountable* uncounted = this; // ok
}

Here are some examples of situations that we warn about as they *might* be potentially unsafe. The logic is that either we're able to guarantee that an argument is safe or it's considered if not a bug then bug-prone.
Here are some examples of situations that we warn about as they *might* be potentially unsafe. The logic is that either we're able to guarantee that a local variable is safe or it's considered unsafe.

.. code-block:: cpp

@@ -3565,11 +3617,48 @@ Here are some examples of situations that we warn about as they *might* be poten
RefCountable* uncounted = counted.get(); // warn
}

We don't warn about these cases - we don't consider them necessarily safe but since they are very common and usually safe we'd introduce a lot of false positives otherwise:
- variable defined in condition part of an ```if``` statement
- variable defined in init statement condition of a ```for``` statement
alpha.webkit.UncheckedLocalVarsChecker
""""""""""""""""""""""""""""""""""""""
The goal of this rule is to make sure that any unchecked local variable is backed by a CheckedPtr or CheckedRef with lifetime that is strictly larger than the scope of the unchecked local variable. To be on the safe side we require the scope of an unchecked variable to be embedded in the scope of CheckedPtr/CheckRef object that backs it.

These are examples of cases that we consider safe:

.. code-block:: cpp

void foo1() {
CheckedPtr<RefCountable> counted;
// The scope of uncounted is EMBEDDED in the scope of counted.
{
RefCountable* uncounted = counted.get(); // ok
}
}

void foo2(CheckedPtr<RefCountable> counted_param) {
RefCountable* uncounted = counted_param.get(); // ok
}

void FooClass::foo_method() {
RefCountable* uncounted = this; // ok
}

Here are some examples of situations that we warn about as they *might* be potentially unsafe. The logic is that either we're able to guarantee that a local variable is safe or it's considered unsafe.

For the time being we also don't warn about uninitialized uncounted local variables.
.. code-block:: cpp

void foo1() {
RefCountable* uncounted = new RefCountable; // warn
}

RefCountable* global_uncounted;
void foo2() {
RefCountable* uncounted = global_uncounted; // warn
}

void foo3() {
RefPtr<RefCountable> counted;
// The scope of uncounted is not EMBEDDED in the scope of counted.
RefCountable* uncounted = counted.get(); // warn
}

Debug Checkers
---------------
5 changes: 3 additions & 2 deletions clang/include/clang/ASTMatchers/ASTMatchers.h
Original file line number Diff line number Diff line change
@@ -4033,7 +4033,7 @@ AST_POLYMORPHIC_MATCHER_P_OVERLOAD(
AST_POLYMORPHIC_MATCHER_P_OVERLOAD(
hasType,
AST_POLYMORPHIC_SUPPORTED_TYPES(Expr, FriendDecl, ValueDecl,
CXXBaseSpecifier),
CXXBaseSpecifier, ObjCInterfaceDecl),
internal::Matcher<Decl>, InnerMatcher, 1) {
QualType QT = internal::getUnderlyingType(Node);
if (!QT.isNull())
@@ -7433,7 +7433,8 @@ extern const AstTypeMatcher<RValueReferenceType> rValueReferenceType;
AST_TYPELOC_TRAVERSE_MATCHER_DECL(
pointee, getPointee,
AST_POLYMORPHIC_SUPPORTED_TYPES(BlockPointerType, MemberPointerType,
PointerType, ReferenceType));
PointerType, ReferenceType,
ObjCObjectPointerType));

/// Matches typedef types.
///
10 changes: 9 additions & 1 deletion clang/include/clang/ASTMatchers/ASTMatchersInternal.h
Original file line number Diff line number Diff line change
@@ -161,6 +161,9 @@ inline QualType getUnderlyingType(const FriendDecl &Node) {
inline QualType getUnderlyingType(const CXXBaseSpecifier &Node) {
return Node.getType();
}
inline QualType getUnderlyingType(const ObjCInterfaceDecl &Node) {
return Node.getTypeForDecl()->getPointeeType();
}

/// Unifies obtaining a `TypeSourceInfo` from different node types.
template <typename T,
@@ -1113,6 +1116,11 @@ class HasDeclarationMatcher : public MatcherInterface<T> {
return matchesDecl(Node.getDecl(), Finder, Builder);
}

bool matchesSpecialized(const ObjCInterfaceDecl &Node, ASTMatchFinder *Finder,
BoundNodesTreeBuilder *Builder) const {
return matchesDecl(Node.getCanonicalDecl(), Finder, Builder);
}

/// Extracts the operator new of the new call and returns whether the
/// inner matcher matches on it.
bool matchesSpecialized(const CXXNewExpr &Node,
@@ -1213,7 +1221,7 @@ using HasDeclarationSupportedTypes =
ElaboratedType, InjectedClassNameType, LabelStmt, AddrLabelExpr,
MemberExpr, QualType, RecordType, TagType,
TemplateSpecializationType, TemplateTypeParmType, TypedefType,
UnresolvedUsingType, ObjCIvarRefExpr>;
UnresolvedUsingType, ObjCIvarRefExpr, ObjCInterfaceDecl>;

/// A Matcher that allows binding the node it matches to an id.
///
16 changes: 16 additions & 0 deletions clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
Original file line number Diff line number Diff line change
@@ -1787,12 +1787,28 @@ def UncountedLambdaCapturesChecker : Checker<"UncountedLambdaCapturesChecker">,

let ParentPackage = WebKitAlpha in {

def MemoryUnsafeCastChecker : Checker<"MemoryUnsafeCastChecker">,
HelpText<"Check for memory unsafe casts from base type to derived type.">,
Documentation<HasDocumentation>;

def NoUncheckedPtrMemberChecker : Checker<"NoUncheckedPtrMemberChecker">,
HelpText<"Check for no unchecked member variables.">,
Documentation<HasDocumentation>;

def UncountedCallArgsChecker : Checker<"UncountedCallArgsChecker">,
HelpText<"Check uncounted call arguments.">,
Documentation<HasDocumentation>;

def UncheckedCallArgsChecker : Checker<"UncheckedCallArgsChecker">,
HelpText<"Check unchecked call arguments.">,
Documentation<HasDocumentation>;

def UncountedLocalVarsChecker : Checker<"UncountedLocalVarsChecker">,
HelpText<"Check uncounted local variables.">,
Documentation<HasDocumentation>;

def UncheckedLocalVarsChecker : Checker<"UncheckedLocalVarsChecker">,
HelpText<"Check unchecked local variables.">,
Documentation<HasDocumentation>;

} // end alpha.webkit
3 changes: 2 additions & 1 deletion clang/lib/ASTMatchers/ASTMatchersInternal.cpp
Original file line number Diff line number Diff line change
@@ -1086,7 +1086,8 @@ AST_TYPELOC_TRAVERSE_MATCHER_DEF(hasValueType,
AST_TYPELOC_TRAVERSE_MATCHER_DEF(
pointee,
AST_POLYMORPHIC_SUPPORTED_TYPES(BlockPointerType, MemberPointerType,
PointerType, ReferenceType));
PointerType, ReferenceType,
ObjCObjectPointerType));

const internal::VariadicDynCastAllOfMatcher<Stmt, OMPExecutableDirective>
ompExecutableDirective;
1 change: 1 addition & 0 deletions clang/lib/Rewrite/HTMLRewrite.cpp
Original file line number Diff line number Diff line change
@@ -334,6 +334,7 @@ h1 { font-size:14pt }
.keyword { color: blue }
.string_literal { color: red }
.directive { color: darkmagenta }
.anchor { display: block; height: 250px; margin-top: -250px; visibility: hidden; }

/* Macros and variables could have pop-up notes hidden by default.
- Macro pop-up: expansion of the macro
7 changes: 4 additions & 3 deletions clang/lib/StaticAnalyzer/Checkers/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -133,13 +133,14 @@ add_clang_library(clangStaticAnalyzerCheckers
VLASizeChecker.cpp
ValistChecker.cpp
VirtualCallChecker.cpp
WebKit/NoUncountedMembersChecker.cpp
WebKit/RawPtrRefMemberChecker.cpp
WebKit/ASTUtils.cpp
WebKit/MemoryUnsafeCastChecker.cpp
WebKit/PtrTypesSemantics.cpp
WebKit/RefCntblBaseVirtualDtorChecker.cpp
WebKit/UncountedCallArgsChecker.cpp
WebKit/RawPtrRefCallArgsChecker.cpp
WebKit/UncountedLambdaCapturesChecker.cpp
WebKit/UncountedLocalVarsChecker.cpp
WebKit/RawPtrRefLocalVarsChecker.cpp

LINK_LIBS
clangAST
Loading