Nicolas Palix | 9e39555 | 2013-03-02 22:36:26 +0100 | [diff] [blame] | 1 | #!/bin/bash |
Nicolas Palix | 74425ee | 2010-06-06 17:15:01 +0200 | [diff] [blame] | 2 | |
Nicolas Palix | ec97946 | 2013-07-03 16:41:01 +0200 | [diff] [blame] | 3 | # |
| 4 | # This script requires at least spatch |
| 5 | # version 1.0.0-rc11. |
| 6 | # |
| 7 | |
Nicolas Palix | 74425ee | 2010-06-06 17:15:01 +0200 | [diff] [blame] | 8 | SPATCH="`which ${SPATCH:=spatch}`" |
| 9 | |
Luis R. Rodriguez | 13d9486 | 2016-06-29 15:14:51 -0700 | [diff] [blame] | 10 | if [ ! -x "$SPATCH" ]; then |
| 11 | echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/' |
| 12 | exit 1 |
| 13 | fi |
| 14 | |
Luis R. Rodriguez | c930a1b | 2016-06-29 15:14:53 -0700 | [diff] [blame] | 15 | USE_JOBS="no" |
| 16 | $SPATCH --help | grep "\-\-jobs" > /dev/null && USE_JOBS="yes" |
Kees Cook | 90d06a4 | 2013-06-18 14:49:29 -0700 | [diff] [blame] | 17 | |
Bernd Schubert | 26e5672 | 2013-01-29 17:03:37 +0100 | [diff] [blame] | 18 | # The verbosity may be set by the environmental parameter V= |
| 19 | # as for example with 'make V=1 coccicheck' |
| 20 | |
| 21 | if [ -n "$V" -a "$V" != "0" ]; then |
Kees Cook | 90d06a4 | 2013-06-18 14:49:29 -0700 | [diff] [blame] | 22 | VERBOSE="$V" |
Bernd Schubert | 26e5672 | 2013-01-29 17:03:37 +0100 | [diff] [blame] | 23 | else |
| 24 | VERBOSE=0 |
| 25 | fi |
| 26 | |
Kees Cook | 90d06a4 | 2013-06-18 14:49:29 -0700 | [diff] [blame] | 27 | if [ -z "$J" ]; then |
| 28 | NPROC=$(getconf _NPROCESSORS_ONLN) |
| 29 | else |
| 30 | NPROC="$J" |
| 31 | fi |
| 32 | |
Luis R. Rodriguez | 8e826ad | 2016-06-29 15:14:52 -0700 | [diff] [blame] | 33 | FLAGS="--very-quiet" |
Nicolas Palix | 9e39555 | 2013-03-02 22:36:26 +0100 | [diff] [blame] | 34 | |
| 35 | # spatch only allows include directories with the syntax "-I include" |
| 36 | # while gcc also allows "-Iinclude" and "-include include" |
| 37 | COCCIINCLUDE=${LINUXINCLUDE//-I/-I } |
Andrzej Hajda | 5b16910 | 2015-09-22 15:15:30 +0200 | [diff] [blame] | 38 | COCCIINCLUDE=${COCCIINCLUDE// -include/ --include} |
Nicolas Palix | 9e39555 | 2013-03-02 22:36:26 +0100 | [diff] [blame] | 39 | |
Nicolas Palix | 1e9dea2 | 2010-06-13 09:26:34 +0200 | [diff] [blame] | 40 | if [ "$C" = "1" -o "$C" = "2" ]; then |
| 41 | ONLINE=1 |
| 42 | |
Nicolas Palix | 9e39555 | 2013-03-02 22:36:26 +0100 | [diff] [blame] | 43 | # Take only the last argument, which is the C file to test |
| 44 | shift $(( $# - 1 )) |
| 45 | OPTIONS="$COCCIINCLUDE $1" |
Nicolas Palix | 1e9dea2 | 2010-06-13 09:26:34 +0200 | [diff] [blame] | 46 | else |
| 47 | ONLINE=0 |
Greg Dietsche | d0bc1fb | 2011-11-05 20:59:43 -0500 | [diff] [blame] | 48 | if [ "$KBUILD_EXTMOD" = "" ] ; then |
Nicolas Palix | 93f1446 | 2013-06-20 13:10:56 +0200 | [diff] [blame] | 49 | OPTIONS="--dir $srctree $COCCIINCLUDE" |
Greg Dietsche | d0bc1fb | 2011-11-05 20:59:43 -0500 | [diff] [blame] | 50 | else |
Nicolas Palix | 93f1446 | 2013-06-20 13:10:56 +0200 | [diff] [blame] | 51 | OPTIONS="--dir $KBUILD_EXTMOD $COCCIINCLUDE" |
Greg Dietsche | d0bc1fb | 2011-11-05 20:59:43 -0500 | [diff] [blame] | 52 | fi |
Nicolas Palix | 1e9dea2 | 2010-06-13 09:26:34 +0200 | [diff] [blame] | 53 | fi |
| 54 | |
Nicolas Palix | bad6a40 | 2013-03-02 22:36:28 +0100 | [diff] [blame] | 55 | if [ "$KBUILD_EXTMOD" != "" ] ; then |
Nicolas Palix | 93f1446 | 2013-06-20 13:10:56 +0200 | [diff] [blame] | 56 | OPTIONS="--patch $srctree $OPTIONS" |
Nicolas Palix | bad6a40 | 2013-03-02 22:36:28 +0100 | [diff] [blame] | 57 | fi |
| 58 | |
Luis R. Rodriguez | c930a1b | 2016-06-29 15:14:53 -0700 | [diff] [blame] | 59 | # You can override by using SPFLAGS |
| 60 | if [ "$USE_JOBS" = "no" ]; then |
| 61 | trap kill_running SIGTERM SIGINT |
| 62 | declare -a SPATCH_PID |
| 63 | elif [ "$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" |
| 67 | fi |
| 68 | |
Nicolas Palix | 74425ee | 2010-06-06 17:15:01 +0200 | [diff] [blame] | 69 | if [ "$MODE" = "" ] ; then |
Nicolas Palix | 1e9dea2 | 2010-06-13 09:26:34 +0200 | [diff] [blame] | 70 | if [ "$ONLINE" = "0" ] ; then |
Nicolas Palix | 1f0a674 | 2013-06-06 23:39:52 +0200 | [diff] [blame] | 71 | 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 Palix | 1e9dea2 | 2010-06-13 09:26:34 +0200 | [diff] [blame] | 73 | echo 'You can specify the mode with "make coccicheck MODE=<mode>"' |
Nicolas Palix | 1f0a674 | 2013-06-06 23:39:52 +0200 | [diff] [blame] | 74 | echo 'Note however that some modes are not implemented by some semantic patches.' |
Nicolas Palix | 1e9dea2 | 2010-06-13 09:26:34 +0200 | [diff] [blame] | 75 | fi |
Nicolas Palix | 1f0a674 | 2013-06-06 23:39:52 +0200 | [diff] [blame] | 76 | MODE="report" |
| 77 | fi |
| 78 | |
| 79 | if [ "$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 Palix | 03ee0c4 | 2010-10-08 21:27:41 +0200 | [diff] [blame] | 84 | elif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then |
Deepa Dinamani | 7a2358b | 2016-06-12 12:04:39 -0700 | [diff] [blame] | 85 | FLAGS="--no-show-diff $FLAGS" |
Nicolas Palix | 74425ee | 2010-06-06 17:15:01 +0200 | [diff] [blame] | 86 | fi |
| 87 | |
Nicolas Palix | 1e9dea2 | 2010-06-13 09:26:34 +0200 | [diff] [blame] | 88 | if [ "$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 '' |
| 93 | fi |
Nicolas Palix | 74425ee | 2010-06-06 17:15:01 +0200 | [diff] [blame] | 94 | |
Luis R. Rodriguez | c930a1b | 2016-06-29 15:14:53 -0700 | [diff] [blame] | 95 | run_cmd_parmap() { |
| 96 | if [ $VERBOSE -ne 0 ] ; then |
| 97 | echo "Running ($NPROC in parallel): $@" |
| 98 | fi |
Luis R. Rodriguez | be1fa90 | 2016-06-29 15:14:54 -0700 | [diff] [blame^] | 99 | if [ "$DEBUG_FILE" != "/dev/null" -a "$DEBUG_FILE" != "" ]; then |
| 100 | if [ -f $DEBUG_FILE ]; then |
| 101 | echo "Debug file $DEBUG_FILE exists, bailing" |
| 102 | exit |
| 103 | fi |
| 104 | else |
| 105 | DEBUG_FILE="/dev/null" |
| 106 | fi |
| 107 | $@ 2>$DEBUG_FILE |
Luis R. Rodriguez | c930a1b | 2016-06-29 15:14:53 -0700 | [diff] [blame] | 108 | if [[ $? -ne 0 ]]; then |
| 109 | echo "coccicheck failed" |
| 110 | exit $? |
| 111 | fi |
| 112 | } |
| 113 | |
| 114 | run_cmd_old() { |
Kees Cook | 90d06a4 | 2013-06-18 14:49:29 -0700 | [diff] [blame] | 115 | local i |
Bernd Schubert | 5303265 | 2013-01-29 17:03:42 +0100 | [diff] [blame] | 116 | if [ $VERBOSE -ne 0 ] ; then |
Kees Cook | 90d06a4 | 2013-06-18 14:49:29 -0700 | [diff] [blame] | 117 | echo "Running ($NPROC in parallel): $@" |
Bernd Schubert | 5303265 | 2013-01-29 17:03:42 +0100 | [diff] [blame] | 118 | fi |
Kees Cook | 90d06a4 | 2013-06-18 14:49:29 -0700 | [diff] [blame] | 119 | for i in $(seq 0 $(( NPROC - 1)) ); do |
Nicolas Palix | 93f1446 | 2013-06-20 13:10:56 +0200 | [diff] [blame] | 120 | eval "$@ --max $NPROC --index $i &" |
Kees Cook | 90d06a4 | 2013-06-18 14:49:29 -0700 | [diff] [blame] | 121 | SPATCH_PID[$i]=$! |
| 122 | if [ $VERBOSE -eq 2 ] ; then |
| 123 | echo "${SPATCH_PID[$i]} running" |
| 124 | fi |
| 125 | done |
| 126 | wait |
Bernd Schubert | 5303265 | 2013-01-29 17:03:42 +0100 | [diff] [blame] | 127 | } |
| 128 | |
Luis R. Rodriguez | c930a1b | 2016-06-29 15:14:53 -0700 | [diff] [blame] | 129 | run_cmd() { |
| 130 | if [ "$USE_JOBS" = "yes" ]; then |
| 131 | run_cmd_parmap $@ |
| 132 | else |
| 133 | run_cmd_old $@ |
| 134 | fi |
| 135 | } |
| 136 | |
Kees Cook | 90d06a4 | 2013-06-18 14:49:29 -0700 | [diff] [blame] | 137 | kill_running() { |
Kees Cook | 2552a39 | 2016-05-16 05:55:58 -0700 | [diff] [blame] | 138 | for i in $(seq 0 $(( NPROC - 1 )) ); do |
Kees Cook | 90d06a4 | 2013-06-18 14:49:29 -0700 | [diff] [blame] | 139 | if [ $VERBOSE -eq 2 ] ; then |
| 140 | echo "Killing ${SPATCH_PID[$i]}" |
| 141 | fi |
| 142 | kill ${SPATCH_PID[$i]} 2>/dev/null |
| 143 | done |
| 144 | } |
Bernd Schubert | 5303265 | 2013-01-29 17:03:42 +0100 | [diff] [blame] | 145 | |
Luis R. Rodriguez | 8e826ad | 2016-06-29 15:14:52 -0700 | [diff] [blame] | 146 | # You can override heuristics with SPFLAGS, these must always go last |
| 147 | OPTIONS="$OPTIONS $SPFLAGS" |
| 148 | |
Nicolas Palix | 1e9dea2 | 2010-06-13 09:26:34 +0200 | [diff] [blame] | 149 | coccinelle () { |
Nicolas Palix | 74425ee | 2010-06-06 17:15:01 +0200 | [diff] [blame] | 150 | COCCI="$1" |
Nicolas Palix | 74425ee | 2010-06-06 17:15:01 +0200 | [diff] [blame] | 151 | |
| 152 | OPT=`grep "Option" $COCCI | cut -d':' -f2` |
Nicolas Palix | 74425ee | 2010-06-06 17:15:01 +0200 | [diff] [blame] | 153 | |
Nicolas Palix | 93f1446 | 2013-06-20 13:10:56 +0200 | [diff] [blame] | 154 | # The option '--parse-cocci' can be used to syntactically check the SmPL files. |
Nicolas Palix | 74425ee | 2010-06-06 17:15:01 +0200 | [diff] [blame] | 155 | # |
Nicolas Palix | 1e9dea2 | 2010-06-13 09:26:34 +0200 | [diff] [blame] | 156 | # $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null |
Nicolas Palix | 74425ee | 2010-06-06 17:15:01 +0200 | [diff] [blame] | 157 | |
Nicolas Palix | 35d88a3 | 2013-03-02 22:36:25 +0100 | [diff] [blame] | 158 | if [ $VERBOSE -ne 0 -a $ONLINE -eq 0 ] ; then |
Nicolas Palix | 1e9dea2 | 2010-06-13 09:26:34 +0200 | [diff] [blame] | 159 | |
| 160 | FILE=`echo $COCCI | sed "s|$srctree/||"` |
| 161 | |
Nicolas Palix | 3c90841 | 2010-10-08 21:27:38 +0200 | [diff] [blame] | 162 | echo "Processing `basename $COCCI`" |
| 163 | echo "with option(s) \"$OPT\"" |
| 164 | echo '' |
Nicolas Palix | 1e9dea2 | 2010-06-13 09:26:34 +0200 | [diff] [blame] | 165 | echo 'Message example to submit a patch:' |
| 166 | |
Nicolas Palix | 3c90841 | 2010-10-08 21:27:38 +0200 | [diff] [blame] | 167 | sed -ne 's|^///||p' $COCCI |
Nicolas Palix | 1e9dea2 | 2010-06-13 09:26:34 +0200 | [diff] [blame] | 168 | |
Nicolas Palix | 062c182 | 2010-10-24 23:37:34 +0200 | [diff] [blame] | 169 | if [ "$MODE" = "patch" ] ; then |
| 170 | echo ' The semantic patch that makes this change is available' |
| 171 | elif [ "$MODE" = "report" ] ; then |
| 172 | echo ' The semantic patch that makes this report is available' |
| 173 | elif [ "$MODE" = "context" ] ; then |
| 174 | echo ' The semantic patch that spots this code is available' |
| 175 | elif [ "$MODE" = "org" ] ; then |
| 176 | echo ' The semantic patch that makes this Org report is available' |
| 177 | else |
| 178 | echo ' The semantic patch that makes this output is available' |
| 179 | fi |
Nicolas Palix | 1e9dea2 | 2010-06-13 09:26:34 +0200 | [diff] [blame] | 180 | echo " in $FILE." |
| 181 | echo '' |
| 182 | echo ' More information about semantic patching is available at' |
| 183 | echo ' http://coccinelle.lip6.fr/' |
| 184 | echo '' |
| 185 | |
Nicolas Palix | 3c90841 | 2010-10-08 21:27:38 +0200 | [diff] [blame] | 186 | if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then |
| 187 | echo 'Semantic patch information:' |
| 188 | sed -ne 's|^//#||p' $COCCI |
| 189 | echo '' |
| 190 | fi |
Nicolas Palix | 2c1160c8 | 2010-10-08 21:27:40 +0200 | [diff] [blame] | 191 | fi |
Nicolas Palix | 3c90841 | 2010-10-08 21:27:38 +0200 | [diff] [blame] | 192 | |
Nicolas Palix | 2c1160c8 | 2010-10-08 21:27:40 +0200 | [diff] [blame] | 193 | if [ "$MODE" = "chain" ] ; then |
Bernd Schubert | 5303265 | 2013-01-29 17:03:42 +0100 | [diff] [blame] | 194 | run_cmd $SPATCH -D patch \ |
Nicolas Palix | 93f1446 | 2013-06-20 13:10:56 +0200 | [diff] [blame] | 195 | $FLAGS --cocci-file $COCCI $OPT $OPTIONS || \ |
Bernd Schubert | 5303265 | 2013-01-29 17:03:42 +0100 | [diff] [blame] | 196 | run_cmd $SPATCH -D report \ |
Nicolas Palix | 93f1446 | 2013-06-20 13:10:56 +0200 | [diff] [blame] | 197 | $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || \ |
Bernd Schubert | 5303265 | 2013-01-29 17:03:42 +0100 | [diff] [blame] | 198 | run_cmd $SPATCH -D context \ |
Nicolas Palix | 93f1446 | 2013-06-20 13:10:56 +0200 | [diff] [blame] | 199 | $FLAGS --cocci-file $COCCI $OPT $OPTIONS || \ |
Bernd Schubert | 5303265 | 2013-01-29 17:03:42 +0100 | [diff] [blame] | 200 | run_cmd $SPATCH -D org \ |
Nicolas Palix | 93f1446 | 2013-06-20 13:10:56 +0200 | [diff] [blame] | 201 | $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || exit 1 |
Nicolas Palix | c05cd6d | 2012-09-20 22:30:46 +0200 | [diff] [blame] | 202 | elif [ "$MODE" = "rep+ctxt" ] ; then |
Bernd Schubert | 5303265 | 2013-01-29 17:03:42 +0100 | [diff] [blame] | 203 | run_cmd $SPATCH -D report \ |
Nicolas Palix | 93f1446 | 2013-06-20 13:10:56 +0200 | [diff] [blame] | 204 | $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff && \ |
Bernd Schubert | 5303265 | 2013-01-29 17:03:42 +0100 | [diff] [blame] | 205 | run_cmd $SPATCH -D context \ |
Nicolas Palix | 93f1446 | 2013-06-20 13:10:56 +0200 | [diff] [blame] | 206 | $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1 |
Nicolas Palix | 1e9dea2 | 2010-06-13 09:26:34 +0200 | [diff] [blame] | 207 | else |
Nicolas Palix | 93f1446 | 2013-06-20 13:10:56 +0200 | [diff] [blame] | 208 | run_cmd $SPATCH -D $MODE $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1 |
Nicolas Palix | 1e9dea2 | 2010-06-13 09:26:34 +0200 | [diff] [blame] | 209 | fi |
| 210 | |
Nicolas Palix | 74425ee | 2010-06-06 17:15:01 +0200 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | if [ "$COCCI" = "" ] ; then |
| 214 | for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do |
Nicolas Palix | 1e9dea2 | 2010-06-13 09:26:34 +0200 | [diff] [blame] | 215 | coccinelle $f |
Nicolas Palix | 74425ee | 2010-06-06 17:15:01 +0200 | [diff] [blame] | 216 | done |
| 217 | else |
Nicolas Palix | 1e9dea2 | 2010-06-13 09:26:34 +0200 | [diff] [blame] | 218 | coccinelle $COCCI |
Nicolas Palix | 74425ee | 2010-06-06 17:15:01 +0200 | [diff] [blame] | 219 | fi |