Commit Graph

4997 Commits

Author SHA1 Message Date
Exile
1de8ea75d9 [analyzer] Fix crash on dereference invalid return value of getAdjustedParameterIndex() (#83585)
Fixes #78810
Thanks for Snape3058 's comment

---------

Co-authored-by: miaozhiyuan <miaozhiyuan@feysh.com>
(cherry picked from commit d4687fe7d1639ea5d16190c89a54de1f2c6e2a9a)
2024-03-12 21:53:55 -07:00
Andrey Ali Khan Bolshakov
0eb163cc27 Fix analyzer crash on 'StructuralValue' (#79764)
`OpaqueValueExpr` doesn't necessarily contain a source expression.
Particularly, after #78041, it is used to carry the type and the value
kind of a non-type template argument of floating-point type or referring
to a subobject (those are so called `StructuralValue` arguments).

This fixes #79575.

(cherry picked from commit ef67f63fa5f950f4056b5783e92e137342805d74)
2024-02-01 12:55:47 -08:00
Jie Fu
45c84f8011 [clang][analyzer] Remove unused variable in StreamChecker.cpp (NFC)
llvm-project/clang/lib/StaticAnalyzer/Checkers/StreamChecker.cpp:1125:17:
 error: unused variable 'ASTC' [-Werror,-Wunused-variable]
 1125 |     ASTContext &ASTC = C.getASTContext();
      |                 ^~~~
1 error generated.
2024-01-23 09:24:22 +08:00
Ben Shi
ea75542db9
[clang][analyzer] Support 'getdelim' and 'getline' in StreamChecker (#78693) 2024-01-23 09:19:38 +08:00
Balázs Kéri
0845514d1a
[clang][analyzer] Add function 'fscanf' to StreamChecker. (#78180) 2024-01-22 09:58:09 +01:00
Ben Shi
1d9a65b220
[clang][analyzer][NFC] Simplify ranges in StdLibraryFunctionsChecker (#78886) 2024-01-22 09:31:32 +08:00
Ben Shi
02232307ce
[clang][analyzer] Improve modeling of 'fdopen' in StdLibraryFunctionsChecker (#78680) 2024-01-20 11:02:40 +08:00
cor3ntin
e90e43fb9c
[Clang][NFC] Rename CXXMethodDecl::isPure -> is VirtualPure (#78463)
To avoid any possible confusion with the notion of pure function and the
gnu::pure attribute.
2024-01-18 15:30:58 +01:00
Jonas Devlieghere
a4fe6a1838
[clang] Upstream XROS support in Clang (#78392)
Upstream XROS support in the clang frontend and driver.
2024-01-17 21:09:15 -08:00
Ben Shi
da6806dcb4
Improve modeling of two functions in StdLibraryFunctionsChecker (#78079)
Improve 'errno' modeling of 'opendir' and 'fdopendir'.
2024-01-16 21:15:41 +08:00
Ben Shi
27d963a708
[clang][analyzer] Improve modeling of 'fseeko' and 'ftello' in StdLibraryFunctionsChecker (#77902) 2024-01-16 16:58:07 +08:00
Balázs Kéri
8550e8845c
[clang][analyzer] Add function 'fprintf' to StreamChecker. (#77613)
[clang][analyzer] Add function 'fprintf' to StreamChecker.
2024-01-12 17:00:14 +01:00
Kazu Hirata
bfb026e17c [StaticAnalyzer] Use StringRef::contains_insensitive (NFC) 2024-01-11 22:48:19 -08:00
Artem Dergachev
721dd3bc2f [analyzer] NFC: Don't regenerate duplicate HTML reports.
This is a performance optimization for HTML diagnostics output mode.

Currently they're incredibly inefficient:

* The HTMLRewriter is re-run from scratch on every file on every report.
  Each such re-run involves re-lexing the entire file and producing
  a syntax-highlighted webpage of the entire file, with text behind macros
  duplicated as pop-up macro expansion tooltips. Then, warning and note
  bubbles are injected into the page. Only the bubble part is different
  across reports; everything else can theoretically be cached.

* Additionally, if duplicate reports are emitted (with the same issue hash),
  HTMLRewriter will be re-run even though the output file is going to be
  discarded due to filename collision. This is mostly an issue for
  path-insensitive bug reports because path-sensitive bug reports
  are already deduplicated by the BugReporter as part of searching
  for the shortest bug path. But on some translation units almost 80% of
  bug reports are dry-run here.

We only get away with all this because there are usually very few reports
emitted per file. But if loud checkers are enabled, such as `webkit.*`,
this may explode in complexity and even cause the compiler to run over
the 32-bit SourceLocation addressing limit. (We're re-lexing everything
each time, remember?)

This patch hotfixes the *second* problem. Adds a FIXME for the first problem,
which will require more yak shaving to solve.

rdar://120801986
2024-01-11 15:16:10 -08:00
Ben Shi
19081f4a50
[clang][analyzer] Support 'tello' and 'fseeko' in the StreamChecker (#77580) 2024-01-11 18:48:31 +08:00
Ben Shi
66d022f326
[clang][analyzer] Fix incorrect range of 'ftell' in the StdLibraryFunctionsChecker (#77576)
According to https://pubs.opengroup.org/onlinepubs/9699919799/, the
return value of `ftell` is not restricted to `> 0`, and may return `0`
in real world.
2024-01-11 09:10:34 +08:00
Balázs Kéri
8f78dd4b92
[clang][analyzer] Add function 'ungetc' to StreamChecker. (#77331)
`StdLibraryFunctionsChecker` is updated too with `ungetc`.
2024-01-10 09:09:51 +01:00
Ben Shi
7dd20637c8
Improve modeling of 'getcwd' in the StdLibraryFunctionsChecker (#77040)
1. Improve the 'errno' modeling.
2. Improve constraints of the arguments.
2024-01-09 09:27:57 +08:00
Ben Shi
18c0f59b3e
[clang][analyzer] Support 'fdopen' in the StreamChecker (#76776) 2024-01-04 15:52:52 +08:00
Ben Shi
3db749afcb
[clang][analyzer] Improve 'errno' modeling of 'mkdtemp' (#76671) 2024-01-03 18:23:45 +08:00
Balazs Benics
8ee3dfd746 [analyzer][NFC] Take SVal and NonLoc by value 2024-01-01 22:00:32 +01:00
Balazs Benics
7619050cd7 [analyzer][NFC] Take StringRef by value 2024-01-01 22:00:32 +01:00
Balazs Benics
18f219c5ac
[analyzer][NFC] Cleanup BugType lazy-init patterns (#76655)
Cleanup most of the lazy-init `BugType` legacy.
Some will be preserved, as those are slightly more complicated to
refactor.

Notice, that the default category for `BugType` is `LogicError`. I
omitted setting this explicitly where I could.

Please, actually have a look at the diff. I did this manually, and we
rarely check the bug type descriptions and stuff in tests, so the
testing might be shallow on this one.
2024-01-01 18:53:36 +01:00
Ben Shi
925ff9e1a2
[clang][analyzer] Support 'fflush' in the StdLibraryFunctionsChecker (#76557)
Co-authored-by: Balazs Benics <benicsbalazs@gmail.com>
2023-12-30 14:49:42 +08:00
Balazs Benics
a49cf6c14a [analyzer] Fix "sprintf" parameter modeling in CStringChecker
`CE->getCalleeDecl()` returns `VarDecl` if the callee is actually a
function pointer variable. Consequently, calling `getAsFunction()` will
return null.

To workaround the case, we should use the `CallEvent::parameters()`,
which will internally recover the function being called and do the right
thing.

Fixes #74269
Depends on "[analyzer][NFC] Prefer CallEvent over CallExpr in APIs"
2023-12-28 16:06:21 +01:00
Balazs Benics
d1856b2f18 [analyzer][NFC] Prefer CallEvent over CallExpr in APIs
This change only uplifts existing APIs, without any semantic changes.
This is the continuation of 44820630df.

Benefits of using CallEvents over CallExprs:
The callee decl is traced through function pointers if possible.
This will be important to fix #74269 in a follow-up patch.
2023-12-28 15:55:33 +01:00
Kazu Hirata
68f832f56d [clang] Use StringRef::consume_front (NFC) 2023-12-25 12:54:35 -08:00
Ben Shi
73948ec6b2
[clang][analyzer] Support fflush in the StreamChecker (#74296) 2023-12-21 17:57:06 +08:00
Artem Dergachev
ef3f476097 [attributes][analyzer] Implement [[clang::suppress]] - suppress static analysis warnings.
The new attribute can be placed on statements in order to suppress
arbitrary warnings produced by static analysis tools at those statements.

Previously such suppressions were implemented as either informal comments
(eg. clang-tidy `// NOLINT:`) or with preprocessor macros (eg.
clang static analyzer's `#ifdef __clang_analyzer__`). The attribute
provides a universal, formal, flexible and neat-looking suppression mechanism.

Implement support for the new attribute in the clang static analyzer;
clang-tidy coming soon.

The attribute allows specifying which specific warnings to suppress,
in the form of free-form strings that are intended to be specific to
the tools, but currently none are actually supported; so this is also
going to be a future improvement.

Differential Revision: https://reviews.llvm.org/D93110
2023-12-13 18:09:16 -08:00
Kazu Hirata
f3dcc2351c
[clang] Use StringRef::{starts,ends}_with (NFC) (#75149)
This patch replaces uses of StringRef::{starts,ends}with with
StringRef::{starts,ends}_with for consistency with
std::{string,string_view}::{starts,ends}_with in C++20.

I'm planning to deprecate and eventually remove
StringRef::{starts,ends}with.
2023-12-13 08:54:13 -08:00
DonatNagyE
c873f77e87
[analyzer] Move alpha checker EnumCastOutOfRange to optin (#67157)
The checker EnumCastOutOfRange verifies the (helpful, but not
standard-mandated) design rule that integer to enum casts should not
produce values that don't have a corresponding enumerator. As it was
improved and cleaned up by recent changes, this commit renames it from
`alpha.cplusplus.EnumCastOutOfRange` to `optin.core.EnumCastOutOfRange`
to reflect that it's no longer alpha quality.

As this checker handles a basic language feature (which is also present
in plain C), I moved it to a "core" subpackage within "optin".

In addition to the renaming, this commit cleans up the documentation in
`checkers.rst` and adds the new example code to a test file to ensure
that it's indeed producing the behavior claimend in the documentation.
2023-12-12 16:29:37 +01:00
DonatNagyE
2f29ded4f9
[analyzer] EnumCastOutOfRangeChecker: report the value (#74503)
...that is causing the bug report when it's converted to the enum type.
This commit only improves the diagnostics and does not affect the set of
reports.
2023-12-07 11:38:34 +01:00
DonatNagyE
e4c7ee3c44
[analyzer][NFC] Simplify BugType handling in core.BitwiseShift (#74609)
Eliminate the `mutable unique_ptr` hack because it's no longer needed.
(This cleanup could be done anywhere, I'm doing it here now because it
was me who published this checker with the old hack when it was already
superfluous.)
2023-12-07 10:23:19 +01:00
Ben Shi
4699789249
[clang][analyzer][NFC] Supplement comments in evalFtell of StreamChecker (#74291) 2023-12-06 10:26:30 +08:00
DonatNagyE
dfdedaf6da
[analyzer] Switch to PostStmt callbacks in ArrayBoundV2 (#72107)
...instead of the currently used, more abstract Location callback. The
main advantage of this change is that after it the checker will check
`array[index].field` while the previous implementation ignored this
situation (because here the ElementRegion is wrapped in a FieldRegion
object). This improvement fixes PR #70187.

Note that after this change `&array[idx]` will be handled as an access
to the `idx`th element of `array`, which is technically incorrect but
matches the programmer intuitions. In my opinion it's more helpful if
the report points to the source location where the indexing happens
(instead of the location where a pointer is finally dereferenced).

As a special case, this change allows code that forms the past-the-end
pointer of an array as `&arr[size]` (but still rejects code like
`if (idx >= size) return &array[idx];` and code that dereferences a
past-the-end pointer).

In addition to this primary improvement, this change tweaks the message
for the tainted index/offset case (using the more concrete information
that's available now) and clarifies/improves a few testcases.

The main change of this commit (replacing `check::Location` with
`check::PostStmt<...>` callbacks) was already proposed in my change
https://reviews.llvm.org/D150446 and https://reviews.llvm.org/D159107 by
steakhal. Those reviews were both abandoned, but the problems that led
to abandonment were unrelated to the change that is introduced in this
PR.
2023-12-05 16:17:35 +01:00
DonatNagyE
67f387c67e
[analyzer] Let the checkers query upper and lower bounds on symbols (#74141)
This commit extends the class `SValBuilder` with the methods
`getMinValue()` and `getMaxValue()` to that work like
`SValBuilder::getKnownValue()` but return the minimal/maximal possible
value the `SVal` is not perfectly constrained.

This extension of the ConstraintManager API is discussed at:
https://discourse.llvm.org/t/expose-the-inferred-range-information-in-warning-messages/75192

As a simple proof-of-concept application of this new API, this commit
extends a message from `core.BitwiseShift` with some range information
that reports the assumptions of the analyzer.

My main motivation for adding these methods is that I'll also want to
use them in `ArrayBoundCheckerV2` to make the error messages less
awkward, but I'm starting with this simpler and less important usecase
because I want to avoid merge conflicts with my other commit
https://github.com/llvm/llvm-project/pull/72107 which is currently under
review.

The testcase `too_large_right_operand_compound()` shows a situation
where querying the range information does not work (and the extra
information is not added to the error message). This also affects the
debug utility `clang_analyzer_value()`, so the problem isn't in the
fresh code. I'll do some investigations to resolve this, but I think
that this commit is a step forward even with this limitation.
2023-12-04 17:19:50 +01:00
Ben Shi
47df664c7a
[clang][analyzer] Support fgets in the SteamChecker (#73638) 2023-11-29 19:20:49 +08:00
DonatNagyE
0424546ed4
[analyzer] Use AllocaRegion in MallocChecker (#72402)
...to model the results of alloca() and _alloca() calls. Previously it
acted as if these functions were returning memory from the heap, which
led to alpha.security.ArrayBoundV2 producing incorrect messages.
2023-11-28 16:34:44 +01:00
Ben Shi
95a47bca5e
[clang][analyzer] Support fputs in the StreamChecker (#73335) 2023-11-28 20:14:51 +08:00
Ben Shi
dd0b3c2fa6
[clang][analyzer] Support fprintf in the SecuritySyntaxChecker (#73247) 2023-11-24 16:23:56 +08:00
Ben Shi
53578e5c85
[clang][analyzer] Support fgetc in StreamChecker (#72627) 2023-11-23 23:11:19 +08:00
Gábor Spaits
527fcb8e5d
[analyzer] Add std::variant checker (#66481)
As my BSc thesis I've implemented a checker for std::variant and
std::any, and in the following weeks I'll upload a revised version of
them here.

# Prelude

@Szelethus and I sent out an email with our initial plans here:
https://discourse.llvm.org/t/analyzer-new-checker-for-std-any-as-a-bsc-thesis/65613/2
We also created a stub checker patch here:
https://reviews.llvm.org/D142354.

Upon the recommendation of @haoNoQ , we explored an option where instead
of writing a checker, we tried to improve on how the analyzer natively
inlined the methods of std::variant and std::any. Our attempt is in this
patch https://reviews.llvm.org/D145069, but in a nutshell, this is what
happened: The analyzer was able to model much of what happened inside
those classes, but our false positive suppression machinery erroneously
suppressed it. After months of trying, we could not find a satisfying
enhancement on the heuristic without introducing an allowlist/denylist
of which functions to not suppress.

As a result (and partly on the encouragement of @Xazax-hun) I wrote a
dedicated checker!

The advantage of the checker is that it is not dependent on the
standard's implementation and won't put warnings in the standard library
definitions. Also without the checker it would be difficult to create
nice user-friendly warnings and NoteTags -- as per the standard's
specification, the analysis is sinked by an exception, which we don't
model well now.

# Design ideas

The working of the checker is straightforward: We find the creation of
an std::variant instance, store the type of the variable we want to
store in it, then save this type for the instance. When retrieving type
from the instance we check what type we want to retrieve as, and compare
it to the actual type. If the two don't march we emit an error.

Distinguishing variants by instance (e.g. MemRegion *) is not the most
optimal way. Other checkers, like MallocChecker uses a symbol-to-trait
map instead of region-to-trait. The upside of using symbols (which would
be the value of a variant, not the variant itself itself) is that the
analyzer would take care of modeling copies, moves, invalidation, etc,
out of the box. The problem is that for compound types, the analyzer
doesn't create a symbol as a result of a constructor call that is fit
for this job. MallocChecker in contrast manipulates simple pointers.

My colleges and I considered the option of making adjustments directly
to the memory model of the analyzer, but for the time being decided
against it, and go with the bit more cumbersome, but immediately viable
option of simply using MemRegions.

# Current state and review plan

This patch contains an already working checker that can find and report
certain variant/any misuses, but still lands it in alpha. I plan to
upload the rest of the checker in later patches.

The full checker is also able to "follow" the symbolic value held by the
std::variant and updates the program state whenever we assign the value
stored in the variant. I have also built a library that is meant to
model union-like types similar to variant, hence some functions being a
bit more multipurpose then is immediately needed.

I also intend to publish my std::any checker in a later commit.

---------

Co-authored-by: Gabor Spaits <gabor.spaits@ericsson.com>
Co-authored-by: Balazs Benics <benicsbalazs@gmail.com>
2023-11-21 14:02:22 +01:00
Kazu Hirata
f8e8530f73 [StaticAnalyzer] Fix warnings about missing override
This patch fixes:

  clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp:609:23:
  error: 'describe' overrides a member function but is not marked
  'override' [-Werror,-Winconsistent-missing-override]

  clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp:627:23:
  error: 'describe' overrides a member function but is not marked
  'override' [-Werror,-Winconsistent-missing-override]
2023-11-16 11:06:01 -08:00
Balázs Kéri
699e1019af
[clang][analyzer] Improve 'errno' handling in StdLibraryFunctionsChecker. (#71392)
The checker now displays one combined note tag for errno-related and
"case"-related notes. Previous functions in the errno-modeling part that
were used for construction of note tags are removed. The note tag added
by StdLibraryFunctionsChecker contains the code to display the note tag
for 'errno' (this was done previously by these removed functions).
2023-11-16 18:06:51 +01:00
Ben Shi
917a550f4d
[clang][Analyzer][NFC] Use condition type for comparison in several checkers (#72358) 2023-11-16 07:37:33 +08:00
Ben Shi
d5af076a99
[clang][analyzer] Support fputc in StreamChecker (#71518) 2023-11-16 07:36:57 +08:00
Ben Shi
0f7f8dbcf5
[clang][analyzer] Restrict 'fopen' modeling to POSIX versions in SimpleStreamChecker (#72016) 2023-11-15 18:17:32 +08:00
Balázs Kéri
41fe5c9a08
[clang][analyzer] Improve StdLibraryFunctionsChecker 'readlink' modeling. (#71373)
The functions 'readlink' and 'readlinkat' do return 0 only if the
'bufsize' argument is 0.
2023-11-14 10:58:05 +01:00
Kazu Hirata
651c502fc8 [clang] Stop including llvm/ADT/DenseSet.h (NFC)
Identified with clangd.
2023-11-11 22:25:48 -08:00
Kazu Hirata
f7a759d1b7 [StaticAnalyzer] Stop including llvm/ADT/ImmutableMap.h (NFC)
Identified with clangd.
2023-11-11 13:36:08 -08:00