coccinelle.rst 16 KB

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