12b2d50bdSJarkko Sakkinen#!/usr/bin/env bash 2b2441318SGreg Kroah-Hartman# SPDX-License-Identifier: GPL-2.0 3c100d537SLuis R. Rodriguez# Linux kernel coccicheck 4c100d537SLuis R. Rodriguez# 51e01892eSMarkus Elfring# Read Documentation/dev-tools/coccinelle.rst 6ec97946eSNicolas Palix# 7ec97946eSNicolas Palix# This script requires at least spatch 8ec97946eSNicolas Palix# version 1.0.0-rc11. 9ec97946eSNicolas Palix 10a9e064c0SLuis R. RodriguezDIR="$(dirname $(readlink -f $0))/.." 1174425eeeSNicolas PalixSPATCH="`which ${SPATCH:=spatch}`" 1274425eeeSNicolas Palix 1313d94865SLuis R. Rodriguezif [ ! -x "$SPATCH" ]; then 1413d94865SLuis R. Rodriguez echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/' 1513d94865SLuis R. Rodriguez exit 1 1613d94865SLuis R. Rodriguezfi 1713d94865SLuis R. Rodriguez 18a9e064c0SLuis R. RodriguezSPATCH_VERSION=$($SPATCH --version | head -1 | awk '{print $3}') 19a9e064c0SLuis R. Rodriguez 20c930a1b2SLuis R. RodriguezUSE_JOBS="no" 21180da5efSPeter Foley$SPATCH --help | grep -e "--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 328e826ad5SLuis R. RodriguezFLAGS="--very-quiet" 339e395550SNicolas Palix 345c384dbaSLuis R. Rodriguez# You can use SPFLAGS to append extra arguments to coccicheck or override any 355c384dbaSLuis R. Rodriguez# heuristics done in this file as Coccinelle accepts the last options when 365c384dbaSLuis R. Rodriguez# options conflict. 375c384dbaSLuis R. Rodriguez# 385c384dbaSLuis R. Rodriguez# A good example for use of SPFLAGS is if you want to debug your cocci script, 395c384dbaSLuis R. Rodriguez# you can for instance use the following: 405c384dbaSLuis R. Rodriguez# 415c384dbaSLuis R. Rodriguez# $ export COCCI=scripts/coccinelle/misc/irqf_oneshot.cocci 425c384dbaSLuis R. Rodriguez# $ make coccicheck MODE=report DEBUG_FILE="all.err" SPFLAGS="--profile --show-trying" M=./drivers/mfd/arizona-irq.c 435c384dbaSLuis R. Rodriguez# 445c384dbaSLuis R. Rodriguez# "--show-trying" should show you what rule is being processed as it goes to 455c384dbaSLuis R. Rodriguez# stdout, you do not need a debug file for that. The profile output will be 465c384dbaSLuis R. Rodriguez# be sent to stdout, if you provide a DEBUG_FILE the profiling data can be 475c384dbaSLuis R. Rodriguez# inspected there. 485c384dbaSLuis R. Rodriguez# 495c384dbaSLuis R. Rodriguez# --profile will not output if --very-quiet is used, so avoid it. 502d63e6a3SGreg Kroah-Hartmanecho $SPFLAGS | grep -E -e "--profile|--show-trying" 2>&1 > /dev/null 515c384dbaSLuis R. Rodriguezif [ $? -eq 0 ]; then 525c384dbaSLuis R. Rodriguez FLAGS="--quiet" 535c384dbaSLuis R. Rodriguezfi 545c384dbaSLuis R. Rodriguez 559e395550SNicolas Palix# spatch only allows include directories with the syntax "-I include" 569e395550SNicolas Palix# while gcc also allows "-Iinclude" and "-include include" 579e395550SNicolas PalixCOCCIINCLUDE=${LINUXINCLUDE//-I/-I } 585b169108SAndrzej HajdaCOCCIINCLUDE=${COCCIINCLUDE// -include/ --include} 599e395550SNicolas Palix 601e9dea2aSNicolas Palixif [ "$C" = "1" -o "$C" = "2" ]; then 611e9dea2aSNicolas Palix ONLINE=1 621e9dea2aSNicolas Palix 63d8f6e5c6SSumera Priyadarsini if [[ $# -le 0 ]]; then 64d8f6e5c6SSumera Priyadarsini echo '' 65d8f6e5c6SSumera Priyadarsini echo 'Specifying both the variable "C" and rule "coccicheck" in the make 66d8f6e5c6SSumera Priyadarsinicommand results in a shift count error.' 67d8f6e5c6SSumera Priyadarsini echo '' 68d8f6e5c6SSumera Priyadarsini echo 'Try specifying "scripts/coccicheck" as a value for the CHECK variable instead.' 69d8f6e5c6SSumera Priyadarsini echo '' 70d8f6e5c6SSumera Priyadarsini echo 'Example: make C=2 CHECK=scripts/coccicheck drivers/net/ethernet/ethoc.o' 71d8f6e5c6SSumera Priyadarsini echo '' 72d8f6e5c6SSumera Priyadarsini exit 1 73d8f6e5c6SSumera Priyadarsini fi 74d8f6e5c6SSumera Priyadarsini 759e395550SNicolas Palix # Take only the last argument, which is the C file to test 761e9dea2aSNicolas Palix shift $(( $# - 1 )) 779e395550SNicolas Palix OPTIONS="$COCCIINCLUDE $1" 78d7059ca0SMasahiro Yamada 79d7059ca0SMasahiro Yamada # No need to parallelize Coccinelle since this mode takes one input file. 80d7059ca0SMasahiro Yamada NPROC=1 811e9dea2aSNicolas Palixelse 821e9dea2aSNicolas Palix ONLINE=0 83*13b25489SMasahiro Yamada OPTIONS="--dir $srcroot $COCCIINCLUDE" 84d7059ca0SMasahiro Yamada 85e16a7c47SSumera Priyadarsini # Use only one thread per core by default if hyperthreading is enabled 86f5b3553bSMauro Carvalho Chehab THREADS_PER_CORE=$(LANG=C lscpu | grep "Thread(s) per core: " | tr -cd "[:digit:]") 87d7059ca0SMasahiro Yamada if [ -z "$J" ]; then 88d7059ca0SMasahiro Yamada NPROC=$(getconf _NPROCESSORS_ONLN) 89c5864560SSumera Priyadarsini if [ $THREADS_PER_CORE -gt 1 -a $NPROC -gt 4 ] ; then 90e16a7c47SSumera Priyadarsini NPROC=$((NPROC/2)) 91e16a7c47SSumera Priyadarsini fi 92d7059ca0SMasahiro Yamada else 93d7059ca0SMasahiro Yamada NPROC="$J" 94d7059ca0SMasahiro Yamada fi 951e9dea2aSNicolas Palixfi 961e9dea2aSNicolas Palix 97bad6a409SNicolas Palixif [ "$KBUILD_EXTMOD" != "" ] ; then 9893f14468SNicolas Palix OPTIONS="--patch $srctree $OPTIONS" 99bad6a409SNicolas Palixfi 100bad6a409SNicolas Palix 101c930a1b2SLuis R. Rodriguez# You can override by using SPFLAGS 102c930a1b2SLuis R. Rodriguezif [ "$USE_JOBS" = "no" ]; then 103c930a1b2SLuis R. Rodriguez trap kill_running SIGTERM SIGINT 104c930a1b2SLuis R. Rodriguez declare -a SPATCH_PID 105c930a1b2SLuis R. Rodriguezelif [ "$NPROC" != "1" ]; then 106c930a1b2SLuis R. Rodriguez # Using 0 should work as well, refer to _SC_NPROCESSORS_ONLN use on 107c930a1b2SLuis R. Rodriguez # https://github.com/rdicosmo/parmap/blob/master/setcore_stubs.c 108c930a1b2SLuis R. Rodriguez OPTIONS="$OPTIONS --jobs $NPROC --chunksize 1" 109c930a1b2SLuis R. Rodriguezfi 110c930a1b2SLuis R. Rodriguez 11174425eeeSNicolas Palixif [ "$MODE" = "" ] ; then 1121e9dea2aSNicolas Palix if [ "$ONLINE" = "0" ] ; then 1131f0a6742SNicolas Palix echo 'You have not explicitly specified the mode to use. Using default "report" mode.' 1144b2bd20cSSumera Priyadarsini echo 'Available modes are the following: patch, report, context, org, chain' 11574425eeeSNicolas Palix echo 'You can specify the mode with "make coccicheck MODE=<mode>"' 1161f0a6742SNicolas Palix echo 'Note however that some modes are not implemented by some semantic patches.' 1171e9dea2aSNicolas Palix fi 1181f0a6742SNicolas Palix MODE="report" 1191f0a6742SNicolas Palixfi 1201f0a6742SNicolas Palix 1211f0a6742SNicolas Palixif [ "$MODE" = "chain" ] ; then 1221f0a6742SNicolas Palix if [ "$ONLINE" = "0" ] ; then 1231f0a6742SNicolas Palix echo 'You have selected the "chain" mode.' 1241f0a6742SNicolas Palix echo 'All available modes will be tried (in that order): patch, report, context, org' 1251f0a6742SNicolas Palix fi 12603ee0c42SNicolas Palixelif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then 1277a2358b3SDeepa Dinamani FLAGS="--no-show-diff $FLAGS" 12874425eeeSNicolas Palixfi 12974425eeeSNicolas Palix 1301e9dea2aSNicolas Palixif [ "$ONLINE" = "0" ] ; then 13174425eeeSNicolas Palix echo '' 13274425eeeSNicolas Palix echo 'Please check for false positives in the output before submitting a patch.' 13374425eeeSNicolas Palix echo 'When using "patch" mode, carefully review the patch before submitting it.' 13474425eeeSNicolas Palix echo '' 1351e9dea2aSNicolas Palixfi 13674425eeeSNicolas Palix 137c930a1b2SLuis R. Rodriguezrun_cmd_parmap() { 138c930a1b2SLuis R. Rodriguez if [ $VERBOSE -ne 0 ] ; then 139c930a1b2SLuis R. Rodriguez echo "Running ($NPROC in parallel): $@" 140c930a1b2SLuis R. Rodriguez fi 141a19d1358SSumera Priyadarsini if [ "$DEBUG_FILE" != "/dev/null" -a "$DEBUG_FILE" != "" ]; then 1429ed07adaSJulia Lawall echo $@>>$DEBUG_FILE 1439ed07adaSJulia Lawall $@ 2>>$DEBUG_FILE 144a19d1358SSumera Priyadarsini else 145a19d1358SSumera Priyadarsini echo $@ 146a19d1358SSumera Priyadarsini $@ 2>&1 147a19d1358SSumera Priyadarsini fi 148a19d1358SSumera Priyadarsini 149512ddf7dSDenis Efremov err=$? 150512ddf7dSDenis Efremov if [[ $err -ne 0 ]]; then 151c930a1b2SLuis R. Rodriguez echo "coccicheck failed" 152512ddf7dSDenis Efremov exit $err 153c930a1b2SLuis R. Rodriguez fi 154c930a1b2SLuis R. Rodriguez} 155c930a1b2SLuis R. Rodriguez 156c930a1b2SLuis R. Rodriguezrun_cmd_old() { 15790d06a46SKees Cook local i 1585303265aSBernd Schubert if [ $VERBOSE -ne 0 ] ; then 15990d06a46SKees Cook echo "Running ($NPROC in parallel): $@" 1605303265aSBernd Schubert fi 16190d06a46SKees Cook for i in $(seq 0 $(( NPROC - 1)) ); do 16293f14468SNicolas Palix eval "$@ --max $NPROC --index $i &" 16390d06a46SKees Cook SPATCH_PID[$i]=$! 16490d06a46SKees Cook if [ $VERBOSE -eq 2 ] ; then 16590d06a46SKees Cook echo "${SPATCH_PID[$i]} running" 16690d06a46SKees Cook fi 16790d06a46SKees Cook done 16890d06a46SKees Cook wait 1695303265aSBernd Schubert} 1705303265aSBernd Schubert 171c930a1b2SLuis R. Rodriguezrun_cmd() { 172c930a1b2SLuis R. Rodriguez if [ "$USE_JOBS" = "yes" ]; then 173c930a1b2SLuis R. Rodriguez run_cmd_parmap $@ 174c930a1b2SLuis R. Rodriguez else 175c930a1b2SLuis R. Rodriguez run_cmd_old $@ 176c930a1b2SLuis R. Rodriguez fi 177c930a1b2SLuis R. Rodriguez} 178c930a1b2SLuis R. Rodriguez 17990d06a46SKees Cookkill_running() { 1802552a39fSKees Cook for i in $(seq 0 $(( NPROC - 1 )) ); do 18190d06a46SKees Cook if [ $VERBOSE -eq 2 ] ; then 18290d06a46SKees Cook echo "Killing ${SPATCH_PID[$i]}" 18390d06a46SKees Cook fi 18490d06a46SKees Cook kill ${SPATCH_PID[$i]} 2>/dev/null 18590d06a46SKees Cook done 18690d06a46SKees Cook} 1875303265aSBernd Schubert 1888e826ad5SLuis R. Rodriguez# You can override heuristics with SPFLAGS, these must always go last 1898e826ad5SLuis R. RodriguezOPTIONS="$OPTIONS $SPFLAGS" 1908e826ad5SLuis R. Rodriguez 1911e9dea2aSNicolas Palixcoccinelle () { 19274425eeeSNicolas Palix COCCI="$1" 19374425eeeSNicolas Palix 194e0be348eSMasahiro Yamada OPT=`grep "Options:" $COCCI | cut -d':' -f2` 195e0be348eSMasahiro Yamada REQ=`grep "Requires:" $COCCI | cut -d':' -f2 | sed "s| ||"` 19633114c43SMasahiro Yamada if [ -n "$REQ" ] && ! { echo "$REQ"; echo "$SPATCH_VERSION"; } | sort -CV ; then 1971640eea3SJulia Lawall echo "Skipping coccinelle SmPL patch: $COCCI" 198a9e064c0SLuis R. Rodriguez echo "You have coccinelle: $SPATCH_VERSION" 199a9e064c0SLuis R. Rodriguez echo "This SmPL patch requires: $REQ" 200a9e064c0SLuis R. Rodriguez return 201a9e064c0SLuis R. Rodriguez fi 2021e9dea2aSNicolas Palix 20393f14468SNicolas Palix# The option '--parse-cocci' can be used to syntactically check the SmPL files. 2041e9dea2aSNicolas Palix# 2051e9dea2aSNicolas Palix# $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null 2061e9dea2aSNicolas Palix 20735d88a38SNicolas Palix if [ $VERBOSE -ne 0 -a $ONLINE -eq 0 ] ; then 2081e9dea2aSNicolas Palix 209cd1af7cfSMasahiro Yamada FILE=${COCCI#$srctree/} 21074425eeeSNicolas Palix 2113c908417SNicolas Palix echo "Processing `basename $COCCI`" 2123c908417SNicolas Palix echo "with option(s) \"$OPT\"" 2133c908417SNicolas Palix echo '' 21474425eeeSNicolas Palix echo 'Message example to submit a patch:' 21574425eeeSNicolas Palix 2163c908417SNicolas Palix sed -ne 's|^///||p' $COCCI 21774425eeeSNicolas Palix 218062c1825SNicolas Palix if [ "$MODE" = "patch" ] ; then 21974425eeeSNicolas Palix echo ' The semantic patch that makes this change is available' 220062c1825SNicolas Palix elif [ "$MODE" = "report" ] ; then 221062c1825SNicolas Palix echo ' The semantic patch that makes this report is available' 222062c1825SNicolas Palix elif [ "$MODE" = "context" ] ; then 223062c1825SNicolas Palix echo ' The semantic patch that spots this code is available' 224062c1825SNicolas Palix elif [ "$MODE" = "org" ] ; then 225062c1825SNicolas Palix echo ' The semantic patch that makes this Org report is available' 226062c1825SNicolas Palix else 227062c1825SNicolas Palix echo ' The semantic patch that makes this output is available' 228062c1825SNicolas Palix fi 22974425eeeSNicolas Palix echo " in $FILE." 23074425eeeSNicolas Palix echo '' 23174425eeeSNicolas Palix echo ' More information about semantic patching is available at' 23274425eeeSNicolas Palix echo ' http://coccinelle.lip6.fr/' 23374425eeeSNicolas Palix echo '' 23474425eeeSNicolas Palix 2353c908417SNicolas Palix if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then 2363c908417SNicolas Palix echo 'Semantic patch information:' 2373c908417SNicolas Palix sed -ne 's|^//#||p' $COCCI 2383c908417SNicolas Palix echo '' 2393c908417SNicolas Palix fi 2402c1160c8SNicolas Palix fi 2413c908417SNicolas Palix 2422c1160c8SNicolas Palix if [ "$MODE" = "chain" ] ; then 2435303265aSBernd Schubert run_cmd $SPATCH -D patch \ 24493f14468SNicolas Palix $FLAGS --cocci-file $COCCI $OPT $OPTIONS || \ 2455303265aSBernd Schubert run_cmd $SPATCH -D report \ 24693f14468SNicolas Palix $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || \ 2475303265aSBernd Schubert run_cmd $SPATCH -D context \ 24893f14468SNicolas Palix $FLAGS --cocci-file $COCCI $OPT $OPTIONS || \ 2495303265aSBernd Schubert run_cmd $SPATCH -D org \ 25093f14468SNicolas Palix $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || exit 1 251c05cd6ddSNicolas Palix elif [ "$MODE" = "rep+ctxt" ] ; then 2525303265aSBernd Schubert run_cmd $SPATCH -D report \ 25393f14468SNicolas Palix $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff && \ 2545303265aSBernd Schubert run_cmd $SPATCH -D context \ 25593f14468SNicolas Palix $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1 2561e9dea2aSNicolas Palix else 25793f14468SNicolas Palix run_cmd $SPATCH -D $MODE $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1 2581e9dea2aSNicolas Palix fi 25974425eeeSNicolas Palix 26074425eeeSNicolas Palix} 26174425eeeSNicolas Palix 2629ed07adaSJulia Lawallif [ "$DEBUG_FILE" != "/dev/null" -a "$DEBUG_FILE" != "" ]; then 2639ed07adaSJulia Lawall if [ -f $DEBUG_FILE ]; then 2649ed07adaSJulia Lawall echo "Debug file $DEBUG_FILE exists, bailing" 2659ed07adaSJulia Lawall exit 2669ed07adaSJulia Lawall fi 2679ed07adaSJulia Lawallelse 2689ed07adaSJulia Lawall DEBUG_FILE="/dev/null" 2699ed07adaSJulia Lawallfi 2709ed07adaSJulia Lawall 27174425eeeSNicolas Palixif [ "$COCCI" = "" ] ; then 27274425eeeSNicolas Palix for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do 2731e9dea2aSNicolas Palix coccinelle $f 27474425eeeSNicolas Palix done 27574425eeeSNicolas Palixelse 2761e9dea2aSNicolas Palix coccinelle $COCCI 27774425eeeSNicolas Palixfi 278