4661 Commits

Author SHA1 Message Date
Ryosuke Niwa
517605c20e
[alpha.webkit.UnretainedCallArgsChecker] Add the support for RetainPtrArc (#135532)
WebKit uses #define to rename RetainPtr to RetainPtrArc so add the
support for it.
2025-04-15 20:00:51 -07:00
Connector Switch
cc354d6a6d
[NFC] Fix destroy typo. (#135640) 2025-04-15 08:20:44 +08:00
Ryosuke Niwa
2206e15e78
[alpha.webkit.UnretainedCallArgsChecker] Don't emit a warning for RetainPtr::operator= (#135526)
Generalize the check for operator= so that it works for RetainPtr and
CheckedPtr instead of just RefPtr.
2025-04-14 15:03:21 -07:00
Ryosuke Niwa
6136019780
[alpha.webkit.ForwardDeclChecker] Recognize a forward declared template specialization (#134545)
This PR fixes a bug that when a template specialization is declared with
a forward declaration of a template, the checker fails to find its
definition in the same translation unit and erroneously emit an unsafe
forward declaration warning.
2025-04-10 15:28:36 -07:00
Ryosuke Niwa
c26d097d0c
[alpha.webkit.RetainPtrCtorAdoptChecker] Support adopt(cast(copy(~)) (#132316)
This PR adds the support for recognizing calling adoptCF/adoptNS on the
result of a cast operation on the return value of a function which
creates NS or CF types. It also fixes a bug that we weren't reporting
memory leaks when CF types are created without ever calling RetainPtr's
constructor, adoptCF, or adoptNS.

To do this, this PR adds a new mechanism to report a memory leak
whenever create or copy CF functions are invoked unless this CallExpr
has already been visited while validating a call to adoptCF. Also added
an early exit when isOwned returns IsOwnedResult::Skip due to an
unresolved template argument.
2025-04-10 15:26:10 -07:00
Ryosuke Niwa
2c31403f4f
[alpha.webkit.UnretainedLambdaCapturesChecker] Add the support for protectedSelf (#132518)
This PR adds the support for treating capturing of "self" as safe if the
lambda simultaneously captures "protectedSelf", which is a RetainPtr of
"self".

This PR also fixes a bug that the checker wasn't generating a warning
when "self" is implicitly captured. Note when "self" is implicitly
captured, we use the lambda's getBeginLoc as a fallback source location.
2025-04-09 11:52:36 -07:00
Ryosuke Niwa
c8d49e9dd2
[alpha.webkit.RetainPtrCtorAdoptChecker] Recognize mutableCopy from literal as +1 (#132350)
This PR adds the support for recognizing the return value of copy /
mutableCopy as +1. isAllocInit and isOwned now traverses
PseudoObjectExpr to its last semantic expression.
2025-04-09 10:33:08 -07:00
Balázs Kéri
31ef7acf12
[clang][analyzer] Fix a possible crash in CastSizeChecker (#134387) 2025-04-07 09:46:03 +02:00
Ryosuke Niwa
d8fd665960
[alpha.webkit.ForwardDeclChecker] Ignore forward declared struct. (#133804)
There are some system libraries such as sqlite3 which forward declare a
struct then use a pointer to that forward declared type in various APIs.
Ignore these types ForwardDeclChecker like other pointer types.
2025-04-04 12:04:20 -07:00
Ryosuke Niwa
bf1d27889b
[WebKit checkers] Treat Objective-C message send return value as safe (#133605)
Objective-C selectors are supposed to return autoreleased object. Treat
these return values as safe.
2025-04-04 11:25:24 -07:00
Nikolas Klauser
04676c6160
Revert "Enable unnecessary-virtual-specifier by default" (#134105)
Reverts llvm/llvm-project#133265

This causes the whole libc++ CI to fail, since we're not building
against a compiler built from current trunk. Specifically, the CMake
changes causes some feature detection to fail, resulting in CMake
being unable to configure libc++.
2025-04-02 17:59:08 +02:00
Ryosuke Niwa
6ff33edcdc
[alpha.webkit.NoUnretainedMemberChecker] Ignore system-header-defined ivar / property of a forward declared type (#133755)
Prior to this PR, we were emitting warnings for Objective-C ivars and
properties if the forward declaration of the type appeared first in a
non-system header. This PR fixes the checker so tha we'd ignore ivars
and properties defined for a forward declared type.
2025-03-31 14:59:41 -07:00
Devon Loehr
4007de00a0
Enable unnecessary-virtual-specifier by default (#133265)
This turns on the unnecessary-virtual-specifier warning in general, but
disables it when building LLVM. It also tweaks the warning description
to be slightly more accurate.

Background: I've been working on cleaning up this warning in two
codebases: LLVM and chromium (plus its dependencies). The chromium
cleanup has been straightforward. Git archaeology shows that there are
two reasons for the warnings: classes to which `final` was added after
they were initially committed, and classes with virtual destructors that
nobody remarks on. Presumably the latter case is because people are just
very used to destructors being virtual.

The LLVM cleanup was more surprising: I discovered that we have an [old
policy](https://llvm.org/docs/CodingStandards.html#provide-a-virtual-method-anchor-for-classes-in-headers)
about including out-of-line virtual functions in every class with a
vtable, even `final` ones. This means our codebase has many virtual
"anchor" functions which do nothing except control where the vtable is
emitted, and which trigger the warning. I looked into alternatives to
satisfy the policy, such as using destructors instead of introducing a
new function, but it wasn't clear if they had larger implications.

Overall, it seems like the warning is genuinely useful in most codebases
(evidenced by chromium and its dependencies), and LLVM is an unusual
case. Therefore we should enable the warning by default, and turn it off
only for LLVM builds.
2025-03-31 16:28:53 +02:00
T-Gruber
d63cc4c876
[analyzer] Unknown array lvalue element in Store (#133381)
Remove the early return for BaseRegions of type ElementRegion. Return
meaningful MemRegionVal for these cases as well.
Previous discussion:
https://discourse.llvm.org/t/lvalueelement-returns-unknownval-for-multi-dimensional-arrays/85476
2025-03-31 08:44:28 +02:00
Ryosuke Niwa
304b3c5000
[alpha.webkit.RawPtrRefMemberChecker] The checker doesn't warn Objective-C types in ivars. (#132833)
This PR fixes the bug that we weren't generating warnings when a raw
poiner is used to point to a NS type in Objective-C ivars. Also fix the
bug that we weren't suppressing this warning in system headers.
2025-03-28 17:34:51 -07:00
Balázs Benics
319045d8c4
[analyzer] Add metrics tracking time spent in Z3 solver (#133236)
These metrics would turn out to be useful for verifying an upgrade of Z3.
2025-03-28 11:26:28 +01:00
Donát Nagy
50d4ae4a62
[analyzer] Fix format attribute handling in GenericTaintChecker (#132765)
Previously `optin.taint.GenericTaint` misinterpreted the parameter
indices and produced false positives in situations when a [format
attribute](https://clang.llvm.org/docs/AttributeReference.html#format)
is applied on a non-static method. This commit fixes this bug
2025-03-28 10:20:26 +01:00
Ryosuke Niwa
a285be320a
[WebKit Checkers] Recognize Objective-C and CF pointer conversion functions. (#132784)
Recognize dynamic_objc_cast, checked_objc_cast, dynamic_cf_cast, and
checked_cf_cast.
2025-03-27 15:47:38 -07:00
Ryosuke Niwa
2b43ecd27b
[webkit.RefCntblBaseVirtualDtor] Add support for NoVirtualDestructorBase. (#132497)
This PR adds the support for WTF::NoVirtualDestructorBase, which
signifies to the checker that the class is exempt from having a virtual
destructor.
2025-03-26 16:57:49 -07:00
Abhinav Kumar
99e8321953
[clang][analyzer] Ignore unnamed bitfields in UninitializedObjectChecker (#132427)
Fixes #132001 

Co-authored-by: YLChenZ <chentongyongcz@gmail.com>
2025-03-26 16:07:51 +01:00
flovent
b55dd8f607
[clang][analyzer] Correctly handle structured bindings captured by lambda (#132579)
this PR fixes #91835.

For `DeclRefExpr` in lambda's function body, it will references to
original variable declaration in AST rather than `FieldDecl` for lambda
class, so it's needed to find the corresponding `FieldDecl` and bind
`DeclRefExpr`'s value to it.

This is already implemented for variables that are not in a structured
binding structure, so I extracted that part of the code so that it can
be used in the structured binding case.
2025-03-26 16:03:43 +01:00
Balázs Kéri
322b2fe61e
[clang][analyzer] Move 'alpha.core.FixedAddressDereference' out of alpha (#132404) 2025-03-26 09:24:18 +01:00
flovent
e60fe2e584
[clang][analyzer] Fix InvalidatedIterator crash caused by overload operator member function with explicit this (#132581)
Fixes #116372

From this PR #83585, CSA starts to model overload operator member
function with explicit this as `SimpleFunctionCall` rather than
`CXXMemberOperatorCall` (derived from `CXXInstanceCall`), so
`CXXInstanceCall` only represents a non-static C++ member function call
`with implicit this`.

For this checker, it models `operator=` for STL containers, which always
uses implicit this, so the situation using explicit this can be skipped
directly.
2025-03-24 13:51:11 +01:00
flovent
2fe7585686
[clang][analyzer] Improve the modeling of insert in MismatchedIteratorChecker (#132596)
Fixes #132010

Associative containers in STL has an unique `insert` overload member
function comparing to un-associative
containers(https://en.cppreference.com/w/cpp/container/unordered_set/insert):
```
template< class InputIt >
void insert( InputIt first, InputIt last );
```

Add support for this `insert` overload in `MismatchedIteratorChecker`,
verify if `first` and `last` belongs to the same container in this case.
2025-03-23 09:49:37 +01:00
Matheus Izvekov
14f7bd63b9
Reland: [clang] preserve class type sugar when taking pointer to member (#132401)
Original PR: #130537
Originally reverted due to revert of dependent commit. Relanding with no
changes.

This changes the MemberPointerType representation to use a
NestedNameSpecifier instead of a Type to represent the base class.

Since the qualifiers are always parsed as nested names, there was an
impedance mismatch when converting these back and forth into types, and
this led to issues in preserving sugar.

The nested names are indeed a better match for these, as the differences
which a QualType can represent cannot be expressed syntatically, and
they represent the use case more exactly, being either dependent or
referring to a CXXRecord, unqualified.

This patch also makes the MemberPointerType able to represent sugar for
a {up/downcast}cast conversion of the base class, although for now the
underlying type is canonical, as preserving the sugar up to that point
requires further work.

As usual, includes a few drive-by fixes in order to make use of the
improvements.
2025-03-21 13:20:52 -03:00
Matheus Izvekov
5151e6d7fe
Revert "Reland: [clang] preserve class type sugar when taking pointer to member" (#132280)
Reverts llvm/llvm-project#132234

Needs to be reverted due to dependency.

This blocks reverting another PR, see here:
https://github.com/llvm/llvm-project/pull/131965#issuecomment-2741619498
2025-03-20 17:52:48 -03:00
Matheus Izvekov
578f38cd08
Reland: [clang] preserve class type sugar when taking pointer to member (#132234)
Original PR: #130537
Reland after updating lldb too.

This changes the MemberPointerType representation to use a
NestedNameSpecifier instead of a Type to represent the base class.

Since the qualifiers are always parsed as nested names, there was an
impedance mismatch when converting these back and forth into types, and
this led to issues in preserving sugar.

The nested names are indeed a better match for these, as the differences
which a QualType can represent cannot be expressed syntatically, and
they represent the use case more exactly, being either dependent or
referring to a CXXRecord, unqualified.

This patch also makes the MemberPointerType able to represent sugar for
a {up/downcast}cast conversion of the base class, although for now the
underlying type is canonical, as preserving the sugar up to that point
requires further work.

As usual, includes a few drive-by fixes in order to make use of the
improvements.
2025-03-20 15:33:54 -03:00
Matheus Izvekov
6cd62ad08c
Revert "[clang] improve class type sugar preservation in pointers to members" (#132215)
Reverts llvm/llvm-project#130537

This missed updating lldb, which we didn't notice due to lack of
pre-commit CI.
2025-03-20 11:09:25 -03:00
Matheus Izvekov
9b1f905b48
[clang] improve class type sugar preservation in pointers to members (#130537)
This changes the MemberPointerType representation to use a
NestedNameSpecifier instead of a Type to represent the class.

Since the qualifiers are always parsed as nested names, there was an
impedance mismatch when converting these back and forth into types, and
this led to issues in preserving sugar.

The nested names are indeed a better match for these, as the differences
which a QualType can represent cannot be expressed syntactically, and it
also represents the use case more exactly, being either dependent or
referring to a CXXRecord, unqualified.

This patch also makes the MemberPointerType able to represent sugar for
a {up/downcast}cast conversion of the base class, although for now the
underlying type is canonical, as preserving the sugar up to that point
requires further work.

As usual, includes a few drive-by fixes in order to make use of the
improvements, and removing some duplications, for example
CheckBaseClassAccess is deduplicated from across SemaAccess and
SemaCast.
2025-03-20 10:30:24 -03:00
Donát Nagy
03adb0ec7d
[analyzer] Remove deprecated option VirtualCall:PureOnly (#131823)
VirtualCallChecker.cpp implements two related checkers:
- `optin.cplusplus.VirtualCall` which reports situations when
constructors or destructors call virtual methods (which is bugprone
because it does not trigger virtual dispatch, but can be legitmate).
- `cplusplus.PureVirtualCall` reports situations when constructors or
destructors call _pure_ virtual methods, which is an error.

Six years ago these two bug types were both reported by the same checker
(called `optin.cplusplus.VirtualCall`) and it had an option called
`PureOnly` which limited its output to the pure case.

When (in 2019) the two checker parts were separated by the commit
d3971fe97b64785c079d64bf4c8c3e2b5e1f85a1, the option `PureOnly` was
preserved for the sake of compatibility, but it is no longer useful
(when it is set to true, it just suppresses all reports from
`optin.cplusplus.VirtualCall`) so it was marked as deprecated.

I'm removing this deprecated option now because it is no longer relevant
and its presence caused minor complications when I was porting
`VirtualCallChecker.cpp` to the new multipart checker framework
(introduced in 27099982da2f5a6c2d282d6b385e79d080669546).
2025-03-19 18:22:00 +01:00
Ryosuke Niwa
bb0cc69487
[webkit.NoUncountedMemberChecker] Fix a regression that every class is treated as if it's ref countable. (#131249)
This PR fixes a regression that webkit.NoUncountedMemberChecker and
alpha.webkit.NoUncheckedMemberChecker emits warnings for every class as
if they supported ref counting and checked ptr because we were
erroneously coercing the return value of isRefCountable and
isCheckedPtrCapable, which is std::optional<bool>, to boolean values.
2025-03-18 15:43:43 -07:00
Ryosuke Niwa
4781941160
[alpha.webkit.UncountedCallArgsChecker] os_log functions should be treated as safe. (#131500)
…os_log functions should be treated as safe in call arguments checkers.

Also treat __builtin_* functions and __libcpp_verbose_abort functions as
"trivial" for the purpose in call argument checkers.
2025-03-17 23:47:10 -07:00
Balázs Kéri
fd41f1b0ce
[clang][analyzer] Add BugReporterVisitor messages for non-null fixed pointer (#129557) 2025-03-17 09:46:58 +01:00
Arseniy Zaostrovnykh
57e36419b2
[analyzer] Introduce per-entry-point statistics (#131175)
So far CSA was relying on the LLVM Statistic package that allowed us to
gather some data about analysis of an entire translation unit. However,
the translation unit consists of a collection of loosely related entry
points. Aggregating data across multiple such entry points is often
counter productive.

This change introduces a new lightweight always-on facility to collect
Boolean or numerical statistics for each entry point and dump them in a
CSV format. Such format makes it easy to aggregate data across multiple
translation units and analyze it with common data-processing tools.

We break down the existing statistics that were collected on the per-TU
basis into values per entry point.

Additionally, we enable the statistics unconditionally (STATISTIC ->
ALWAYS_ENABLED_STATISTIC) to facilitate their use (you can gather the
data with a simple run-time flag rather than having to recompile the
analyzer). These statistics are very light and add virtually no
overhead.

Co-authored-by: Balazs Benics <benicsbalazs@gmail.com>
CPP-6160
2025-03-17 08:23:31 +01:00
Ryosuke Niwa
f4043f451d
Skip more WebKit checker tests on targets where builtin is not supported. (#131501) 2025-03-15 23:39:30 -07:00
Ryosuke Niwa
75349d7ca4
Skip unretained-call-args.mm on platforms where builtin is not supported. (#131252) 2025-03-13 21:33:30 -07:00
Ryosuke Niwa
4dcba5e08d
[alpha.webkit.ForwardDeclChecker] Add a new WebKit checker for forward declarations (#130554)
Add a new static analyzer which emits warnings for function call
arguments, local variables, and member variables that are only forward
declared. These forward declaration prevents other WebKit checkers from
checking the safety of code.
2025-03-12 20:21:22 -07:00
Ryosuke Niwa
be9ca85d64
[alpha.webkit.webkit.RetainPtrCtorAdoptChecker] Add a new WebKit checker for correct use of RetainPtr, adoptNS, and adoptCF (#128679)
Add a new WebKit checker to validate the correct use of RetainPtr
constructor as well as adoptNS and adoptCF functions. adoptNS and
adoptCf are used for +1 semantics and RetainPtr constructor is used for
+0 semantics.
2025-03-12 19:09:05 -07:00
Ryosuke Niwa
27d83184c4
[alpha.webkit.UncountedCallArgsChecker] Treat an explicit construction of Ref from a Ref return value safe. (#130911)
Fix a bug that an explicit construction of Ref out of a Ref return value
would not be treated as safe. It is definitely safe albit redundant.
2025-03-12 18:33:17 -07:00
Ryosuke Niwa
caf301891a
Add unretained call args checker (#130901)
Reland https://github.com/llvm/llvm-project/pull/130729
2025-03-12 10:37:13 -07:00
Ryosuke Niwa
8b093e5e17
[webkit.UncountedLambdaCapturesChecker] Recognize std::move(protectedThis) (#130925)
In WebKit, it's a common pattern for a lambda to capture "this" along
with "protectedThis" of Ref/RefPtr type, and re-capture "this" and
"std::move(protectedThis)" for a nested inner lambda. Recognize this
pattern and treat it as safe.
2025-03-12 10:23:46 -07:00
Ryosuke Niwa
7573ee1781
Revert "[alpha.webkit.UnretainedCallArgsChecker] Add a checker for NS or CF type call arguments." (#130828)
Reverts llvm/llvm-project#130729
2025-03-11 13:42:31 -07:00
Ryosuke Niwa
9e64fc6fb7
[alpha.webkit.UnretainedCallArgsChecker] Add a checker for NS or CF type call arguments. (#130729)
This PR adds alpha.webkit.UnretainedCallArgsChecker by generalizing
RawPtrRefCallArgsChecker. It checks call arguments of NS or CF types are
backed by a RetainPtr or not. The new checker emits warnings for CF
types in Objective-C++ code regardless of ARC and it emits warning for
NS types when ARC is disabled.

Hard-code a few cases as safe: class and superclass properties on class
interfaces and constant dictionary or array literals as well as the
return value of NSClassFromString.

Finally, add the support for variadic function calls and selector
invocations.
2025-03-11 12:47:13 -07:00
Ziqing Luo
6501647508
[StaticAnalyzer] Relax the pre-condition of 'setsockopt' (#130683)
For the unix function
`int setsockopt(int, int, int, const void *, socklen_t);`, the last two
parameters represent a buffer and a size.

In case the size is zero, buffer can be null. Previously, the hard-coded
pre-condition requires the buffer to never be null, which can cause
false positives.

(rdar://146678142)
2025-03-11 10:23:03 -07:00
Balazs Benics
f10a8706a1
[analyzer] Sink false [[assume]] execution paths (#130418)
This PR splits the existing modeling of builtin assume from the
BuiltinFunctionChecker.

We just sink the execution path if we are about to leave the assume
expression with a false assumption.
Assumptions with side-effects are skipped, and ignored. Their values are
"UnknownVal" anyway.
2025-03-11 18:11:09 +01:00
Donát Nagy
a7d7db71db
[NFC][analyzer] OOB test consolidation IV: rename files (#129697)
This commit finishes the reorganization of the tests for the checker
`security.ArrayBound`.

Previously these tests were all named `out-of-bounds-*` which was only
weakly connected to the checker name; this commit moves them to a
directory named after the checker (`ArrayBound`). I decided to use a
directory instead of the more common filename prefix ("poor man's
directory") system because it seems to be a more natural use of the
filesystem and there are already a few precedents for it.

I also added (or edited) comments at the beginning of each test file to
describe their purpose; and I added a single new testcase to highlight
that the assumption note tags can be added to reports by any checker.
(Previously all tests in the file triggered out-of-bounds reports to
reveal the note tags; but that was just for convenience.)
2025-03-11 13:54:41 +01:00
Donát Nagy
405c28bb10
[NFC][analyzer] Split [[assume]] tests to a separate file (#130763)
Recently commit 7e5821bae80db3f3f0fe0d5f8ce62f79e548eed5 (which is a
re-application of 89da344e5879e5347b5057520d5230e40ae24831) added some
tests to `out-of-bounds-new.cpp`, which use a very simple out of bounds
report to reveal the internal state of the analyzer, but are otherwise
completely unrelated to the checker `security.ArrayBound`, which is
tested in `out-of-bounds-new.cpp`.

(Instead, they test handling of `__builtin_assume` and `[[assume()]]`
annotations.)

This commit reverts `out-of-bounds-new.cpp` to its previous state and
moves the new tests to a separate test file.
2025-03-11 13:52:55 +01:00
Ryosuke Niwa
3ce43c8b16
[WebKit checkers] Don't treat virtual functions as safe. (#129632)
Prior to this PR, WebKit checkers erroneously treated functions to be
safe if it has a trivial body even if it was marked as virtual. In the
case of a virtual function, it can have an override which does not pass
the triviality check so we must not make such an assumption.

This PR also restricts the allowed operator overloading while finding
the pointer origin to just operators on smart pointer types: Ref,
RefPtr, CheckedRef, CheckedPtr, RetainPtr, WeakPtr, WeakRef, unique_ptr,
and UniqueRef.
2025-03-10 21:01:39 -07:00
Ryosuke Niwa
c8ec8073aa
[alpha.webkit.NoUnretainedMemberChecker] Add a new WebKit checker for unretained member variables and ivars. (#128641)
Add a new WebKit checker for member variables and instance variables of
NS and CF types. A member variable or instance variable to a CF type
should be RetainPtr regardless of whether ARC is enabled or disabled,
and that of a NS type should be RetainPtr when ARC is disabled.
2025-03-09 23:30:08 -07:00
Ryosuke Niwa
5c3b05996f
[alpha.webkit.UnretainedLambdaCapturesChecker] Add a WebKit checker for lambda capturing NS or CF types. (#128651)
Add a new WebKit checker for checking that lambda captures of CF types
use RetainPtr either when ARC is disabled or enabled, and those of NS
types use RetainPtr when ARC is disabled.
2025-03-09 14:59:46 -07:00