blob: 5319fae910b466ce83b13e8ce2068002f4870d74 [file] [log] [blame]
Nicolas Palix9e395552013-03-02 22:36:26 +01001#!/bin/bash
Nicolas Palix74425ee2010-06-06 17:15:01 +02002
Nicolas Palixec979462013-07-03 16:41:01 +02003#
4# This script requires at least spatch
5# version 1.0.0-rc11.
6#
7
Nicolas Palix74425ee2010-06-06 17:15:01 +02008SPATCH="`which ${SPATCH:=spatch}`"
9
Luis R. Rodriguez13d94862016-06-29 15:14:51 -070010if [ ! -x "$SPATCH" ]; then
11 echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/'
12 exit 1
13fi
14
Kees Cook90d06a42013-06-18 14:49:29 -070015trap kill_running SIGTERM SIGINT
16declare -a SPATCH_PID
17
Bernd Schubert26e56722013-01-29 17:03:37 +010018# The verbosity may be set by the environmental parameter V=
19# as for example with 'make V=1 coccicheck'
20
21if [ -n "$V" -a "$V" != "0" ]; then
Kees Cook90d06a42013-06-18 14:49:29 -070022 VERBOSE="$V"
Bernd Schubert26e56722013-01-29 17:03:37 +010023else
24 VERBOSE=0
25fi
26
Kees Cook90d06a42013-06-18 14:49:29 -070027if [ -z "$J" ]; then
28 NPROC=$(getconf _NPROCESSORS_ONLN)
29else
30 NPROC="$J"
31fi
32
Luis R. Rodriguez8e826ad2016-06-29 15:14:52 -070033FLAGS="--very-quiet"
Nicolas Palix9e395552013-03-02 22:36:26 +010034
35# spatch only allows include directories with the syntax "-I include"
36# while gcc also allows "-Iinclude" and "-include include"
37COCCIINCLUDE=${LINUXINCLUDE//-I/-I }
Andrzej Hajda5b169102015-09-22 15:15:30 +020038COCCIINCLUDE=${COCCIINCLUDE// -include/ --include}
Nicolas Palix9e395552013-03-02 22:36:26 +010039
Nicolas Palix1e9dea22010-06-13 09:26:34 +020040if [ "$C" = "1" -o "$C" = "2" ]; then
41 ONLINE=1
42
Nicolas Palix9e395552013-03-02 22:36:26 +010043 # Take only the last argument, which is the C file to test
44 shift $(( $# - 1 ))
45 OPTIONS="$COCCIINCLUDE $1"
Nicolas Palix1e9dea22010-06-13 09:26:34 +020046else
47 ONLINE=0
Greg Dietsched0bc1fb2011-11-05 20:59:43 -050048 if [ "$KBUILD_EXTMOD" = "" ] ; then
Nicolas Palix93f14462013-06-20 13:10:56 +020049 OPTIONS="--dir $srctree $COCCIINCLUDE"
Greg Dietsched0bc1fb2011-11-05 20:59:43 -050050 else
Nicolas Palix93f14462013-06-20 13:10:56 +020051 OPTIONS="--dir $KBUILD_EXTMOD $COCCIINCLUDE"
Greg Dietsched0bc1fb2011-11-05 20:59:43 -050052 fi
Nicolas Palix1e9dea22010-06-13 09:26:34 +020053fi
54
Nicolas Palixbad6a402013-03-02 22:36:28 +010055if [ "$KBUILD_EXTMOD" != "" ] ; then
Nicolas Palix93f14462013-06-20 13:10:56 +020056 OPTIONS="--patch $srctree $OPTIONS"
Nicolas Palixbad6a402013-03-02 22:36:28 +010057fi
58
Nicolas Palix74425ee2010-06-06 17:15:01 +020059if [ "$MODE" = "" ] ; then
Nicolas Palix1e9dea22010-06-13 09:26:34 +020060 if [ "$ONLINE" = "0" ] ; then
Nicolas Palix1f0a6742013-06-06 23:39:52 +020061 echo 'You have not explicitly specified the mode to use. Using default "report" mode.'
62 echo 'Available modes are the following: patch, report, context, org'
Nicolas Palix1e9dea22010-06-13 09:26:34 +020063 echo 'You can specify the mode with "make coccicheck MODE=<mode>"'
Nicolas Palix1f0a6742013-06-06 23:39:52 +020064 echo 'Note however that some modes are not implemented by some semantic patches.'
Nicolas Palix1e9dea22010-06-13 09:26:34 +020065 fi
Nicolas Palix1f0a6742013-06-06 23:39:52 +020066 MODE="report"
67fi
68
69if [ "$MODE" = "chain" ] ; then
70 if [ "$ONLINE" = "0" ] ; then
71 echo 'You have selected the "chain" mode.'
72 echo 'All available modes will be tried (in that order): patch, report, context, org'
73 fi
Nicolas Palix03ee0c42010-10-08 21:27:41 +020074elif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then
Deepa Dinamani7a2358b2016-06-12 12:04:39 -070075 FLAGS="--no-show-diff $FLAGS"
Nicolas Palix74425ee2010-06-06 17:15:01 +020076fi
77
Nicolas Palix1e9dea22010-06-13 09:26:34 +020078if [ "$ONLINE" = "0" ] ; then
79 echo ''
80 echo 'Please check for false positives in the output before submitting a patch.'
81 echo 'When using "patch" mode, carefully review the patch before submitting it.'
82 echo ''
83fi
Nicolas Palix74425ee2010-06-06 17:15:01 +020084
Bernd Schubert53032652013-01-29 17:03:42 +010085run_cmd() {
Kees Cook90d06a42013-06-18 14:49:29 -070086 local i
Bernd Schubert53032652013-01-29 17:03:42 +010087 if [ $VERBOSE -ne 0 ] ; then
Kees Cook90d06a42013-06-18 14:49:29 -070088 echo "Running ($NPROC in parallel): $@"
Bernd Schubert53032652013-01-29 17:03:42 +010089 fi
Kees Cook90d06a42013-06-18 14:49:29 -070090 for i in $(seq 0 $(( NPROC - 1)) ); do
Nicolas Palix93f14462013-06-20 13:10:56 +020091 eval "$@ --max $NPROC --index $i &"
Kees Cook90d06a42013-06-18 14:49:29 -070092 SPATCH_PID[$i]=$!
93 if [ $VERBOSE -eq 2 ] ; then
94 echo "${SPATCH_PID[$i]} running"
95 fi
96 done
97 wait
Bernd Schubert53032652013-01-29 17:03:42 +010098}
99
Kees Cook90d06a42013-06-18 14:49:29 -0700100kill_running() {
Kees Cook2552a392016-05-16 05:55:58 -0700101 for i in $(seq 0 $(( NPROC - 1 )) ); do
Kees Cook90d06a42013-06-18 14:49:29 -0700102 if [ $VERBOSE -eq 2 ] ; then
103 echo "Killing ${SPATCH_PID[$i]}"
104 fi
105 kill ${SPATCH_PID[$i]} 2>/dev/null
106 done
107}
Bernd Schubert53032652013-01-29 17:03:42 +0100108
Luis R. Rodriguez8e826ad2016-06-29 15:14:52 -0700109# You can override heuristics with SPFLAGS, these must always go last
110OPTIONS="$OPTIONS $SPFLAGS"
111
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200112coccinelle () {
Nicolas Palix74425ee2010-06-06 17:15:01 +0200113 COCCI="$1"
Nicolas Palix74425ee2010-06-06 17:15:01 +0200114
115 OPT=`grep "Option" $COCCI | cut -d':' -f2`
Nicolas Palix74425ee2010-06-06 17:15:01 +0200116
Nicolas Palix93f14462013-06-20 13:10:56 +0200117# The option '--parse-cocci' can be used to syntactically check the SmPL files.
Nicolas Palix74425ee2010-06-06 17:15:01 +0200118#
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200119# $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null
Nicolas Palix74425ee2010-06-06 17:15:01 +0200120
Nicolas Palix35d88a32013-03-02 22:36:25 +0100121 if [ $VERBOSE -ne 0 -a $ONLINE -eq 0 ] ; then
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200122
123 FILE=`echo $COCCI | sed "s|$srctree/||"`
124
Nicolas Palix3c908412010-10-08 21:27:38 +0200125 echo "Processing `basename $COCCI`"
126 echo "with option(s) \"$OPT\""
127 echo ''
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200128 echo 'Message example to submit a patch:'
129
Nicolas Palix3c908412010-10-08 21:27:38 +0200130 sed -ne 's|^///||p' $COCCI
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200131
Nicolas Palix062c1822010-10-24 23:37:34 +0200132 if [ "$MODE" = "patch" ] ; then
133 echo ' The semantic patch that makes this change is available'
134 elif [ "$MODE" = "report" ] ; then
135 echo ' The semantic patch that makes this report is available'
136 elif [ "$MODE" = "context" ] ; then
137 echo ' The semantic patch that spots this code is available'
138 elif [ "$MODE" = "org" ] ; then
139 echo ' The semantic patch that makes this Org report is available'
140 else
141 echo ' The semantic patch that makes this output is available'
142 fi
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200143 echo " in $FILE."
144 echo ''
145 echo ' More information about semantic patching is available at'
146 echo ' http://coccinelle.lip6.fr/'
147 echo ''
148
Nicolas Palix3c908412010-10-08 21:27:38 +0200149 if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then
150 echo 'Semantic patch information:'
151 sed -ne 's|^//#||p' $COCCI
152 echo ''
153 fi
Nicolas Palix2c1160c82010-10-08 21:27:40 +0200154 fi
Nicolas Palix3c908412010-10-08 21:27:38 +0200155
Nicolas Palix2c1160c82010-10-08 21:27:40 +0200156 if [ "$MODE" = "chain" ] ; then
Bernd Schubert53032652013-01-29 17:03:42 +0100157 run_cmd $SPATCH -D patch \
Nicolas Palix93f14462013-06-20 13:10:56 +0200158 $FLAGS --cocci-file $COCCI $OPT $OPTIONS || \
Bernd Schubert53032652013-01-29 17:03:42 +0100159 run_cmd $SPATCH -D report \
Nicolas Palix93f14462013-06-20 13:10:56 +0200160 $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || \
Bernd Schubert53032652013-01-29 17:03:42 +0100161 run_cmd $SPATCH -D context \
Nicolas Palix93f14462013-06-20 13:10:56 +0200162 $FLAGS --cocci-file $COCCI $OPT $OPTIONS || \
Bernd Schubert53032652013-01-29 17:03:42 +0100163 run_cmd $SPATCH -D org \
Nicolas Palix93f14462013-06-20 13:10:56 +0200164 $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || exit 1
Nicolas Palixc05cd6d2012-09-20 22:30:46 +0200165 elif [ "$MODE" = "rep+ctxt" ] ; then
Bernd Schubert53032652013-01-29 17:03:42 +0100166 run_cmd $SPATCH -D report \
Nicolas Palix93f14462013-06-20 13:10:56 +0200167 $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff && \
Bernd Schubert53032652013-01-29 17:03:42 +0100168 run_cmd $SPATCH -D context \
Nicolas Palix93f14462013-06-20 13:10:56 +0200169 $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200170 else
Nicolas Palix93f14462013-06-20 13:10:56 +0200171 run_cmd $SPATCH -D $MODE $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200172 fi
173
Nicolas Palix74425ee2010-06-06 17:15:01 +0200174}
175
176if [ "$COCCI" = "" ] ; then
177 for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200178 coccinelle $f
Nicolas Palix74425ee2010-06-06 17:15:01 +0200179 done
180else
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200181 coccinelle $COCCI
Nicolas Palix74425ee2010-06-06 17:15:01 +0200182fi