blob: 4b65a0fd50a118b7ef0563a6498fa6a896eefdec [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
Luis R. Rodriguezc930a1b2016-06-29 15:14:53 -070015USE_JOBS="no"
16$SPATCH --help | grep "\-\-jobs" > /dev/null && USE_JOBS="yes"
Kees Cook90d06a42013-06-18 14:49:29 -070017
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
Luis R. Rodriguezc930a1b2016-06-29 15:14:53 -070059# You can override by using SPFLAGS
60if [ "$USE_JOBS" = "no" ]; then
61 trap kill_running SIGTERM SIGINT
62 declare -a SPATCH_PID
63elif [ "$NPROC" != "1" ]; then
64 # Using 0 should work as well, refer to _SC_NPROCESSORS_ONLN use on
65 # https://github.com/rdicosmo/parmap/blob/master/setcore_stubs.c
66 OPTIONS="$OPTIONS --jobs $NPROC --chunksize 1"
67fi
68
Nicolas Palix74425ee2010-06-06 17:15:01 +020069if [ "$MODE" = "" ] ; then
Nicolas Palix1e9dea22010-06-13 09:26:34 +020070 if [ "$ONLINE" = "0" ] ; then
Nicolas Palix1f0a6742013-06-06 23:39:52 +020071 echo 'You have not explicitly specified the mode to use. Using default "report" mode.'
72 echo 'Available modes are the following: patch, report, context, org'
Nicolas Palix1e9dea22010-06-13 09:26:34 +020073 echo 'You can specify the mode with "make coccicheck MODE=<mode>"'
Nicolas Palix1f0a6742013-06-06 23:39:52 +020074 echo 'Note however that some modes are not implemented by some semantic patches.'
Nicolas Palix1e9dea22010-06-13 09:26:34 +020075 fi
Nicolas Palix1f0a6742013-06-06 23:39:52 +020076 MODE="report"
77fi
78
79if [ "$MODE" = "chain" ] ; then
80 if [ "$ONLINE" = "0" ] ; then
81 echo 'You have selected the "chain" mode.'
82 echo 'All available modes will be tried (in that order): patch, report, context, org'
83 fi
Nicolas Palix03ee0c42010-10-08 21:27:41 +020084elif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then
Deepa Dinamani7a2358b2016-06-12 12:04:39 -070085 FLAGS="--no-show-diff $FLAGS"
Nicolas Palix74425ee2010-06-06 17:15:01 +020086fi
87
Nicolas Palix1e9dea22010-06-13 09:26:34 +020088if [ "$ONLINE" = "0" ] ; then
89 echo ''
90 echo 'Please check for false positives in the output before submitting a patch.'
91 echo 'When using "patch" mode, carefully review the patch before submitting it.'
92 echo ''
93fi
Nicolas Palix74425ee2010-06-06 17:15:01 +020094
Luis R. Rodriguezc930a1b2016-06-29 15:14:53 -070095run_cmd_parmap() {
96 if [ $VERBOSE -ne 0 ] ; then
97 echo "Running ($NPROC in parallel): $@"
98 fi
99 $@ 2>/dev/null
100 if [[ $? -ne 0 ]]; then
101 echo "coccicheck failed"
102 exit $?
103 fi
104}
105
106run_cmd_old() {
Kees Cook90d06a42013-06-18 14:49:29 -0700107 local i
Bernd Schubert53032652013-01-29 17:03:42 +0100108 if [ $VERBOSE -ne 0 ] ; then
Kees Cook90d06a42013-06-18 14:49:29 -0700109 echo "Running ($NPROC in parallel): $@"
Bernd Schubert53032652013-01-29 17:03:42 +0100110 fi
Kees Cook90d06a42013-06-18 14:49:29 -0700111 for i in $(seq 0 $(( NPROC - 1)) ); do
Nicolas Palix93f14462013-06-20 13:10:56 +0200112 eval "$@ --max $NPROC --index $i &"
Kees Cook90d06a42013-06-18 14:49:29 -0700113 SPATCH_PID[$i]=$!
114 if [ $VERBOSE -eq 2 ] ; then
115 echo "${SPATCH_PID[$i]} running"
116 fi
117 done
118 wait
Bernd Schubert53032652013-01-29 17:03:42 +0100119}
120
Luis R. Rodriguezc930a1b2016-06-29 15:14:53 -0700121run_cmd() {
122 if [ "$USE_JOBS" = "yes" ]; then
123 run_cmd_parmap $@
124 else
125 run_cmd_old $@
126 fi
127}
128
Kees Cook90d06a42013-06-18 14:49:29 -0700129kill_running() {
Kees Cook2552a392016-05-16 05:55:58 -0700130 for i in $(seq 0 $(( NPROC - 1 )) ); do
Kees Cook90d06a42013-06-18 14:49:29 -0700131 if [ $VERBOSE -eq 2 ] ; then
132 echo "Killing ${SPATCH_PID[$i]}"
133 fi
134 kill ${SPATCH_PID[$i]} 2>/dev/null
135 done
136}
Bernd Schubert53032652013-01-29 17:03:42 +0100137
Luis R. Rodriguez8e826ad2016-06-29 15:14:52 -0700138# You can override heuristics with SPFLAGS, these must always go last
139OPTIONS="$OPTIONS $SPFLAGS"
140
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200141coccinelle () {
Nicolas Palix74425ee2010-06-06 17:15:01 +0200142 COCCI="$1"
Nicolas Palix74425ee2010-06-06 17:15:01 +0200143
144 OPT=`grep "Option" $COCCI | cut -d':' -f2`
Nicolas Palix74425ee2010-06-06 17:15:01 +0200145
Nicolas Palix93f14462013-06-20 13:10:56 +0200146# The option '--parse-cocci' can be used to syntactically check the SmPL files.
Nicolas Palix74425ee2010-06-06 17:15:01 +0200147#
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200148# $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null
Nicolas Palix74425ee2010-06-06 17:15:01 +0200149
Nicolas Palix35d88a32013-03-02 22:36:25 +0100150 if [ $VERBOSE -ne 0 -a $ONLINE -eq 0 ] ; then
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200151
152 FILE=`echo $COCCI | sed "s|$srctree/||"`
153
Nicolas Palix3c908412010-10-08 21:27:38 +0200154 echo "Processing `basename $COCCI`"
155 echo "with option(s) \"$OPT\""
156 echo ''
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200157 echo 'Message example to submit a patch:'
158
Nicolas Palix3c908412010-10-08 21:27:38 +0200159 sed -ne 's|^///||p' $COCCI
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200160
Nicolas Palix062c1822010-10-24 23:37:34 +0200161 if [ "$MODE" = "patch" ] ; then
162 echo ' The semantic patch that makes this change is available'
163 elif [ "$MODE" = "report" ] ; then
164 echo ' The semantic patch that makes this report is available'
165 elif [ "$MODE" = "context" ] ; then
166 echo ' The semantic patch that spots this code is available'
167 elif [ "$MODE" = "org" ] ; then
168 echo ' The semantic patch that makes this Org report is available'
169 else
170 echo ' The semantic patch that makes this output is available'
171 fi
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200172 echo " in $FILE."
173 echo ''
174 echo ' More information about semantic patching is available at'
175 echo ' http://coccinelle.lip6.fr/'
176 echo ''
177
Nicolas Palix3c908412010-10-08 21:27:38 +0200178 if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then
179 echo 'Semantic patch information:'
180 sed -ne 's|^//#||p' $COCCI
181 echo ''
182 fi
Nicolas Palix2c1160c82010-10-08 21:27:40 +0200183 fi
Nicolas Palix3c908412010-10-08 21:27:38 +0200184
Nicolas Palix2c1160c82010-10-08 21:27:40 +0200185 if [ "$MODE" = "chain" ] ; then
Bernd Schubert53032652013-01-29 17:03:42 +0100186 run_cmd $SPATCH -D patch \
Nicolas Palix93f14462013-06-20 13:10:56 +0200187 $FLAGS --cocci-file $COCCI $OPT $OPTIONS || \
Bernd Schubert53032652013-01-29 17:03:42 +0100188 run_cmd $SPATCH -D report \
Nicolas Palix93f14462013-06-20 13:10:56 +0200189 $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || \
Bernd Schubert53032652013-01-29 17:03:42 +0100190 run_cmd $SPATCH -D context \
Nicolas Palix93f14462013-06-20 13:10:56 +0200191 $FLAGS --cocci-file $COCCI $OPT $OPTIONS || \
Bernd Schubert53032652013-01-29 17:03:42 +0100192 run_cmd $SPATCH -D org \
Nicolas Palix93f14462013-06-20 13:10:56 +0200193 $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || exit 1
Nicolas Palixc05cd6d2012-09-20 22:30:46 +0200194 elif [ "$MODE" = "rep+ctxt" ] ; then
Bernd Schubert53032652013-01-29 17:03:42 +0100195 run_cmd $SPATCH -D report \
Nicolas Palix93f14462013-06-20 13:10:56 +0200196 $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff && \
Bernd Schubert53032652013-01-29 17:03:42 +0100197 run_cmd $SPATCH -D context \
Nicolas Palix93f14462013-06-20 13:10:56 +0200198 $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200199 else
Nicolas Palix93f14462013-06-20 13:10:56 +0200200 run_cmd $SPATCH -D $MODE $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200201 fi
202
Nicolas Palix74425ee2010-06-06 17:15:01 +0200203}
204
205if [ "$COCCI" = "" ] ; then
206 for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200207 coccinelle $f
Nicolas Palix74425ee2010-06-06 17:15:01 +0200208 done
209else
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200210 coccinelle $COCCI
Nicolas Palix74425ee2010-06-06 17:15:01 +0200211fi