coccinelle.rst 15 KB

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