2022-03-30 16:30:57

by Marcelo Schmitt

[permalink] [raw]
Subject: [PATCH v2 0/2] Add a section for static analysis tools

Hi,

Thanks to everybody who commented on v1 for your kind and much helpful feedback.

I tried to add suggestions and ideas while keeping the text concise.
Also, I took Dan and Julia's comments and included them into the
documentation (patch 2) because I think they were very helpful in
comparing the tools.
I didn't feel comfortable adding something comparing Sparse and
Coccinelle directly as I'm not an expert with any of these tools either.
Anyhow, that can be something to do in the future.

Thanks,
Marcelo

Marcelo Schmitt (2):
Documentation: dev-tools: Add a section for static analysis tools
Documentation: dev-tools: Enhance static analysis section with
discussion

Documentation/dev-tools/testing-overview.rst | 64 ++++++++++++++++++++
1 file changed, 64 insertions(+)

--
2.35.1


2022-03-31 03:39:42

by Marcelo Schmitt

[permalink] [raw]
Subject: [PATCH v2 2/2] Documentation: dev-tools: Enhance static analysis section with discussion

Enhance the static analysis tools section with a discussion on when to
use each of them.

This was mainly taken from Dan Carpenter and Julia Lawall's comments on
the previous documentation patch for static analysis tools.

Lore: https://lore.kernel.org/linux-doc/20220329090911.GX3293@kadam/T/#mb97770c8e938095aadc3ee08f4ac7fe32ae386e6

Signed-off-by: Marcelo Schmitt <[email protected]>
Cc: Dan Carpenter <[email protected]>
Cc: Julia Lawall <[email protected]>
---
Documentation/dev-tools/testing-overview.rst | 33 ++++++++++++++++++++
1 file changed, 33 insertions(+)

diff --git a/Documentation/dev-tools/testing-overview.rst b/Documentation/dev-tools/testing-overview.rst
index b5e02dd3fd94..91e479045d3a 100644
--- a/Documentation/dev-tools/testing-overview.rst
+++ b/Documentation/dev-tools/testing-overview.rst
@@ -146,3 +146,36 @@ Documentation/dev-tools/coccinelle.rst documentation page for details.

Beware, though, that static analysis tools suffer from **false positives**.
Errors and warns need to be evaluated carefully before attempting to fix them.
+
+When to use Sparse and Smatch
+-----------------------------
+
+Sparse is useful for type checking, detecting places that use ``__user``
+pointers improperly, or finding endianness bugs. Sparse runs much faster than
+Smatch.
+
+Smatch does flow analysis and, if allowed to build the function database, it
+also does cross function analysis. Smatch tries to answer questions like where
+is this buffer allocated? How big is it? Can this index be controlled by the
+user? Is this variable larger than that variable?
+
+It's generally easier to write checks in Smatch than it is to write checks in
+Sparse. Nevertheless, there are some overlaps between Sparse and Smatch checks
+because there is no reason for re-implementing Sparse's check in Smatch.
+
+Strong points of Smatch and Coccinelle
+--------------------------------------
+
+Coccinelle is probably the easiest for writing checks. It works before the
+pre-compiler so it's easier to check for bugs in macros using Coccinelle.
+Coccinelle also writes patches fixes for you which no other tool does.
+
+With Coccinelle you can do a mass conversion from
+``kmalloc(x * size, GFP_KERNEL)`` to ``kmalloc_array(x, size, GFP_KERNEL)``, and
+that's really useful. If you just created a Smatch warning and try to push the
+work of converting on to the maintainers they would be annoyed. You'd have to
+argue about each warning if can really overflow or not.
+
+Coccinelle does no analysis of variable values, which is the strong point of
+Smatch. On the other hand, Coccinelle allows you to do simple things in a simple
+way.
--
2.35.1

2022-03-31 03:56:33

by Marcelo Schmitt

[permalink] [raw]
Subject: [PATCH v2 1/2] Documentation: dev-tools: Add a section for static analysis tools

