1d228af5bSJonathan Corbet.. Copyright 2004 Linus Torvalds 2d228af5bSJonathan Corbet.. Copyright 2004 Pavel Machek <[email protected]> 3d228af5bSJonathan Corbet.. Copyright 2006 Bob Copeland <[email protected]> 4d228af5bSJonathan Corbet 5d228af5bSJonathan CorbetSparse 6d228af5bSJonathan Corbet====== 7d228af5bSJonathan Corbet 8d228af5bSJonathan CorbetSparse is a semantic checker for C programs; it can be used to find a 9d228af5bSJonathan Corbetnumber of potential problems with kernel code. See 10d228af5bSJonathan Corbethttps://lwn.net/Articles/689907/ for an overview of sparse; this document 11d228af5bSJonathan Corbetcontains some kernel-specific sparse information. 121cb3863aSLuc Van OostenryckMore information on sparse, mainly about its internals, can be found in 131cb3863aSLuc Van Oostenryckits official pages at https://sparse.docs.kernel.org. 14d228af5bSJonathan Corbet 15d228af5bSJonathan Corbet 16d228af5bSJonathan CorbetUsing sparse for typechecking 17d228af5bSJonathan Corbet----------------------------- 18d228af5bSJonathan Corbet 19d228af5bSJonathan Corbet"__bitwise" is a type attribute, so you have to do something like this:: 20d228af5bSJonathan Corbet 21d228af5bSJonathan Corbet typedef int __bitwise pm_request_t; 22d228af5bSJonathan Corbet 23d228af5bSJonathan Corbet enum pm_request { 24d228af5bSJonathan Corbet PM_SUSPEND = (__force pm_request_t) 1, 25d228af5bSJonathan Corbet PM_RESUME = (__force pm_request_t) 2 26d228af5bSJonathan Corbet }; 27d228af5bSJonathan Corbet 28d228af5bSJonathan Corbetwhich makes PM_SUSPEND and PM_RESUME "bitwise" integers (the "__force" is 29d228af5bSJonathan Corbetthere because sparse will complain about casting to/from a bitwise type, 30d228af5bSJonathan Corbetbut in this case we really _do_ want to force the conversion). And because 31d228af5bSJonathan Corbetthe enum values are all the same type, now "enum pm_request" will be that 32d228af5bSJonathan Corbettype too. 33d228af5bSJonathan Corbet 34d228af5bSJonathan CorbetAnd with gcc, all the "__bitwise"/"__force stuff" goes away, and it all 35d228af5bSJonathan Corbetends up looking just like integers to gcc. 36d228af5bSJonathan Corbet 37d228af5bSJonathan CorbetQuite frankly, you don't need the enum there. The above all really just 38d228af5bSJonathan Corbetboils down to one special "int __bitwise" type. 39d228af5bSJonathan Corbet 40d228af5bSJonathan CorbetSo the simpler way is to just do:: 41d228af5bSJonathan Corbet 42d228af5bSJonathan Corbet typedef int __bitwise pm_request_t; 43d228af5bSJonathan Corbet 44d228af5bSJonathan Corbet #define PM_SUSPEND ((__force pm_request_t) 1) 45d228af5bSJonathan Corbet #define PM_RESUME ((__force pm_request_t) 2) 46d228af5bSJonathan Corbet 47d228af5bSJonathan Corbetand you now have all the infrastructure needed for strict typechecking. 48d228af5bSJonathan Corbet 49d228af5bSJonathan CorbetOne small note: the constant integer "0" is special. You can use a 50d228af5bSJonathan Corbetconstant zero as a bitwise integer type without sparse ever complaining. 51d228af5bSJonathan CorbetThis is because "bitwise" (as the name implies) was designed for making 52d228af5bSJonathan Corbetsure that bitwise types don't get mixed up (little-endian vs big-endian 53d228af5bSJonathan Corbetvs cpu-endian vs whatever), and there the constant "0" really _is_ 54d228af5bSJonathan Corbetspecial. 55d228af5bSJonathan Corbet 56d228af5bSJonathan CorbetUsing sparse for lock checking 57d228af5bSJonathan Corbet------------------------------ 58d228af5bSJonathan Corbet 59d228af5bSJonathan CorbetThe following macros are undefined for gcc and defined during a sparse 60d228af5bSJonathan Corbetrun to use the "context" tracking feature of sparse, applied to 61d228af5bSJonathan Corbetlocking. These annotations tell sparse when a lock is held, with 62d228af5bSJonathan Corbetregard to the annotated function's entry and exit. 63d228af5bSJonathan Corbet 64d228af5bSJonathan Corbet__must_hold - The specified lock is held on function entry and exit. 65d228af5bSJonathan Corbet 66d228af5bSJonathan Corbet__acquires - The specified lock is held on function exit, but not entry. 67d228af5bSJonathan Corbet 68d228af5bSJonathan Corbet__releases - The specified lock is held on function entry, but not exit. 69d228af5bSJonathan Corbet 70d228af5bSJonathan CorbetIf the function enters and exits without the lock held, acquiring and 71d228af5bSJonathan Corbetreleasing the lock inside the function in a balanced way, no 723b7ea9f0SEric Engestromannotation is needed. The three annotations above are for cases where 73d228af5bSJonathan Corbetsparse would otherwise report a context imbalance. 74d228af5bSJonathan Corbet 75d228af5bSJonathan CorbetGetting sparse 76d228af5bSJonathan Corbet-------------- 77d228af5bSJonathan Corbet 7848d4b96cSLuc Van OostenryckYou can get tarballs of the latest released versions from: 7948d4b96cSLuc Van Oostenryckhttps://www.kernel.org/pub/software/devel/sparse/dist/ 80d228af5bSJonathan Corbet 81d228af5bSJonathan CorbetAlternatively, you can get snapshots of the latest development version 82d228af5bSJonathan Corbetof sparse using git to clone:: 83d228af5bSJonathan Corbet 84d228af5bSJonathan Corbet git://git.kernel.org/pub/scm/devel/sparse/sparse.git 85d228af5bSJonathan Corbet 86d228af5bSJonathan CorbetOnce you have it, just do:: 87d228af5bSJonathan Corbet 88d228af5bSJonathan Corbet make 89d228af5bSJonathan Corbet make install 90d228af5bSJonathan Corbet 91d228af5bSJonathan Corbetas a regular user, and it will install sparse in your ~/bin directory. 92d228af5bSJonathan Corbet 93d228af5bSJonathan CorbetUsing sparse 94d228af5bSJonathan Corbet------------ 95d228af5bSJonathan Corbet 96d228af5bSJonathan CorbetDo a kernel make with "make C=1" to run sparse on all the C files that get 97d228af5bSJonathan Corbetrecompiled, or use "make C=2" to run sparse on the files whether they need to 98d228af5bSJonathan Corbetbe recompiled or not. The latter is a fast way to check the whole tree if you 99d228af5bSJonathan Corbethave already built it. 100d228af5bSJonathan Corbet 101d228af5bSJonathan CorbetThe optional make variable CF can be used to pass arguments to sparse. The 102dc67a9f7SMichael S. Tsirkinbuild system passes -Wbitwise to sparse automatically. 103*179fd6baSBjorn Helgaas 104*179fd6baSBjorn HelgaasNote that sparse defines the __CHECKER__ preprocessor symbol. 105