coccinelle.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. .. Copyright 2010 Nicolas Palix <npalix@diku.dk>
  2. .. Copyright 2010 Julia Lawall <julia@diku.dk>
  3. .. Copyright 2010 Gilles Muller <Gilles.Muller@lip6.fr>
  4. .. highlight:: none
  5. .. _devtools_coccinelle:
  6. Coccinelle
  7. ==========
  8. Coccinelle is a tool for pattern matching and text transformation that has
  9. many uses in kernel development, including the application of complex,
  10. tree-wide patches and detection of problematic programming patterns.
  11. Getting Coccinelle
  12. ------------------
  13. The semantic patches included in the kernel use features and options
  14. which are provided by Coccinelle version 1.0.0-rc11 and above.
  15. Using earlier versions will fail as the option names used by
  16. the Coccinelle files and coccicheck have been updated.
  17. Coccinelle is available through the package manager
  18. of many distributions, e.g. :
  19. - Debian
  20. - Fedora
  21. - Ubuntu
  22. - OpenSUSE
  23. - Arch Linux
  24. - NetBSD
  25. - FreeBSD
  26. Some distribution packages are obsolete and it is recommended
  27. to use the latest version released from the Coccinelle homepage at
  28. http://coccinelle.lip6.fr/
  29. Or from Github at:
  30. https://github.com/coccinelle/coccinelle
  31. Once you have it, run the following commands::
  32. ./autogen
  33. ./configure
  34. make
  35. as a regular user, and install it with::
  36. sudo make install
  37. More detailed installation instructions to build from source can be
  38. found at:
  39. https://github.com/coccinelle/coccinelle/blob/master/install.txt
  40. Supplemental documentation
  41. --------------------------
  42. For supplemental documentation refer to the wiki:
  43. https://bottest.wiki.kernel.org/coccicheck
  44. The wiki documentation always refers to the linux-next version of the script.
  45. For Semantic Patch Language(SmPL) grammar documentation refer to:
  46. http://coccinelle.lip6.fr/documentation.php
  47. Using Coccinelle on the Linux kernel
  48. ------------------------------------
  49. A Coccinelle-specific target is defined in the top level
  50. Makefile. This target is named ``coccicheck`` and calls the ``coccicheck``
  51. front-end in the ``scripts`` directory.
  52. Four basic modes are defined: ``patch``, ``report``, ``context``, and
  53. ``org``. The mode to use is specified by setting the MODE variable with
  54. ``MODE=<mode>``.
  55. - ``patch`` proposes a fix, when possible.
  56. - ``report`` generates a list in the following format:
  57. file:line:column-column: message
  58. - ``context`` highlights lines of interest and their context in a
  59. diff-like style.Lines of interest are indicated with ``-``.
  60. - ``org`` generates a report in the Org mode format of Emacs.
  61. Note that not all semantic patches implement all modes. For easy use
  62. of Coccinelle, the default mode is "report".
  63. Two other modes provide some common combinations of these modes.
  64. - ``chain`` tries the previous modes in the order above until one succeeds.
  65. - ``rep+ctxt`` runs successively the report mode and the context mode.
  66. It should be used with the C option (described later)
  67. which checks the code on a file basis.
  68. Examples
  69. ~~~~~~~~
  70. To make a report for every semantic patch, run the following command::
  71. make coccicheck MODE=report
  72. To produce patches, run::
  73. make coccicheck MODE=patch
  74. The coccicheck target applies every semantic patch available in the
  75. sub-directories of ``scripts/coccinelle`` to the entire Linux kernel.
  76. For each semantic patch, a commit message is proposed. It gives a
  77. description of the problem being checked by the semantic patch, and
  78. includes a reference to Coccinelle.
  79. As any static code analyzer, Coccinelle produces false
  80. positives. Thus, reports must be carefully checked, and patches
  81. reviewed.
  82. To enable verbose messages set the V= variable, for example::
  83. make coccicheck MODE=report V=1
  84. Coccinelle parallelization
  85. --------------------------
  86. By default, coccicheck tries to run as parallel as possible. To change
  87. the parallelism, set the J= variable. For example, to run across 4 CPUs::
  88. make coccicheck MODE=report J=4
  89. As of Coccinelle 1.0.2 Coccinelle uses Ocaml parmap for parallelization,
  90. if support for this is detected you will benefit from parmap parallelization.
  91. When parmap is enabled coccicheck will enable dynamic load balancing by using
  92. ``--chunksize 1`` argument, this ensures we keep feeding threads with work
  93. one by one, so that we avoid the situation where most work gets done by only
  94. a few threads. With dynamic load balancing, if a thread finishes early we keep
  95. feeding it more work.
  96. When parmap is enabled, if an error occurs in Coccinelle, this error
  97. value is propagated back, the return value of the ``make coccicheck``
  98. captures this return value.
  99. Using Coccinelle with a single semantic patch
  100. ---------------------------------------------
  101. The optional make variable COCCI can be used to check a single
  102. semantic patch. In that case, the variable must be initialized with
  103. the name of the semantic patch to apply.
  104. For instance::
  105. make coccicheck COCCI=<my_SP.cocci> MODE=patch
  106. or::
  107. make coccicheck COCCI=<my_SP.cocci> MODE=report
  108. Controlling Which Files are Processed by Coccinelle
  109. ---------------------------------------------------
  110. By default the entire kernel source tree is checked.
  111. To apply Coccinelle to a specific directory, ``M=`` can be used.
  112. For example, to check drivers/net/wireless/ one may write::
  113. make coccicheck M=drivers/net/wireless/
  114. To apply Coccinelle on a file basis, instead of a directory basis, the
  115. following command may be used::
  116. make C=1 CHECK="scripts/coccicheck"
  117. To check only newly edited code, use the value 2 for the C flag, i.e.::
  118. make C=2 CHECK="scripts/coccicheck"
  119. In these modes, which works on a file basis, there is no information
  120. about semantic patches displayed, and no commit message proposed.
  121. This runs every semantic patch in scripts/coccinelle by default. The
  122. COCCI variable may additionally be used to only apply a single
  123. semantic patch as shown in the previous section.
  124. The "report" mode is the default. You can select another one with the
  125. MODE variable explained above.
  126. Debugging Coccinelle SmPL patches
  127. ---------------------------------
  128. Using coccicheck is best as it provides in the spatch command line
  129. include options matching the options used when we compile the kernel.
  130. You can learn what these options are by using V=1, you could then
  131. manually run Coccinelle with debug options added.
  132. Alternatively you can debug running Coccinelle against SmPL patches
  133. by asking for stderr to be redirected to stderr, by default stderr
  134. is redirected to /dev/null, if you'd like to capture stderr you
  135. can specify the ``DEBUG_FILE="file.txt"`` option to coccicheck. For
  136. instance::
  137. rm -f cocci.err
  138. make coccicheck COCCI=scripts/coccinelle/free/kfree.cocci MODE=report DEBUG_FILE=cocci.err
  139. cat cocci.err
  140. You can use SPFLAGS to add debugging flags, for instance you may want to
  141. add both --profile --show-trying to SPFLAGS when debugging. For instance
  142. you may want to use::
  143. rm -f err.log
  144. export COCCI=scripts/coccinelle/misc/irqf_oneshot.cocci
  145. make coccicheck DEBUG_FILE="err.log" MODE=report SPFLAGS="--profile --show-trying" M=./drivers/mfd/arizona-irq.c
  146. err.log will now have the profiling information, while stdout will
  147. provide some progress information as Coccinelle moves forward with
  148. work.
  149. DEBUG_FILE support is only supported when using coccinelle >= 1.0.2.
  150. .cocciconfig support
  151. --------------------
  152. Coccinelle supports reading .cocciconfig for default Coccinelle options that
  153. should be used every time spatch is spawned, the order of precedence for
  154. variables for .cocciconfig is as follows:
  155. - Your current user's home directory is processed first
  156. - Your directory from which spatch is called is processed next
  157. - The directory provided with the --dir option is processed last, if used
  158. Since coccicheck runs through make, it naturally runs from the kernel
  159. proper dir, as such the second rule above would be implied for picking up a
  160. .cocciconfig when using ``make coccicheck``.
  161. ``make coccicheck`` also supports using M= targets. If you do not supply
  162. any M= target, it is assumed you want to target the entire kernel.
  163. The kernel coccicheck script has::
  164. if [ "$KBUILD_EXTMOD" = "" ] ; then
  165. OPTIONS="--dir $srctree $COCCIINCLUDE"
  166. else
  167. OPTIONS="--dir $KBUILD_EXTMOD $COCCIINCLUDE"
  168. fi
  169. KBUILD_EXTMOD is set when an explicit target with M= is used. For both cases
  170. the spatch --dir argument is used, as such third rule applies when whether M=
  171. is used or not, and when M= is used the target directory can have its own
  172. .cocciconfig file. When M= is not passed as an argument to coccicheck the
  173. target directory is the same as the directory from where spatch was called.
  174. If not using the kernel's coccicheck target, keep the above precedence
  175. order logic of .cocciconfig reading. If using the kernel's coccicheck target,
  176. override any of the kernel's .coccicheck's settings using SPFLAGS.
  177. We help Coccinelle when used against Linux with a set of sensible defaults
  178. options for Linux with our own Linux .cocciconfig. This hints to coccinelle
  179. git can be used for ``git grep`` queries over coccigrep. A timeout of 200
  180. seconds should suffice for now.
  181. The options picked up by coccinelle when reading a .cocciconfig do not appear
  182. as arguments to spatch processes running on your system, to confirm what
  183. options will be used by Coccinelle run::
  184. spatch --print-options-only
  185. You can override with your own preferred index option by using SPFLAGS. Take
  186. note that when there are conflicting options Coccinelle takes precedence for
  187. the last options passed. Using .cocciconfig is possible to use idutils, however
  188. given the order of precedence followed by Coccinelle, since the kernel now
  189. carries its own .cocciconfig, you will need to use SPFLAGS to use idutils if
  190. desired. See below section "Additional flags" for more details on how to use
  191. idutils.
  192. Additional flags
  193. ----------------
  194. Additional flags can be passed to spatch through the SPFLAGS
  195. variable. This works as Coccinelle respects the last flags
  196. given to it when options are in conflict. ::
  197. make SPFLAGS=--use-glimpse coccicheck
  198. Coccinelle supports idutils as well but requires coccinelle >= 1.0.6.
  199. When no ID file is specified coccinelle assumes your ID database file
  200. is in the file .id-utils.index on the top level of the kernel, coccinelle
  201. carries a script scripts/idutils_index.sh which creates the database with::
  202. mkid -i C --output .id-utils.index
  203. If you have another database filename you can also just symlink with this
  204. name. ::
  205. make SPFLAGS=--use-idutils coccicheck
  206. Alternatively you can specify the database filename explicitly, for
  207. instance::
  208. make SPFLAGS="--use-idutils /full-path/to/ID" coccicheck
  209. See ``spatch --help`` to learn more about spatch options.
  210. Note that the ``--use-glimpse`` and ``--use-idutils`` options
  211. require external tools for indexing the code. None of them is
  212. thus active by default. However, by indexing the code with
  213. one of these tools, and according to the cocci file used,
  214. spatch could proceed the entire code base more quickly.
  215. SmPL patch specific options
  216. ---------------------------
  217. SmPL patches can have their own requirements for options passed
  218. to Coccinelle. SmPL patch specific options can be provided by
  219. providing them at the top of the SmPL patch, for instance::
  220. // Options: --no-includes --include-headers
  221. SmPL patch Coccinelle requirements
  222. ----------------------------------
  223. As Coccinelle features get added some more advanced SmPL patches
  224. may require newer versions of Coccinelle. If an SmPL patch requires
  225. at least a version of Coccinelle, this can be specified as follows,
  226. as an example if requiring at least Coccinelle >= 1.0.5::
  227. // Requires: 1.0.5
  228. Proposing new semantic patches
  229. ------------------------------
  230. New semantic patches can be proposed and submitted by kernel
  231. developers. For sake of clarity, they should be organized in the
  232. sub-directories of ``scripts/coccinelle/``.
  233. Detailed description of the ``report`` mode
  234. -------------------------------------------
  235. ``report`` generates a list in the following format::
  236. file:line:column-column: message
  237. Example
  238. ~~~~~~~
  239. Running::
  240. make coccicheck MODE=report COCCI=scripts/coccinelle/api/err_cast.cocci
  241. will execute the following part of the SmPL script::
  242. <smpl>
  243. @r depends on !context && !patch && (org || report)@
  244. expression x;
  245. position p;
  246. @@
  247. ERR_PTR@p(PTR_ERR(x))
  248. @script:python depends on report@
  249. p << r.p;
  250. x << r.x;
  251. @@
  252. msg="ERR_CAST can be used with %s" % (x)
  253. coccilib.report.print_report(p[0], msg)
  254. </smpl>
  255. This SmPL excerpt generates entries on the standard output, as
  256. illustrated below::
  257. /home/user/linux/crypto/ctr.c:188:9-16: ERR_CAST can be used with alg
  258. /home/user/linux/crypto/authenc.c:619:9-16: ERR_CAST can be used with auth
  259. /home/user/linux/crypto/xts.c:227:9-16: ERR_CAST can be used with alg
  260. Detailed description of the ``patch`` mode
  261. ------------------------------------------
  262. When the ``patch`` mode is available, it proposes a fix for each problem
  263. identified.
  264. Example
  265. ~~~~~~~
  266. Running::
  267. make coccicheck MODE=patch COCCI=scripts/coccinelle/api/err_cast.cocci
  268. will execute the following part of the SmPL script::
  269. <smpl>
  270. @ depends on !context && patch && !org && !report @
  271. expression x;
  272. @@
  273. - ERR_PTR(PTR_ERR(x))
  274. + ERR_CAST(x)
  275. </smpl>
  276. This SmPL excerpt generates patch hunks on the standard output, as
  277. illustrated below::
  278. diff -u -p a/crypto/ctr.c b/crypto/ctr.c
  279. --- a/crypto/ctr.c 2010-05-26 10:49:38.000000000 +0200
  280. +++ b/crypto/ctr.c 2010-06-03 23:44:49.000000000 +0200
  281. @@ -185,7 +185,7 @@ static struct crypto_instance *crypto_ct
  282. alg = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_CIPHER,
  283. CRYPTO_ALG_TYPE_MASK);
  284. if (IS_ERR(alg))
  285. - return ERR_PTR(PTR_ERR(alg));
  286. + return ERR_CAST(alg);
  287. /* Block size must be >= 4 bytes. */
  288. err = -EINVAL;
  289. Detailed description of the ``context`` mode
  290. --------------------------------------------
  291. ``context`` highlights lines of interest and their context
  292. in a diff-like style.
  293. **NOTE**: The diff-like output generated is NOT an applicable patch. The
  294. intent of the ``context`` mode is to highlight the important lines
  295. (annotated with minus, ``-``) and gives some surrounding context
  296. lines around. This output can be used with the diff mode of
  297. Emacs to review the code.
  298. Example
  299. ~~~~~~~
  300. Running::
  301. make coccicheck MODE=context COCCI=scripts/coccinelle/api/err_cast.cocci
  302. will execute the following part of the SmPL script::
  303. <smpl>
  304. @ depends on context && !patch && !org && !report@
  305. expression x;
  306. @@
  307. * ERR_PTR(PTR_ERR(x))
  308. </smpl>
  309. This SmPL excerpt generates diff hunks on the standard output, as
  310. illustrated below::
  311. diff -u -p /home/user/linux/crypto/ctr.c /tmp/nothing
  312. --- /home/user/linux/crypto/ctr.c 2010-05-26 10:49:38.000000000 +0200
  313. +++ /tmp/nothing
  314. @@ -185,7 +185,6 @@ static struct crypto_instance *crypto_ct
  315. alg = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_CIPHER,
  316. CRYPTO_ALG_TYPE_MASK);
  317. if (IS_ERR(alg))
  318. - return ERR_PTR(PTR_ERR(alg));
  319. /* Block size must be >= 4 bytes. */
  320. err = -EINVAL;
  321. Detailed description of the ``org`` mode
  322. ----------------------------------------
  323. ``org`` generates a report in the Org mode format of Emacs.
  324. Example
  325. ~~~~~~~
  326. Running::
  327. make coccicheck MODE=org COCCI=scripts/coccinelle/api/err_cast.cocci
  328. will execute the following part of the SmPL script::
  329. <smpl>
  330. @r depends on !context && !patch && (org || report)@
  331. expression x;
  332. position p;
  333. @@
  334. ERR_PTR@p(PTR_ERR(x))
  335. @script:python depends on org@
  336. p << r.p;
  337. x << r.x;
  338. @@
  339. msg="ERR_CAST can be used with %s" % (x)
  340. msg_safe=msg.replace("[","@(").replace("]",")")
  341. coccilib.org.print_todo(p[0], msg_safe)
  342. </smpl>
  343. This SmPL excerpt generates Org entries on the standard output, as
  344. illustrated below::
  345. * TODO [[view:/home/user/linux/crypto/ctr.c::face=ovl-face1::linb=188::colb=9::cole=16][ERR_CAST can be used with alg]]
  346. * TODO [[view:/home/user/linux/crypto/authenc.c::face=ovl-face1::linb=619::colb=9::cole=16][ERR_CAST can be used with auth]]
  347. * TODO [[view:/home/user/linux/crypto/xts.c::face=ovl-face1::linb=227::colb=9::cole=16][ERR_CAST can be used with alg]]