coccinelle.txt 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 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 "chain" which tries the previous
  45. modes in the order above until one succeeds.
  46. To make a report for every semantic patch, run the following command:
  47. make coccicheck MODE=report
  48. NB: The 'report' mode is the default one.
  49. To produce patches, run:
  50. make coccicheck MODE=patch
  51. The coccicheck target applies every semantic patch available in the
  52. sub-directories of 'scripts/coccinelle' to the entire Linux kernel.
  53. For each semantic patch, a commit message is proposed. It gives a
  54. description of the problem being checked by the semantic patch, and
  55. includes a reference to Coccinelle.
  56. As any static code analyzer, Coccinelle produces false
  57. positives. Thus, reports must be carefully checked, and patches
  58. reviewed.
  59. Using Coccinelle with a single semantic patch
  60. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  61. The optional make variable COCCI can be used to check a single
  62. semantic patch. In that case, the variable must be initialized with
  63. the name of the semantic patch to apply.
  64. For instance:
  65. make coccicheck COCCI=<my_SP.cocci> MODE=patch
  66. or
  67. make coccicheck COCCI=<my_SP.cocci> MODE=report
  68. Controlling Which Files are Processed by Coccinelle
  69. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  70. By default the entire kernel source tree is checked.
  71. To apply Coccinelle to a specific directory, M= can be used.
  72. For example, to check drivers/net/wireless/ one may write:
  73. make coccicheck M=drivers/net/wireless/
  74. To apply Coccinelle on a file basis, instead of a directory basis, the
  75. following command may be used:
  76. make C=1 CHECK="scripts/coccicheck"
  77. To check only newly edited code, use the value 2 for the C flag, i.e.
  78. make C=2 CHECK="scripts/coccicheck"
  79. This runs every semantic patch in scripts/coccinelle by default. The
  80. COCCI variable may additionally be used to only apply a single
  81. semantic patch as shown in the previous section.
  82. The "chain" mode is the default. You can select another one with the
  83. MODE variable explained above.
  84. In this mode, there is no information about semantic patches
  85. displayed, and no commit message proposed.
  86. Proposing new semantic patches
  87. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  88. New semantic patches can be proposed and submitted by kernel
  89. developers. For sake of clarity, they should be organized in the
  90. sub-directories of 'scripts/coccinelle/'.
  91. Detailed description of the 'report' mode
  92. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  93. 'report' generates a list in the following format:
  94. file:line:column-column: message
  95. Example:
  96. Running
  97. make coccicheck MODE=report COCCI=scripts/coccinelle/api/err_cast.cocci
  98. will execute the following part of the SmPL script.
  99. <smpl>
  100. @r depends on !context && !patch && (org || report)@
  101. expression x;
  102. position p;
  103. @@
  104. ERR_PTR@p(PTR_ERR(x))
  105. @script:python depends on report@
  106. p << r.p;
  107. x << r.x;
  108. @@
  109. msg="ERR_CAST can be used with %s" % (x)
  110. coccilib.report.print_report(p[0], msg)
  111. </smpl>
  112. This SmPL excerpt generates entries on the standard output, as
  113. illustrated below:
  114. /home/user/linux/crypto/ctr.c:188:9-16: ERR_CAST can be used with alg
  115. /home/user/linux/crypto/authenc.c:619:9-16: ERR_CAST can be used with auth
  116. /home/user/linux/crypto/xts.c:227:9-16: ERR_CAST can be used with alg
  117. Detailed description of the 'patch' mode
  118. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  119. When the 'patch' mode is available, it proposes a fix for each problem
  120. identified.
  121. Example:
  122. Running
  123. make coccicheck MODE=patch COCCI=scripts/coccinelle/api/err_cast.cocci
  124. will execute the following part of the SmPL script.
  125. <smpl>
  126. @ depends on !context && patch && !org && !report @
  127. expression x;
  128. @@
  129. - ERR_PTR(PTR_ERR(x))
  130. + ERR_CAST(x)
  131. </smpl>
  132. This SmPL excerpt generates patch hunks on the standard output, as
  133. illustrated below:
  134. diff -u -p a/crypto/ctr.c b/crypto/ctr.c
  135. --- a/crypto/ctr.c 2010-05-26 10:49:38.000000000 +0200
  136. +++ b/crypto/ctr.c 2010-06-03 23:44:49.000000000 +0200
  137. @@ -185,7 +185,7 @@ static struct crypto_instance *crypto_ct
  138. alg = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_CIPHER,
  139. CRYPTO_ALG_TYPE_MASK);
  140. if (IS_ERR(alg))
  141. - return ERR_PTR(PTR_ERR(alg));
  142. + return ERR_CAST(alg);
  143. /* Block size must be >= 4 bytes. */
  144. err = -EINVAL;
  145. Detailed description of the 'context' mode
  146. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  147. 'context' highlights lines of interest and their context
  148. in a diff-like style.
  149. NOTE: The diff-like output generated is NOT an applicable patch. The
  150. intent of the 'context' mode is to highlight the important lines
  151. (annotated with minus, '-') and gives some surrounding context
  152. lines around. This output can be used with the diff mode of
  153. Emacs to review the code.
  154. Example:
  155. Running
  156. make coccicheck MODE=context COCCI=scripts/coccinelle/api/err_cast.cocci
  157. will execute the following part of the SmPL script.
  158. <smpl>
  159. @ depends on context && !patch && !org && !report@
  160. expression x;
  161. @@
  162. * ERR_PTR(PTR_ERR(x))
  163. </smpl>
  164. This SmPL excerpt generates diff hunks on the standard output, as
  165. illustrated below:
  166. diff -u -p /home/user/linux/crypto/ctr.c /tmp/nothing
  167. --- /home/user/linux/crypto/ctr.c 2010-05-26 10:49:38.000000000 +0200
  168. +++ /tmp/nothing
  169. @@ -185,7 +185,6 @@ static struct crypto_instance *crypto_ct
  170. alg = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_CIPHER,
  171. CRYPTO_ALG_TYPE_MASK);
  172. if (IS_ERR(alg))
  173. - return ERR_PTR(PTR_ERR(alg));
  174. /* Block size must be >= 4 bytes. */
  175. err = -EINVAL;
  176. Detailed description of the 'org' mode
  177. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  178. 'org' generates a report in the Org mode format of Emacs.
  179. Example:
  180. Running
  181. make coccicheck MODE=org COCCI=scripts/coccinelle/api/err_cast.cocci
  182. will execute the following part of the SmPL script.
  183. <smpl>
  184. @r depends on !context && !patch && (org || report)@
  185. expression x;
  186. position p;
  187. @@
  188. ERR_PTR@p(PTR_ERR(x))
  189. @script:python depends on org@
  190. p << r.p;
  191. x << r.x;
  192. @@
  193. msg="ERR_CAST can be used with %s" % (x)
  194. msg_safe=msg.replace("[","@(").replace("]",")")
  195. coccilib.org.print_todo(p[0], msg_safe)
  196. </smpl>
  197. This SmPL excerpt generates Org entries on the standard output, as
  198. illustrated below:
  199. * TODO [[view:/home/user/linux/crypto/ctr.c::face=ovl-face1::linb=188::colb=9::cole=16][ERR_CAST can be used with alg]]
  200. * TODO [[view:/home/user/linux/crypto/authenc.c::face=ovl-face1::linb=619::colb=9::cole=16][ERR_CAST can be used with auth]]
  201. * TODO [[view:/home/user/linux/crypto/xts.c::face=ovl-face1::linb=227::colb=9::cole=16][ERR_CAST can be used with alg]]