coccicheck 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. if [ -z "$J" ]; then
  66. NPROC=$(getconf _NPROCESSORS_ONLN)
  67. else
  68. NPROC="$J"
  69. fi
  70. fi
  71. if [ "$KBUILD_EXTMOD" != "" ] ; then
  72. OPTIONS="--patch $srctree $OPTIONS"
  73. fi
  74. # You can override by using SPFLAGS
  75. if [ "$USE_JOBS" = "no" ]; then
  76. trap kill_running SIGTERM SIGINT
  77. declare -a SPATCH_PID
  78. elif [ "$NPROC" != "1" ]; then
  79. # Using 0 should work as well, refer to _SC_NPROCESSORS_ONLN use on
  80. # https://github.com/rdicosmo/parmap/blob/master/setcore_stubs.c
  81. OPTIONS="$OPTIONS --jobs $NPROC --chunksize 1"
  82. fi
  83. if [ "$MODE" = "" ] ; then
  84. if [ "$ONLINE" = "0" ] ; then
  85. echo 'You have not explicitly specified the mode to use. Using default "report" mode.'
  86. echo 'Available modes are the following: patch, report, context, org'
  87. echo 'You can specify the mode with "make coccicheck MODE=<mode>"'
  88. echo 'Note however that some modes are not implemented by some semantic patches.'
  89. fi
  90. MODE="report"
  91. fi
  92. if [ "$MODE" = "chain" ] ; then
  93. if [ "$ONLINE" = "0" ] ; then
  94. echo 'You have selected the "chain" mode.'
  95. echo 'All available modes will be tried (in that order): patch, report, context, org'
  96. fi
  97. elif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then
  98. FLAGS="--no-show-diff $FLAGS"
  99. fi
  100. if [ "$ONLINE" = "0" ] ; then
  101. echo ''
  102. echo 'Please check for false positives in the output before submitting a patch.'
  103. echo 'When using "patch" mode, carefully review the patch before submitting it.'
  104. echo ''
  105. fi
  106. run_cmd_parmap() {
  107. if [ $VERBOSE -ne 0 ] ; then
  108. echo "Running ($NPROC in parallel): $@"
  109. fi
  110. echo $@ >>$DEBUG_FILE
  111. $@ 2>>$DEBUG_FILE
  112. err=$?
  113. if [[ $err -ne 0 ]]; then
  114. echo "coccicheck failed"
  115. exit $err
  116. fi
  117. }
  118. run_cmd_old() {
  119. local i
  120. if [ $VERBOSE -ne 0 ] ; then
  121. echo "Running ($NPROC in parallel): $@"
  122. fi
  123. for i in $(seq 0 $(( NPROC - 1)) ); do
  124. eval "$@ --max $NPROC --index $i &"
  125. SPATCH_PID[$i]=$!
  126. if [ $VERBOSE -eq 2 ] ; then
  127. echo "${SPATCH_PID[$i]} running"
  128. fi
  129. done
  130. wait
  131. }
  132. run_cmd() {
  133. if [ "$USE_JOBS" = "yes" ]; then
  134. run_cmd_parmap $@
  135. else
  136. run_cmd_old $@
  137. fi
  138. }
  139. kill_running() {
  140. for i in $(seq 0 $(( NPROC - 1 )) ); do
  141. if [ $VERBOSE -eq 2 ] ; then
  142. echo "Killing ${SPATCH_PID[$i]}"
  143. fi
  144. kill ${SPATCH_PID[$i]} 2>/dev/null
  145. done
  146. }
  147. # You can override heuristics with SPFLAGS, these must always go last
  148. OPTIONS="$OPTIONS $SPFLAGS"
  149. coccinelle () {
  150. COCCI="$1"
  151. OPT=`grep "Options:" $COCCI | cut -d':' -f2`
  152. REQ=`grep "Requires:" $COCCI | cut -d':' -f2 | sed "s| ||"`
  153. REQ_NUM=$(echo $REQ | ${DIR}/scripts/ld-version.sh)
  154. if [ "$REQ_NUM" != "0" ] ; then
  155. if [ "$SPATCH_VERSION_NUM" -lt "$REQ_NUM" ] ; then
  156. echo "Skipping coccinelle SmPL patch: $COCCI"
  157. echo "You have coccinelle: $SPATCH_VERSION"
  158. echo "This SmPL patch requires: $REQ"
  159. return
  160. fi
  161. fi
  162. # The option '--parse-cocci' can be used to syntactically check the SmPL files.
  163. #
  164. # $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null
  165. if [ $VERBOSE -ne 0 -a $ONLINE -eq 0 ] ; then
  166. FILE=${COCCI#$srctree/}
  167. echo "Processing `basename $COCCI`"
  168. echo "with option(s) \"$OPT\""
  169. echo ''
  170. echo 'Message example to submit a patch:'
  171. sed -ne 's|^///||p' $COCCI
  172. if [ "$MODE" = "patch" ] ; then
  173. echo ' The semantic patch that makes this change is available'
  174. elif [ "$MODE" = "report" ] ; then
  175. echo ' The semantic patch that makes this report is available'
  176. elif [ "$MODE" = "context" ] ; then
  177. echo ' The semantic patch that spots this code is available'
  178. elif [ "$MODE" = "org" ] ; then
  179. echo ' The semantic patch that makes this Org report is available'
  180. else
  181. echo ' The semantic patch that makes this output is available'
  182. fi
  183. echo " in $FILE."
  184. echo ''
  185. echo ' More information about semantic patching is available at'
  186. echo ' http://coccinelle.lip6.fr/'
  187. echo ''
  188. if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then
  189. echo 'Semantic patch information:'
  190. sed -ne 's|^//#||p' $COCCI
  191. echo ''
  192. fi
  193. fi
  194. if [ "$MODE" = "chain" ] ; then
  195. run_cmd $SPATCH -D patch \
  196. $FLAGS --cocci-file $COCCI $OPT $OPTIONS || \
  197. run_cmd $SPATCH -D report \
  198. $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || \
  199. run_cmd $SPATCH -D context \
  200. $FLAGS --cocci-file $COCCI $OPT $OPTIONS || \
  201. run_cmd $SPATCH -D org \
  202. $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || exit 1
  203. elif [ "$MODE" = "rep+ctxt" ] ; then
  204. run_cmd $SPATCH -D report \
  205. $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff && \
  206. run_cmd $SPATCH -D context \
  207. $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1
  208. else
  209. run_cmd $SPATCH -D $MODE $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1
  210. fi
  211. }
  212. if [ "$DEBUG_FILE" != "/dev/null" -a "$DEBUG_FILE" != "" ]; then
  213. if [ -f $DEBUG_FILE ]; then
  214. echo "Debug file $DEBUG_FILE exists, bailing"
  215. exit
  216. fi
  217. else
  218. DEBUG_FILE="/dev/null"
  219. fi
  220. if [ "$COCCI" = "" ] ; then
  221. for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do
  222. coccinelle $f
  223. done
  224. else
  225. coccinelle $COCCI
  226. fi