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 | |
Kees Cook | 90d06a4 | 2013-06-18 14:49:29 -0700 | [diff] [blame] | 10 | trap kill_running SIGTERM SIGINT |
| 11 | declare -a SPATCH_PID |
| 12 | |
Bernd Schubert | 26e5672 | 2013-01-29 17:03:37 +0100 | [diff] [blame] | 13 | # The verbosity may be set by the environmental parameter V= |
| 14 | # as for example with 'make V=1 coccicheck' |
| 15 | |
| 16 | if [ -n "$V" -a "$V" != "0" ]; then |
Kees Cook | 90d06a4 | 2013-06-18 14:49:29 -0700 | [diff] [blame] | 17 | VERBOSE="$V" |
Bernd Schubert | 26e5672 | 2013-01-29 17:03:37 +0100 | [diff] [blame] | 18 | else |
| 19 | VERBOSE=0 |
| 20 | fi |
| 21 | |
Kees Cook | 90d06a4 | 2013-06-18 14:49:29 -0700 | [diff] [blame] | 22 | if [ -z "$J" ]; then |
| 23 | NPROC=$(getconf _NPROCESSORS_ONLN) |
| 24 | else |
| 25 | NPROC="$J" |
| 26 | fi |
| 27 | |
Deepa Dinamani | 7a2358b | 2016-06-12 12:04:39 -0700 | [diff] [blame^] | 28 | FLAGS="--very-quiet $SPFLAGS" |
Nicolas Palix | 9e39555 | 2013-03-02 22:36:26 +0100 | [diff] [blame] | 29 | |
| 30 | # spatch only allows include directories with the syntax "-I include" |
| 31 | # while gcc also allows "-Iinclude" and "-include include" |
| 32 | COCCIINCLUDE=${LINUXINCLUDE//-I/-I } |
Andrzej Hajda | 5b16910 | 2015-09-22 15:15:30 +0200 | [diff] [blame] | 33 | COCCIINCLUDE=${COCCIINCLUDE// -include/ --include} |
Nicolas Palix | 9e39555 | 2013-03-02 22:36:26 +0100 | [diff] [blame] | 34 | |
Nicolas Palix | 1e9dea2 | 2010-06-13 09:26:34 +0200 | [diff] [blame] | 35 | if [ "$C" = "1" -o "$C" = "2" ]; then |
| 36 | ONLINE=1 |
| 37 | |
Nicolas Palix | 9e39555 | 2013-03-02 22:36:26 +0100 | [diff] [blame] | 38 | # Take only the last argument, which is the C file to test |
| 39 | shift $(( $# - 1 )) |
| 40 | OPTIONS="$COCCIINCLUDE $1" |
Nicolas Palix | 1e9dea2 | 2010-06-13 09:26:34 +0200 | [diff] [blame] | 41 | else |
| 42 | ONLINE=0 |
Greg Dietsche | d0bc1fb | 2011-11-05 20:59:43 -0500 | [diff] [blame] | 43 | if [ "$KBUILD_EXTMOD" = "" ] ; then |
Nicolas Palix | 93f1446 | 2013-06-20 13:10:56 +0200 | [diff] [blame] | 44 | OPTIONS="--dir $srctree $COCCIINCLUDE" |
Greg Dietsche | d0bc1fb | 2011-11-05 20:59:43 -0500 | [diff] [blame] | 45 | else |
Nicolas Palix | 93f1446 | 2013-06-20 13:10:56 +0200 | [diff] [blame] | 46 | OPTIONS="--dir $KBUILD_EXTMOD $COCCIINCLUDE" |
Greg Dietsche | d0bc1fb | 2011-11-05 20:59:43 -0500 | [diff] [blame] | 47 | fi |
Nicolas Palix | 1e9dea2 | 2010-06-13 09:26:34 +0200 | [diff] [blame] | 48 | fi |
| 49 | |
Nicolas Palix | bad6a40 | 2013-03-02 22:36:28 +0100 | [diff] [blame] | 50 | if [ "$KBUILD_EXTMOD" != "" ] ; then |
Nicolas Palix | 93f1446 | 2013-06-20 13:10:56 +0200 | [diff] [blame] | 51 | OPTIONS="--patch $srctree $OPTIONS" |
Nicolas Palix | bad6a40 | 2013-03-02 22:36:28 +0100 | [diff] [blame] | 52 | fi |
| 53 | |
Nicolas Palix | 74425ee | 2010-06-06 17:15:01 +0200 | [diff] [blame] | 54 | if [ ! -x "$SPATCH" ]; then |
| 55 | echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/' |
| 56 | exit 1 |
| 57 | fi |
| 58 | |
| 59 | if [ "$MODE" = "" ] ; then |
Nicolas Palix | 1e9dea2 | 2010-06-13 09:26:34 +0200 | [diff] [blame] | 60 | if [ "$ONLINE" = "0" ] ; then |
Nicolas Palix | 1f0a674 | 2013-06-06 23:39:52 +0200 | [diff] [blame] | 61 | 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 Palix | 1e9dea2 | 2010-06-13 09:26:34 +0200 | [diff] [blame] | 63 | echo 'You can specify the mode with "make coccicheck MODE=<mode>"' |
Nicolas Palix | 1f0a674 | 2013-06-06 23:39:52 +0200 | [diff] [blame] | 64 | 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] | 65 | fi |
Nicolas Palix | 1f0a674 | 2013-06-06 23:39:52 +0200 | [diff] [blame] | 66 | MODE="report" |
| 67 | fi |
| 68 | |
| 69 | if [ "$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 Palix | 03ee0c4 | 2010-10-08 21:27:41 +0200 | [diff] [blame] | 74 | elif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then |
Deepa Dinamani | 7a2358b | 2016-06-12 12:04:39 -0700 | [diff] [blame^] | 75 | FLAGS="--no-show-diff $FLAGS" |
Nicolas Palix | 74425ee | 2010-06-06 17:15:01 +0200 | [diff] [blame] | 76 | fi |
| 77 | |
Nicolas Palix | 1e9dea2 | 2010-06-13 09:26:34 +0200 | [diff] [blame] | 78 | if [ "$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 '' |
| 83 | fi |
Nicolas Palix | 74425ee | 2010-06-06 17:15:01 +0200 | [diff] [blame] | 84 | |
Bernd Schubert | 5303265 | 2013-01-29 17:03:42 +0100 | [diff] [blame] | 85 | run_cmd() { |
Kees Cook | 90d06a4 | 2013-06-18 14:49:29 -0700 | [diff] [blame] | 86 | local i |
Bernd Schubert | 5303265 | 2013-01-29 17:03:42 +0100 | [diff] [blame] | 87 | if [ $VERBOSE -ne 0 ] ; then |
Kees Cook | 90d06a4 | 2013-06-18 14:49:29 -0700 | [diff] [blame] | 88 | echo "Running ($NPROC in parallel): $@" |
Bernd Schubert | 5303265 | 2013-01-29 17:03:42 +0100 | [diff] [blame] | 89 | fi |
Kees Cook | 90d06a4 | 2013-06-18 14:49:29 -0700 | [diff] [blame] | 90 | for i in $(seq 0 $(( NPROC - 1)) ); do |
Nicolas Palix | 93f1446 | 2013-06-20 13:10:56 +0200 | [diff] [blame] | 91 | eval "$@ --max $NPROC --index $i &" |
Kees Cook | 90d06a4 | 2013-06-18 14:49:29 -0700 | [diff] [blame] | 92 | SPATCH_PID[$i]=$! |
| 93 | if [ $VERBOSE -eq 2 ] ; then |
| 94 | echo "${SPATCH_PID[$i]} running" |
| 95 | fi |
| 96 | done |
| 97 | wait |
Bernd Schubert | 5303265 | 2013-01-29 17:03:42 +0100 | [diff] [blame] | 98 | } |
| 99 | |
Kees Cook | 90d06a4 | 2013-06-18 14:49:29 -0700 | [diff] [blame] | 100 | kill_running() { |
Kees Cook | 2552a39 | 2016-05-16 05:55:58 -0700 | [diff] [blame] | 101 | for i in $(seq 0 $(( NPROC - 1 )) ); do |
Kees Cook | 90d06a4 | 2013-06-18 14:49:29 -0700 | [diff] [blame] | 102 | 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 Schubert | 5303265 | 2013-01-29 17:03:42 +0100 | [diff] [blame] | 108 | |
Nicolas Palix | 1e9dea2 | 2010-06-13 09:26:34 +0200 | [diff] [blame] | 109 | coccinelle () { |
Nicolas Palix | 74425ee | 2010-06-06 17:15:01 +0200 | [diff] [blame] | 110 | COCCI="$1" |
Nicolas Palix | 74425ee | 2010-06-06 17:15:01 +0200 | [diff] [blame] | 111 | |
| 112 | OPT=`grep "Option" $COCCI | cut -d':' -f2` |
Nicolas Palix | 74425ee | 2010-06-06 17:15:01 +0200 | [diff] [blame] | 113 | |
Nicolas Palix | 93f1446 | 2013-06-20 13:10:56 +0200 | [diff] [blame] | 114 | # 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] | 115 | # |
Nicolas Palix | 1e9dea2 | 2010-06-13 09:26:34 +0200 | [diff] [blame] | 116 | # $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null |
Nicolas Palix | 74425ee | 2010-06-06 17:15:01 +0200 | [diff] [blame] | 117 | |
Nicolas Palix | 35d88a3 | 2013-03-02 22:36:25 +0100 | [diff] [blame] | 118 | if [ $VERBOSE -ne 0 -a $ONLINE -eq 0 ] ; then |
Nicolas Palix | 1e9dea2 | 2010-06-13 09:26:34 +0200 | [diff] [blame] | 119 | |
| 120 | FILE=`echo $COCCI | sed "s|$srctree/||"` |
| 121 | |
Nicolas Palix | 3c90841 | 2010-10-08 21:27:38 +0200 | [diff] [blame] | 122 | echo "Processing `basename $COCCI`" |
| 123 | echo "with option(s) \"$OPT\"" |
| 124 | echo '' |
Nicolas Palix | 1e9dea2 | 2010-06-13 09:26:34 +0200 | [diff] [blame] | 125 | echo 'Message example to submit a patch:' |
| 126 | |
Nicolas Palix | 3c90841 | 2010-10-08 21:27:38 +0200 | [diff] [blame] | 127 | sed -ne 's|^///||p' $COCCI |
Nicolas Palix | 1e9dea2 | 2010-06-13 09:26:34 +0200 | [diff] [blame] | 128 | |
Nicolas Palix | 062c182 | 2010-10-24 23:37:34 +0200 | [diff] [blame] | 129 | if [ "$MODE" = "patch" ] ; then |
| 130 | echo ' The semantic patch that makes this change is available' |
| 131 | elif [ "$MODE" = "report" ] ; then |
| 132 | echo ' The semantic patch that makes this report is available' |
| 133 | elif [ "$MODE" = "context" ] ; then |
| 134 | echo ' The semantic patch that spots this code is available' |
| 135 | elif [ "$MODE" = "org" ] ; then |
| 136 | echo ' The semantic patch that makes this Org report is available' |
| 137 | else |
| 138 | echo ' The semantic patch that makes this output is available' |
| 139 | fi |
Nicolas Palix | 1e9dea2 | 2010-06-13 09:26:34 +0200 | [diff] [blame] | 140 | echo " in $FILE." |
| 141 | echo '' |
| 142 | echo ' More information about semantic patching is available at' |
| 143 | echo ' http://coccinelle.lip6.fr/' |
| 144 | echo '' |
| 145 | |
Nicolas Palix | 3c90841 | 2010-10-08 21:27:38 +0200 | [diff] [blame] | 146 | if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then |
| 147 | echo 'Semantic patch information:' |
| 148 | sed -ne 's|^//#||p' $COCCI |
| 149 | echo '' |
| 150 | fi |
Nicolas Palix | 2c1160c8 | 2010-10-08 21:27:40 +0200 | [diff] [blame] | 151 | fi |
Nicolas Palix | 3c90841 | 2010-10-08 21:27:38 +0200 | [diff] [blame] | 152 | |
Nicolas Palix | 2c1160c8 | 2010-10-08 21:27:40 +0200 | [diff] [blame] | 153 | if [ "$MODE" = "chain" ] ; then |
Bernd Schubert | 5303265 | 2013-01-29 17:03:42 +0100 | [diff] [blame] | 154 | run_cmd $SPATCH -D patch \ |
Nicolas Palix | 93f1446 | 2013-06-20 13:10:56 +0200 | [diff] [blame] | 155 | $FLAGS --cocci-file $COCCI $OPT $OPTIONS || \ |
Bernd Schubert | 5303265 | 2013-01-29 17:03:42 +0100 | [diff] [blame] | 156 | run_cmd $SPATCH -D report \ |
Nicolas Palix | 93f1446 | 2013-06-20 13:10:56 +0200 | [diff] [blame] | 157 | $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || \ |
Bernd Schubert | 5303265 | 2013-01-29 17:03:42 +0100 | [diff] [blame] | 158 | run_cmd $SPATCH -D context \ |
Nicolas Palix | 93f1446 | 2013-06-20 13:10:56 +0200 | [diff] [blame] | 159 | $FLAGS --cocci-file $COCCI $OPT $OPTIONS || \ |
Bernd Schubert | 5303265 | 2013-01-29 17:03:42 +0100 | [diff] [blame] | 160 | run_cmd $SPATCH -D org \ |
Nicolas Palix | 93f1446 | 2013-06-20 13:10:56 +0200 | [diff] [blame] | 161 | $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || exit 1 |
Nicolas Palix | c05cd6d | 2012-09-20 22:30:46 +0200 | [diff] [blame] | 162 | elif [ "$MODE" = "rep+ctxt" ] ; then |
Bernd Schubert | 5303265 | 2013-01-29 17:03:42 +0100 | [diff] [blame] | 163 | run_cmd $SPATCH -D report \ |
Nicolas Palix | 93f1446 | 2013-06-20 13:10:56 +0200 | [diff] [blame] | 164 | $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff && \ |
Bernd Schubert | 5303265 | 2013-01-29 17:03:42 +0100 | [diff] [blame] | 165 | run_cmd $SPATCH -D context \ |
Nicolas Palix | 93f1446 | 2013-06-20 13:10:56 +0200 | [diff] [blame] | 166 | $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1 |
Nicolas Palix | 1e9dea2 | 2010-06-13 09:26:34 +0200 | [diff] [blame] | 167 | else |
Nicolas Palix | 93f1446 | 2013-06-20 13:10:56 +0200 | [diff] [blame] | 168 | 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] | 169 | fi |
| 170 | |
Nicolas Palix | 74425ee | 2010-06-06 17:15:01 +0200 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | if [ "$COCCI" = "" ] ; then |
| 174 | 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] | 175 | coccinelle $f |
Nicolas Palix | 74425ee | 2010-06-06 17:15:01 +0200 | [diff] [blame] | 176 | done |
| 177 | else |
Nicolas Palix | 1e9dea2 | 2010-06-13 09:26:34 +0200 | [diff] [blame] | 178 | coccinelle $COCCI |
Nicolas Palix | 74425ee | 2010-06-06 17:15:01 +0200 | [diff] [blame] | 179 | fi |