102 Commits

Author SHA1 Message Date
Manna, Soumi
06c9cc7eaa [NFC][CLANG] Fix static analyzer bugs about unnecessary object copies with auto
Reviewed By: tahonermann

Differential Revision: https://reviews.llvm.org/D159474
2023-09-28 09:00:55 -07:00
Ziqing Luo
700baeb765 [-Wunsafe-buffer-usage] Use Strategy to determine whether to fix a parameter
- Use Strategy to determine whether to fix a parameter
- Fix the `Strategy` construction so that only variables on the graph
are assigned the `std::span` strategy

Reviewed by: t-rasmud (Rashmi Mudduluru), NoQ (Artem Dergachev)

Differential revision: https://reviews.llvm.org/D157441
2023-09-21 15:06:22 -07:00
Ziqing Luo
33f6161d9e [-Wunsafe-buffer-usage] Group parameter fix-its
For a function `F` whose parameters need to be fixed, we group fix-its
of F's parameters together so that either all of the parameters get
fixed or none of them gets fixed.

Reviewed by: NoQ (Artem Dergachev), t-rasmud (Rashmi Mudduluru), jkorous (Jan Korous)

Differential revision: https://reviews.llvm.org/D153059
2023-09-21 12:45:30 -07:00
Kazu Hirata
c81ff8b700 [Analysis] Use DenseMap::lookup (NFC) 2023-08-27 00:18:17 -07:00
Ziqing Luo
b58e528898 [-Wunsafe-buffer-usage] Stop generating incorrect fix-its for variable declarations with unsupported specifiers
We have to give up on fixing a variable declaration if it has
specifiers that are not supported yet.  We could support these
specifiers incrementally using the same approach as how we deal with
cv-qualifiers. If a fixing variable declaration has a storage
specifier, instead of trying to find out the source location of the
specifier or to avoid touching it, we add the keyword to a
canonicalized place in the fix-it text that replaces the whole
declaration.

Reviewed by: NoQ (Artem Dergachev), jkorous (Jan Korous)

Differential revision: https://reviews.llvm.org/D156192
2023-08-21 16:34:44 -07:00
Ziqing Luo
3a67b91238 [-Wunsafe-buffer-usage] Refactor to let local variable fix-its and parameter fix-its share common code
Refactor the code for local variable fix-its so that it reuses the
code for parameter fix-its, which is in general better. For example,
cv-qualifiers are supported.

Reviewed by: NoQ (Artem Dergachev), t-rasmud (Rashmi Mudduluru)

Differential revision: https://reviews.llvm.org/D156189
2023-08-21 14:50:04 -07:00
Younan Zhang
69a8636c4d [clang][NFC] Remove redundant whitespaces
This breaks the clang check-format on CI.

+ grep -rnI '[[:blank:]]$' clang/lib clang/include clang/docs
clang/lib/Analysis/UnsafeBufferUsage.cpp:2277:#endif
2023-08-19 19:25:32 +08:00
ziqingluo-90
acc8a33b25 [-Wunsafe-buffer-usage][NFC] Refactor getFixIts---where fix-its are generated
Refactor the getFixIts function for better readability.

Reviewed by: NoQ (Artem Dergachev), t-rasmud (Rashmi Mudduluru)

Differential revision: https://reviews.llvm.org/D156762
2023-08-18 17:44:22 -07:00
ziqingluo-90
472a510bbc Re-land "[-Wunsafe-buffer-usage][NFC] Slightly refactor and optimize the code"
This reverts commit ac9a76d7487b9af1ace626eb90064194cb12c53d.

Previously an abstract class has no pure virtual function.  It causes build error on some bots.
2023-08-18 13:22:01 -07:00
Anton Rydahl
630ba7d705 Removed whitespace that made "grep -rnI '[[:blank:]]$' clang/lib clang/include clang/docs" fail 2023-08-18 12:12:36 -07:00
Ziqing Luo
ac9a76d748 Revert "[-Wunsafe-buffer-usage][NFC] Slightly refactor and optimize the code"
This reverts commit 843784764ab58e35f8aa2da97f07dc5e810f4bcb.
There is a build failure caused by this commit.
2023-08-17 16:42:30 -07:00
Ziqing Luo
843784764a [-Wunsafe-buffer-usage][NFC] Slightly refactor and optimize the code
Slightly refactor and optimize the code in preparation for
implementing grouping parameters for a single fix-it.

Reviewed by: NoQ (Artem Dergachev), t-rasmud (Rashmi Mudduluru)

