coccinelle.txt 6.7 KB

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