Complement the Kernel Testing Guide documentation page by adding a
section about static analysis tools.

Signed-off-by: Marcelo Schmitt <[email protected]>
Acked-by: Daniel Latypov <[email protected]>
Acked-by: Dan Carpenter <[email protected]>
Reviewed-by: David Gow <[email protected]>
Reviewed-by: Shuah Khan <[email protected]>
---
Change log:
- Brought generic tool characteristics to the intro paragraph
- Made explicit that these tools run at compile time
- Added a note of caution about false positives
- Updated Coccinelle info to make it sound better and be more skimmable

Documentation/dev-tools/testing-overview.rst | 31 ++++++++++++++++++++
1 file changed, 31 insertions(+)

diff --git a/Documentation/dev-tools/testing-overview.rst b/Documentation/dev-tools/testing-overview.rst
index 65feb81edb14..b5e02dd3fd94 100644
--- a/Documentation/dev-tools/testing-overview.rst
+++ b/Documentation/dev-tools/testing-overview.rst
@@ -115,3 +115,34 @@ that none of these errors are occurring during the test.
Some of these tools integrate with KUnit or kselftest and will
automatically fail tests if an issue is detected.

+Static Analysis Tools
+=====================
+
+In addition to testing a running kernel, one can also analyze kernel source code
+directly (**at compile time**) using **static analysis** tools. The tools
+commonly used in the kernel allow one to inspect the whole source tree or just
+specific files within it. They make it easier to detect and fix problems during
+the development process.
+
+Sparse can help test the kernel by performing type-checking, lock checking,
+value range checking, in addition to reporting various errors and warnings while
+examining the code. See the Documentation/dev-tools/sparse.rst documentation
+page for details on how to use it.
+
+Smatch extends Sparse and provides additional checks for programming logic
+mistakes such as missing breaks in switch statements, unused return values on
+error checking, forgetting to set an error code in the return of an error path,
+etc. Smatch also has tests against more serious issues such as integer
+overflows, null pointer dereferences, and memory leaks. See the project page at
+http://smatch.sourceforge.net/.
+
+Coccinelle is another static analyzer at our disposal. Coccinelle is often used
+to aid refactoring and collateral evolution of source code, but it can also help
+to avoid certain bugs that occur in common code patterns. The types of tests
+available include API tests, tests for correct usage of kernel iterators, checks
+for the soundness of free operations, analysis of locking behavior, and further
+tests known to help keep consistent kernel usage. See the
+Documentation/dev-tools/coccinelle.rst documentation page for details.
+
+Beware, though, that static analysis tools suffer from **false positives**.
+Errors and warns need to be evaluated carefully before attempting to fix them.
--
2.35.1

2022-03-31 04:45:54

by David Gow

[permalink] [raw]
Subject: Re: [PATCH v2 1/2] Documentation: dev-tools: Add a section for static analysis tools

On Wed, Mar 30, 2022 at 7:22 AM Marcelo Schmitt
<[email protected]> wrote:
>
> Complement the Kernel Testing Guide documentation page by adding a
> section about static analysis tools.
>
> Signed-off-by: Marcelo Schmitt <[email protected]>
> Acked-by: Daniel Latypov <[email protected]>
> Acked-by: Dan Carpenter <[email protected]>
> Reviewed-by: David Gow <[email protected]>
> Reviewed-by: Shuah Khan <[email protected]>
> ---
> Change log:
> - Brought generic tool characteristics to the intro paragraph
> - Made explicit that these tools run at compile time
> - Added a note of caution about false positives
> - Updated Coccinelle info to make it sound better and be more skimmable
>

Looks better to me: most of the things which I feel are still missing
are added in the next patch.

I think it would be possible to combine the two if you wanted to,
which might make the overall descriptions of the tools a bit stronger,
but this works either way.

This is still
Reviewed-by: David Gow <[email protected]>

-- David


Attachments:
smime.p7s (3.91 kB)
S/MIME Cryptographic Signature