coccicheck 7.1 KB

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