* [PATCH v2 0/2] Add a section for static analysis tools
@ 2022-03-29 23:21 Marcelo Schmitt
2022-03-29 23:22 ` [PATCH v2 1/2] Documentation: dev-tools: " Marcelo Schmitt
2022-03-29 23:23 ` [PATCH v2 2/2] Documentation: dev-tools: Enhance static analysis section with discussion Marcelo Schmitt
0 siblings, 2 replies; 13+ messages in thread
From: Marcelo Schmitt @ 2022-03-29 23:21 UTC (permalink / raw)
To: corbet, mchehab+huawei, dlatypov, davidgow
Cc: linux-doc, linux-sparse, cocci, smatch, linux-kernel, skhan,
dan.carpenter, julia.lawall
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
^ permalink raw reply [flat|nested] 13+ messages in thread* [PATCH v2 1/2] Documentation: dev-tools: Add a section for static analysis tools 2022-03-29 23:21 [PATCH v2 0/2] Add a section for static analysis tools Marcelo Schmitt @ 2022-03-29 23:22 ` Marcelo Schmitt 2022-03-29 23:48 ` Daniel Latypov ` (2 more replies) 2022-03-29 23:23 ` [PATCH v2 2/2] Documentation: dev-tools: Enhance static analysis section with discussion Marcelo Schmitt 1 sibling, 3 replies; 13+ messages in thread From: Marcelo Schmitt @ 2022-03-29 23:22 UTC (permalink / raw) To: corbet, mchehab+huawei, dlatypov, davidgow Cc: linux-doc, linux-sparse, cocci, smatch, linux-kernel, skhan, dan.carpenter, julia.lawall Complement the Kernel Testing Guide documentation page by adding a section about static analysis tools. Signed-off-by: Marcelo Schmitt <marcelo.schmitt1@gmail.com> Acked-by: Daniel Latypov <dlatypov@google.com> Acked-by: Dan Carpenter <dan.carpenter@oracle.com> Reviewed-by: David Gow <davidgow@google.com> Reviewed-by: Shuah Khan <skhan@linuxfoundation.org> --- 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 ^ permalink raw reply related [flat|nested] 13+ messages in thread
* Re: [PATCH v2 1/2] Documentation: dev-tools: Add a section for static analysis tools 2022-03-29 23:22 ` [PATCH v2 1/2] Documentation: dev-tools: " Marcelo Schmitt @ 2022-03-29 23:48 ` Daniel Latypov 2022-03-30 2:33 ` David Gow 2022-03-30 8:04 ` Julia Lawall 2 siblings, 0 replies; 13+ messages in thread From: Daniel Latypov @ 2022-03-29 23:48 UTC (permalink / raw) To: Marcelo Schmitt Cc: corbet, mchehab+huawei, davidgow, linux-doc, linux-sparse, cocci, smatch, linux-kernel, skhan, dan.carpenter, julia.lawall On Tue, Mar 29, 2022 at 6:22 PM Marcelo Schmitt <marcelo.schmitt1@gmail.com> wrote: > > Complement the Kernel Testing Guide documentation page by adding a > section about static analysis tools. > > Signed-off-by: Marcelo Schmitt <marcelo.schmitt1@gmail.com> > Acked-by: Daniel Latypov <dlatypov@google.com> > Acked-by: Dan Carpenter <dan.carpenter@oracle.com> > Reviewed-by: David Gow <davidgow@google.com> > Reviewed-by: Shuah Khan <skhan@linuxfoundation.org> > --- > 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 This looks a lot nicer to me! Thanks for doing this. Daniel ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH v2 1/2] Documentation: dev-tools: Add a section for static analysis tools 2022-03-29 23:22 ` [PATCH v2 1/2] Documentation: dev-tools: " Marcelo Schmitt 2022-03-29 23:48 ` Daniel Latypov @ 2022-03-30 2:33 ` David Gow 2022-03-30 8:04 ` Julia Lawall 2 siblings, 0 replies; 13+ messages in thread From: David Gow @ 2022-03-30 2:33 UTC (permalink / raw) To: Marcelo Schmitt Cc: Jonathan Corbet, Mauro Carvalho Chehab, Daniel Latypov, open list:DOCUMENTATION, linux-sparse, cocci, smatch, Linux Kernel Mailing List, Shuah Khan, Dan Carpenter, julia.lawall [-- Attachment #1: Type: text/plain, Size: 1061 bytes --] On Wed, Mar 30, 2022 at 7:22 AM Marcelo Schmitt <marcelo.schmitt1@gmail.com> wrote: > > Complement the Kernel Testing Guide documentation page by adding a > section about static analysis tools. > > Signed-off-by: Marcelo Schmitt <marcelo.schmitt1@gmail.com> > Acked-by: Daniel Latypov <dlatypov@google.com> > Acked-by: Dan Carpenter <dan.carpenter@oracle.com> > Reviewed-by: David Gow <davidgow@google.com> > Reviewed-by: Shuah Khan <skhan@linuxfoundation.org> > --- > 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 <davidgow@google.com> -- David [-- Attachment #2: S/MIME Cryptographic Signature --] [-- Type: application/pkcs7-signature, Size: 4003 bytes --] ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH v2 1/2] Documentation: dev-tools: Add a section for static analysis tools 2022-03-29 23:22 ` [PATCH v2 1/2] Documentation: dev-tools: " Marcelo Schmitt 2022-03-29 23:48 ` Daniel Latypov 2022-03-30 2:33 ` David Gow @ 2022-03-30 8:04 ` Julia Lawall 2 siblings, 0 replies; 13+ messages in thread From: Julia Lawall @ 2022-03-30 8:04 UTC (permalink / raw) To: Marcelo Schmitt Cc: corbet, mchehab+huawei, dlatypov, davidgow, linux-doc, linux-sparse, cocci, smatch, linux-kernel, skhan, dan.carpenter, julia.lawall On Tue, 29 Mar 2022, Marcelo Schmitt wrote: > Complement the Kernel Testing Guide documentation page by adding a > section about static analysis tools. > > Signed-off-by: Marcelo Schmitt <marcelo.schmitt1@gmail.com> > Acked-by: Daniel Latypov <dlatypov@google.com> > Acked-by: Dan Carpenter <dan.carpenter@oracle.com> > Reviewed-by: David Gow <davidgow@google.com> > Reviewed-by: Shuah Khan <skhan@linuxfoundation.org> Acked-by: Julia Lawall <julia.lawall@inria.fr> > --- > 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 > > ^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH v2 2/2] Documentation: dev-tools: Enhance static analysis section with discussion 2022-03-29 23:21 [PATCH v2 0/2] Add a section for static analysis tools Marcelo Schmitt 2022-03-29 23:22 ` [PATCH v2 1/2] Documentation: dev-tools: " Marcelo Schmitt @ 2022-03-29 23:23 ` Marcelo Schmitt 2022-03-30 2:48 ` David Gow 2022-03-30 8:06 ` Julia Lawall 1 sibling, 2 replies; 13+ messages in thread From: Marcelo Schmitt @ 2022-03-29 23:23 UTC (permalink / raw) To: corbet, mchehab+huawei, dlatypov, davidgow Cc: linux-doc, linux-sparse, cocci, smatch, linux-kernel, skhan, dan.carpenter, julia.lawall 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 <marcelo.schmitt1@gmail.com> Cc: Dan Carpenter <dan.carpenter@oracle.com> Cc: Julia Lawall <julia.lawall@inria.fr> --- 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 ^ permalink raw reply related [flat|nested] 13+ messages in thread
* Re: [PATCH v2 2/2] Documentation: dev-tools: Enhance static analysis section with discussion 2022-03-29 23:23 ` [PATCH v2 2/2] Documentation: dev-tools: Enhance static analysis section with discussion Marcelo Schmitt @ 2022-03-30 2:48 ` David Gow 2022-03-30 8:07 ` Julia Lawall ` (2 more replies) 2022-03-30 8:06 ` Julia Lawall 1 sibling, 3 replies; 13+ messages in thread From: David Gow @ 2022-03-30 2:48 UTC (permalink / raw) To: Marcelo Schmitt Cc: Jonathan Corbet, Mauro Carvalho Chehab, Daniel Latypov, open list:DOCUMENTATION, linux-sparse, cocci, smatch, Linux Kernel Mailing List, Shuah Khan, Dan Carpenter, julia.lawall On Wed, Mar 30, 2022 at 7:23 AM Marcelo Schmitt <marcelo.schmitt1@gmail.com> wrote: > > 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 <marcelo.schmitt1@gmail.com> > Cc: Dan Carpenter <dan.carpenter@oracle.com> > Cc: Julia Lawall <julia.lawall@inria.fr> > --- Thanks: this sort of "when to use which tool" information is really what the testing guide page needs. I'm not familiar enough with these tools that I can really review the details properly, but nothing stands out as obviously wrong to me. I've made a few comments below regardless, but feel free to ignore them if they're not quite right. Acked-by: David Gow <davidgow@google.com> Cheers, -- David > 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. Given that the __user pointer and endianness stuff is found as a result of Sparse's type checking support, would rewording this as "Sparse does type checking, such as [detecting places...]" or similar be more clear? > + > +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. This last sentence isn't totally clear to me. Should this "because" be "so"? > + > +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 (Maybe start this with "For example," just to make it clear that this paragraph is mostly following on from how useful it is that Coccinelle produces fixes, not just warnings.) > +``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 > ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH v2 2/2] Documentation: dev-tools: Enhance static analysis section with discussion 2022-03-30 2:48 ` David Gow @ 2022-03-30 8:07 ` Julia Lawall 2022-03-30 19:30 ` Marcelo Schmitt 2022-03-31 8:14 ` Dan Carpenter 2 siblings, 0 replies; 13+ messages in thread From: Julia Lawall @ 2022-03-30 8:07 UTC (permalink / raw) To: David Gow Cc: Marcelo Schmitt, Jonathan Corbet, Mauro Carvalho Chehab, Daniel Latypov, open list:DOCUMENTATION, linux-sparse, cocci, smatch, Linux Kernel Mailing List, Shuah Khan, Dan Carpenter, julia.lawall > > +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 > > (Maybe start this with "For example," just to make it clear that this > paragraph is mostly following on from how useful it is that Coccinelle > produces fixes, not just warnings.) I also suggested "for example", in a different place, but either is fine. julia > > > +``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 > > > ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH v2 2/2] Documentation: dev-tools: Enhance static analysis section with discussion 2022-03-30 2:48 ` David Gow 2022-03-30 8:07 ` Julia Lawall @ 2022-03-30 19:30 ` Marcelo Schmitt 2022-04-01 0:18 ` David Gow 2022-03-31 8:14 ` Dan Carpenter 2 siblings, 1 reply; 13+ messages in thread From: Marcelo Schmitt @ 2022-03-30 19:30 UTC (permalink / raw) To: David Gow Cc: Jonathan Corbet, Mauro Carvalho Chehab, Daniel Latypov, open list:DOCUMENTATION, linux-sparse, cocci, smatch, Linux Kernel Mailing List, Shuah Khan, Dan Carpenter, julia.lawall On 03/30, David Gow wrote: > On Wed, Mar 30, 2022 at 7:23 AM Marcelo Schmitt > <marcelo.schmitt1@gmail.com> wrote: > > > > 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 <marcelo.schmitt1@gmail.com> > > Cc: Dan Carpenter <dan.carpenter@oracle.com> > > Cc: Julia Lawall <julia.lawall@inria.fr> > > --- > > Thanks: this sort of "when to use which tool" information is really > what the testing guide page needs. > > I'm not familiar enough with these tools that I can really review the > details properly, but nothing stands out as obviously wrong to me. > I've made a few comments below regardless, but feel free to ignore > them if they're not quite right. > > Acked-by: David Gow <davidgow@google.com> > > Cheers, > -- David > > > 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. > > Given that the __user pointer and endianness stuff is found as a > result of Sparse's type checking support, would rewording this as > "Sparse does type checking, such as [detecting places...]" or similar > be more clear? Myabe. I tried changing it a little while adding a bit of information from https://lwn.net/Articles/689907/ "Sparse does type checking, such as verifying that annotated variables do not cause endianness bugs, detecting places that use ``__user`` pointers improperly, and analyzing the compatibility of symbol initializers." Does it sound better? > > > + > > +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. > > This last sentence isn't totally clear to me. Should this "because" be "so"? Smatch uses (is shipped with) a modified Sparse implementation which it uses as a C parser. Apparently, Sparse does some checkings while parsing the code for Smatch so that's why we have some overlapping between the checks made when we run Smatch and the ones made when we run Sparse alone. I didn't dig into the code, but I guess further modifying Sparse to prevent it from doing some types of cheks wouldn't add much to Smatch. That last saying should've reflected this fact, but it seems to cause confusion without a proper context. Reading the sentence back again, I think we could just drop the last part: "Nevertheless, there are some overlaps between Sparse and Smatch checks." > > > + > > +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 > > (Maybe start this with "For example," just to make it clear that this > paragraph is mostly following on from how useful it is that Coccinelle > produces fixes, not just warnings.) Will do > > > +``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 > > ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH v2 2/2] Documentation: dev-tools: Enhance static analysis section with discussion 2022-03-30 19:30 ` Marcelo Schmitt @ 2022-04-01 0:18 ` David Gow 0 siblings, 0 replies; 13+ messages in thread From: David Gow @ 2022-04-01 0:18 UTC (permalink / raw) To: Marcelo Schmitt Cc: Jonathan Corbet, Mauro Carvalho Chehab, Daniel Latypov, open list:DOCUMENTATION, linux-sparse, cocci, smatch, Linux Kernel Mailing List, Shuah Khan, Dan Carpenter, julia.lawall On Thu, Mar 31, 2022 at 3:30 AM Marcelo Schmitt <marcelo.schmitt1@gmail.com> wrote: > > On 03/30, David Gow wrote: > > On Wed, Mar 30, 2022 at 7:23 AM Marcelo Schmitt > > <marcelo.schmitt1@gmail.com> wrote: > > > > > > 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 <marcelo.schmitt1@gmail.com> > > > Cc: Dan Carpenter <dan.carpenter@oracle.com> > > > Cc: Julia Lawall <julia.lawall@inria.fr> > > > --- > > > > Thanks: this sort of "when to use which tool" information is really > > what the testing guide page needs. > > > > I'm not familiar enough with these tools that I can really review the > > details properly, but nothing stands out as obviously wrong to me. > > I've made a few comments below regardless, but feel free to ignore > > them if they're not quite right. > > > > Acked-by: David Gow <davidgow@google.com> > > > > Cheers, > > -- David > > > > > 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. > > > > Given that the __user pointer and endianness stuff is found as a > > result of Sparse's type checking support, would rewording this as > > "Sparse does type checking, such as [detecting places...]" or similar > > be more clear? > > Myabe. I tried changing it a little while adding a bit of information from > https://lwn.net/Articles/689907/ > > "Sparse does type checking, such as verifying that annotated variables do not > cause endianness bugs, detecting places that use ``__user`` pointers improperly, > and analyzing the compatibility of symbol initializers." > > Does it sound better? > Yeah: that sounds much better to me. Thanks! > > > > > + > > > +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. > > > > This last sentence isn't totally clear to me. Should this "because" be "so"? > > Smatch uses (is shipped with) a modified Sparse implementation which it uses as > a C parser. Apparently, Sparse does some checkings while parsing the code for > Smatch so that's why we have some overlapping between the checks made when we > run Smatch and the ones made when we run Sparse alone. > > I didn't dig into the code, but I guess further modifying Sparse to prevent it > from doing some types of cheks wouldn't add much to Smatch. That last saying > should've reflected this fact, but it seems to cause confusion without a proper > context. Reading the sentence back again, I think we could just drop the last > part: > > "Nevertheless, there are some overlaps between Sparse and Smatch checks." > Yeah, I do think that makes more sense. I don't think the fact that some of the checks overlap causes any problems at all, to be honest, so you _could_ get rid of the whole sentence without losing too much, but I'm also happy with it as it is in v3. > > > > > + > > > +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 > > > > (Maybe start this with "For example," just to make it clear that this > > paragraph is mostly following on from how useful it is that Coccinelle > > produces fixes, not just warnings.) > > Will do > > > > > > +``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 > > > ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH v2 2/2] Documentation: dev-tools: Enhance static analysis section with discussion 2022-03-30 2:48 ` David Gow 2022-03-30 8:07 ` Julia Lawall 2022-03-30 19:30 ` Marcelo Schmitt @ 2022-03-31 8:14 ` Dan Carpenter 2022-04-01 0:19 ` David Gow 2 siblings, 1 reply; 13+ messages in thread From: Dan Carpenter @ 2022-03-31 8:14 UTC (permalink / raw) To: David Gow Cc: Marcelo Schmitt, Jonathan Corbet, Mauro Carvalho Chehab, Daniel Latypov, open list:DOCUMENTATION, linux-sparse, cocci, smatch, Linux Kernel Mailing List, Shuah Khan, julia.lawall On Wed, Mar 30, 2022 at 10:48:13AM +0800, David Gow wrote: > > + > > +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. > > This last sentence isn't totally clear to me. Should this "because" be "so"? > I stopped reading your email when you wrote "Cheers, David" but I should have scrolled down. There is not very much overlap between Sparse and Smatch. Both have a warning for if (!x & y). That is a tiny thing. The big overlap is when it comes to the locking checks. The Smatch check for locking is honestly way better and more capable. I always run both Sparse and Smatch on my patches. I should run Coccinelle as well, but I'm more familiar with Sparse and Smatch. regards, dan carpenter ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH v2 2/2] Documentation: dev-tools: Enhance static analysis section with discussion 2022-03-31 8:14 ` Dan Carpenter @ 2022-04-01 0:19 ` David Gow 0 siblings, 0 replies; 13+ messages in thread From: David Gow @ 2022-04-01 0:19 UTC (permalink / raw) To: Dan Carpenter Cc: Marcelo Schmitt, Jonathan Corbet, Mauro Carvalho Chehab, Daniel Latypov, open list:DOCUMENTATION, linux-sparse, cocci, smatch, Linux Kernel Mailing List, Shuah Khan, julia.lawall On Thu, Mar 31, 2022 at 4:14 PM Dan Carpenter <dan.carpenter@oracle.com> wrote: > > On Wed, Mar 30, 2022 at 10:48:13AM +0800, David Gow wrote: > > > + > > > +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. > > > > This last sentence isn't totally clear to me. Should this "because" be "so"? > > > > I stopped reading your email when you wrote "Cheers, David" but I should > have scrolled down. > > There is not very much overlap between Sparse and Smatch. Both have a > warning for if (!x & y). That is a tiny thing. The big overlap is when > it comes to the locking checks. The Smatch check for locking is > honestly way better and more capable. > > I always run both Sparse and Smatch on my patches. I should run > Coccinelle as well, but I'm more familiar with Sparse and Smatch. Makes sense. I agree that the overlap doesn't seem particularly important: it's the differences which should be more evident. v3[1] of the patch cuts this down to just "Nevertheless, there are some overlaps between Sparse and Smatch checks.", which I think is an improvement. Thanks, -- David [1]: https://lore.kernel.org/all/62f461a20600b95e694016c4e5348ef2e260fa87.1648674305.git.marcelo.schmitt1@gmail.com/ ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH v2 2/2] Documentation: dev-tools: Enhance static analysis section with discussion 2022-03-29 23:23 ` [PATCH v2 2/2] Documentation: dev-tools: Enhance static analysis section with discussion Marcelo Schmitt 2022-03-30 2:48 ` David Gow @ 2022-03-30 8:06 ` Julia Lawall 1 sibling, 0 replies; 13+ messages in thread From: Julia Lawall @ 2022-03-30 8:06 UTC (permalink / raw) To: Marcelo Schmitt Cc: corbet, mchehab+huawei, dlatypov, davidgow, linux-doc, linux-sparse, cocci, smatch, linux-kernel, skhan, dan.carpenter, julia.lawall > +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. pre-processor > +Coccinelle also writes patches fixes for you which no other tool does. writes patches fixes -> creates patches > + > +With Coccinelle you can do a mass conversion from you can -> you can, for example, julia > +``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 > > ^ permalink raw reply [flat|nested] 13+ messages in thread
end of thread, other threads:[~2022-04-01 0:19 UTC | newest] Thread overview: 13+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2022-03-29 23:21 [PATCH v2 0/2] Add a section for static analysis tools Marcelo Schmitt 2022-03-29 23:22 ` [PATCH v2 1/2] Documentation: dev-tools: " Marcelo Schmitt 2022-03-29 23:48 ` Daniel Latypov 2022-03-30 2:33 ` David Gow 2022-03-30 8:04 ` Julia Lawall 2022-03-29 23:23 ` [PATCH v2 2/2] Documentation: dev-tools: Enhance static analysis section with discussion Marcelo Schmitt 2022-03-30 2:48 ` David Gow 2022-03-30 8:07 ` Julia Lawall 2022-03-30 19:30 ` Marcelo Schmitt 2022-04-01 0:18 ` David Gow 2022-03-31 8:14 ` Dan Carpenter 2022-04-01 0:19 ` David Gow 2022-03-30 8:06 ` Julia Lawall
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).