coccinelle.txt 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. Getting Coccinelle
  5. ~~~~~~~~~~~~~~~~~~~~
  6. The semantic patches included in the kernel use features and options
  7. which are provided by Coccinelle version 1.0.0-rc11 and above.
  8. Using earlier versions will fail as the option names used by
  9. the Coccinelle files and coccicheck have been updated.
  10. Coccinelle is available through the package manager
  11. of many distributions, e.g. :
  12. - Debian
  13. - Fedora
  14. - Ubuntu
  15. - OpenSUSE
  16. - Arch Linux
  17. - NetBSD
  18. - FreeBSD
  19. You can get the latest version released from the Coccinelle homepage at
  20. http://coccinelle.lip6.fr/
  21. Information and tips about Coccinelle are also provided on the wiki
  22. pages at http://cocci.ekstranet.diku.dk/wiki/doku.php
  23. Once you have it, run the following command:
  24. ./configure
  25. make
  26. as a regular user, and install it with
  27. sudo make install
  28. Using Coccinelle on the Linux kernel
  29. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  30. A Coccinelle-specific target is defined in the top level
  31. Makefile. This target is named 'coccicheck' and calls the 'coccicheck'
  32. front-end in the 'scripts' directory.
  33. Four basic modes are defined: patch, report, context, and org. The mode to
  34. use is specified by setting the MODE variable with 'MODE=<mode>'.
  35. 'patch' proposes a fix, when possible.
  36. 'report' generates a list in the following format:
  37. file:line:column-column: message
  38. 'context' highlights lines of interest and their context in a
  39. diff-like style.Lines of interest are indicated with '-'.
  40. 'org' generates a report in the Org mode format of Emacs.
  41. Note that not all semantic patches implement all modes. For easy use
  42. of Coccinelle, the default mode is "report".
  43. Two other modes provide some common combinations of these modes.
  44. 'chain' tries the previous modes in the order above until one succeeds.
  45. 'rep+ctxt' runs successively the report mode and the context mode.
  46. It should be used with the C option (described later)
  47. which checks the code on a file basis.
  48. Examples:
  49. To make a report for every semantic patch, run the following command:
  50. make coccicheck MODE=report
  51. To produce patches, run:
  52. make coccicheck MODE=patch
  53. The coccicheck target applies every semantic patch available in the
  54. sub-directories of 'scripts/coccinelle' to the entire Linux kernel.
  55. For each semantic patch, a commit message is proposed. It gives a
  56. description of the problem being checked by the semantic patch, and
  57. includes a reference to Coccinelle.
  58. As any static code analyzer, Coccinelle produces false
  59. positives. Thus, reports must be carefully checked, and patches
  60. reviewed.
  61. To enable verbose messages set the V= variable, for example:
  62. make coccicheck MODE=report V=1
  63. By default, coccicheck tries to run as parallel as possible. To change
  64. the parallelism, set the J= variable. For example, to run across 4 CPUs:
  65. make coccicheck MODE=report J=4
  66. Using Coccinelle with a single semantic patch
  67. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  68. The optional make variable COCCI can be used to check a single
  69. semantic patch. In that case, the variable must be initialized with
  70. the name of the semantic patch to apply.
  71. For instance:
  72. make coccicheck COCCI=<my_SP.cocci> MODE=patch
  73. or
  74. make coccicheck COCCI=<my_SP.cocci> MODE=report
  75. Controlling Which Files are Processed by Coccinelle
  76. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  77. By default the entire kernel source tree is checked.
  78. To apply Coccinelle to a specific directory, M= can be used.
  79. For example, to check drivers/net/wireless/ one may write:
  80. make coccicheck M=drivers/net/wireless/
  81. To apply Coccinelle on a file basis, instead of a directory basis, the
  82. following command may be used:
  83. make C=1 CHECK="scripts/coccicheck"
  84. To check only newly edited code, use the value 2 for the C flag, i.e.
  85. make C=2 CHECK="scripts/coccicheck"
  86. In these modes, which works on a file basis, there is no information
  87. about semantic patches displayed, and no commit message proposed.
  88. This runs every semantic patch in scripts/coccinelle by default. The
  89. COCCI variable may additionally be used to only apply a single
  90. semantic patch as shown in the previous section.
  91. The "report" mode is the default. You can select another one with the
  92. MODE variable explained above.
  93. Additional flags
  94. ~~~~~~~~~~~~~~~~~~
  95. Additional flags can be passed to spatch through the SPFLAGS
  96. variable.
  97. make SPFLAGS=--use-glimpse coccicheck
  98. make SPFLAGS=--use-idutils coccicheck
  99. See spatch --help to learn more about spatch options.
  100. Note that the '--use-glimpse' and '--use-idutils' options
  101. require external tools for indexing the code. None of them is
  102. thus active by default. However, by indexing the code with
  103. one of these tools, and according to the cocci file used,
  104. spatch could proceed the entire code base more quickly.
  105. Proposing new semantic patches
  106. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  107. New semantic patches can be proposed and submitted by kernel
  108. developers. For sake of clarity, they should be organized in the
  109. sub-directories of 'scripts/coccinelle/'.
  110. Detailed description of the 'report' mode
  111. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  112. 'report' generates a list in the following format:
  113. file:line:column-column: message
  114. Example:
  115. Running
  116. make coccicheck MODE=report COCCI=scripts/coccinelle/api/err_cast.cocci
  117. will execute the following part of the SmPL script.
  118. <smpl>
  119. @r depends on !context && !patch && (org || report)@
  120. expression x;
  121. position p;
  122. @@
  123. ERR_PTR@p(PTR_ERR(x))
  124. @script:python depends on report@
  125. p << r.p;
  126. x << r.x;
  127. @@
  128. msg="ERR_CAST can be used with %s" % (x)
  129. coccilib.report.print_report(p[0], msg)
  130. </smpl>
  131. This SmPL excerpt generates entries on the standard output, as
  132. illustrated below:
  133. /home/user/linux/crypto/ctr.c:188:9-16: ERR_CAST can be used with alg
  134. /home/user/linux/crypto/authenc.c:619:9-16: ERR_CAST can be used with auth
  135. /home/user/linux/crypto/xts.c:227:9-16: ERR_CAST can be used with alg
  136. Detailed description of the 'patch' mode
  137. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  138. When the 'patch' mode is available, it proposes a fix for each problem
  139. identified.
  140. Example:
  141. Running
  142. make coccicheck MODE=patch COCCI=scripts/coccinelle/api/err_cast.cocci
  143. will execute the following part of the SmPL script.
  144. <smpl>
  145. @ depends on !context && patch && !org && !report @
  146. expression x;
  147. @@
  148. - ERR_PTR(PTR_ERR(x))
  149. + ERR_CAST(x)
  150. </smpl>
  151. This SmPL excerpt generates patch hunks on the standard output, as
  152. illustrated below:
  153. diff -u -p a/crypto/ctr.c b/crypto/ctr.c
  154. --- a/crypto/ctr.c 2010-05-26 10:49:38.000000000 +0200
  155. +++ b/crypto/ctr.c 2010-06-03 23:44:49.000000000 +0200
  156. @@ -185,7 +185,7 @@ static struct crypto_instance *crypto_ct
  157. alg = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_CIPHER,
  158. CRYPTO_ALG_TYPE_MASK);
  159. if (IS_ERR(alg))
  160. - return ERR_PTR(PTR_ERR(alg));
  161. + return ERR_CAST(alg);
  162. /* Block size must be >= 4 bytes. */
  163. err = -EINVAL;
  164. Detailed description of the 'context' mode
  165. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  166. 'context' highlights lines of interest and their context
  167. in a diff-like style.
  168. NOTE: The diff-like output generated is NOT an applicable patch. The
  169. intent of the 'context' mode is to highlight the important lines
  170. (annotated with minus, '-') and gives some surrounding context
  171. lines around. This output can be used with the diff mode of
  172. Emacs to review the code.
  173. Example:
  174. Running
  175. make coccicheck MODE=context COCCI=scripts/coccinelle/api/err_cast.cocci
  176. will execute the following part of the SmPL script.
  177. <smpl>
  178. @ depends on context && !patch && !org && !report@
  179. expression x;
  180. @@
  181. * ERR_PTR(PTR_ERR(x))
  182. </smpl>
  183. This SmPL excerpt generates diff hunks on the standard output, as
  184. illustrated below:
  185. diff -u -p /home/user/linux/crypto/ctr.c /tmp/nothing
  186. --- /home/user/linux/crypto/ctr.c 2010-05-26 10:49:38.000000000 +0200
  187. +++ /tmp/nothing
  188. @@ -185,7 +185,6 @@ static struct crypto_instance *crypto_ct
  189. alg = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_CIPHER,
  190. CRYPTO_ALG_TYPE_MASK);
  191. if (IS_ERR(alg))
  192. - return ERR_PTR(PTR_ERR(alg));
  193. /* Block size must be >= 4 bytes. */
  194. err = -EINVAL;
  195. Detailed description of the 'org' mode
  196. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  197. 'org' generates a report in the Org mode format of Emacs.
  198. Example:
  199. Running
  200. make coccicheck MODE=org COCCI=scripts/coccinelle/api/err_cast.cocci
  201. will execute the following part of the SmPL script.
  202. <smpl>
  203. @r depends on !context && !patch && (org || report)@
  204. expression x;
  205. position p;
  206. @@
  207. ERR_PTR@p(PTR_ERR(x))
  208. @script:python depends on org@
  209. p << r.p;
  210. x << r.x;
  211. @@
  212. msg="ERR_CAST can be used with %s" % (x)
  213. msg_safe=msg.replace("[","@(").replace("]",")")
  214. coccilib.org.print_todo(p[0], msg_safe)
  215. </smpl>
  216. This SmPL excerpt generates Org entries on the standard output, as
  217. illustrated below:
  218. * TODO [[view:/home/user/linux/crypto/ctr.c::face=ovl-face1::linb=188::colb=9::cole=16][ERR_CAST can be used with alg]]
  219. * TODO [[view:/home/user/linux/crypto/authenc.c::face=ovl-face1::linb=619::colb=9::cole=16][ERR_CAST can be used with auth]]
  220. * TODO [[view:/home/user/linux/crypto/xts.c::face=ovl-face1::linb=227::colb=9::cole=16][ERR_CAST can be used with alg]]