19e395550SNicolas Palix#!/bin/bash 274425eeeSNicolas Palix 3ec97946eSNicolas Palix# 4ec97946eSNicolas Palix# This script requires at least spatch 5ec97946eSNicolas Palix# version 1.0.0-rc11. 6ec97946eSNicolas Palix# 7ec97946eSNicolas Palix 874425eeeSNicolas PalixSPATCH="`which ${SPATCH:=spatch}`" 974425eeeSNicolas Palix 1013d94865SLuis R. Rodriguezif [ ! -x "$SPATCH" ]; then 1113d94865SLuis R. Rodriguez echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/' 1213d94865SLuis R. Rodriguez exit 1 1313d94865SLuis R. Rodriguezfi 1413d94865SLuis R. Rodriguez 15*c930a1b2SLuis R. RodriguezUSE_JOBS="no" 16*c930a1b2SLuis R. Rodriguez$SPATCH --help | grep "\-\-jobs" > /dev/null && USE_JOBS="yes" 1790d06a46SKees Cook 1826e56720SBernd Schubert# The verbosity may be set by the environmental parameter V= 1926e56720SBernd Schubert# as for example with 'make V=1 coccicheck' 2026e56720SBernd Schubert 2126e56720SBernd Schubertif [ -n "$V" -a "$V" != "0" ]; then 2290d06a46SKees Cook VERBOSE="$V" 2326e56720SBernd Schubertelse 2426e56720SBernd Schubert VERBOSE=0 2526e56720SBernd Schubertfi 2626e56720SBernd Schubert 2790d06a46SKees Cookif [ -z "$J" ]; then 2890d06a46SKees Cook NPROC=$(getconf _NPROCESSORS_ONLN) 2990d06a46SKees Cookelse 3090d06a46SKees Cook NPROC="$J" 3190d06a46SKees Cookfi 3290d06a46SKees Cook 338e826ad5SLuis R. RodriguezFLAGS="--very-quiet" 349e395550SNicolas Palix 359e395550SNicolas Palix# spatch only allows include directories with the syntax "-I include" 369e395550SNicolas Palix# while gcc also allows "-Iinclude" and "-include include" 379e395550SNicolas PalixCOCCIINCLUDE=${LINUXINCLUDE//-I/-I } 385b169108SAndrzej HajdaCOCCIINCLUDE=${COCCIINCLUDE// -include/ --include} 399e395550SNicolas Palix 401e9dea2aSNicolas Palixif [ "$C" = "1" -o "$C" = "2" ]; then 411e9dea2aSNicolas Palix ONLINE=1 421e9dea2aSNicolas Palix 439e395550SNicolas Palix # Take only the last argument, which is the C file to test 441e9dea2aSNicolas Palix shift $(( $# - 1 )) 459e395550SNicolas Palix OPTIONS="$COCCIINCLUDE $1" 461e9dea2aSNicolas Palixelse 471e9dea2aSNicolas Palix ONLINE=0 48d0bc1fb4SGreg Dietsche if [ "$KBUILD_EXTMOD" = "" ] ; then 4993f14468SNicolas Palix OPTIONS="--dir $srctree $COCCIINCLUDE" 50d0bc1fb4SGreg Dietsche else 5193f14468SNicolas Palix OPTIONS="--dir $KBUILD_EXTMOD $COCCIINCLUDE" 52d0bc1fb4SGreg Dietsche fi 531e9dea2aSNicolas Palixfi 541e9dea2aSNicolas Palix 55bad6a409SNicolas Palixif [ "$KBUILD_EXTMOD" != "" ] ; then 5693f14468SNicolas Palix OPTIONS="--patch $srctree $OPTIONS" 57bad6a409SNicolas Palixfi 58bad6a409SNicolas Palix 59*c930a1b2SLuis R. Rodriguez# You can override by using SPFLAGS 60*c930a1b2SLuis R. Rodriguezif [ "$USE_JOBS" = "no" ]; then 61*c930a1b2SLuis R. Rodriguez trap kill_running SIGTERM SIGINT 62*c930a1b2SLuis R. Rodriguez declare -a SPATCH_PID 63*c930a1b2SLuis R. Rodriguezelif [ "$NPROC" != "1" ]; then 64*c930a1b2SLuis R. Rodriguez # Using 0 should work as well, refer to _SC_NPROCESSORS_ONLN use on 65*c930a1b2SLuis R. Rodriguez # https://github.com/rdicosmo/parmap/blob/master/setcore_stubs.c 66*c930a1b2SLuis R. Rodriguez OPTIONS="$OPTIONS --jobs $NPROC --chunksize 1" 67*c930a1b2SLuis R. Rodriguezfi 68*c930a1b2SLuis R. Rodriguez 6974425eeeSNicolas Palixif [ "$MODE" = "" ] ; then 701e9dea2aSNicolas Palix if [ "$ONLINE" = "0" ] ; then 711f0a6742SNicolas Palix echo 'You have not explicitly specified the mode to use. Using default "report" mode.' 721f0a6742SNicolas Palix echo 'Available modes are the following: patch, report, context, org' 7374425eeeSNicolas Palix echo 'You can specify the mode with "make coccicheck MODE=<mode>"' 741f0a6742SNicolas Palix echo 'Note however that some modes are not implemented by some semantic patches.' 751e9dea2aSNicolas Palix fi 761f0a6742SNicolas Palix MODE="report" 771f0a6742SNicolas Palixfi 781f0a6742SNicolas Palix 791f0a6742SNicolas Palixif [ "$MODE" = "chain" ] ; then 801f0a6742SNicolas Palix if [ "$ONLINE" = "0" ] ; then 811f0a6742SNicolas Palix echo 'You have selected the "chain" mode.' 821f0a6742SNicolas Palix echo 'All available modes will be tried (in that order): patch, report, context, org' 831f0a6742SNicolas Palix fi 8403ee0c42SNicolas Palixelif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then 857a2358b3SDeepa Dinamani FLAGS="--no-show-diff $FLAGS" 8674425eeeSNicolas Palixfi 8774425eeeSNicolas Palix 881e9dea2aSNicolas Palixif [ "$ONLINE" = "0" ] ; then 8974425eeeSNicolas Palix echo '' 9074425eeeSNicolas Palix echo 'Please check for false positives in the output before submitting a patch.' 9174425eeeSNicolas Palix echo 'When using "patch" mode, carefully review the patch before submitting it.' 9274425eeeSNicolas Palix echo '' 931e9dea2aSNicolas Palixfi 9474425eeeSNicolas Palix 95*c930a1b2SLuis R. Rodriguezrun_cmd_parmap() { 96*c930a1b2SLuis R. Rodriguez if [ $VERBOSE -ne 0 ] ; then 97*c930a1b2SLuis R. Rodriguez echo "Running ($NPROC in parallel): $@" 98*c930a1b2SLuis R. Rodriguez fi 99*c930a1b2SLuis R. Rodriguez $@ 2>/dev/null 100*c930a1b2SLuis R. Rodriguez if [[ $? -ne 0 ]]; then 101*c930a1b2SLuis R. Rodriguez echo "coccicheck failed" 102*c930a1b2SLuis R. Rodriguez exit $? 103*c930a1b2SLuis R. Rodriguez fi 104*c930a1b2SLuis R. Rodriguez} 105*c930a1b2SLuis R. Rodriguez 106*c930a1b2SLuis R. Rodriguezrun_cmd_old() { 10790d06a46SKees Cook local i 1085303265aSBernd Schubert if [ $VERBOSE -ne 0 ] ; then 10990d06a46SKees Cook echo "Running ($NPROC in parallel): $@" 1105303265aSBernd Schubert fi 11190d06a46SKees Cook for i in $(seq 0 $(( NPROC - 1)) ); do 11293f14468SNicolas Palix eval "$@ --max $NPROC --index $i &" 11390d06a46SKees Cook SPATCH_PID[$i]=$! 11490d06a46SKees Cook if [ $VERBOSE -eq 2 ] ; then 11590d06a46SKees Cook echo "${SPATCH_PID[$i]} running" 11690d06a46SKees Cook fi 11790d06a46SKees Cook done 11890d06a46SKees Cook wait 1195303265aSBernd Schubert} 1205303265aSBernd Schubert 121*c930a1b2SLuis R. Rodriguezrun_cmd() { 122*c930a1b2SLuis R. Rodriguez if [ "$USE_JOBS" = "yes" ]; then 123*c930a1b2SLuis R. Rodriguez run_cmd_parmap $@ 124*c930a1b2SLuis R. Rodriguez else 125*c930a1b2SLuis R. Rodriguez run_cmd_old $@ 126*c930a1b2SLuis R. Rodriguez fi 127*c930a1b2SLuis R. Rodriguez} 128*c930a1b2SLuis R. Rodriguez 12990d06a46SKees Cookkill_running() { 1302552a39fSKees Cook for i in $(seq 0 $(( NPROC - 1 )) ); do 13190d06a46SKees Cook if [ $VERBOSE -eq 2 ] ; then 13290d06a46SKees Cook echo "Killing ${SPATCH_PID[$i]}" 13390d06a46SKees Cook fi 13490d06a46SKees Cook kill ${SPATCH_PID[$i]} 2>/dev/null 13590d06a46SKees Cook done 13690d06a46SKees Cook} 1375303265aSBernd Schubert 1388e826ad5SLuis R. Rodriguez# You can override heuristics with SPFLAGS, these must always go last 1398e826ad5SLuis R. RodriguezOPTIONS="$OPTIONS $SPFLAGS" 1408e826ad5SLuis R. Rodriguez 1411e9dea2aSNicolas Palixcoccinelle () { 14274425eeeSNicolas Palix COCCI="$1" 14374425eeeSNicolas Palix 14474425eeeSNicolas Palix OPT=`grep "Option" $COCCI | cut -d':' -f2` 1451e9dea2aSNicolas Palix 14693f14468SNicolas Palix# The option '--parse-cocci' can be used to syntactically check the SmPL files. 1471e9dea2aSNicolas Palix# 1481e9dea2aSNicolas Palix# $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null 1491e9dea2aSNicolas Palix 15035d88a38SNicolas Palix if [ $VERBOSE -ne 0 -a $ONLINE -eq 0 ] ; then 1511e9dea2aSNicolas Palix 1521e9dea2aSNicolas Palix FILE=`echo $COCCI | sed "s|$srctree/||"` 15374425eeeSNicolas Palix 1543c908417SNicolas Palix echo "Processing `basename $COCCI`" 1553c908417SNicolas Palix echo "with option(s) \"$OPT\"" 1563c908417SNicolas Palix echo '' 15774425eeeSNicolas Palix echo 'Message example to submit a patch:' 15874425eeeSNicolas Palix 1593c908417SNicolas Palix sed -ne 's|^///||p' $COCCI 16074425eeeSNicolas Palix 161062c1825SNicolas Palix if [ "$MODE" = "patch" ] ; then 16274425eeeSNicolas Palix echo ' The semantic patch that makes this change is available' 163062c1825SNicolas Palix elif [ "$MODE" = "report" ] ; then 164062c1825SNicolas Palix echo ' The semantic patch that makes this report is available' 165062c1825SNicolas Palix elif [ "$MODE" = "context" ] ; then 166062c1825SNicolas Palix echo ' The semantic patch that spots this code is available' 167062c1825SNicolas Palix elif [ "$MODE" = "org" ] ; then 168062c1825SNicolas Palix echo ' The semantic patch that makes this Org report is available' 169062c1825SNicolas Palix else 170062c1825SNicolas Palix echo ' The semantic patch that makes this output is available' 171062c1825SNicolas Palix fi 17274425eeeSNicolas Palix echo " in $FILE." 17374425eeeSNicolas Palix echo '' 17474425eeeSNicolas Palix echo ' More information about semantic patching is available at' 17574425eeeSNicolas Palix echo ' http://coccinelle.lip6.fr/' 17674425eeeSNicolas Palix echo '' 17774425eeeSNicolas Palix 1783c908417SNicolas Palix if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then 1793c908417SNicolas Palix echo 'Semantic patch information:' 1803c908417SNicolas Palix sed -ne 's|^//#||p' $COCCI 1813c908417SNicolas Palix echo '' 1823c908417SNicolas Palix fi 1832c1160c8SNicolas Palix fi 1843c908417SNicolas Palix 1852c1160c8SNicolas Palix if [ "$MODE" = "chain" ] ; then 1865303265aSBernd Schubert run_cmd $SPATCH -D patch \ 18793f14468SNicolas Palix $FLAGS --cocci-file $COCCI $OPT $OPTIONS || \ 1885303265aSBernd Schubert run_cmd $SPATCH -D report \ 18993f14468SNicolas Palix $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || \ 1905303265aSBernd Schubert run_cmd $SPATCH -D context \ 19193f14468SNicolas Palix $FLAGS --cocci-file $COCCI $OPT $OPTIONS || \ 1925303265aSBernd Schubert run_cmd $SPATCH -D org \ 19393f14468SNicolas Palix $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || exit 1 194c05cd6ddSNicolas Palix elif [ "$MODE" = "rep+ctxt" ] ; then 1955303265aSBernd Schubert run_cmd $SPATCH -D report \ 19693f14468SNicolas Palix $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff && \ 1975303265aSBernd Schubert run_cmd $SPATCH -D context \ 19893f14468SNicolas Palix $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1 1991e9dea2aSNicolas Palix else 20093f14468SNicolas Palix run_cmd $SPATCH -D $MODE $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1 2011e9dea2aSNicolas Palix fi 20274425eeeSNicolas Palix 20374425eeeSNicolas Palix} 20474425eeeSNicolas Palix 20574425eeeSNicolas Palixif [ "$COCCI" = "" ] ; then 20674425eeeSNicolas Palix for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do 2071e9dea2aSNicolas Palix coccinelle $f 20874425eeeSNicolas Palix done 20974425eeeSNicolas Palixelse 2101e9dea2aSNicolas Palix coccinelle $COCCI 21174425eeeSNicolas Palixfi 212