Differential revision: https://reviews.llvm.org/D156474
2023-08-17 16:24:47 -07:00
Rashmi Mudduluru
2afcda693a [-Wunsafe-buffer-usage] Fix assertion failure in case of BindingDecl
Differential Revision: https://reviews.llvm.org/D158112#inline-1530312
2023-08-17 16:18:38 -07:00
Ziqing Luo
41279e870f [-Wunsafe-buffer-usage] Refactor and improve for parameter fix-its
- Factor out the code that will be shared by both parameter and local variable fix-its
- Add a check to ensure that a TypeLoc::isNull is false before using the TypeLoc
- Remove the special check for whether a fixing variable involves unnamed types. This check is unnecessary now.
- Move tests for cv-qualified parameters and unnamed types out of the "...-unsupported.cpp" test file.

Reviewed by: NoQ (Artem Dergachev)

Differential revision: https://reviews.llvm.org/D156188
2023-08-17 15:27:38 -07:00
Rashmi Mudduluru
cf1c64b94d [-Wunsafe-buffer-usage] Replace assert that declarations are always found
Differential Revision: https://reviews.llvm.org/D157018
2023-08-15 15:41:56 -07:00
Amirreza Ashouri
82b94a9f7b Fix the linting problems in UnsafeBufferUsage.cpp
Fix the linting problems which causes `clang/utils/ci/run-buildbot check-format` to return 1.

Also make a correction for the email address of the author of
0fd4175907b40fe63131482c162d7e0f76000521:

  The correct email address is "ar.ashouri999@gmail.com", not "ar.ashouri999@google.com".

Reviewed by: ziqingluo-90 (Ziqing Luo)

Differential revision: https://reviews.llvm.org/D155814
2023-08-01 16:17:40 -07:00
dingfei
38b648b4a1 [clang][Analysis][NFC] Remove indented empty lines (fix format checking) 2023-07-28 08:36:51 +08:00
Simon Pilgrim
4cd7d8e30a [clang] UnsafeBufferUsage.cpp - fix MSVC "not all control paths return a value" warning. NFC. 2023-07-27 10:17:57 +01:00
Rashmi Mudduluru
a6ae740e74 [-Wunsafe-buffer-usage] Add a facility for debugging low fixit coverage
Differential Revision: https://reviews.llvm.org/D154880
2023-07-26 17:07:36 -07:00
Ziqing Luo
cfcf76c6ad [-Wunsafe-buffer-usage] Ignore the FixableGadgets that will not be fixed at an earlier stage
`FixableGadget`s are not always associated with variables that are unsafe
(warned). For example, they could be associated with variables whose
unsafe operations are suppressed or that are not used in any unsafe
operation. Such `FixableGadget`s will not be fixed. Removing these
`FixableGadget` as early as possible helps improve the performance
and stability of the analysis.

Reviewed by: NoQ (Artem Dergachev), t-rasmud (Rashmi Mudduluru)

Differential revision: https://reviews.llvm.org/D155524
2023-07-25 16:58:27 -07:00
Rashmi Mudduluru
070358ec92 [-Wunsafe-buffer-usage] Fix a fallthrough case in UPCStandalonePointer getFixits
Differential Revision: https://reviews.llvm.org/D155526
2023-07-25 13:40:33 -07:00
Mark de Wever
910450a28b [NFC][clang] Fixes whitespace.
These changes cause the Buildkite CI to fail.
2023-07-22 16:50:58 +02:00
Rashmi Mudduluru
27c1033783 [WIP][-Wunsafe-buffer-usage] Handle lambda expressions within a method.
Differential Revision: https://reviews.llvm.org/D150386
2023-07-20 10:00:16 -07:00
ziqingluo-90
a6302b6934 [-Wunsafe-buffer-usage] Check source location validity before using TypeLocs
The safe-buffer analysis analyzes TypeLocs of types of variable
declarations in order to get source locations of them.

However, in some cases, the source locations of a TypeLoc are not
valid. Using invalid source locations results in assertion violation
or incorrect analysis or fix-its.

It is still not clear to me in what circumstances a TypeLoc does not
have valid source locations (it looks like a bug in Clang to me, but
it is not our responsibility to fix it). So we will conservatively
give up the analysis when required source locations are not valid.

Reviewed By: NoQ (Artem Dergachev)

