coccicheck 7.2 KB

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