coccicheck 7.1 KB

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