Jonathan Corbet | d228af5 | 2016-08-07 15:09:14 -0600 | [diff] [blame] | 1 | .. Copyright 2004 Linus Torvalds |
| 2 | .. Copyright 2004 Pavel Machek <pavel@ucw.cz> |
| 3 | .. Copyright 2006 Bob Copeland <me@bobcopeland.com> |
| 4 | |
| 5 | Sparse |
| 6 | ====== |
| 7 | |
| 8 | Sparse is a semantic checker for C programs; it can be used to find a |
| 9 | number of potential problems with kernel code. See |
| 10 | https://lwn.net/Articles/689907/ for an overview of sparse; this document |
| 11 | contains some kernel-specific sparse information. |
Luc Van Oostenryck | 1cb3863 | 2020-06-29 18:13:10 +0200 | [diff] [blame] | 12 | More information on sparse, mainly about its internals, can be found in |
| 13 | its official pages at https://sparse.docs.kernel.org. |
Jonathan Corbet | d228af5 | 2016-08-07 15:09:14 -0600 | [diff] [blame] | 14 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
| 16 | Using sparse for typechecking |
Jonathan Corbet | d228af5 | 2016-08-07 15:09:14 -0600 | [diff] [blame] | 17 | ----------------------------- |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
Jonathan Corbet | d228af5 | 2016-08-07 15:09:14 -0600 | [diff] [blame] | 19 | "__bitwise" is a type attribute, so you have to do something like this:: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
| 21 | typedef int __bitwise pm_request_t; |
| 22 | |
| 23 | enum pm_request { |
| 24 | PM_SUSPEND = (__force pm_request_t) 1, |
| 25 | PM_RESUME = (__force pm_request_t) 2 |
| 26 | }; |
| 27 | |
| 28 | which makes PM_SUSPEND and PM_RESUME "bitwise" integers (the "__force" is |
| 29 | there because sparse will complain about casting to/from a bitwise type, |
| 30 | but in this case we really _do_ want to force the conversion). And because |
| 31 | the enum values are all the same type, now "enum pm_request" will be that |
| 32 | type too. |
| 33 | |
Jonathan Corbet | d228af5 | 2016-08-07 15:09:14 -0600 | [diff] [blame] | 34 | And with gcc, all the "__bitwise"/"__force stuff" goes away, and it all |
| 35 | ends up looking just like integers to gcc. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | Quite frankly, you don't need the enum there. The above all really just |
| 38 | boils down to one special "int __bitwise" type. |
| 39 | |
Jonathan Corbet | d228af5 | 2016-08-07 15:09:14 -0600 | [diff] [blame] | 40 | So the simpler way is to just do:: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
| 42 | typedef int __bitwise pm_request_t; |
| 43 | |
| 44 | #define PM_SUSPEND ((__force pm_request_t) 1) |
| 45 | #define PM_RESUME ((__force pm_request_t) 2) |
| 46 | |
| 47 | and you now have all the infrastructure needed for strict typechecking. |
| 48 | |
| 49 | One small note: the constant integer "0" is special. You can use a |
| 50 | constant zero as a bitwise integer type without sparse ever complaining. |
| 51 | This is because "bitwise" (as the name implies) was designed for making |
| 52 | sure that bitwise types don't get mixed up (little-endian vs big-endian |
| 53 | vs cpu-endian vs whatever), and there the constant "0" really _is_ |
| 54 | special. |
| 55 | |
Ed Cashin | 6e97663 | 2012-12-17 16:03:25 -0800 | [diff] [blame] | 56 | Using sparse for lock checking |
Jonathan Corbet | d228af5 | 2016-08-07 15:09:14 -0600 | [diff] [blame] | 57 | ------------------------------ |
Ed Cashin | 6e97663 | 2012-12-17 16:03:25 -0800 | [diff] [blame] | 58 | |
| 59 | The following macros are undefined for gcc and defined during a sparse |
| 60 | run to use the "context" tracking feature of sparse, applied to |
| 61 | locking. These annotations tell sparse when a lock is held, with |
| 62 | regard to the annotated function's entry and exit. |
| 63 | |
| 64 | __must_hold - The specified lock is held on function entry and exit. |
| 65 | |
| 66 | __acquires - The specified lock is held on function exit, but not entry. |
| 67 | |
| 68 | __releases - The specified lock is held on function entry, but not exit. |
| 69 | |
| 70 | If the function enters and exits without the lock held, acquiring and |
| 71 | releasing the lock inside the function in a balanced way, no |
Eric Engestrom | 3b7ea9f | 2018-03-13 11:10:58 +0000 | [diff] [blame] | 72 | annotation is needed. The three annotations above are for cases where |
Ed Cashin | 6e97663 | 2012-12-17 16:03:25 -0800 | [diff] [blame] | 73 | sparse would otherwise report a context imbalance. |
Sam Ravnborg | 20375bf | 2009-04-10 13:18:08 +0200 | [diff] [blame] | 74 | |
Bob Copeland | e833195 | 2006-06-23 02:06:09 -0700 | [diff] [blame] | 75 | Getting sparse |
Jonathan Corbet | d228af5 | 2016-08-07 15:09:14 -0600 | [diff] [blame] | 76 | -------------- |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
Luc Van Oostenryck | 48d4b96 | 2020-06-21 17:33:30 +0200 | [diff] [blame] | 78 | You can get tarballs of the latest released versions from: |
| 79 | https://www.kernel.org/pub/software/devel/sparse/dist/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
Dave Jones | a55028f | 2007-03-08 19:45:26 -0500 | [diff] [blame] | 81 | Alternatively, you can get snapshots of the latest development version |
Jonathan Corbet | d228af5 | 2016-08-07 15:09:14 -0600 | [diff] [blame] | 82 | of sparse using git to clone:: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
Bill Pemberton | 05be7a8 | 2010-04-27 16:20:15 -0400 | [diff] [blame] | 84 | git://git.kernel.org/pub/scm/devel/sparse/sparse.git |
Dave Jones | a55028f | 2007-03-08 19:45:26 -0500 | [diff] [blame] | 85 | |
Jonathan Corbet | d228af5 | 2016-08-07 15:09:14 -0600 | [diff] [blame] | 86 | Once you have it, just do:: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
| 88 | make |
| 89 | make install |
| 90 | |
Bob Copeland | e833195 | 2006-06-23 02:06:09 -0700 | [diff] [blame] | 91 | as a regular user, and it will install sparse in your ~/bin directory. |
| 92 | |
| 93 | Using sparse |
Jonathan Corbet | d228af5 | 2016-08-07 15:09:14 -0600 | [diff] [blame] | 94 | ------------ |
Bob Copeland | e833195 | 2006-06-23 02:06:09 -0700 | [diff] [blame] | 95 | |
| 96 | Do a kernel make with "make C=1" to run sparse on all the C files that get |
| 97 | recompiled, or use "make C=2" to run sparse on the files whether they need to |
| 98 | be recompiled or not. The latter is a fast way to check the whole tree if you |
| 99 | have already built it. |
| 100 | |
Geert Uytterhoeven | a887a07 | 2008-06-20 15:45:12 +0200 | [diff] [blame] | 101 | The optional make variable CF can be used to pass arguments to sparse. The |
Michael S. Tsirkin | dc67a9f | 2016-12-11 06:41:20 +0200 | [diff] [blame] | 102 | build system passes -Wbitwise to sparse automatically. |