Differential Revision: https://reviews.llvm.org/D155667
2023-07-19 15:04:42 -07:00
Ziqing Luo
4b5f17e008 [-Wunsafe-buffer-usage] Do not assert that function parameters have names
It is possible that a function parameter does not have a name even in
a function definition.  This patch deals with such cases in generating
function overload fix-its for safe buffers.

Reviewed by: NoQ (Artem Dergachev)

Differential revision: https://reviews.llvm.org/D155641
2023-07-19 14:14:28 -07:00
Evan Wilde
d7b45945fb
[NFC][clang] Fix format in UnsafeBufferUsage.cpp
There were two whitespaces on an otherwise empty that were causing the
bots to fail due to formatting issues. Deleting extra whitespace now.
2023-07-16 22:45:29 -07:00
ziqingluo-90
a07a6f6c74 Re-land "5b012bf5ab5fcb840fe7f6c8664b8981ce6f24f3"
Removed dependency on `clangSema` from UnsafeBufferAnalysis.
2023-07-15 16:11:37 -07:00
NAKAMURA Takumi
ba7273b025 Reformat 2023-07-15 13:55:11 +09:00
NAKAMURA Takumi
c915908f6d Revert D150338, "[-Wunsafe-buffer-usage] Improving insertion of the [[clang::unsafe_buffer_usage]] attribute"
clangAnalysis should not depend on clangSema.

This reverts commit 5b012bf5ab5fcb840fe7f6c8664b8981ce6f24f3.
2023-07-15 13:55:11 +09:00
ziqingluo-90
5b012bf5ab [-Wunsafe-buffer-usage] Improving insertion of the [[clang::unsafe_buffer_usage]] attribute
For a fix-it that inserts the `[[clang::unsafe_buffer_usage]]`
attribute, it will lookup existing macros defined for the attribute
and use the (last defined such) macro directly. Fix-its will use raw
`[[clang::unsafe_buffer_usage]]` if no such macro is defined.

The implementation mimics how a similar machine for the
`[[fallthrough]]` attribute was implemented.

Reviewed by: NoQ (Artem Dergachev)

Differential revision: https://reviews.llvm.org/D150338
2023-07-14 14:47:07 -07:00
Artem Dergachev
7c7b191434 [-Wunsafe-buffer-usage] Unbreak clang-format on UnsafeBufferUsage.cpp. NFC. 2023-07-10 15:57:10 -07:00
Nikolas Klauser
f6d557ee34 [clang][NFC] Remove trailing whitespaces and enforce it in lib, include and docs
A lot of editors remove trailing whitespaces. This patch removes any trailing whitespaces and makes sure that no new ones are added.

Reviewed By: erichkeane, paulkirth, #libc, philnik

Spies: wangpc, aheejin, MaskRay, pcwang-thead, cfe-commits, libcxx-commits, dschuff, nemanjai, arichardson, kbarton, asb, rbar, johnrusso, simoncook, sabuasal, niosHD, jrtc27, zzheng, edward-jones, rogfer01, MartinMosbeck, brucehoult, the_o, PkmX, jocewei, Jim, s.egerton, sameer.abuasal, apazos, luismarques, martong, frasercrmck, steakhal, luke

Differential Revision: https://reviews.llvm.org/D151963
2023-06-26 09:34:36 -07:00
Rashmi Mudduluru
db3dcedb9c [-Wunsafe-buffer-usage] Handle pointer initializations for grouping related variables
Differential Revision: https://reviews.llvm.org/D150489
2023-06-21 15:54:09 -07:00
Rashmi Mudduluru
d1ae844dc2 [-Wunsafe-buffer-usage] Do not emit fixits for C++ interfaces with C linkage
Differential Revision: https://reviews.llvm.org/D153064
2023-06-16 13:28:34 -07:00
ziqingluo-90
10e8300536 [-Wunsafe-buffer-usage] Remove the unnecessary const qualifier in safe buffer analysis
Casting away the qualifier raises a warning in 1e270be0886c3a770e7a967679552a02dfc1dca9
2023-06-09 16:41:49 -07:00
ziqingluo-90
1e270be088 [-Wunsafe-buffer-usage] Add fix-its for function parameters using the span strategy
Generate fix-its for function parameters that are raw pointers used
unsafely.  Currently, the analyzer fixes one parameter at a time.

Fix-its for a function parameter includes:

- Fix the parameter declaration of the definition, result in a new
  overload of the function. We call the function with the original
  signature the old overload.
- For any other existing declaration of the old overload, mark it with
  the [[unsafe_buffer_usage]] attribute and generate a new overload
  declaration next to it.
