coccinelle.txt 9.2 KB

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