139 Commits

Author SHA1 Message Date
Jan Korous
8b6ae9bd74 [-Wunsafe-buffer-usage] Emit Fix-Its only for C++20 and later standards
The transformation strategy we are bringing up heavily relies on std::span which was introduced as part of C++20.

Differential Revision: https://reviews.llvm.org/D143455
2023-02-09 17:28:27 -08:00
Ziqing Luo
829bcb06ec [-Wunsafe-buffer-usage] Add unsafe buffer checking opt-out pragmas
Add a pair of clang pragmas:
- `#pragma clang unsafe_buffer_usage begin` and
- `#pragma clang unsafe_buffer_usage end`,
which specify the start and end of an (unsafe buffer checking) opt-out
region, respectively.

Behaviors of opt-out regions conform to the following rules:

- No nested nor overlapped opt-out regions are allowed. One cannot
  start an opt-out region with `... unsafe_buffer_usage begin` but never
  close it with `... unsafe_buffer_usage end`. Mis-use of the pragmas
  will be warned.
- Warnings raised from unsafe buffer operations inside such an opt-out
  region will always be suppressed. This behavior CANNOT be changed by
  `clang diagnostic` pragmas or command-line flags.
- Warnings raised from unsafe operations outside of such opt-out
  regions may be reported on declarations inside opt-out
  regions. These warnings are NOT suppressed.
- An un-suppressed unsafe operation warning may be attached with
  notes. These notes are NOT suppressed as well regardless of whether
  they are in opt-out regions.

The implementation maintains a separate sequence of location pairs
representing opt-out regions in `Preprocessor`.  The `UnsafeBufferUsage`
analyzer reads the region sequence to check if an unsafe operation is
in an opt-out region. If it is, discard the warning raised from the
operation immediately.

This is a re-land after I reverting it at 9aa00c8a306561c4e3ddb09058e66bae322a0769.
The compilation error should be resolved.

Reviewed by: NoQ

Differential revision: https://reviews.llvm.org/D140179
2023-02-08 14:12:03 -08:00
Ziqing Luo
9aa00c8a30 Revert "[-Wunsafe-buffer-usage] Add unsafe buffer checking opt-out pragmas"
This reverts commit aef05b5dc5c566bcaa15b66c989ccb8d2841ac71.
It causes a buildbot failure: https://lab.llvm.org/buildbot/#/builders/216/builds/16879/steps/6/logs/stdio
2023-02-07 17:06:20 -08:00
Ziqing Luo
aef05b5dc5 [-Wunsafe-buffer-usage] Add unsafe buffer checking opt-out pragmas
Add a pair of clang pragmas:
- `#pragma clang unsafe_buffer_usage begin` and
- `#pragma clang unsafe_buffer_usage end`,
which specify the start and end of an (unsafe buffer checking) opt-out
region, respectively.

Behaviors of opt-out regions conform to the following rules:

- No nested nor overlapped opt-out regions are allowed. One cannot
  start an opt-out region with `... unsafe_buffer_usage begin` but never
  close it with `... unsafe_buffer_usage end`. Mis-use of the pragmas
  will be warned.
- Warnings raised from unsafe buffer operations inside such an opt-out
  region will always be suppressed. This behavior CANNOT be changed by
  `clang diagnostic` pragmas or command-line flags.
- Warnings raised from unsafe operations outside of such opt-out
  regions may be reported on declarations inside opt-out
  regions. These warnings are NOT suppressed.
- An un-suppressed unsafe operation warning may be attached with
  notes. These notes are NOT suppressed as well regardless of whether
  they are in opt-out regions.

The implementation maintains a separate sequence of location pairs
representing opt-out regions in `Preprocessor`.  The `UnsafeBufferUsage`
analyzer reads the region sequence to check if an unsafe operation is
in an opt-out region. If it is, discard the warning raised from the
operation immediately.

Reviewed by: NoQ

Differential revision: https://reviews.llvm.org/D140179
2023-02-07 16:54:39 -08:00
Ziqing Luo
692da6245d [-Wunsafe-buffer-usage] Filter out conflicting fix-its
Two fix-its conflict if they have overlapping source ranges. We shall
not emit conflicting fix-its.  This patch checks conflicts in fix-its
generated for one variable (including variable declaration fix-its and
variable usage fix-its). If there is any, we do NOT emit any fix-it
for that variable.

