blob: 408439d078897a98c950fa1c9ec13fa894d13bf7 [file] [log] [blame]
Nicolas Palixe228b1e2010-06-06 17:15:02 +02001Copyright 2010 Nicolas Palix <npalix@diku.dk>
2Copyright 2010 Julia Lawall <julia@diku.dk>
3Copyright 2010 Gilles Muller <Gilles.Muller@lip6.fr>
4
5
6 Getting Coccinelle
7~~~~~~~~~~~~~~~~~~~~
8
9The semantic patches included in the kernel use the 'virtual rule'
10feature which was introduced in Coccinelle version 0.1.11.
11
12Coccinelle (>=0.2.0) is available through the package manager
13of many distributions, e.g. :
14
15 - Debian (>=squeeze)
16 - Fedora (>=13)
Kulikov Vasiliyfbe32902010-06-29 16:31:16 +040017 - Ubuntu (>=10.04 Lucid Lynx)
Nicolas Palixe228b1e2010-06-06 17:15:02 +020018 - OpenSUSE
19 - Arch Linux
20 - NetBSD
21 - FreeBSD
22
23
24You can get the latest version released from the Coccinelle homepage at
25http://coccinelle.lip6.fr/
26
Nicolas Palix32af0892010-10-13 00:49:07 +020027Information and tips about Coccinelle are also provided on the wiki
28pages at http://cocci.ekstranet.diku.dk/wiki/doku.php
29
Nicolas Palixe228b1e2010-06-06 17:15:02 +020030Once you have it, run the following command:
31
32 ./configure
33 make
34
35as a regular user, and install it with
36
37 sudo make install
38
Julia Lawalla1087ef2010-11-24 15:54:18 +010039The semantic patches in the kernel will work best with Coccinelle version
400.2.4 or later. Using earlier versions may incur some parse errors in the
41semantic patch code, but any results that are obtained should still be
42correct.
Nicolas Palixe228b1e2010-06-06 17:15:02 +020043
44 Using Coccinelle on the Linux kernel
45~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
46
47A Coccinelle-specific target is defined in the top level
48Makefile. This target is named 'coccicheck' and calls the 'coccicheck'
49front-end in the 'scripts' directory.
50
Nicolas Palix32af0892010-10-13 00:49:07 +020051Four modes are defined: patch, report, context, and org. The mode to
Nicolas Palixe228b1e2010-06-06 17:15:02 +020052use is specified by setting the MODE variable with 'MODE=<mode>'.
53
Nicolas Palix32af0892010-10-13 00:49:07 +020054'patch' proposes a fix, when possible.
55
Nicolas Palixe228b1e2010-06-06 17:15:02 +020056'report' generates a list in the following format:
57 file:line:column-column: message
58
Nicolas Palixe228b1e2010-06-06 17:15:02 +020059'context' highlights lines of interest and their context in a
60diff-like style.Lines of interest are indicated with '-'.
61
62'org' generates a report in the Org mode format of Emacs.
63
Nicolas Palix32af0892010-10-13 00:49:07 +020064Note that not all semantic patches implement all modes. For easy use
65of Coccinelle, the default mode is "chain" which tries the previous
66modes in the order above until one succeeds.
Nicolas Palixe228b1e2010-06-06 17:15:02 +020067
68To make a report for every semantic patch, run the following command:
69
70 make coccicheck MODE=report
71
72NB: The 'report' mode is the default one.
73
74To produce patches, run:
75
76 make coccicheck MODE=patch
77
78
79The coccicheck target applies every semantic patch available in the
Nicolas Palix32af0892010-10-13 00:49:07 +020080sub-directories of 'scripts/coccinelle' to the entire Linux kernel.
Nicolas Palixe228b1e2010-06-06 17:15:02 +020081
Nicolas Palix32af0892010-10-13 00:49:07 +020082For each semantic patch, a commit message is proposed. It gives a
Nicolas Palixe228b1e2010-06-06 17:15:02 +020083description of the problem being checked by the semantic patch, and
84includes a reference to Coccinelle.
85
86As any static code analyzer, Coccinelle produces false
87positives. Thus, reports must be carefully checked, and patches
88reviewed.
89
Bernd Schubert26e56722013-01-29 17:03:37 +010090To enable verbose messages set the V= variable, for example:
91
92 make coccicheck MODE=report V=1
93
Kees Cook90d06a42013-06-18 14:49:29 -070094By default, coccicheck tries to run as parallel as possible. To change
95the parallelism, set the J= variable. For example, to run across 4 CPUs:
96
97 make coccicheck MODE=report J=4
98
Nicolas Palixe228b1e2010-06-06 17:15:02 +020099
100 Using Coccinelle with a single semantic patch
101~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
102
103The optional make variable COCCI can be used to check a single
104semantic patch. In that case, the variable must be initialized with
105the name of the semantic patch to apply.
106
107For instance:
108
109 make coccicheck COCCI=<my_SP.cocci> MODE=patch
110or
111 make coccicheck COCCI=<my_SP.cocci> MODE=report
112
113
Greg Dietschef95ab202011-11-05 20:59:44 -0500114 Controlling Which Files are Processed by Coccinelle
115~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
116By default the entire kernel source tree is checked.
Nicolas Palix32af0892010-10-13 00:49:07 +0200117
Greg Dietschef95ab202011-11-05 20:59:44 -0500118To apply Coccinelle to a specific directory, M= can be used.
119For example, to check drivers/net/wireless/ one may write:
120
121 make coccicheck M=drivers/net/wireless/
Nicolas Palixed621cc2013-03-02 22:36:27 +0100122
Nicolas Palix32af0892010-10-13 00:49:07 +0200123To apply Coccinelle on a file basis, instead of a directory basis, the
124following command may be used:
125
126 make C=1 CHECK="scripts/coccicheck"
127
128To check only newly edited code, use the value 2 for the C flag, i.e.
129
130 make C=2 CHECK="scripts/coccicheck"
131
132This runs every semantic patch in scripts/coccinelle by default. The
133COCCI variable may additionally be used to only apply a single
134semantic patch as shown in the previous section.
135
136The "chain" mode is the default. You can select another one with the
137MODE variable explained above.
138
139In this mode, there is no information about semantic patches
140displayed, and no commit message proposed.
141
Nicolas Palixed621cc2013-03-02 22:36:27 +0100142 Additional flags
143~~~~~~~~~~~~~~~~~~
144
145Additional flags can be passed to spatch through the SPFLAGS
146variable.
147
148 make SPFLAGS=--use_glimpse coccicheck
149
150See spatch --help to learn more about spatch options.
Nicolas Palix32af0892010-10-13 00:49:07 +0200151
Nicolas Palixe228b1e2010-06-06 17:15:02 +0200152 Proposing new semantic patches
153~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
154
155New semantic patches can be proposed and submitted by kernel
156developers. For sake of clarity, they should be organized in the
Nicolas Palix32af0892010-10-13 00:49:07 +0200157sub-directories of 'scripts/coccinelle/'.
Nicolas Palixe228b1e2010-06-06 17:15:02 +0200158
159
160 Detailed description of the 'report' mode
161~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
162
163'report' generates a list in the following format:
164 file:line:column-column: message
165
166Example:
167
168Running
169
Nicolas Palix9dcf7992010-10-24 23:37:33 +0200170 make coccicheck MODE=report COCCI=scripts/coccinelle/api/err_cast.cocci
Nicolas Palixe228b1e2010-06-06 17:15:02 +0200171
172will execute the following part of the SmPL script.
173
174<smpl>
175@r depends on !context && !patch && (org || report)@
176expression x;
177position p;
178@@
179
180 ERR_PTR@p(PTR_ERR(x))
181
182@script:python depends on report@
183p << r.p;
184x << r.x;
185@@
186
187msg="ERR_CAST can be used with %s" % (x)
188coccilib.report.print_report(p[0], msg)
189</smpl>
190
191This SmPL excerpt generates entries on the standard output, as
192illustrated below:
193
194/home/user/linux/crypto/ctr.c:188:9-16: ERR_CAST can be used with alg
195/home/user/linux/crypto/authenc.c:619:9-16: ERR_CAST can be used with auth
196/home/user/linux/crypto/xts.c:227:9-16: ERR_CAST can be used with alg
197
198
199 Detailed description of the 'patch' mode
200~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
201
202When the 'patch' mode is available, it proposes a fix for each problem
203identified.
204
205Example:
206
207Running
Nicolas Palix9dcf7992010-10-24 23:37:33 +0200208 make coccicheck MODE=patch COCCI=scripts/coccinelle/api/err_cast.cocci
Nicolas Palixe228b1e2010-06-06 17:15:02 +0200209
210will execute the following part of the SmPL script.
211
212<smpl>
213@ depends on !context && patch && !org && !report @
214expression x;
215@@
216
217- ERR_PTR(PTR_ERR(x))
218+ ERR_CAST(x)
219</smpl>
220
221This SmPL excerpt generates patch hunks on the standard output, as
222illustrated below:
223
224diff -u -p a/crypto/ctr.c b/crypto/ctr.c
225--- a/crypto/ctr.c 2010-05-26 10:49:38.000000000 +0200
226+++ b/crypto/ctr.c 2010-06-03 23:44:49.000000000 +0200
227@@ -185,7 +185,7 @@ static struct crypto_instance *crypto_ct
228 alg = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_CIPHER,
229 CRYPTO_ALG_TYPE_MASK);
230 if (IS_ERR(alg))
231- return ERR_PTR(PTR_ERR(alg));
232+ return ERR_CAST(alg);
233
234 /* Block size must be >= 4 bytes. */
235 err = -EINVAL;
236
237 Detailed description of the 'context' mode
238~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
239
240'context' highlights lines of interest and their context
241in a diff-like style.
242
243NOTE: The diff-like output generated is NOT an applicable patch. The
244 intent of the 'context' mode is to highlight the important lines
245 (annotated with minus, '-') and gives some surrounding context
246 lines around. This output can be used with the diff mode of
247 Emacs to review the code.
248
249Example:
250
251Running
Nicolas Palix9dcf7992010-10-24 23:37:33 +0200252 make coccicheck MODE=context COCCI=scripts/coccinelle/api/err_cast.cocci
Nicolas Palixe228b1e2010-06-06 17:15:02 +0200253
254will execute the following part of the SmPL script.
255
256<smpl>
257@ depends on context && !patch && !org && !report@
258expression x;
259@@
260
261* ERR_PTR(PTR_ERR(x))
262</smpl>
263
264This SmPL excerpt generates diff hunks on the standard output, as
265illustrated below:
266
267diff -u -p /home/user/linux/crypto/ctr.c /tmp/nothing
268--- /home/user/linux/crypto/ctr.c 2010-05-26 10:49:38.000000000 +0200
269+++ /tmp/nothing
270@@ -185,7 +185,6 @@ static struct crypto_instance *crypto_ct
271 alg = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_CIPHER,
272 CRYPTO_ALG_TYPE_MASK);
273 if (IS_ERR(alg))
274- return ERR_PTR(PTR_ERR(alg));
275
276 /* Block size must be >= 4 bytes. */
277 err = -EINVAL;
278
279 Detailed description of the 'org' mode
280~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
281
282'org' generates a report in the Org mode format of Emacs.
283
284Example:
285
286Running
Nicolas Palix9dcf7992010-10-24 23:37:33 +0200287 make coccicheck MODE=org COCCI=scripts/coccinelle/api/err_cast.cocci
Nicolas Palixe228b1e2010-06-06 17:15:02 +0200288
289will execute the following part of the SmPL script.
290
291<smpl>
292@r depends on !context && !patch && (org || report)@
293expression x;
294position p;
295@@
296
297 ERR_PTR@p(PTR_ERR(x))
298
299@script:python depends on org@
300p << r.p;
301x << r.x;
302@@
303
304msg="ERR_CAST can be used with %s" % (x)
305msg_safe=msg.replace("[","@(").replace("]",")")
306coccilib.org.print_todo(p[0], msg_safe)
307</smpl>
308
309This SmPL excerpt generates Org entries on the standard output, as
310illustrated below:
311
312* TODO [[view:/home/user/linux/crypto/ctr.c::face=ovl-face1::linb=188::colb=9::cole=16][ERR_CAST can be used with alg]]
313* TODO [[view:/home/user/linux/crypto/authenc.c::face=ovl-face1::linb=619::colb=9::cole=16][ERR_CAST can be used with auth]]
314* TODO [[view:/home/user/linux/crypto/xts.c::face=ovl-face1::linb=227::colb=9::cole=16][ERR_CAST can be used with alg]]