coccicheck 8.2 KB

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