19e395550SNicolas Palix#!/bin/bash 2c100d537SLuis R. Rodriguez# Linux kernel coccicheck 3c100d537SLuis R. Rodriguez# 41e01892eSMarkus Elfring# Read Documentation/dev-tools/coccinelle.rst 5ec97946eSNicolas Palix# 6ec97946eSNicolas Palix# This script requires at least spatch 7ec97946eSNicolas Palix# version 1.0.0-rc11. 8ec97946eSNicolas Palix 9a9e064c0SLuis R. RodriguezDIR="$(dirname $(readlink -f $0))/.." 1074425eeeSNicolas PalixSPATCH="`which ${SPATCH:=spatch}`" 1174425eeeSNicolas Palix 1213d94865SLuis R. Rodriguezif [ ! -x "$SPATCH" ]; then 1313d94865SLuis R. Rodriguez echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/' 1413d94865SLuis R. Rodriguez exit 1 1513d94865SLuis R. Rodriguezfi 1613d94865SLuis R. Rodriguez 17a9e064c0SLuis R. RodriguezSPATCH_VERSION=$($SPATCH --version | head -1 | awk '{print $3}') 18a9e064c0SLuis R. RodriguezSPATCH_VERSION_NUM=$(echo $SPATCH_VERSION | ${DIR}/scripts/ld-version.sh) 19a9e064c0SLuis R. Rodriguez 20c930a1b2SLuis R. RodriguezUSE_JOBS="no" 21c930a1b2SLuis R. Rodriguez$SPATCH --help | grep "\-\-jobs" > /dev/null && USE_JOBS="yes" 2290d06a46SKees Cook 2326e56720SBernd Schubert# The verbosity may be set by the environmental parameter V= 2426e56720SBernd Schubert# as for example with 'make V=1 coccicheck' 2526e56720SBernd Schubert 2626e56720SBernd Schubertif [ -n "$V" -a "$V" != "0" ]; then 2790d06a46SKees Cook VERBOSE="$V" 2826e56720SBernd Schubertelse 2926e56720SBernd Schubert VERBOSE=0 3026e56720SBernd Schubertfi 3126e56720SBernd Schubert 3290d06a46SKees Cookif [ -z "$J" ]; then 3390d06a46SKees Cook NPROC=$(getconf _NPROCESSORS_ONLN) 3490d06a46SKees Cookelse 3590d06a46SKees Cook NPROC="$J" 3690d06a46SKees Cookfi 3790d06a46SKees Cook 388e826ad5SLuis R. RodriguezFLAGS="--very-quiet" 399e395550SNicolas Palix 405c384dbaSLuis R. Rodriguez# You can use SPFLAGS to append extra arguments to coccicheck or override any 415c384dbaSLuis R. Rodriguez# heuristics done in this file as Coccinelle accepts the last options when 425c384dbaSLuis R. Rodriguez# options conflict. 435c384dbaSLuis R. Rodriguez# 445c384dbaSLuis R. Rodriguez# A good example for use of SPFLAGS is if you want to debug your cocci script, 455c384dbaSLuis R. Rodriguez# you can for instance use the following: 465c384dbaSLuis R. Rodriguez# 475c384dbaSLuis R. Rodriguez# $ export COCCI=scripts/coccinelle/misc/irqf_oneshot.cocci 485c384dbaSLuis R. Rodriguez# $ make coccicheck MODE=report DEBUG_FILE="all.err" SPFLAGS="--profile --show-trying" M=./drivers/mfd/arizona-irq.c 495c384dbaSLuis R. Rodriguez# 505c384dbaSLuis R. Rodriguez# "--show-trying" should show you what rule is being processed as it goes to 515c384dbaSLuis R. Rodriguez# stdout, you do not need a debug file for that. The profile output will be 525c384dbaSLuis R. Rodriguez# be sent to stdout, if you provide a DEBUG_FILE the profiling data can be 535c384dbaSLuis R. Rodriguez# inspected there. 545c384dbaSLuis R. Rodriguez# 555c384dbaSLuis R. Rodriguez# --profile will not output if --very-quiet is used, so avoid it. 565c384dbaSLuis R. Rodriguezecho $SPFLAGS | egrep -e "--profile|--show-trying" 2>&1 > /dev/null 575c384dbaSLuis R. Rodriguezif [ $? -eq 0 ]; then 585c384dbaSLuis R. Rodriguez FLAGS="--quiet" 595c384dbaSLuis R. Rodriguezfi 605c384dbaSLuis R. Rodriguez 619e395550SNicolas Palix# spatch only allows include directories with the syntax "-I include" 629e395550SNicolas Palix# while gcc also allows "-Iinclude" and "-include include" 639e395550SNicolas PalixCOCCIINCLUDE=${LINUXINCLUDE//-I/-I } 645b169108SAndrzej HajdaCOCCIINCLUDE=${COCCIINCLUDE// -include/ --include} 659e395550SNicolas Palix 661e9dea2aSNicolas Palixif [ "$C" = "1" -o "$C" = "2" ]; then 671e9dea2aSNicolas Palix ONLINE=1 681e9dea2aSNicolas Palix 699e395550SNicolas Palix # Take only the last argument, which is the C file to test 701e9dea2aSNicolas Palix shift $(( $# - 1 )) 719e395550SNicolas Palix OPTIONS="$COCCIINCLUDE $1" 721e9dea2aSNicolas Palixelse 731e9dea2aSNicolas Palix ONLINE=0 74d0bc1fb4SGreg Dietsche if [ "$KBUILD_EXTMOD" = "" ] ; then 7593f14468SNicolas Palix OPTIONS="--dir $srctree $COCCIINCLUDE" 76d0bc1fb4SGreg Dietsche else 7793f14468SNicolas Palix OPTIONS="--dir $KBUILD_EXTMOD $COCCIINCLUDE" 78d0bc1fb4SGreg Dietsche fi 791e9dea2aSNicolas Palixfi 801e9dea2aSNicolas Palix 81bad6a409SNicolas Palixif [ "$KBUILD_EXTMOD" != "" ] ; then 8293f14468SNicolas Palix OPTIONS="--patch $srctree $OPTIONS" 83bad6a409SNicolas Palixfi 84bad6a409SNicolas Palix 85c930a1b2SLuis R. Rodriguez# You can override by using SPFLAGS 86c930a1b2SLuis R. Rodriguezif [ "$USE_JOBS" = "no" ]; then 87c930a1b2SLuis R. Rodriguez trap kill_running SIGTERM SIGINT 88c930a1b2SLuis R. Rodriguez declare -a SPATCH_PID 89c930a1b2SLuis R. Rodriguezelif [ "$NPROC" != "1" ]; then 90c930a1b2SLuis R. Rodriguez # Using 0 should work as well, refer to _SC_NPROCESSORS_ONLN use on 91c930a1b2SLuis R. Rodriguez # https://github.com/rdicosmo/parmap/blob/master/setcore_stubs.c 92c930a1b2SLuis R. Rodriguez OPTIONS="$OPTIONS --jobs $NPROC --chunksize 1" 93c930a1b2SLuis R. Rodriguezfi 94c930a1b2SLuis R. Rodriguez 9574425eeeSNicolas Palixif [ "$MODE" = "" ] ; then 961e9dea2aSNicolas Palix if [ "$ONLINE" = "0" ] ; then 971f0a6742SNicolas Palix echo 'You have not explicitly specified the mode to use. Using default "report" mode.' 981f0a6742SNicolas Palix echo 'Available modes are the following: patch, report, context, org' 9974425eeeSNicolas Palix echo 'You can specify the mode with "make coccicheck MODE=<mode>"' 1001f0a6742SNicolas Palix echo 'Note however that some modes are not implemented by some semantic patches.' 1011e9dea2aSNicolas Palix fi 1021f0a6742SNicolas Palix MODE="report" 1031f0a6742SNicolas Palixfi 1041f0a6742SNicolas Palix 1051f0a6742SNicolas Palixif [ "$MODE" = "chain" ] ; then 1061f0a6742SNicolas Palix if [ "$ONLINE" = "0" ] ; then 1071f0a6742SNicolas Palix echo 'You have selected the "chain" mode.' 1081f0a6742SNicolas Palix echo 'All available modes will be tried (in that order): patch, report, context, org' 1091f0a6742SNicolas Palix fi 11003ee0c42SNicolas Palixelif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then 1117a2358b3SDeepa Dinamani FLAGS="--no-show-diff $FLAGS" 11274425eeeSNicolas Palixfi 11374425eeeSNicolas Palix 1141e9dea2aSNicolas Palixif [ "$ONLINE" = "0" ] ; then 11574425eeeSNicolas Palix echo '' 11674425eeeSNicolas Palix echo 'Please check for false positives in the output before submitting a patch.' 11774425eeeSNicolas Palix echo 'When using "patch" mode, carefully review the patch before submitting it.' 11874425eeeSNicolas Palix echo '' 1191e9dea2aSNicolas Palixfi 12074425eeeSNicolas Palix 121c930a1b2SLuis R. Rodriguezrun_cmd_parmap() { 122c930a1b2SLuis R. Rodriguez if [ $VERBOSE -ne 0 ] ; then 123c930a1b2SLuis R. Rodriguez echo "Running ($NPROC in parallel): $@" 124c930a1b2SLuis R. Rodriguez fi 1259ed07adaSJulia Lawall echo $@ >>$DEBUG_FILE 1269ed07adaSJulia Lawall $@ 2>>$DEBUG_FILE 127c930a1b2SLuis R. Rodriguez if [[ $? -ne 0 ]]; then 128c930a1b2SLuis R. Rodriguez echo "coccicheck failed" 129c930a1b2SLuis R. Rodriguez exit $? 130c930a1b2SLuis R. Rodriguez fi 131c930a1b2SLuis R. Rodriguez} 132c930a1b2SLuis R. Rodriguez 133c930a1b2SLuis R. Rodriguezrun_cmd_old() { 13490d06a46SKees Cook local i 1355303265aSBernd Schubert if [ $VERBOSE -ne 0 ] ; then 13690d06a46SKees Cook echo "Running ($NPROC in parallel): $@" 1375303265aSBernd Schubert fi 13890d06a46SKees Cook for i in $(seq 0 $(( NPROC - 1)) ); do 13993f14468SNicolas Palix eval "$@ --max $NPROC --index $i &" 14090d06a46SKees Cook SPATCH_PID[$i]=$! 14190d06a46SKees Cook if [ $VERBOSE -eq 2 ] ; then 14290d06a46SKees Cook echo "${SPATCH_PID[$i]} running" 14390d06a46SKees Cook fi 14490d06a46SKees Cook done 14590d06a46SKees Cook wait 1465303265aSBernd Schubert} 1475303265aSBernd Schubert 148c930a1b2SLuis R. Rodriguezrun_cmd() { 149c930a1b2SLuis R. Rodriguez if [ "$USE_JOBS" = "yes" ]; then 150c930a1b2SLuis R. Rodriguez run_cmd_parmap $@ 151c930a1b2SLuis R. Rodriguez else 152c930a1b2SLuis R. Rodriguez run_cmd_old $@ 153c930a1b2SLuis R. Rodriguez fi 154c930a1b2SLuis R. Rodriguez} 155c930a1b2SLuis R. Rodriguez 15690d06a46SKees Cookkill_running() { 1572552a39fSKees Cook for i in $(seq 0 $(( NPROC - 1 )) ); do 15890d06a46SKees Cook if [ $VERBOSE -eq 2 ] ; then 15990d06a46SKees Cook echo "Killing ${SPATCH_PID[$i]}" 16090d06a46SKees Cook fi 16190d06a46SKees Cook kill ${SPATCH_PID[$i]} 2>/dev/null 16290d06a46SKees Cook done 16390d06a46SKees Cook} 1645303265aSBernd Schubert 1658e826ad5SLuis R. Rodriguez# You can override heuristics with SPFLAGS, these must always go last 1668e826ad5SLuis R. RodriguezOPTIONS="$OPTIONS $SPFLAGS" 1678e826ad5SLuis R. Rodriguez 1681e9dea2aSNicolas Palixcoccinelle () { 16974425eeeSNicolas Palix COCCI="$1" 17074425eeeSNicolas Palix 171e0be348eSMasahiro Yamada OPT=`grep "Options:" $COCCI | cut -d':' -f2` 172e0be348eSMasahiro Yamada REQ=`grep "Requires:" $COCCI | cut -d':' -f2 | sed "s| ||"` 173a9e064c0SLuis R. Rodriguez REQ_NUM=$(echo $REQ | ${DIR}/scripts/ld-version.sh) 174a9e064c0SLuis R. Rodriguez if [ "$REQ_NUM" != "0" ] ; then 175a9e064c0SLuis R. Rodriguez if [ "$SPATCH_VERSION_NUM" -lt "$REQ_NUM" ] ; then 176a9e064c0SLuis R. Rodriguez echo "Skipping coccinele SmPL patch: $COCCI" 177a9e064c0SLuis R. Rodriguez echo "You have coccinelle: $SPATCH_VERSION" 178a9e064c0SLuis R. Rodriguez echo "This SmPL patch requires: $REQ" 179a9e064c0SLuis R. Rodriguez return 180a9e064c0SLuis R. Rodriguez fi 181a9e064c0SLuis R. Rodriguez fi 1821e9dea2aSNicolas Palix 18393f14468SNicolas Palix# The option '--parse-cocci' can be used to syntactically check the SmPL files. 1841e9dea2aSNicolas Palix# 1851e9dea2aSNicolas Palix# $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null 1861e9dea2aSNicolas Palix 18735d88a38SNicolas Palix if [ $VERBOSE -ne 0 -a $ONLINE -eq 0 ] ; then 1881e9dea2aSNicolas Palix 189*cd1af7cfSMasahiro Yamada FILE=${COCCI#$srctree/} 19074425eeeSNicolas Palix 1913c908417SNicolas Palix echo "Processing `basename $COCCI`" 1923c908417SNicolas Palix echo "with option(s) \"$OPT\"" 1933c908417SNicolas Palix echo '' 19474425eeeSNicolas Palix echo 'Message example to submit a patch:' 19574425eeeSNicolas Palix 1963c908417SNicolas Palix sed -ne 's|^///||p' $COCCI 19774425eeeSNicolas Palix 198062c1825SNicolas Palix if [ "$MODE" = "patch" ] ; then 19974425eeeSNicolas Palix echo ' The semantic patch that makes this change is available' 200062c1825SNicolas Palix elif [ "$MODE" = "report" ] ; then 201062c1825SNicolas Palix echo ' The semantic patch that makes this report is available' 202062c1825SNicolas Palix elif [ "$MODE" = "context" ] ; then 203062c1825SNicolas Palix echo ' The semantic patch that spots this code is available' 204062c1825SNicolas Palix elif [ "$MODE" = "org" ] ; then 205062c1825SNicolas Palix echo ' The semantic patch that makes this Org report is available' 206062c1825SNicolas Palix else 207062c1825SNicolas Palix echo ' The semantic patch that makes this output is available' 208062c1825SNicolas Palix fi 20974425eeeSNicolas Palix echo " in $FILE." 21074425eeeSNicolas Palix echo '' 21174425eeeSNicolas Palix echo ' More information about semantic patching is available at' 21274425eeeSNicolas Palix echo ' http://coccinelle.lip6.fr/' 21374425eeeSNicolas Palix echo '' 21474425eeeSNicolas Palix 2153c908417SNicolas Palix if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then 2163c908417SNicolas Palix echo 'Semantic patch information:' 2173c908417SNicolas Palix sed -ne 's|^//#||p' $COCCI 2183c908417SNicolas Palix echo '' 2193c908417SNicolas Palix fi 2202c1160c8SNicolas Palix fi 2213c908417SNicolas Palix 2222c1160c8SNicolas Palix if [ "$MODE" = "chain" ] ; then 2235303265aSBernd Schubert run_cmd $SPATCH -D patch \ 22493f14468SNicolas Palix $FLAGS --cocci-file $COCCI $OPT $OPTIONS || \ 2255303265aSBernd Schubert run_cmd $SPATCH -D report \ 22693f14468SNicolas Palix $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || \ 2275303265aSBernd Schubert run_cmd $SPATCH -D context \ 22893f14468SNicolas Palix $FLAGS --cocci-file $COCCI $OPT $OPTIONS || \ 2295303265aSBernd Schubert run_cmd $SPATCH -D org \ 23093f14468SNicolas Palix $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || exit 1 231c05cd6ddSNicolas Palix elif [ "$MODE" = "rep+ctxt" ] ; then 2325303265aSBernd Schubert run_cmd $SPATCH -D report \ 23393f14468SNicolas Palix $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff && \ 2345303265aSBernd Schubert run_cmd $SPATCH -D context \ 23593f14468SNicolas Palix $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1 2361e9dea2aSNicolas Palix else 23793f14468SNicolas Palix run_cmd $SPATCH -D $MODE $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1 2381e9dea2aSNicolas Palix fi 23974425eeeSNicolas Palix 24074425eeeSNicolas Palix} 24174425eeeSNicolas Palix 2429ed07adaSJulia Lawallif [ "$DEBUG_FILE" != "/dev/null" -a "$DEBUG_FILE" != "" ]; then 2439ed07adaSJulia Lawall if [ -f $DEBUG_FILE ]; then 2449ed07adaSJulia Lawall echo "Debug file $DEBUG_FILE exists, bailing" 2459ed07adaSJulia Lawall exit 2469ed07adaSJulia Lawall fi 2479ed07adaSJulia Lawallelse 2489ed07adaSJulia Lawall DEBUG_FILE="/dev/null" 2499ed07adaSJulia Lawallfi 2509ed07adaSJulia Lawall 25174425eeeSNicolas Palixif [ "$COCCI" = "" ] ; then 25274425eeeSNicolas Palix for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do 2531e9dea2aSNicolas Palix coccinelle $f 25474425eeeSNicolas Palix done 25574425eeeSNicolas Palixelse 2561e9dea2aSNicolas Palix coccinelle $COCCI 25774425eeeSNicolas Palixfi 258