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