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 15c930a1b2SLuis R. RodriguezUSE_JOBS="no" 16c930a1b2SLuis 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 35*5c384dbaSLuis R. Rodriguez# You can use SPFLAGS to append extra arguments to coccicheck or override any 36*5c384dbaSLuis R. Rodriguez# heuristics done in this file as Coccinelle accepts the last options when 37*5c384dbaSLuis R. Rodriguez# options conflict. 38*5c384dbaSLuis R. Rodriguez# 39*5c384dbaSLuis R. Rodriguez# A good example for use of SPFLAGS is if you want to debug your cocci script, 40*5c384dbaSLuis R. Rodriguez# you can for instance use the following: 41*5c384dbaSLuis R. Rodriguez# 42*5c384dbaSLuis R. Rodriguez# $ export COCCI=scripts/coccinelle/misc/irqf_oneshot.cocci 43*5c384dbaSLuis R. Rodriguez# $ make coccicheck MODE=report DEBUG_FILE="all.err" SPFLAGS="--profile --show-trying" M=./drivers/mfd/arizona-irq.c 44*5c384dbaSLuis R. Rodriguez# 45*5c384dbaSLuis R. Rodriguez# "--show-trying" should show you what rule is being processed as it goes to 46*5c384dbaSLuis R. Rodriguez# stdout, you do not need a debug file for that. The profile output will be 47*5c384dbaSLuis R. Rodriguez# be sent to stdout, if you provide a DEBUG_FILE the profiling data can be 48*5c384dbaSLuis R. Rodriguez# inspected there. 49*5c384dbaSLuis R. Rodriguez# 50*5c384dbaSLuis R. Rodriguez# --profile will not output if --very-quiet is used, so avoid it. 51*5c384dbaSLuis R. Rodriguezecho $SPFLAGS | egrep -e "--profile|--show-trying" 2>&1 > /dev/null 52*5c384dbaSLuis R. Rodriguezif [ $? -eq 0 ]; then 53*5c384dbaSLuis R. Rodriguez FLAGS="--quiet" 54*5c384dbaSLuis R. Rodriguezfi 55*5c384dbaSLuis R. Rodriguez 569e395550SNicolas Palix# spatch only allows include directories with the syntax "-I include" 579e395550SNicolas Palix# while gcc also allows "-Iinclude" and "-include include" 589e395550SNicolas PalixCOCCIINCLUDE=${LINUXINCLUDE//-I/-I } 595b169108SAndrzej HajdaCOCCIINCLUDE=${COCCIINCLUDE// -include/ --include} 609e395550SNicolas Palix 611e9dea2aSNicolas Palixif [ "$C" = "1" -o "$C" = "2" ]; then 621e9dea2aSNicolas Palix ONLINE=1 631e9dea2aSNicolas Palix 649e395550SNicolas Palix # Take only the last argument, which is the C file to test 651e9dea2aSNicolas Palix shift $(( $# - 1 )) 669e395550SNicolas Palix OPTIONS="$COCCIINCLUDE $1" 671e9dea2aSNicolas Palixelse 681e9dea2aSNicolas Palix ONLINE=0 69d0bc1fb4SGreg Dietsche if [ "$KBUILD_EXTMOD" = "" ] ; then 7093f14468SNicolas Palix OPTIONS="--dir $srctree $COCCIINCLUDE" 71d0bc1fb4SGreg Dietsche else 7293f14468SNicolas Palix OPTIONS="--dir $KBUILD_EXTMOD $COCCIINCLUDE" 73d0bc1fb4SGreg Dietsche fi 741e9dea2aSNicolas Palixfi 751e9dea2aSNicolas Palix 76bad6a409SNicolas Palixif [ "$KBUILD_EXTMOD" != "" ] ; then 7793f14468SNicolas Palix OPTIONS="--patch $srctree $OPTIONS" 78bad6a409SNicolas Palixfi 79bad6a409SNicolas Palix 80c930a1b2SLuis R. Rodriguez# You can override by using SPFLAGS 81c930a1b2SLuis R. Rodriguezif [ "$USE_JOBS" = "no" ]; then 82c930a1b2SLuis R. Rodriguez trap kill_running SIGTERM SIGINT 83c930a1b2SLuis R. Rodriguez declare -a SPATCH_PID 84c930a1b2SLuis R. Rodriguezelif [ "$NPROC" != "1" ]; then 85c930a1b2SLuis R. Rodriguez # Using 0 should work as well, refer to _SC_NPROCESSORS_ONLN use on 86c930a1b2SLuis R. Rodriguez # https://github.com/rdicosmo/parmap/blob/master/setcore_stubs.c 87c930a1b2SLuis R. Rodriguez OPTIONS="$OPTIONS --jobs $NPROC --chunksize 1" 88c930a1b2SLuis R. Rodriguezfi 89c930a1b2SLuis R. Rodriguez 9074425eeeSNicolas Palixif [ "$MODE" = "" ] ; then 911e9dea2aSNicolas Palix if [ "$ONLINE" = "0" ] ; then 921f0a6742SNicolas Palix echo 'You have not explicitly specified the mode to use. Using default "report" mode.' 931f0a6742SNicolas Palix echo 'Available modes are the following: patch, report, context, org' 9474425eeeSNicolas Palix echo 'You can specify the mode with "make coccicheck MODE=<mode>"' 951f0a6742SNicolas Palix echo 'Note however that some modes are not implemented by some semantic patches.' 961e9dea2aSNicolas Palix fi 971f0a6742SNicolas Palix MODE="report" 981f0a6742SNicolas Palixfi 991f0a6742SNicolas Palix 1001f0a6742SNicolas Palixif [ "$MODE" = "chain" ] ; then 1011f0a6742SNicolas Palix if [ "$ONLINE" = "0" ] ; then 1021f0a6742SNicolas Palix echo 'You have selected the "chain" mode.' 1031f0a6742SNicolas Palix echo 'All available modes will be tried (in that order): patch, report, context, org' 1041f0a6742SNicolas Palix fi 10503ee0c42SNicolas Palixelif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then 1067a2358b3SDeepa Dinamani FLAGS="--no-show-diff $FLAGS" 10774425eeeSNicolas Palixfi 10874425eeeSNicolas Palix 1091e9dea2aSNicolas Palixif [ "$ONLINE" = "0" ] ; then 11074425eeeSNicolas Palix echo '' 11174425eeeSNicolas Palix echo 'Please check for false positives in the output before submitting a patch.' 11274425eeeSNicolas Palix echo 'When using "patch" mode, carefully review the patch before submitting it.' 11374425eeeSNicolas Palix echo '' 1141e9dea2aSNicolas Palixfi 11574425eeeSNicolas Palix 116c930a1b2SLuis R. Rodriguezrun_cmd_parmap() { 117c930a1b2SLuis R. Rodriguez if [ $VERBOSE -ne 0 ] ; then 118c930a1b2SLuis R. Rodriguez echo "Running ($NPROC in parallel): $@" 119c930a1b2SLuis R. Rodriguez fi 120be1fa900SLuis R. Rodriguez if [ "$DEBUG_FILE" != "/dev/null" -a "$DEBUG_FILE" != "" ]; then 121be1fa900SLuis R. Rodriguez if [ -f $DEBUG_FILE ]; then 122be1fa900SLuis R. Rodriguez echo "Debug file $DEBUG_FILE exists, bailing" 123be1fa900SLuis R. Rodriguez exit 124be1fa900SLuis R. Rodriguez fi 125be1fa900SLuis R. Rodriguez else 126be1fa900SLuis R. Rodriguez DEBUG_FILE="/dev/null" 127be1fa900SLuis R. Rodriguez fi 128be1fa900SLuis R. Rodriguez $@ 2>$DEBUG_FILE 129c930a1b2SLuis R. Rodriguez if [[ $? -ne 0 ]]; then 130c930a1b2SLuis R. Rodriguez echo "coccicheck failed" 131c930a1b2SLuis R. Rodriguez exit $? 132c930a1b2SLuis R. Rodriguez fi 133c930a1b2SLuis R. Rodriguez} 134c930a1b2SLuis R. Rodriguez 135c930a1b2SLuis R. Rodriguezrun_cmd_old() { 13690d06a46SKees Cook local i 1375303265aSBernd Schubert if [ $VERBOSE -ne 0 ] ; then 13890d06a46SKees Cook echo "Running ($NPROC in parallel): $@" 1395303265aSBernd Schubert fi 14090d06a46SKees Cook for i in $(seq 0 $(( NPROC - 1)) ); do 14193f14468SNicolas Palix eval "$@ --max $NPROC --index $i &" 14290d06a46SKees Cook SPATCH_PID[$i]=$! 14390d06a46SKees Cook if [ $VERBOSE -eq 2 ] ; then 14490d06a46SKees Cook echo "${SPATCH_PID[$i]} running" 14590d06a46SKees Cook fi 14690d06a46SKees Cook done 14790d06a46SKees Cook wait 1485303265aSBernd Schubert} 1495303265aSBernd Schubert 150c930a1b2SLuis R. Rodriguezrun_cmd() { 151c930a1b2SLuis R. Rodriguez if [ "$USE_JOBS" = "yes" ]; then 152c930a1b2SLuis R. Rodriguez run_cmd_parmap $@ 153c930a1b2SLuis R. Rodriguez else 154c930a1b2SLuis R. Rodriguez run_cmd_old $@ 155c930a1b2SLuis R. Rodriguez fi 156c930a1b2SLuis R. Rodriguez} 157c930a1b2SLuis R. Rodriguez 15890d06a46SKees Cookkill_running() { 1592552a39fSKees Cook for i in $(seq 0 $(( NPROC - 1 )) ); do 16090d06a46SKees Cook if [ $VERBOSE -eq 2 ] ; then 16190d06a46SKees Cook echo "Killing ${SPATCH_PID[$i]}" 16290d06a46SKees Cook fi 16390d06a46SKees Cook kill ${SPATCH_PID[$i]} 2>/dev/null 16490d06a46SKees Cook done 16590d06a46SKees Cook} 1665303265aSBernd Schubert 1678e826ad5SLuis R. Rodriguez# You can override heuristics with SPFLAGS, these must always go last 1688e826ad5SLuis R. RodriguezOPTIONS="$OPTIONS $SPFLAGS" 1698e826ad5SLuis R. Rodriguez 1701e9dea2aSNicolas Palixcoccinelle () { 17174425eeeSNicolas Palix COCCI="$1" 17274425eeeSNicolas Palix 17374425eeeSNicolas Palix OPT=`grep "Option" $COCCI | cut -d':' -f2` 1741e9dea2aSNicolas Palix 17593f14468SNicolas Palix# The option '--parse-cocci' can be used to syntactically check the SmPL files. 1761e9dea2aSNicolas Palix# 1771e9dea2aSNicolas Palix# $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null 1781e9dea2aSNicolas Palix 17935d88a38SNicolas Palix if [ $VERBOSE -ne 0 -a $ONLINE -eq 0 ] ; then 1801e9dea2aSNicolas Palix 1811e9dea2aSNicolas Palix FILE=`echo $COCCI | sed "s|$srctree/||"` 18274425eeeSNicolas Palix 1833c908417SNicolas Palix echo "Processing `basename $COCCI`" 1843c908417SNicolas Palix echo "with option(s) \"$OPT\"" 1853c908417SNicolas Palix echo '' 18674425eeeSNicolas Palix echo 'Message example to submit a patch:' 18774425eeeSNicolas Palix 1883c908417SNicolas Palix sed -ne 's|^///||p' $COCCI 18974425eeeSNicolas Palix 190062c1825SNicolas Palix if [ "$MODE" = "patch" ] ; then 19174425eeeSNicolas Palix echo ' The semantic patch that makes this change is available' 192062c1825SNicolas Palix elif [ "$MODE" = "report" ] ; then 193062c1825SNicolas Palix echo ' The semantic patch that makes this report is available' 194062c1825SNicolas Palix elif [ "$MODE" = "context" ] ; then 195062c1825SNicolas Palix echo ' The semantic patch that spots this code is available' 196062c1825SNicolas Palix elif [ "$MODE" = "org" ] ; then 197062c1825SNicolas Palix echo ' The semantic patch that makes this Org report is available' 198062c1825SNicolas Palix else 199062c1825SNicolas Palix echo ' The semantic patch that makes this output is available' 200062c1825SNicolas Palix fi 20174425eeeSNicolas Palix echo " in $FILE." 20274425eeeSNicolas Palix echo '' 20374425eeeSNicolas Palix echo ' More information about semantic patching is available at' 20474425eeeSNicolas Palix echo ' http://coccinelle.lip6.fr/' 20574425eeeSNicolas Palix echo '' 20674425eeeSNicolas Palix 2073c908417SNicolas Palix if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then 2083c908417SNicolas Palix echo 'Semantic patch information:' 2093c908417SNicolas Palix sed -ne 's|^//#||p' $COCCI 2103c908417SNicolas Palix echo '' 2113c908417SNicolas Palix fi 2122c1160c8SNicolas Palix fi 2133c908417SNicolas Palix 2142c1160c8SNicolas Palix if [ "$MODE" = "chain" ] ; then 2155303265aSBernd Schubert run_cmd $SPATCH -D patch \ 21693f14468SNicolas Palix $FLAGS --cocci-file $COCCI $OPT $OPTIONS || \ 2175303265aSBernd Schubert run_cmd $SPATCH -D report \ 21893f14468SNicolas Palix $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || \ 2195303265aSBernd Schubert run_cmd $SPATCH -D context \ 22093f14468SNicolas Palix $FLAGS --cocci-file $COCCI $OPT $OPTIONS || \ 2215303265aSBernd Schubert run_cmd $SPATCH -D org \ 22293f14468SNicolas Palix $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || exit 1 223c05cd6ddSNicolas Palix elif [ "$MODE" = "rep+ctxt" ] ; then 2245303265aSBernd Schubert run_cmd $SPATCH -D report \ 22593f14468SNicolas Palix $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff && \ 2265303265aSBernd Schubert run_cmd $SPATCH -D context \ 22793f14468SNicolas Palix $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1 2281e9dea2aSNicolas Palix else 22993f14468SNicolas Palix run_cmd $SPATCH -D $MODE $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1 2301e9dea2aSNicolas Palix fi 23174425eeeSNicolas Palix 23274425eeeSNicolas Palix} 23374425eeeSNicolas Palix 23474425eeeSNicolas Palixif [ "$COCCI" = "" ] ; then 23574425eeeSNicolas Palix for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do 2361e9dea2aSNicolas Palix coccinelle $f 23774425eeeSNicolas Palix done 23874425eeeSNicolas Palixelse 2391e9dea2aSNicolas Palix coccinelle $COCCI 24074425eeeSNicolas Palixfi 241