- Creates a new definition for the old overload, which is simply
  defined by a call to the new overload.

Reviewed by: NoQ (Artem Dergachev), t-rasmud (Rashmi Mudduluru), and
             jkorous (Jan Korous)

Differential revision: https://reviews.llvm.org/D143048
2023-06-09 15:44:38 -07:00
Rashmi Mudduluru
171dfc5462 [-Wunsafe-buffer-usage] Group variables associated by pointer assignments
Differential Revision: https://reviews.llvm.org/D145739
2023-05-25 11:31:27 -07:00
Douglas Yung
2e6325c71f Revert "[-Wunsafe-buffer-usage] Group variables associated by pointer assignments"
This reverts commit ee6b08e99375fc48d1e5848704a66c2e8e57eb3b.

One of the added tests warn-unsafe-buffer-usage-multi-decl-warnings.cpp does
not seem to be deterministic, and seems to be especially problematic on Windows.

Failures of this one test on llvm-clang-x86_64-sie-win:
- https://lab.llvm.org/buildbot/#/builders/216/builds/21758
- https://lab.llvm.org/buildbot/#/builders/216/builds/21761
- https://lab.llvm.org/buildbot/#/builders/216/builds/21762
- https://lab.llvm.org/buildbot/#/builders/216/builds/21765
- https://lab.llvm.org/buildbot/#/builders/216/builds/21770
- https://lab.llvm.org/buildbot/#/builders/216/builds/21771
- https://lab.llvm.org/buildbot/#/builders/216/builds/21773
- https://lab.llvm.org/buildbot/#/builders/216/builds/21776
- https://lab.llvm.org/buildbot/#/builders/216/builds/21777
- https://lab.llvm.org/buildbot/#/builders/216/builds/21778
- https://lab.llvm.org/buildbot/#/builders/216/builds/21779

Other random bot failures:
- https://lab.llvm.org/buildbot/#/builders/65/builds/9821
- https://lab.llvm.org/buildbot/#/builders/65/builds/9822
- https://lab.llvm.org/buildbot/#/builders/65/builds/9824
- https://lab.llvm.org/buildbot/#/builders/119/builds/13440
- https://lab.llvm.org/buildbot/#/builders/119/builds/13442
- https://lab.llvm.org/buildbot/#/builders/119/builds/13444
- https://lab.llvm.org/buildbot/#/builders/119/builds/13445
- https://lab.llvm.org/buildbot/#/builders/60/builds/12156
- https://lab.llvm.org/buildbot/#/builders/60/builds/12157
- https://lab.llvm.org/buildbot/#/builders/60/builds/12160
2023-05-25 02:10:32 -07:00
Rashmi Mudduluru
ee6b08e993 [-Wunsafe-buffer-usage] Group variables associated by pointer assignments
Differential Revision: https://reviews.llvm.org/D145739
2023-05-24 16:20:55 -07:00
Artem Dergachev
b7bdf1996f [-Wunsafe-buffer-usage] Hide fixits/suggestions behind an extra flag.
This patch implements a new clang driver flag -fsafe-buffer-usage-suggestions
which allows turning the smart suggestion machine on and off (defaults to off).
This is valuable for stability reasons, as the machine is being rapidly improved\
and we don't want accidental breakages to ruin the build for innocent users.
It is also arguably useful in general because it enables separation of concerns
between project contributors: some users will actively update the code to
conform to the programming model, while others simply want to make sure that
they aren't regressing it. Finally, there could be other valid reasons to
opt out of suggestions entirely on some codebases (while continuing to enforce
-Wunsafe-buffer-usage warnings), such as lack of access to hardened libc++
(or even to the C++ standard library in general) on the target platform.

When the flag is disabled, the unsafe buffer usage analysis is reduced to
an extremely minimal mode of operation that contains virtually no smarts:
not only it doesn't offer automatic fixits, but also textual suggestions
such as "change the type of this variable to std::span to preserve bounds
information" are not displayed, and in fact the machine doesn't even try
to blame specific variables in the first place, it simply warns on
the operations and leaves everything else to the user. So this flag turns off
a lot more of our complex machinery than what we already turn off in presence
of say -fno-diagnostic-fixit-info.

The flag is discoverable: when it's off, the warnings are accompanied by a note:
telling the user that there's a flag they can use.

Differential Revision: https://reviews.llvm.org/D146669
2023-05-18 17:20:27 -07:00
Manna, Soumi
5ebff1ac1b [NFC][Clang] Fix Coverity issues of copy without assign
This patch adds missing copy/move assignment operator to the class which has user-defined copy/move constructor.