Reviewed by: NoQ

Differential revision: https://reviews.llvm.org/D141338
2023-02-07 16:15:28 -08:00
Ziqing Luo
bdf4f2bea5 [-Wunsafe-buffer-usage] Generate fix-it for local variable declarations
Use clang fix-its to transform declarations of local variables, which
are used for buffer access , to be of std::span type.

We placed a few limitations to keep the solution simple:
- it only transforms local variable declarations (no parameter declaration);
- it only considers single level pointers, i.e., pointers of type T * regardless of whether T is again a pointer;
- it only transforms to std::span types (no std::array, or std::span::iterator, or ...);
    - it can only transform a VarDecl that belongs to a DeclStmt whose has a single child.

One of the purposes of keeping this patch simple enough is to first
evaluate if fix-it is an appropriate approach to do the
transformation.

This commit was reverted by 622be09c815266632e204eaf1c7a35f050220459
for a compilation warning and now it is fixed.

Reviewed by: NoQ, jkorous

Differential revision: https://reviews.llvm.org/D139737
2023-02-07 15:40:19 -08:00
Ziqing Luo
622be09c81 Revert "[-Wunsafe-buffer-usage] Generate fix-it for local variable declarations"
This reverts commit a29e67614c3b7018287e5f68c57bba7618aa880e.
2023-02-07 14:47:43 -08:00
Ziqing Luo
a29e67614c [-Wunsafe-buffer-usage] Generate fix-it for local variable declarations
Use clang fix-its to transform declarations of local variables, which are used for buffer access , to be of std::span type.

We placed a few limitations to keep the solution simple:
- it only transforms local variable declarations (no parameter declaration);
- it only considers single level pointers, i.e., pointers of type T * regardless of whether T is again a pointer;
- it only transforms to std::span types (no std::array, or std::span::iterator, or ...);
- it can only transform a VarDecl that belongs to a DeclStmt whose has a single child.

One of the purposes of keeping this patch simple enough is to first
evaluate if fix-it is an appropriate approach to do the
transformation.

Reviewed by: NoQ, jkorous

Differential revision: https://reviews.llvm.org/D139737
2023-02-07 13:17:44 -08:00
Rashmi Mudduluru
acc3cc69e4 [-Wunsafe-buffer-usage] Introduce the unsafe_buffer_usage attribute
Differential Revision: https://reviews.llvm.org/D138940
2023-01-31 11:43:34 -08:00
Kazu Hirata
5c9013e266 Use std::optional instead of llvm::Optional (NFC) 2023-01-28 00:45:19 -08:00
Jan Korous
f252333b97 [-Wunsafe-buffer-usage][NFC] Fix Fixables filtering
We have WIP Fixables for local variables and this central part of the machinery
was dropping Fixables attached to local variables instead of keeping those and
dropping everything else.
We are in the process of rewriting our patches for emitting fixits after we
discovered a conceptual problem in our design.
That is why there's currently no tests that would've detected the issue but
that will change very shortly.
2023-01-18 18:54:48 -08:00
Jan Korous
237ca436ad [-Wunsafe-buffer-usage] Group diagnostics by variable
Differential Revision: https://reviews.llvm.org/D141356
2023-01-18 15:00:22 -08:00
Jan Korous
214312ef7e [-Wunsafe-buffer-usage][NFC] Refactor checkUnsafeBufferUsage
Differential Revision: https://reviews.llvm.org/D141333
2023-01-17 18:00:47 -08:00
Rashmi Mudduluru
fe93da22aa [-Wunsafe-buffer-usage] Emit warnings about unsafe operations on arrays
Differential Revision: https://reviews.llvm.org/D141725/new/
2023-01-17 16:30:13 -08:00
Kazu Hirata
6ad0788c33 [clang] Use std::optional instead of llvm::Optional (NFC)
This patch replaces (llvm::|)Optional< with std::optional<.  I'll post
a separate patch to remove #include "llvm/ADT/Optional.h".

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-01-14 12:31:01 -08:00
Kazu Hirata
a1580d7b59 [clang] Add #include <optional> (NFC)
This patch adds #include <optional> to those files containing
llvm::Optional<...> or Optional<...>.

