coccinelle.txt 7.9 KB

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