coccicheck 7.9 KB

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