I'll post a separate patch to actually replace llvm::Optional with
std::optional.

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-01-14 11:07:21 -08:00
ziqingluo-90
5be422b492 [Fix][-Wunsafe-buffer-usage] Add a new forEachDescendant matcher that skips callable declarations
The original patch does include a `new` statement without a matching
`delete`, causing Sanitizer warnings in
https://lab.llvm.org/buildbot/#/builders/5/builds/30522/steps/13/logs/stdio.

This commit is a fix to it.

Differential Revision: https://reviews.llvm.org/D138329
2023-01-06 14:38:12 -08:00
ziqingluo-90
42b1d08b95 Revert "[Fix][-Wunsafe-buffer-usage] Add a new forEachDescendant matcher that skips callable declarations"
This reverts commit 6d140b952805bd9277fba666520ce46c19f2c637.

This commit may causes `test/SemaCXX/warn-unsafe-buffer-usage.cpp` failure.
2023-01-06 13:37:13 -08:00
ziqingluo-90
6d140b9528 [Fix][-Wunsafe-buffer-usage] Add a new forEachDescendant matcher that skips callable declarations
The original patch does include a `new` statement without a matching
`delete`, causing Sanitizer warnings in
https://lab.llvm.org/buildbot/#/builders/5/builds/30522/steps/13/logs/stdio.

This commit is a fix to it.

Differential Revision: https://reviews.llvm.org/D138329
2023-01-06 12:32:35 -08:00
MalavikaSamak
87fe37d9f8 [-Wunsafe-buffer-usage] Changing the use of None with std::nullopt to address a warning. 2023-01-06 12:31:14 -08:00
MalavikaSamak
50d4a1f70e [-Wunsafe-buffer-usage] Safe-buffers re-architecture to introduce Fixable gadgets
Re-architecture of safe-buffers gadgets to re-classify them as warning and fixable
gadgets. The warning gadgets identify unsafe operations on buffer variables and
emit suitable warnings. While the fixable gadgets consider all operations on
variables identified by the warning gadgets and emit necessary fixits.

Differential Revision: https://reviews.llvm.org/D140062?id=486625
2023-01-06 11:45:23 -08:00
ziqingluo-90
7d0d34fbb1 Re-land "[-Wunsafe-buffer-usage] Add a new forEachDescendant matcher that skips callable declarations"
This reverts commit 22df4549a3718dcd8b387ba8246978349e4be50c.

After a quick investigation, realizing that the Sanitizer test
failures caused by this patch is not likely to block other
contributors. I re-land this patch before taking a closer look at
those tests so that it won't block the [-Wunsafe-buffer-usage]
development.
2023-01-06 10:33:21 -08:00
ziqingluo-90
22df4549a3 Revert "[Fix]"[-Wunsafe-buffer-usage] Add a new forEachDescendant matcher that skips callable declarations""
This reverts commit ef47a0a711f12add401394f7af07a0b4d1635b56.

Revert "[-Wunsafe-buffer-usage] Add a new `forEachDescendant` matcher that skips callable declarations"

This reverts commit b2ac5fd724c44cf662caed84bd8f84af574b981d.

