coccicheck 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. # Linux kernel coccicheck
  4. #
  5. # Read Documentation/dev-tools/coccinelle.rst
  6. #
  7. # This script requires at least spatch
  8. # version 1.0.0-rc11.
  9. DIR="$(dirname $(readlink -f $0))/.."
  10. SPATCH="`which ${SPATCH:=spatch}`"
  11. if [ ! -x "$SPATCH" ]; then
  12. echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/'
  13. exit 1
  14. fi
  15. SPATCH_VERSION=$($SPATCH --version | head -1 | awk '{print $3}')
  16. SPATCH_VERSION_NUM=$(echo $SPATCH_VERSION | ${DIR}/scripts/ld-version.sh)
  17. USE_JOBS="no"
  18. $SPATCH --help | grep "\-\-jobs" > /dev/null && USE_JOBS="yes"
  19. # The verbosity may be set by the environmental parameter V=
  20. # as for example with 'make V=1 coccicheck'
  21. if [ -n "$V" -a "$V" != "0" ]; then
  22. VERBOSE="$V"
  23. else
  24. VERBOSE=0
  25. fi
  26. FLAGS="--very-quiet"
  27. # You can use SPFLAGS to append extra arguments to coccicheck or override any
  28. # heuristics done in this file as Coccinelle accepts the last options when
  29. # options conflict.
  30. #
  31. # A good example for use of SPFLAGS is if you want to debug your cocci script,
  32. # you can for instance use the following:
  33. #
  34. # $ export COCCI=scripts/coccinelle/misc/irqf_oneshot.cocci
  35. # $ make coccicheck MODE=report DEBUG_FILE="all.err" SPFLAGS="--profile --show-trying" M=./drivers/mfd/arizona-irq.c
  36. #
  37. # "--show-trying" should show you what rule is being processed as it goes to
  38. # stdout, you do not need a debug file for that. The profile output will be
  39. # be sent to stdout, if you provide a DEBUG_FILE the profiling data can be
  40. # inspected there.
  41. #
  42. # --profile will not output if --very-quiet is used, so avoid it.
  43. echo $SPFLAGS | egrep -e "--profile|--show-trying" 2>&1 > /dev/null
  44. if [ $? -eq 0 ]; then
  45. FLAGS="--quiet"
  46. fi
  47. # spatch only allows include directories with the syntax "-I include"
  48. # while gcc also allows "-Iinclude" and "-include include"
  49. COCCIINCLUDE=${LINUXINCLUDE//-I/-I }
  50. COCCIINCLUDE=${COCCIINCLUDE// -include/ --include}
  51. if [ "$C" = "1" -o "$C" = "2" ]; then
  52. ONLINE=1
  53. # Take only the last argument, which is the C file to test
  54. shift $(( $# - 1 ))
  55. OPTIONS="$COCCIINCLUDE $1"
  56. # No need to parallelize Coccinelle since this mode takes one input file.
  57. NPROC=1
  58. else
  59. ONLINE=0
  60. if [ "$KBUILD_EXTMOD" = "" ] ; then
  61. OPTIONS="--dir $srctree $COCCIINCLUDE"
  62. else
  63. OPTIONS="--dir $KBUILD_EXTMOD $COCCIINCLUDE"
  64. fi
  65. # Use only one thread per core by default if hyperthreading is enabled
  66. THREADS_PER_CORE=$(lscpu | grep "Thread(s) per core: " | tr -cd "[:digit:]")
  67. if [ -z "$J" ]; then
  68. NPROC=$(getconf _NPROCESSORS_ONLN)
  69. if [ $THREADS_PER_CORE -gt 1 -a $NPROC -gt 4 ] ; then
  70. NPROC=$((NPROC/2))
  71. fi
  72. else
  73. NPROC="$J"
  74. fi
  75. fi
  76. if [ "$KBUILD_EXTMOD" != "" ] ; then
  77. OPTIONS="--patch $srctree $OPTIONS"
  78. fi
  79. # You can override by using SPFLAGS
  80. if [ "$USE_JOBS" = "no" ]; then
  81. trap kill_running SIGTERM SIGINT
  82. declare -a SPATCH_PID
  83. elif [ "$NPROC" != "1" ]; then
  84. # Using 0 should work as well, refer to _SC_NPROCESSORS_ONLN use on
  85. # https://github.com/rdicosmo/parmap/blob/master/setcore_stubs.c
  86. OPTIONS="$OPTIONS --jobs $NPROC --chunksize 1"
  87. fi
  88. if [ "$MODE" = "" ] ; then
  89. if [ "$ONLINE" = "0" ] ; then
  90. echo 'You have not explicitly specified the mode to use. Using default "report" mode.'
  91. echo 'Available modes are the following: patch, report, context, org, chain'
  92. echo 'You can specify the mode with "make coccicheck MODE=<mode>"'
  93. echo 'Note however that some modes are not implemented by some semantic patches.'
  94. fi
  95. MODE="report"
  96. fi
  97. if [ "$MODE" = "chain" ] ; then
  98. if [ "$ONLINE" = "0" ] ; then
  99. echo 'You have selected the "chain" mode.'
  100. echo 'All available modes will be tried (in that order): patch, report, context, org'
  101. fi
  102. elif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then
  103. FLAGS="--no-show-diff $FLAGS"
  104. fi
  105. if [ "$ONLINE" = "0" ] ; then
  106. echo ''
  107. echo 'Please check for false positives in the output before submitting a patch.'
  108. echo 'When using "patch" mode, carefully review the patch before submitting it.'
  109. echo ''
  110. fi
  111. run_cmd_parmap() {
  112. if [ $VERBOSE -ne 0 ] ; then
  113. echo "Running ($NPROC in parallel): $@"
  114. fi
  115. if [ "$DEBUG_FILE" != "/dev/null" -a "$DEBUG_FILE" != "" ]; then
  116. echo $@>>$DEBUG_FILE
  117. $@ 2>>$DEBUG_FILE
  118. else
  119. echo $@
  120. $@ 2>&1
  121. fi
  122. err=$?
  123. if [[ $err -ne 0 ]]; then
  124. echo "coccicheck failed"
  125. exit $err
  126. fi
  127. }
  128. run_cmd_old() {
  129. local i
  130. if [ $VERBOSE -ne 0 ] ; then
  131. echo "Running ($NPROC in parallel): $@"
  132. fi
  133. for i in $(seq 0 $(( NPROC - 1)) ); do
  134. eval "$@ --max $NPROC --index $i &"
  135. SPATCH_PID[$i]=$!
  136. if [ $VERBOSE -eq 2 ] ; then
  137. echo "${SPATCH_PID[$i]} running"
  138. fi
  139. done
  140. wait
  141. }
  142. run_cmd() {
  143. if [ "$USE_JOBS" = "yes" ]; then
  144. run_cmd_parmap $@
  145. else
  146. run_cmd_old $@
  147. fi
  148. }
  149. kill_running() {
  150. for i in $(seq 0 $(( NPROC - 1 )) ); do
  151. if [ $VERBOSE -eq 2 ] ; then
  152. echo "Killing ${SPATCH_PID[$i]}"
  153. fi
  154. kill ${SPATCH_PID[$i]} 2>/dev/null
  155. done
  156. }
  157. # You can override heuristics with SPFLAGS, these must always go last
  158. OPTIONS="$OPTIONS $SPFLAGS"
  159. coccinelle () {
  160. COCCI="$1"
  161. OPT=`grep "Options:" $COCCI | cut -d':' -f2`
  162. REQ=`grep "Requires:" $COCCI | cut -d':' -f2 | sed "s| ||"`
  163. REQ_NUM=$(echo $REQ | ${DIR}/scripts/ld-version.sh)
  164. if [ "$REQ_NUM" != "0" ] ; then
  165. if [ "$SPATCH_VERSION_NUM" -lt "$REQ_NUM" ] ; then
  166. echo "Skipping coccinelle SmPL patch: $COCCI"
  167. echo "You have coccinelle: $SPATCH_VERSION"
  168. echo "This SmPL patch requires: $REQ"
  169. return
  170. fi
  171. fi
  172. # The option '--parse-cocci' can be used to syntactically check the SmPL files.
  173. #
  174. # $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null
  175. if [ $VERBOSE -ne 0 -a $ONLINE -eq 0 ] ; then
  176. FILE=${COCCI#$srctree/}
  177. echo "Processing `basename $COCCI`"
  178. echo "with option(s) \"$OPT\""
  179. echo ''
  180. echo 'Message example to submit a patch:'
  181. sed -ne 's|^///||p' $COCCI
  182. if [ "$MODE" = "patch" ] ; then
  183. echo ' The semantic patch that makes this change is available'
  184. elif [ "$MODE" = "report" ] ; then
  185. echo ' The semantic patch that makes this report is available'
  186. elif [ "$MODE" = "context" ] ; then
  187. echo ' The semantic patch that spots this code is available'
  188. elif [ "$MODE" = "org" ] ; then
  189. echo ' The semantic patch that makes this Org report is available'
  190. else
  191. echo ' The semantic patch that makes this output is available'
  192. fi
  193. echo " in $FILE."
  194. echo ''
  195. echo ' More information about semantic patching is available at'
  196. echo ' http://coccinelle.lip6.fr/'
  197. echo ''
  198. if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then
  199. echo 'Semantic patch information:'
  200. sed -ne 's|^//#||p' $COCCI
  201. echo ''
  202. fi
  203. fi
  204. if [ "$MODE" = "chain" ] ; then
  205. run_cmd $SPATCH -D patch \
  206. $FLAGS --cocci-file $COCCI $OPT $OPTIONS || \
  207. run_cmd $SPATCH -D report \
  208. $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || \
  209. run_cmd $SPATCH -D context \
  210. $FLAGS --cocci-file $COCCI $OPT $OPTIONS || \
  211. run_cmd $SPATCH -D org \
  212. $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || exit 1
  213. elif [ "$MODE" = "rep+ctxt" ] ; then
  214. run_cmd $SPATCH -D report \
  215. $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff && \
  216. run_cmd $SPATCH -D context \
  217. $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1
  218. else
  219. run_cmd $SPATCH -D $MODE $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1
  220. fi
  221. }
  222. if [ "$DEBUG_FILE" != "/dev/null" -a "$DEBUG_FILE" != "" ]; then
  223. if [ -f $DEBUG_FILE ]; then
  224. echo "Debug file $DEBUG_FILE exists, bailing"
  225. exit
  226. fi
  227. else
  228. DEBUG_FILE="/dev/null"
  229. fi
  230. if [ "$COCCI" = "" ] ; then
  231. for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do
  232. coccinelle $f
  233. done
  234. else
  235. coccinelle $COCCI
  236. fi