blob: 01fb1dae3163ca569045b2ce2a3c2b395bb7d217 [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
Nicolas Palixec979462013-07-03 16:41:01 +02009The semantic patches included in the kernel use features and options
10which are provided by Coccinelle version 1.0.0-rc11 and above.
11Using earlier versions will fail as the option names used by
12the Coccinelle files and coccicheck have been updated.
Nicolas Palixe228b1e2010-06-06 17:15:02 +020013
Nicolas Palixec979462013-07-03 16:41:01 +020014Coccinelle is available through the package manager
Nicolas Palixe228b1e2010-06-06 17:15:02 +020015of many distributions, e.g. :
16
Nicolas Palixec979462013-07-03 16:41:01 +020017 - Debian
18 - Fedora
19 - Ubuntu
Nicolas Palixe228b1e2010-06-06 17:15:02 +020020 - OpenSUSE
21 - Arch Linux
22 - NetBSD
23 - FreeBSD
24
25
26You can get the latest version released from the Coccinelle homepage at
27http://coccinelle.lip6.fr/
28
Nicolas Palix32af0892010-10-13 00:49:07 +020029Information and tips about Coccinelle are also provided on the wiki
30pages at http://cocci.ekstranet.diku.dk/wiki/doku.php
31
Nicolas Palixe228b1e2010-06-06 17:15:02 +020032Once you have it, run the following command:
33
34 ./configure
35 make
36
37as a regular user, and install it with
38
39 sudo make install
40
Luis R. Rodriguezc100d532016-06-29 15:14:58 -070041 Supplemental documentation
42~~~~~~~~~~~~~~~~~~~~~~~~~~~~
43
44For supplemental documentation refer to the wiki:
45
46https://bottest.wiki.kernel.org/coccicheck
47
48The wiki documentation always refers to the linux-next version of the script.
49
Nicolas Palixe228b1e2010-06-06 17:15:02 +020050 Using Coccinelle on the Linux kernel
51~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
52
53A Coccinelle-specific target is defined in the top level
54Makefile. This target is named 'coccicheck' and calls the 'coccicheck'
55front-end in the 'scripts' directory.
56
Nicolas Palix78a95b92013-06-20 14:00:19 +020057Four basic modes are defined: patch, report, context, and org. The mode to
Nicolas Palixe228b1e2010-06-06 17:15:02 +020058use is specified by setting the MODE variable with 'MODE=<mode>'.
59
Nicolas Palix32af0892010-10-13 00:49:07 +020060'patch' proposes a fix, when possible.
61
Nicolas Palixe228b1e2010-06-06 17:15:02 +020062'report' generates a list in the following format:
63 file:line:column-column: message
64
Nicolas Palixe228b1e2010-06-06 17:15:02 +020065'context' highlights lines of interest and their context in a
66diff-like style.Lines of interest are indicated with '-'.
67
68'org' generates a report in the Org mode format of Emacs.
69
Nicolas Palix32af0892010-10-13 00:49:07 +020070Note that not all semantic patches implement all modes. For easy use
Nicolas Palix78a95b92013-06-20 14:00:19 +020071of Coccinelle, the default mode is "report".
Nicolas Palixe228b1e2010-06-06 17:15:02 +020072
Nicolas Palix78a95b92013-06-20 14:00:19 +020073Two other modes provide some common combinations of these modes.
Nicolas Palixe228b1e2010-06-06 17:15:02 +020074
Nicolas Palix78a95b92013-06-20 14:00:19 +020075'chain' tries the previous modes in the order above until one succeeds.
Nicolas Palixe228b1e2010-06-06 17:15:02 +020076
Nicolas Palix78a95b92013-06-20 14:00:19 +020077'rep+ctxt' runs successively the report mode and the context mode.
78 It should be used with the C option (described later)
79 which checks the code on a file basis.
Nicolas Palixe228b1e2010-06-06 17:15:02 +020080
Nicolas Palix78a95b92013-06-20 14:00:19 +020081Examples:
82 To make a report for every semantic patch, run the following command:
Nicolas Palixe228b1e2010-06-06 17:15:02 +020083
Nicolas Palix78a95b92013-06-20 14:00:19 +020084 make coccicheck MODE=report
85
86 To produce patches, run:
87
88 make coccicheck MODE=patch
Nicolas Palixe228b1e2010-06-06 17:15:02 +020089
90
91The coccicheck target applies every semantic patch available in the
Nicolas Palix32af0892010-10-13 00:49:07 +020092sub-directories of 'scripts/coccinelle' to the entire Linux kernel.
Nicolas Palixe228b1e2010-06-06 17:15:02 +020093
Nicolas Palix32af0892010-10-13 00:49:07 +020094For each semantic patch, a commit message is proposed. It gives a
Nicolas Palixe228b1e2010-06-06 17:15:02 +020095description of the problem being checked by the semantic patch, and
96includes a reference to Coccinelle.
97
98As any static code analyzer, Coccinelle produces false
99positives. Thus, reports must be carefully checked, and patches
100reviewed.
101
Bernd Schubert26e56722013-01-29 17:03:37 +0100102To enable verbose messages set the V= variable, for example:
103
104 make coccicheck MODE=report V=1
105
Luis R. Rodriguezc930a1b2016-06-29 15:14:53 -0700106 Coccinelle parallelization
107~~~~~~~~~~~~~~~~~~~~~~~~~~~~
108
Kees Cook90d06a42013-06-18 14:49:29 -0700109By default, coccicheck tries to run as parallel as possible. To change
110the parallelism, set the J= variable. For example, to run across 4 CPUs:
111
112 make coccicheck MODE=report J=4
113
Luis R. Rodriguezc930a1b2016-06-29 15:14:53 -0700114As of Coccinelle 1.0.2 Coccinelle uses Ocaml parmap for parallelization,
115if support for this is detected you will benefit from parmap parallelization.
116
117When parmap is enabled coccicheck will enable dynamic load balancing by using
118'--chunksize 1' argument, this ensures we keep feeding threads with work
119one by one, so that we avoid the situation where most work gets done by only
120a few threads. With dynamic load balancing, if a thread finishes early we keep
121feeding it more work.
122
123When parmap is enabled, if an error occurs in Coccinelle, this error
124value is propagated back, the return value of the 'make coccicheck'
125captures this return value.
Nicolas Palixe228b1e2010-06-06 17:15:02 +0200126
127 Using Coccinelle with a single semantic patch
128~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
129
130The optional make variable COCCI can be used to check a single
131semantic patch. In that case, the variable must be initialized with
132the name of the semantic patch to apply.
133
134For instance:
135
136 make coccicheck COCCI=<my_SP.cocci> MODE=patch
137or
138 make coccicheck COCCI=<my_SP.cocci> MODE=report
139
140
Greg Dietschef95ab202011-11-05 20:59:44 -0500141 Controlling Which Files are Processed by Coccinelle
142~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
143By default the entire kernel source tree is checked.
Nicolas Palix32af0892010-10-13 00:49:07 +0200144
Greg Dietschef95ab202011-11-05 20:59:44 -0500145To apply Coccinelle to a specific directory, M= can be used.
146For example, to check drivers/net/wireless/ one may write:
147
148 make coccicheck M=drivers/net/wireless/
Nicolas Palixed621cc2013-03-02 22:36:27 +0100149
Nicolas Palix32af0892010-10-13 00:49:07 +0200150To apply Coccinelle on a file basis, instead of a directory basis, the
151following command may be used:
152
153 make C=1 CHECK="scripts/coccicheck"
154
155To check only newly edited code, use the value 2 for the C flag, i.e.
156
157 make C=2 CHECK="scripts/coccicheck"
158
Nicolas Palix78a95b92013-06-20 14:00:19 +0200159In these modes, which works on a file basis, there is no information
160about semantic patches displayed, and no commit message proposed.
161
Nicolas Palix32af0892010-10-13 00:49:07 +0200162This runs every semantic patch in scripts/coccinelle by default. The
163COCCI variable may additionally be used to only apply a single
164semantic patch as shown in the previous section.
165
Nicolas Palix78a95b92013-06-20 14:00:19 +0200166The "report" mode is the default. You can select another one with the
Nicolas Palix32af0892010-10-13 00:49:07 +0200167MODE variable explained above.
168
Luis R. Rodriguezbe1fa902016-06-29 15:14:54 -0700169 Debugging Coccinelle SmPL patches
170~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
171
172Using coccicheck is best as it provides in the spatch command line
173include options matching the options used when we compile the kernel.
174You can learn what these options are by using V=1, you could then
175manually run Coccinelle with debug options added.
176
177Alternatively you can debug running Coccinelle against SmPL patches
178by asking for stderr to be redirected to stderr, by default stderr
179is redirected to /dev/null, if you'd like to capture stderr you
180can specify the DEBUG_FILE="file.txt" option to coccicheck. For
181instance:
182
183 rm -f cocci.err
184 make coccicheck COCCI=scripts/coccinelle/free/kfree.cocci MODE=report DEBUG_FILE=cocci.err
185 cat cocci.err
186
Luis R. Rodriguez5c384db2016-06-29 15:14:55 -0700187You can use SPFLAGS to add debugging flags, for instance you may want to
188add both --profile --show-trying to SPFLAGS when debugging. For instance
189you may want to use:
190
191 rm -f err.log
192 export COCCI=scripts/coccinelle/misc/irqf_oneshot.cocci
193 make coccicheck DEBUG_FILE="err.log" MODE=report SPFLAGS="--profile --show-trying" M=./drivers/mfd/arizona-irq.c
194
195err.log will now have the profiling information, while stdout will
196provide some progress information as Coccinelle moves forward with
197work.
198
Luis R. Rodriguezbe1fa902016-06-29 15:14:54 -0700199DEBUG_FILE support is only supported when using coccinelle >= 1.2.
200
Luis R. Rodriguezdd951fc2016-06-29 15:14:56 -0700201 .cocciconfig support
202~~~~~~~~~~~~~~~~~~~~~~
203
204Coccinelle supports reading .cocciconfig for default Coccinelle options that
205should be used every time spatch is spawned, the order of precedence for
206variables for .cocciconfig is as follows:
207
208 o Your current user's home directory is processed first
209 o Your directory from which spatch is called is processed next
210 o The directory provided with the --dir option is processed last, if used
211
212Since coccicheck runs through make, it naturally runs from the kernel
213proper dir, as such the second rule above would be implied for picking up a
214.cocciconfig when using 'make coccicheck'.
215
216'make coccicheck' also supports using M= targets.If you do not supply
217any M= target, it is assumed you want to target the entire kernel.
218The kernel coccicheck script has:
219
220 if [ "$KBUILD_EXTMOD" = "" ] ; then
221 OPTIONS="--dir $srctree $COCCIINCLUDE"
222 else
223 OPTIONS="--dir $KBUILD_EXTMOD $COCCIINCLUDE"
224 fi
225
226KBUILD_EXTMOD is set when an explicit target with M= is used. For both cases
227the spatch --dir argument is used, as such third rule applies when whether M=
228is used or not, and when M= is used the target directory can have its own
229.cocciconfig file. When M= is not passed as an argument to coccicheck the
230target directory is the same as the directory from where spatch was called.
231
232If not using the kernel's coccicheck target, keep the above precedence
233order logic of .cocciconfig reading. If using the kernel's coccicheck target,
234override any of the kernel's .coccicheck's settings using SPFLAGS.
235
236We help Coccinelle when used against Linux with a set of sensible defaults
237options for Linux with our own Linux .cocciconfig. This hints to coccinelle
238git can be used for 'git grep' queries over coccigrep. A timeout of 200
239seconds should suffice for now.
240
241The options picked up by coccinelle when reading a .cocciconfig do not appear
242as arguments to spatch processes running on your system, to confirm what
243options will be used by Coccinelle run:
244
245 spatch --print-options-only
246
247You can override with your own preferred index option by using SPFLAGS. Take
248note that when there are conflicting options Coccinelle takes precedence for
249the last options passed. Using .cocciconfig is possible to use idutils, however
250given the order of precedence followed by Coccinelle, since the kernel now
251carries its own .cocciconfig, you will need to use SPFLAGS to use idutils if
252desired. See below section "Additional flags" for more details on how to use
253idutils.
254
Nicolas Palixed621cc2013-03-02 22:36:27 +0100255 Additional flags
256~~~~~~~~~~~~~~~~~~
257
258Additional flags can be passed to spatch through the SPFLAGS
Luis R. Rodriguez8e826ad2016-06-29 15:14:52 -0700259variable. This works as Coccinelle respects the last flags
260given to it when options are in conflict.
Nicolas Palixed621cc2013-03-02 22:36:27 +0100261
Nicolas Palix78a95b92013-06-20 14:00:19 +0200262 make SPFLAGS=--use-glimpse coccicheck
Luis R. Rodriguezdd951fc2016-06-29 15:14:56 -0700263
264Coccinelle supports idutils as well but requires coccinelle >= 1.0.6.
265When no ID file is specified coccinelle assumes your ID database file
266is in the file .id-utils.index on the top level of the kernel, coccinelle
267carries a script scripts/idutils_index.sh which creates the database with
268
269 mkid -i C --output .id-utils.index
270
271If you have another database filename you can also just symlink with this
272name.
273
Nicolas Palix78a95b92013-06-20 14:00:19 +0200274 make SPFLAGS=--use-idutils coccicheck
Nicolas Palixed621cc2013-03-02 22:36:27 +0100275
Luis R. Rodriguezdd951fc2016-06-29 15:14:56 -0700276Alternatively you can specify the database filename explicitly, for
277instance:
278
279 make SPFLAGS="--use-idutils /full-path/to/ID" coccicheck
280
Nicolas Palixed621cc2013-03-02 22:36:27 +0100281See spatch --help to learn more about spatch options.
Nicolas Palix32af0892010-10-13 00:49:07 +0200282
Nicolas Palix78a95b92013-06-20 14:00:19 +0200283Note that the '--use-glimpse' and '--use-idutils' options
284require external tools for indexing the code. None of them is
285thus active by default. However, by indexing the code with
286one of these tools, and according to the cocci file used,
287spatch could proceed the entire code base more quickly.
288
Luis R. Rodrigueza9e064c2016-06-29 15:14:57 -0700289 SmPL patch specific options
290~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
291
292SmPL patches can have their own requirements for options passed
293to Coccinelle. SmPL patch specific options can be provided by
294providing them at the top of the SmPL patch, for instance:
295
296// Options: --no-includes --include-headers
297
298 SmPL patch Coccinelle requirements
299~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
300
301As Coccinelle features get added some more advanced SmPL patches
302may require newer versions of Coccinelle. If an SmPL patch requires
303at least a version of Coccinelle, this can be specified as follows,
304as an example if requiring at least Coccinelle >= 1.0.5:
305
306// Requires: 1.0.5
307
Nicolas Palixe228b1e2010-06-06 17:15:02 +0200308 Proposing new semantic patches
309~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
310
311New semantic patches can be proposed and submitted by kernel
312developers. For sake of clarity, they should be organized in the
Nicolas Palix32af0892010-10-13 00:49:07 +0200313sub-directories of 'scripts/coccinelle/'.
Nicolas Palixe228b1e2010-06-06 17:15:02 +0200314
315
316 Detailed description of the 'report' mode
317~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
318
319'report' generates a list in the following format:
320 file:line:column-column: message
321
322Example:
323
324Running
325
Nicolas Palix9dcf7992010-10-24 23:37:33 +0200326 make coccicheck MODE=report COCCI=scripts/coccinelle/api/err_cast.cocci
Nicolas Palixe228b1e2010-06-06 17:15:02 +0200327
328will execute the following part of the SmPL script.
329
330<smpl>
331@r depends on !context && !patch && (org || report)@
332expression x;
333position p;
334@@
335
336 ERR_PTR@p(PTR_ERR(x))
337
338@script:python depends on report@
339p << r.p;
340x << r.x;
341@@
342
343msg="ERR_CAST can be used with %s" % (x)
344coccilib.report.print_report(p[0], msg)
345</smpl>
346
347This SmPL excerpt generates entries on the standard output, as
348illustrated below:
349
350/home/user/linux/crypto/ctr.c:188:9-16: ERR_CAST can be used with alg
351/home/user/linux/crypto/authenc.c:619:9-16: ERR_CAST can be used with auth
352/home/user/linux/crypto/xts.c:227:9-16: ERR_CAST can be used with alg
353
354
355 Detailed description of the 'patch' mode
356~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
357
358When the 'patch' mode is available, it proposes a fix for each problem
359identified.
360
361Example:
362
363Running
Nicolas Palix9dcf7992010-10-24 23:37:33 +0200364 make coccicheck MODE=patch COCCI=scripts/coccinelle/api/err_cast.cocci
Nicolas Palixe228b1e2010-06-06 17:15:02 +0200365
366will execute the following part of the SmPL script.
367
368<smpl>
369@ depends on !context && patch && !org && !report @
370expression x;
371@@
372
373- ERR_PTR(PTR_ERR(x))
374+ ERR_CAST(x)
375</smpl>
376
377This SmPL excerpt generates patch hunks on the standard output, as
378illustrated below:
379
380diff -u -p a/crypto/ctr.c b/crypto/ctr.c
381--- a/crypto/ctr.c 2010-05-26 10:49:38.000000000 +0200
382+++ b/crypto/ctr.c 2010-06-03 23:44:49.000000000 +0200
383@@ -185,7 +185,7 @@ static struct crypto_instance *crypto_ct
384 alg = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_CIPHER,
385 CRYPTO_ALG_TYPE_MASK);
386 if (IS_ERR(alg))
387- return ERR_PTR(PTR_ERR(alg));
388+ return ERR_CAST(alg);
389
390 /* Block size must be >= 4 bytes. */
391 err = -EINVAL;
392
393 Detailed description of the 'context' mode
394~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
395
396'context' highlights lines of interest and their context
397in a diff-like style.
398
399NOTE: The diff-like output generated is NOT an applicable patch. The
400 intent of the 'context' mode is to highlight the important lines
401 (annotated with minus, '-') and gives some surrounding context
402 lines around. This output can be used with the diff mode of
403 Emacs to review the code.
404
405Example:
406
407Running
Nicolas Palix9dcf7992010-10-24 23:37:33 +0200408 make coccicheck MODE=context COCCI=scripts/coccinelle/api/err_cast.cocci
Nicolas Palixe228b1e2010-06-06 17:15:02 +0200409
410will execute the following part of the SmPL script.
411
412<smpl>
413@ depends on context && !patch && !org && !report@
414expression x;
415@@
416
417* ERR_PTR(PTR_ERR(x))
418</smpl>
419
420This SmPL excerpt generates diff hunks on the standard output, as
421illustrated below:
422
423diff -u -p /home/user/linux/crypto/ctr.c /tmp/nothing
424--- /home/user/linux/crypto/ctr.c 2010-05-26 10:49:38.000000000 +0200
425+++ /tmp/nothing
426@@ -185,7 +185,6 @@ static struct crypto_instance *crypto_ct
427 alg = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_CIPHER,
428 CRYPTO_ALG_TYPE_MASK);
429 if (IS_ERR(alg))
430- return ERR_PTR(PTR_ERR(alg));
431
432 /* Block size must be >= 4 bytes. */
433 err = -EINVAL;
434
435 Detailed description of the 'org' mode
436~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
437
438'org' generates a report in the Org mode format of Emacs.
439
440Example:
441
442Running
Nicolas Palix9dcf7992010-10-24 23:37:33 +0200443 make coccicheck MODE=org COCCI=scripts/coccinelle/api/err_cast.cocci
Nicolas Palixe228b1e2010-06-06 17:15:02 +0200444
445will execute the following part of the SmPL script.
446
447<smpl>
448@r depends on !context && !patch && (org || report)@
449expression x;
450position p;
451@@
452
453 ERR_PTR@p(PTR_ERR(x))
454
455@script:python depends on org@
456p << r.p;
457x << r.x;
458@@
459
460msg="ERR_CAST can be used with %s" % (x)
461msg_safe=msg.replace("[","@(").replace("]",")")
462coccilib.org.print_todo(p[0], msg_safe)
463</smpl>
464
465This SmPL excerpt generates Org entries on the standard output, as
466illustrated below:
467
468* TODO [[view:/home/user/linux/crypto/ctr.c::face=ovl-face1::linb=188::colb=9::cole=16][ERR_CAST can be used with alg]]
469* TODO [[view:/home/user/linux/crypto/authenc.c::face=ovl-face1::linb=619::colb=9::cole=16][ERR_CAST can be used with auth]]
470* TODO [[view:/home/user/linux/crypto/xts.c::face=ovl-face1::linb=227::colb=9::cole=16][ERR_CAST can be used with alg]]