This patch is causing failure in some Sanitizer tests
(https://lab.llvm.org/buildbot/#/builders/5/builds/30522/steps/13/logs/stdio).  Reverting the patch and its' fix.
2023-01-05 22:06:46 -08:00
ziqingluo-90
ef47a0a711 [Fix]"[-Wunsafe-buffer-usage] Add a new forEachDescendant matcher that skips callable declarations"
The original patch in commit b2ac5fd724c44cf662caed84bd8f84af574b981d
causes compilation errors which can be reproduced by the
`-fdelayed-template-parsing` flag.  This commit fixes the problem.

Related differential revision: https://reviews.llvm.org/D138329
2023-01-05 12:04:13 -08:00
ziqingluo-90
8641687a43 Revert "Revert "[-Wunsafe-buffer-usage] Add a new forEachDescendant matcher that skips callable declarations""
This reverts commit f58b025354ee2d3bcd7ab2399a11429ec940c1e0.

 The previous revert reverts a patch that causes compilation problem on
 windows which can be reproduced using `-fdelayed-template-parsing`.
 I'm now to revert the patch back and commit a fix next.
2023-01-05 12:04:13 -08:00
Ziqing Luo
f58b025354 Revert "[-Wunsafe-buffer-usage] Add a new forEachDescendant matcher that skips callable declarations"
This reverts commit b2ac5fd724c44cf662caed84bd8f84af574b981d.
2023-01-04 17:16:21 -08:00
Ziqing Luo
f84f17c489 [-Wunsafe-buffer-usage] Add an unsafe gadget for pointer-arithmetic operations
For -Wunsafe-buffer-usage diagnostics, we want to warn about pointer
arithmetics since resulting pointers can be used to access buffers.
Therefore, I add an `UnsafeGadget` representing general pointer
arithmetic operations.

Reviewed by: NoQ
Differential revision: https://reviews.llvm.org/D139233
2023-01-04 16:50:21 -08:00
Ziqing Luo
b2ac5fd724 [-Wunsafe-buffer-usage] Add a new forEachDescendant matcher that skips callable declarations
Note this is a change local to -Wunsafe-buffer-usage checks.

Add a new matcher `forEveryDescendant` that recursively matches
descendants of a `Stmt` but skips nested callable definitions.  This
matcher has same effect as using `forEachDescendant` and skipping
`forCallable` explicitly but does not require the AST construction to be
complete.

Reviewed by: NoQ, xazax.hun

Differential revision: https://reviews.llvm.org/D138329
2023-01-04 15:51:56 -08:00
Kazu Hirata
9b050e5011 [clang] Remove redundant initialization of std::optional (NFC) 2022-12-22 13:46:26 -08:00
Artem Dergachev
8fd62e70cd [-Wunsafe-buffer-usage] Suppress an assertion for visiting VarDecl twice.
The assertion doesn't seem to hold due to ASTMatchers traversing code
inside GNU StmtExpr twice. This can screw up our algorithm's invariants.
We need a further investigation to properly fix this issue, but for now
let's avoid the crash.
2022-12-20 16:05:13 -08:00
ziqingluo-90
f6c54cdbc4 [-Wunsafe-buffer-usage] Ignore array subscript on literal zero
Unsafe Buffer Usage analysis only warns unsafe buffer accesses but not
pointer dereferences.  An array subscript on a literal zero is
equivalent to dereference a pointer thus we do not want to warn it.

Reviewed By: NoQ

Differential Revision: https://reviews.llvm.org/D138321
2022-12-16 21:04:41 -08:00
ziqingluo-90
04202b94b6 [-Wunsafe-buffer-usage] Improve pointer matching pattern
Generalize the pointer expression AST matcher in Unsafe Buffer Usage analysis.
Add test cases for various kinds of pointer usages.

Reviewed By: NoQ, aaron.ballman, xazax.hun

Differential Revision: https://reviews.llvm.org/D138318
2022-12-16 20:40:59 -08:00
Artem Dergachev
8086323a91 [-Wunsafe-buffer-usage] NFC: Implement fix-strategies and variable-use-claiming.
This patch adds more abstractions that we'll need later for emitting
-Wunsafe-buffer-usage fixits. It doesn't emit any actual fixits,
so no change is observed behavior, but it introduces a way to emit fixits,
and existing tests now verify that the compiler still emits no fixits,
despite knowing how to do so.

The purpose of our code transformation analysis is to fix variable types
in the code from raw pointer types to C++ standard collection/view types.

The analysis has to decide on its own which specific type is
the most appropriate for every variable. This patch introduces
the Strategy class that maps variables to their most appropriate types.

In D137348 we've introduced the Gadget abstraction, which describes
a rigid AST pattern that the analysis "fully understands" and may need
to fix. Which specific fix is actually necessary for a given Gadget,
and whether it's necessary at all, and whether it's possible in the first place,
depends on the Strategy. So, this patch adds a virtual method which every
gadget can implement in order to teach the analysis how to fix that gadget:

  Gadget->getFixits(Strategy)

However, even if the analysis knows how to fix every Gadget, doesn't
necessarily mean it can fix the variable. Some uses of the variable may have
never been covered by Gadgets, which corresponds to the situation that
the analysis doesn't fully understand how the variable is used. This patch
introduces a Tracker class that tracks all variable uses (i.e. DeclRefExprs)
in the function. Additionally, each Gadget now provides a new virtual method

  Gadget->getClaimedVarUseSites()

that the Tracker can call to see which DeclRefExprs are "claimed" by the Gadget.
In order to fix the variable with a certain Strategy, the Tracker needs to
confirm that there are no unclaimed uses, and every Gadget has to provide
a fix for that Strategy.

This "conservative" behavior guarantees that fixes emitted by our analysis
are correct by construction. We can now be sure that the analysis won't
attempt to emit a fix if it doesn't understand the code. Later, as we implement
more getFixits() methods in individual Gadget classes, we'll start
progressively emitting more and more fixits.

Differential Revision: https://reviews.llvm.org/D138253
2022-12-16 18:48:29 -08:00
ziqingluo-90
6d1d055fad [-Wunsafe-buffer-usage] Add warnings for unsafe buffer accesses by array subscript operations
Add an unsafe Gadget in the Unsafe Buffer Usage anslysis to represent
(and search (via matchers) for) unsafe buffer-accessing array subscript operations.

Reviewed By: NoQ, jkorous, aaron.ballman, xazax.hun

Differential Revision: https://reviews.llvm.org/D137379
2022-12-16 17:42:07 -08:00
Fangrui Song
893a0ea948 -Wunsafe-buffer-usage: fix build when -DLLVM_ENABLE_ASSERTIONS=off 2022-12-16 23:43:32 +00:00
Artem Dergachev
0d00a9722f [-Wunsafe-buffer-usage] NFC: Introduce an abstraction for fixable code patterns.
This patch introduces a hierarchy of Gadget classes, which are going to be
the core concept in the somewhat sophisticated machine behind the upcoming
-Wunsafe-buffer-usage fix-it hints.

A gadget is a rigid code pattern that constitutes an operation that
the fixit machine "understands" well enough to work with. A gadget may be
"unsafe" (i.e., constitute a raw buffer access) or "safe" (constitute
a use of a pointer or array variable that isn't unsafe per se,
but may need fixing if the participating variable changes type
to a safe container/view).

We'll be able to make decisions about how to fix the code depending on
the set of gadgets that we've enumerated. Basically, in order to fix a type
of a variable, each use of that variable has to participate in a gadget,
and each such gadget has to consent to fixing itself according to
the variable's new type.

Differential Revision: https://reviews.llvm.org/D137348
2022-12-16 15:02:22 -08:00
Artem Dergachev
3b7af2796b Revert "Revert "[-Wunsafe-buffer-usage] Initial commit - Transition away from raw buffers.""
This reverts commit bc0617795f8bdcd5deab3e48b120107cc2833cdc.

Be more careful with #includes to avoid linker errors on some buildbots.
2022-12-08 16:57:59 -08:00
Artem Dergachev
bc0617795f Revert "[-Wunsafe-buffer-usage] Initial commit - Transition away from raw buffers."
This reverts commit 200007ec85f81122fd260a4e68308e54607ca37a.
2022-12-05 15:38:52 -08:00
Artem Dergachev
200007ec85 [-Wunsafe-buffer-usage] Initial commit - Transition away from raw buffers.
This is the initial commit for -Wunsafe-buffer-usage, a warning that helps
codebases (especially modern C++ codebases) transition away from raw buffer
pointers.

The warning is implemented in libAnalysis as it's going to become a non-trivial
analysis, mostly the fixit part where we try to figure out if we understand
a variable's use pattern well enough to suggest a safe container/view
as a replacement. Some parts of this analsysis may eventually prove useful
for any similar fixit machine that tries to change types of variables.

The warning is disabled by default.

RFC/discussion in https://discourse.llvm.org/t/rfc-c-buffer-hardening/65734

Differential Revision: https://reviews.llvm.org/D137346
2022-12-05 15:13:42 -08:00