coccinelle.txt 7.7 KB

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