Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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
a 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>
Acked-by: David Gow <davidgow@google.com>
Cc: Dan Carpenter <dan.carpenter@oracle.com>
Cc: Julia Lawall <julia.lawall@inria.fr>
Signed-off-by: Jonathan Corbet <corbet@lwn.net>

authored by

Marcelo Schmitt and committed by
Jonathan Corbet
a32d5c0f 12379401

+32
+32
Documentation/dev-tools/testing-overview.rst
··· 146 146 147 147 Beware, though, that static analysis tools suffer from **false positives**. 148 148 Errors and warns need to be evaluated carefully before attempting to fix them. 149 + 150 + When to use Sparse and Smatch 151 + ----------------------------- 152 + 153 + Sparse does type checking, such as verifying that annotated variables do not 154 + cause endianness bugs, detecting places that use ``__user`` pointers improperly, 155 + and analyzing the compatibility of symbol initializers. 156 + 157 + Smatch does flow analysis and, if allowed to build the function database, it 158 + also does cross function analysis. Smatch tries to answer questions like where 159 + is this buffer allocated? How big is it? Can this index be controlled by the 160 + user? Is this variable larger than that variable? 161 + 162 + It's generally easier to write checks in Smatch than it is to write checks in 163 + Sparse. Nevertheless, there are some overlaps between Sparse and Smatch checks. 164 + 165 + Strong points of Smatch and Coccinelle 166 + -------------------------------------- 167 + 168 + Coccinelle is probably the easiest for writing checks. It works before the 169 + pre-processor so it's easier to check for bugs in macros using Coccinelle. 170 + Coccinelle also creates patches for you, which no other tool does. 171 + 172 + For example, with Coccinelle you can do a mass conversion from 173 + ``kmalloc(x * size, GFP_KERNEL)`` to ``kmalloc_array(x, size, GFP_KERNEL)``, and 174 + that's really useful. If you just created a Smatch warning and try to push the 175 + work of converting on to the maintainers they would be annoyed. You'd have to 176 + argue about each warning if can really overflow or not. 177 + 178 + Coccinelle does no analysis of variable values, which is the strong point of 179 + Smatch. On the other hand, Coccinelle allows you to do simple things in a simple 180 + way.