Reviewed By: tahonermann

Differential Revision: https://reviews.llvm.org/D149718
2023-05-14 19:49:28 -07:00
Kazu Hirata
e955e4fba6 [clang] Replace None with std::nullopt in comments (NFC)
This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2023-05-04 22:42:52 -07:00
MalavikaSamak
b63b2c2350 Reland "[-Wunsafe-buffer-usage] Bug fix: Handles the assertion violations for code within macros"
This reverts commit 84ec1f7725d4f4575474b59467e598d7c5528a4e.
2023-04-24 16:49:13 -07:00
MalavikaSamak
84ec1f7725 Revert "[-Wunsafe-buffer-usage] Bug fix: Handles the assertion violations for code within macros"
This reverts commit 9bd0db80784e30d40a4a65f1b47109c833f05b54.
2023-04-24 16:48:46 -07:00
Jie Fu
1d097ad73b [clang] Fix -Wimplicit-fallthrough in UnsafeBufferUsage.cpp [NFC]
/Users/jiefu/llvm-project/clang/lib/Analysis/UnsafeBufferUsage.cpp:1272:5: error: unannotated fall-through between switch labels [-Werror,-Wimplicit-fallthrough]
    case Strategy::Kind::Wontfix:
    ^
/Users/jiefu/llvm-project/clang/lib/Analysis/UnsafeBufferUsage.cpp:1272:5: note: insert 'LLVM_FALLTHROUGH;' to silence this warning
    case Strategy::Kind::Wontfix:
    ^
    LLVM_FALLTHROUGH;
/Users/jiefu/llvm-project/clang/lib/Analysis/UnsafeBufferUsage.cpp:1272:5: note: insert 'break;' to avoid fall-through
    case Strategy::Kind::Wontfix:
    ^
    break;
1 error generated.
2023-04-25 07:34:39 +08:00
MalavikaSamak
9bd0db8078 [-Wunsafe-buffer-usage] Bug fix: Handles the assertion violations for code within macros
When macros get expanded, the source location for the expanded code received by the Fixable
gadgets is invalid. We do not want to emit fixits for macro expanded code and it currently
crashes the analysis. This patch fixes the assertion violations that were introduced for
handling code with such invalid locations.

Reviewed by: NoQ, ziqingluo-90, jkorous

Differential revision: https://reviews.llvm.org/D146450
2023-04-24 16:10:15 -07:00
MalavikaSamak
9516419c50 Revert "Revert "[-Wunsafe-buffer-usage] Handle unevaluated contexts that contain unsafe buffer usages""
This reverts commit 7bf5f4692ad6f9ba2d5c155f6b630049bb59876f and adding -frtti flag to support PS4/PS5 builds.
2023-04-19 16:53:34 -07:00
MalavikaSamak
7bf5f4692a Revert "[-Wunsafe-buffer-usage] Handle unevaluated contexts that contain unsafe buffer usages"
This reverts commit 777eb4bcfc3265359edb7c979d3e5ac699ad4641.
2023-04-19 16:09:21 -07:00
MalavikaSamak
777eb4bcfc [-Wunsafe-buffer-usage] Handle unevaluated contexts that contain unsafe buffer usages
This patch handles unevaluated contexts to ensure no warnings are produced by the machinery
for buffer access made within an unevaluated contexts. However, such accesses must be
considered by a FixableGadget and produce the necessary fixits.

Reviewed by: NoQ, ziqingluo-90, jkorous

Differential revision: https://reviews.llvm.org/D144905
2023-04-19 15:53:21 -07:00
Manna, Soumi
7e1b62bd9c [NFC][Clang] Fix static analyzer tool remark about missing user-defined assignment operator
Reported by Coverity:

Copy without assign
This class has a user-defined copy constructor but no user-defined assignment operator. If the copy constructor is necessary to manage owned resources then a corresponding assignment operator is usually required. If an object of this type is assigned memory leaks and/or use-after-free errors may occur. Note that a compiler-generated assignment operator will perform only a bit-wise copy for any fields that do not have their own assignment operators defined.

Class has user-written copy constructor but no user-written assignment operator

copy_without_assign: Class <unnamed>::DeclUseTracker has a user-written copy constructor <unnamed>::DeclUseTracker::DeclUseTracker(<unnamed>::DeclUseTracker const &) =delete but no corresponding user-written assignment operator.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D148189
2023-04-17 22:21:30 -04:00