162306a36Sopenharmony_ci.. Copyright 2004 Linus Torvalds 262306a36Sopenharmony_ci.. Copyright 2004 Pavel Machek <pavel@ucw.cz> 362306a36Sopenharmony_ci.. Copyright 2006 Bob Copeland <me@bobcopeland.com> 462306a36Sopenharmony_ci 562306a36Sopenharmony_ciSparse 662306a36Sopenharmony_ci====== 762306a36Sopenharmony_ci 862306a36Sopenharmony_ciSparse is a semantic checker for C programs; it can be used to find a 962306a36Sopenharmony_cinumber of potential problems with kernel code. See 1062306a36Sopenharmony_cihttps://lwn.net/Articles/689907/ for an overview of sparse; this document 1162306a36Sopenharmony_cicontains some kernel-specific sparse information. 1262306a36Sopenharmony_ciMore information on sparse, mainly about its internals, can be found in 1362306a36Sopenharmony_ciits official pages at https://sparse.docs.kernel.org. 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_ci 1662306a36Sopenharmony_ciUsing sparse for typechecking 1762306a36Sopenharmony_ci----------------------------- 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_ci"__bitwise" is a type attribute, so you have to do something like this:: 2062306a36Sopenharmony_ci 2162306a36Sopenharmony_ci typedef int __bitwise pm_request_t; 2262306a36Sopenharmony_ci 2362306a36Sopenharmony_ci enum pm_request { 2462306a36Sopenharmony_ci PM_SUSPEND = (__force pm_request_t) 1, 2562306a36Sopenharmony_ci PM_RESUME = (__force pm_request_t) 2 2662306a36Sopenharmony_ci }; 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_ciwhich makes PM_SUSPEND and PM_RESUME "bitwise" integers (the "__force" is 2962306a36Sopenharmony_cithere because sparse will complain about casting to/from a bitwise type, 3062306a36Sopenharmony_cibut in this case we really _do_ want to force the conversion). And because 3162306a36Sopenharmony_cithe enum values are all the same type, now "enum pm_request" will be that 3262306a36Sopenharmony_citype too. 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ciAnd with gcc, all the "__bitwise"/"__force stuff" goes away, and it all 3562306a36Sopenharmony_ciends up looking just like integers to gcc. 3662306a36Sopenharmony_ci 3762306a36Sopenharmony_ciQuite frankly, you don't need the enum there. The above all really just 3862306a36Sopenharmony_ciboils down to one special "int __bitwise" type. 3962306a36Sopenharmony_ci 4062306a36Sopenharmony_ciSo the simpler way is to just do:: 4162306a36Sopenharmony_ci 4262306a36Sopenharmony_ci typedef int __bitwise pm_request_t; 4362306a36Sopenharmony_ci 4462306a36Sopenharmony_ci #define PM_SUSPEND ((__force pm_request_t) 1) 4562306a36Sopenharmony_ci #define PM_RESUME ((__force pm_request_t) 2) 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_ciand you now have all the infrastructure needed for strict typechecking. 4862306a36Sopenharmony_ci 4962306a36Sopenharmony_ciOne small note: the constant integer "0" is special. You can use a 5062306a36Sopenharmony_ciconstant zero as a bitwise integer type without sparse ever complaining. 5162306a36Sopenharmony_ciThis is because "bitwise" (as the name implies) was designed for making 5262306a36Sopenharmony_cisure that bitwise types don't get mixed up (little-endian vs big-endian 5362306a36Sopenharmony_civs cpu-endian vs whatever), and there the constant "0" really _is_ 5462306a36Sopenharmony_cispecial. 5562306a36Sopenharmony_ci 5662306a36Sopenharmony_ciUsing sparse for lock checking 5762306a36Sopenharmony_ci------------------------------ 5862306a36Sopenharmony_ci 5962306a36Sopenharmony_ciThe following macros are undefined for gcc and defined during a sparse 6062306a36Sopenharmony_cirun to use the "context" tracking feature of sparse, applied to 6162306a36Sopenharmony_cilocking. These annotations tell sparse when a lock is held, with 6262306a36Sopenharmony_ciregard to the annotated function's entry and exit. 6362306a36Sopenharmony_ci 6462306a36Sopenharmony_ci__must_hold - The specified lock is held on function entry and exit. 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_ci__acquires - The specified lock is held on function exit, but not entry. 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ci__releases - The specified lock is held on function entry, but not exit. 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_ciIf the function enters and exits without the lock held, acquiring and 7162306a36Sopenharmony_cireleasing the lock inside the function in a balanced way, no 7262306a36Sopenharmony_ciannotation is needed. The three annotations above are for cases where 7362306a36Sopenharmony_cisparse would otherwise report a context imbalance. 7462306a36Sopenharmony_ci 7562306a36Sopenharmony_ciGetting sparse 7662306a36Sopenharmony_ci-------------- 7762306a36Sopenharmony_ci 7862306a36Sopenharmony_ciYou can get tarballs of the latest released versions from: 7962306a36Sopenharmony_cihttps://www.kernel.org/pub/software/devel/sparse/dist/ 8062306a36Sopenharmony_ci 8162306a36Sopenharmony_ciAlternatively, you can get snapshots of the latest development version 8262306a36Sopenharmony_ciof sparse using git to clone:: 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_ci git://git.kernel.org/pub/scm/devel/sparse/sparse.git 8562306a36Sopenharmony_ci 8662306a36Sopenharmony_ciOnce you have it, just do:: 8762306a36Sopenharmony_ci 8862306a36Sopenharmony_ci make 8962306a36Sopenharmony_ci make install 9062306a36Sopenharmony_ci 9162306a36Sopenharmony_cias a regular user, and it will install sparse in your ~/bin directory. 9262306a36Sopenharmony_ci 9362306a36Sopenharmony_ciUsing sparse 9462306a36Sopenharmony_ci------------ 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ciDo a kernel make with "make C=1" to run sparse on all the C files that get 9762306a36Sopenharmony_cirecompiled, or use "make C=2" to run sparse on the files whether they need to 9862306a36Sopenharmony_cibe recompiled or not. The latter is a fast way to check the whole tree if you 9962306a36Sopenharmony_cihave already built it. 10062306a36Sopenharmony_ci 10162306a36Sopenharmony_ciThe optional make variable CF can be used to pass arguments to sparse. The 10262306a36Sopenharmony_cibuild system passes -Wbitwise to sparse automatically. 10362306a36Sopenharmony_ci 10462306a36Sopenharmony_ciNote that sparse defines the __CHECKER__ preprocessor symbol. 105