Back to home page

OSCL-LXR

 
 

    


0001 .. Copyright 2010 Nicolas Palix <npalix@diku.dk>
0002 .. Copyright 2010 Julia Lawall <julia@diku.dk>
0003 .. Copyright 2010 Gilles Muller <Gilles.Muller@lip6.fr>
0004 
0005 .. highlight:: none
0006 
0007 .. _devtools_coccinelle:
0008 
0009 Coccinelle
0010 ==========
0011 
0012 Coccinelle is a tool for pattern matching and text transformation that has
0013 many uses in kernel development, including the application of complex,
0014 tree-wide patches and detection of problematic programming patterns.
0015 
0016 Getting Coccinelle
0017 ------------------
0018 
0019 The semantic patches included in the kernel use features and options
0020 which are provided by Coccinelle version 1.0.0-rc11 and above.
0021 Using earlier versions will fail as the option names used by
0022 the Coccinelle files and coccicheck have been updated.
0023 
0024 Coccinelle is available through the package manager
0025 of many distributions, e.g. :
0026 
0027  - Debian
0028  - Fedora
0029  - Ubuntu
0030  - OpenSUSE
0031  - Arch Linux
0032  - NetBSD
0033  - FreeBSD
0034 
0035 Some distribution packages are obsolete and it is recommended
0036 to use the latest version released from the Coccinelle homepage at
0037 http://coccinelle.lip6.fr/
0038 
0039 Or from Github at:
0040 
0041 https://github.com/coccinelle/coccinelle
0042 
0043 Once you have it, run the following commands::
0044 
0045         ./autogen
0046         ./configure
0047         make
0048 
0049 as a regular user, and install it with::
0050 
0051         sudo make install
0052 
0053 More detailed installation instructions to build from source can be
0054 found at:
0055 
0056 https://github.com/coccinelle/coccinelle/blob/master/install.txt
0057 
0058 Supplemental documentation
0059 --------------------------
0060 
0061 For supplemental documentation refer to the wiki:
0062 
0063 https://bottest.wiki.kernel.org/coccicheck
0064 
0065 The wiki documentation always refers to the linux-next version of the script.
0066 
0067 For Semantic Patch Language(SmPL) grammar documentation refer to:
0068 
0069 https://coccinelle.gitlabpages.inria.fr/website/docs/main_grammar.html
0070 
0071 Using Coccinelle on the Linux kernel
0072 ------------------------------------
0073 
0074 A Coccinelle-specific target is defined in the top level
0075 Makefile. This target is named ``coccicheck`` and calls the ``coccicheck``
0076 front-end in the ``scripts`` directory.
0077 
0078 Four basic modes are defined: ``patch``, ``report``, ``context``, and
0079 ``org``. The mode to use is specified by setting the MODE variable with
0080 ``MODE=<mode>``.
0081 
0082 - ``patch`` proposes a fix, when possible.
0083 
0084 - ``report`` generates a list in the following format:
0085   file:line:column-column: message
0086 
0087 - ``context`` highlights lines of interest and their context in a
0088   diff-like style. Lines of interest are indicated with ``-``.
0089 
0090 - ``org`` generates a report in the Org mode format of Emacs.
0091 
0092 Note that not all semantic patches implement all modes. For easy use
0093 of Coccinelle, the default mode is "report".
0094 
0095 Two other modes provide some common combinations of these modes.
0096 
0097 - ``chain`` tries the previous modes in the order above until one succeeds.
0098 
0099 - ``rep+ctxt`` runs successively the report mode and the context mode.
0100   It should be used with the C option (described later)
0101   which checks the code on a file basis.
0102 
0103 Examples
0104 ~~~~~~~~
0105 
0106 To make a report for every semantic patch, run the following command::
0107 
0108                 make coccicheck MODE=report
0109 
0110 To produce patches, run::
0111 
0112                 make coccicheck MODE=patch
0113 
0114 
0115 The coccicheck target applies every semantic patch available in the
0116 sub-directories of ``scripts/coccinelle`` to the entire Linux kernel.
0117 
0118 For each semantic patch, a commit message is proposed.  It gives a
0119 description of the problem being checked by the semantic patch, and
0120 includes a reference to Coccinelle.
0121 
0122 As with any static code analyzer, Coccinelle produces false
0123 positives. Thus, reports must be carefully checked, and patches
0124 reviewed.
0125 
0126 To enable verbose messages set the V= variable, for example::
0127 
0128    make coccicheck MODE=report V=1
0129 
0130 Coccinelle parallelization
0131 --------------------------
0132 
0133 By default, coccicheck tries to run as parallel as possible. To change
0134 the parallelism, set the J= variable. For example, to run across 4 CPUs::
0135 
0136    make coccicheck MODE=report J=4
0137 
0138 As of Coccinelle 1.0.2 Coccinelle uses Ocaml parmap for parallelization;
0139 if support for this is detected you will benefit from parmap parallelization.
0140 
0141 When parmap is enabled coccicheck will enable dynamic load balancing by using
0142 ``--chunksize 1`` argument. This ensures we keep feeding threads with work
0143 one by one, so that we avoid the situation where most work gets done by only
0144 a few threads. With dynamic load balancing, if a thread finishes early we keep
0145 feeding it more work.
0146 
0147 When parmap is enabled, if an error occurs in Coccinelle, this error
0148 value is propagated back, and the return value of the ``make coccicheck``
0149 command captures this return value.
0150 
0151 Using Coccinelle with a single semantic patch
0152 ---------------------------------------------
0153 
0154 The optional make variable COCCI can be used to check a single
0155 semantic patch. In that case, the variable must be initialized with
0156 the name of the semantic patch to apply.
0157 
0158 For instance::
0159 
0160         make coccicheck COCCI=<my_SP.cocci> MODE=patch
0161 
0162 or::
0163 
0164         make coccicheck COCCI=<my_SP.cocci> MODE=report
0165 
0166 
0167 Controlling Which Files are Processed by Coccinelle
0168 ---------------------------------------------------
0169 
0170 By default the entire kernel source tree is checked.
0171 
0172 To apply Coccinelle to a specific directory, ``M=`` can be used.
0173 For example, to check drivers/net/wireless/ one may write::
0174 
0175     make coccicheck M=drivers/net/wireless/
0176 
0177 To apply Coccinelle on a file basis, instead of a directory basis, the
0178 C variable is used by the makefile to select which files to work with.
0179 This variable can be used to run scripts for the entire kernel, a
0180 specific directory, or for a single file.
0181 
0182 For example, to check drivers/bluetooth/bfusb.c, the value 1 is
0183 passed to the C variable to check files that make considers
0184 need to be compiled.::
0185 
0186     make C=1 CHECK=scripts/coccicheck drivers/bluetooth/bfusb.o
0187 
0188 The value 2 is passed to the C variable to check files regardless of
0189 whether they need to be compiled or not.::
0190 
0191     make C=2 CHECK=scripts/coccicheck drivers/bluetooth/bfusb.o
0192 
0193 In these modes, which work on a file basis, there is no information
0194 about semantic patches displayed, and no commit message proposed.
0195 
0196 This runs every semantic patch in scripts/coccinelle by default. The
0197 COCCI variable may additionally be used to only apply a single
0198 semantic patch as shown in the previous section.
0199 
0200 The "report" mode is the default. You can select another one with the
0201 MODE variable explained above.
0202 
0203 Debugging Coccinelle SmPL patches
0204 ---------------------------------
0205 
0206 Using coccicheck is best as it provides in the spatch command line
0207 include options matching the options used when we compile the kernel.
0208 You can learn what these options are by using V=1; you could then
0209 manually run Coccinelle with debug options added.
0210 
0211 Alternatively you can debug running Coccinelle against SmPL patches
0212 by asking for stderr to be redirected to stderr. By default stderr
0213 is redirected to /dev/null; if you'd like to capture stderr you
0214 can specify the ``DEBUG_FILE="file.txt"`` option to coccicheck. For
0215 instance::
0216 
0217     rm -f cocci.err
0218     make coccicheck COCCI=scripts/coccinelle/free/kfree.cocci MODE=report DEBUG_FILE=cocci.err
0219     cat cocci.err
0220 
0221 You can use SPFLAGS to add debugging flags; for instance you may want to
0222 add both --profile --show-trying to SPFLAGS when debugging. For example
0223 you may want to use::
0224 
0225     rm -f err.log
0226     export COCCI=scripts/coccinelle/misc/irqf_oneshot.cocci
0227     make coccicheck DEBUG_FILE="err.log" MODE=report SPFLAGS="--profile --show-trying" M=./drivers/mfd
0228 
0229 err.log will now have the profiling information, while stdout will
0230 provide some progress information as Coccinelle moves forward with
0231 work.
0232 
0233 NOTE:
0234 
0235 DEBUG_FILE support is only supported when using coccinelle >= 1.0.2.
0236 
0237 Currently, DEBUG_FILE support is only available to check folders, and
0238 not single files. This is because checking a single file requires spatch
0239 to be called twice leading to DEBUG_FILE being set both times to the same value,
0240 giving rise to an error.
0241 
0242 .cocciconfig support
0243 --------------------
0244 
0245 Coccinelle supports reading .cocciconfig for default Coccinelle options that
0246 should be used every time spatch is spawned. The order of precedence for
0247 variables for .cocciconfig is as follows:
0248 
0249 - Your current user's home directory is processed first
0250 - Your directory from which spatch is called is processed next
0251 - The directory provided with the --dir option is processed last, if used
0252 
0253 Since coccicheck runs through make, it naturally runs from the kernel
0254 proper dir; as such the second rule above would be implied for picking up a
0255 .cocciconfig when using ``make coccicheck``.
0256 
0257 ``make coccicheck`` also supports using M= targets. If you do not supply
0258 any M= target, it is assumed you want to target the entire kernel.
0259 The kernel coccicheck script has::
0260 
0261     if [ "$KBUILD_EXTMOD" = "" ] ; then
0262         OPTIONS="--dir $srctree $COCCIINCLUDE"
0263     else
0264         OPTIONS="--dir $KBUILD_EXTMOD $COCCIINCLUDE"
0265     fi
0266 
0267 KBUILD_EXTMOD is set when an explicit target with M= is used. For both cases
0268 the spatch --dir argument is used, as such third rule applies when whether M=
0269 is used or not, and when M= is used the target directory can have its own
0270 .cocciconfig file. When M= is not passed as an argument to coccicheck the
0271 target directory is the same as the directory from where spatch was called.
0272 
0273 If not using the kernel's coccicheck target, keep the above precedence
0274 order logic of .cocciconfig reading. If using the kernel's coccicheck target,
0275 override any of the kernel's .coccicheck's settings using SPFLAGS.
0276 
0277 We help Coccinelle when used against Linux with a set of sensible default
0278 options for Linux with our own Linux .cocciconfig. This hints to coccinelle
0279 that git can be used for ``git grep`` queries over coccigrep. A timeout of 200
0280 seconds should suffice for now.
0281 
0282 The options picked up by coccinelle when reading a .cocciconfig do not appear
0283 as arguments to spatch processes running on your system. To confirm what
0284 options will be used by Coccinelle run::
0285 
0286       spatch --print-options-only
0287 
0288 You can override with your own preferred index option by using SPFLAGS. Take
0289 note that when there are conflicting options Coccinelle takes precedence for
0290 the last options passed. Using .cocciconfig is possible to use idutils, however
0291 given the order of precedence followed by Coccinelle, since the kernel now
0292 carries its own .cocciconfig, you will need to use SPFLAGS to use idutils if
0293 desired. See below section "Additional flags" for more details on how to use
0294 idutils.
0295 
0296 Additional flags
0297 ----------------
0298 
0299 Additional flags can be passed to spatch through the SPFLAGS
0300 variable. This works as Coccinelle respects the last flags
0301 given to it when options are in conflict. ::
0302 
0303     make SPFLAGS=--use-glimpse coccicheck
0304 
0305 Coccinelle supports idutils as well but requires coccinelle >= 1.0.6.
0306 When no ID file is specified coccinelle assumes your ID database file
0307 is in the file .id-utils.index on the top level of the kernel. Coccinelle
0308 carries a script scripts/idutils_index.sh which creates the database with::
0309 
0310     mkid -i C --output .id-utils.index
0311 
0312 If you have another database filename you can also just symlink with this
0313 name. ::
0314 
0315     make SPFLAGS=--use-idutils coccicheck
0316 
0317 Alternatively you can specify the database filename explicitly, for
0318 instance::
0319 
0320     make SPFLAGS="--use-idutils /full-path/to/ID" coccicheck
0321 
0322 See ``spatch --help`` to learn more about spatch options.
0323 
0324 Note that the ``--use-glimpse`` and ``--use-idutils`` options
0325 require external tools for indexing the code. None of them is
0326 thus active by default. However, by indexing the code with
0327 one of these tools, and according to the cocci file used,
0328 spatch could proceed the entire code base more quickly.
0329 
0330 SmPL patch specific options
0331 ---------------------------
0332 
0333 SmPL patches can have their own requirements for options passed
0334 to Coccinelle. SmPL patch-specific options can be provided by
0335 providing them at the top of the SmPL patch, for instance::
0336 
0337         // Options: --no-includes --include-headers
0338 
0339 SmPL patch Coccinelle requirements
0340 ----------------------------------
0341 
0342 As Coccinelle features get added some more advanced SmPL patches
0343 may require newer versions of Coccinelle. If an SmPL patch requires
0344 a minimum version of Coccinelle, this can be specified as follows,
0345 as an example if requiring at least Coccinelle >= 1.0.5::
0346 
0347         // Requires: 1.0.5
0348 
0349 Proposing new semantic patches
0350 ------------------------------
0351 
0352 New semantic patches can be proposed and submitted by kernel
0353 developers. For sake of clarity, they should be organized in the
0354 sub-directories of ``scripts/coccinelle/``.
0355 
0356 
0357 Detailed description of the ``report`` mode
0358 -------------------------------------------
0359 
0360 ``report`` generates a list in the following format::
0361 
0362   file:line:column-column: message
0363 
0364 Example
0365 ~~~~~~~
0366 
0367 Running::
0368 
0369         make coccicheck MODE=report COCCI=scripts/coccinelle/api/err_cast.cocci
0370 
0371 will execute the following part of the SmPL script::
0372 
0373    <smpl>
0374    @r depends on !context && !patch && (org || report)@
0375    expression x;
0376    position p;
0377    @@
0378 
0379      ERR_PTR@p(PTR_ERR(x))
0380 
0381    @script:python depends on report@
0382    p << r.p;
0383    x << r.x;
0384    @@
0385 
0386    msg="ERR_CAST can be used with %s" % (x)
0387    coccilib.report.print_report(p[0], msg)
0388    </smpl>
0389 
0390 This SmPL excerpt generates entries on the standard output, as
0391 illustrated below::
0392 
0393     /home/user/linux/crypto/ctr.c:188:9-16: ERR_CAST can be used with alg
0394     /home/user/linux/crypto/authenc.c:619:9-16: ERR_CAST can be used with auth
0395     /home/user/linux/crypto/xts.c:227:9-16: ERR_CAST can be used with alg
0396 
0397 
0398 Detailed description of the ``patch`` mode
0399 ------------------------------------------
0400 
0401 When the ``patch`` mode is available, it proposes a fix for each problem
0402 identified.
0403 
0404 Example
0405 ~~~~~~~
0406 
0407 Running::
0408 
0409         make coccicheck MODE=patch COCCI=scripts/coccinelle/api/err_cast.cocci
0410 
0411 will execute the following part of the SmPL script::
0412 
0413     <smpl>
0414     @ depends on !context && patch && !org && !report @
0415     expression x;
0416     @@
0417 
0418     - ERR_PTR(PTR_ERR(x))
0419     + ERR_CAST(x)
0420     </smpl>
0421 
0422 This SmPL excerpt generates patch hunks on the standard output, as
0423 illustrated below::
0424 
0425     diff -u -p a/crypto/ctr.c b/crypto/ctr.c
0426     --- a/crypto/ctr.c 2010-05-26 10:49:38.000000000 +0200
0427     +++ b/crypto/ctr.c 2010-06-03 23:44:49.000000000 +0200
0428     @@ -185,7 +185,7 @@ static struct crypto_instance *crypto_ct
0429         alg = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_CIPHER,
0430                                   CRYPTO_ALG_TYPE_MASK);
0431         if (IS_ERR(alg))
0432     -           return ERR_PTR(PTR_ERR(alg));
0433     +           return ERR_CAST(alg);
0434 
0435         /* Block size must be >= 4 bytes. */
0436         err = -EINVAL;
0437 
0438 Detailed description of the ``context`` mode
0439 --------------------------------------------
0440 
0441 ``context`` highlights lines of interest and their context
0442 in a diff-like style.
0443 
0444       **NOTE**: The diff-like output generated is NOT an applicable patch. The
0445       intent of the ``context`` mode is to highlight the important lines
0446       (annotated with minus, ``-``) and gives some surrounding context
0447       lines around. This output can be used with the diff mode of
0448       Emacs to review the code.
0449 
0450 Example
0451 ~~~~~~~
0452 
0453 Running::
0454 
0455         make coccicheck MODE=context COCCI=scripts/coccinelle/api/err_cast.cocci
0456 
0457 will execute the following part of the SmPL script::
0458 
0459     <smpl>
0460     @ depends on context && !patch && !org && !report@
0461     expression x;
0462     @@
0463 
0464     * ERR_PTR(PTR_ERR(x))
0465     </smpl>
0466 
0467 This SmPL excerpt generates diff hunks on the standard output, as
0468 illustrated below::
0469 
0470     diff -u -p /home/user/linux/crypto/ctr.c /tmp/nothing
0471     --- /home/user/linux/crypto/ctr.c   2010-05-26 10:49:38.000000000 +0200
0472     +++ /tmp/nothing
0473     @@ -185,7 +185,6 @@ static struct crypto_instance *crypto_ct
0474         alg = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_CIPHER,
0475                                   CRYPTO_ALG_TYPE_MASK);
0476         if (IS_ERR(alg))
0477     -           return ERR_PTR(PTR_ERR(alg));
0478 
0479         /* Block size must be >= 4 bytes. */
0480         err = -EINVAL;
0481 
0482 Detailed description of the ``org`` mode
0483 ----------------------------------------
0484 
0485 ``org`` generates a report in the Org mode format of Emacs.
0486 
0487 Example
0488 ~~~~~~~
0489 
0490 Running::
0491 
0492         make coccicheck MODE=org COCCI=scripts/coccinelle/api/err_cast.cocci
0493 
0494 will execute the following part of the SmPL script::
0495 
0496     <smpl>
0497     @r depends on !context && !patch && (org || report)@
0498     expression x;
0499     position p;
0500     @@
0501 
0502       ERR_PTR@p(PTR_ERR(x))
0503 
0504     @script:python depends on org@
0505     p << r.p;
0506     x << r.x;
0507     @@
0508 
0509     msg="ERR_CAST can be used with %s" % (x)
0510     msg_safe=msg.replace("[","@(").replace("]",")")
0511     coccilib.org.print_todo(p[0], msg_safe)
0512     </smpl>
0513 
0514 This SmPL excerpt generates Org entries on the standard output, as
0515 illustrated below::
0516 
0517     * TODO [[view:/home/user/linux/crypto/ctr.c::face=ovl-face1::linb=188::colb=9::cole=16][ERR_CAST can be used with alg]]
0518     * TODO [[view:/home/user/linux/crypto/authenc.c::face=ovl-face1::linb=619::colb=9::cole=16][ERR_CAST can be used with auth]]
0519     * TODO [[view:/home/user/linux/crypto/xts.c::face=ovl-face1::linb=227::colb=9::cole=16][ERR_CAST can be used with alg]]