Blame view

scripts/coccicheck 7.25 KB
9e3955507   Nicolas Palix   Coccinelle: Clean...
1
  #!/bin/bash
b24413180   Greg Kroah-Hartman   License cleanup: ...
2
  # SPDX-License-Identifier: GPL-2.0
c100d537b   Luis R. Rodriguez   coccicheck: refer...
3
4
  # Linux kernel coccicheck
  #
1e01892e7   Markus Elfring   scripts/coccichec...
5
  # Read Documentation/dev-tools/coccinelle.rst
ec97946ed   Nicolas Palix   Coccinelle: Updat...
6
7
8
  #
  # This script requires at least spatch
  # version 1.0.0-rc11.
ec97946ed   Nicolas Palix   Coccinelle: Updat...
9

a9e064c00   Luis R. Rodriguez   coccicheck: add s...
10
  DIR="$(dirname $(readlink -f $0))/.."
74425eee7   Nicolas Palix   Add a target to u...
11
  SPATCH="`which ${SPATCH:=spatch}`"
13d948653   Luis R. Rodriguez   coccicheck: move ...
12
13
14
15
  if [ ! -x "$SPATCH" ]; then
      echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/'
      exit 1
  fi
a9e064c00   Luis R. Rodriguez   coccicheck: add s...
16
17
  SPATCH_VERSION=$($SPATCH --version | head -1 | awk '{print $3}')
  SPATCH_VERSION_NUM=$(echo $SPATCH_VERSION | ${DIR}/scripts/ld-version.sh)
c930a1b23   Luis R. Rodriguez   coccicheck: enabl...
18
19
  USE_JOBS="no"
  $SPATCH --help | grep "\-\-jobs" > /dev/null && USE_JOBS="yes"
90d06a468   Kees Cook   coccicheck: span ...
20

26e567209   Bernd Schubert   coccicheck: Allow...
21
22
23
24
  # The verbosity may be set by the environmental parameter V=
  # as for example with 'make V=1 coccicheck'
  
  if [ -n "$V" -a "$V" != "0" ]; then
90d06a468   Kees Cook   coccicheck: span ...
25
  	VERBOSE="$V"
26e567209   Bernd Schubert   coccicheck: Allow...
26
27
28
  else
  	VERBOSE=0
  fi
8e826ad52   Luis R. Rodriguez   coccicheck: make ...
29
  FLAGS="--very-quiet"
9e3955507   Nicolas Palix   Coccinelle: Clean...
30

5c384dba9   Luis R. Rodriguez   coccicheck: repla...
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  # You can use SPFLAGS to append extra arguments to coccicheck or override any
  # heuristics done in this file as Coccinelle accepts the last options when
  # options conflict.
  #
  # A good example for use of SPFLAGS is if you want to debug your cocci script,
  # you can for instance use the following:
  #
  # $ export COCCI=scripts/coccinelle/misc/irqf_oneshot.cocci
  # $ make coccicheck MODE=report DEBUG_FILE="all.err" SPFLAGS="--profile --show-trying" M=./drivers/mfd/arizona-irq.c
  #
  # "--show-trying" should show you what rule is being processed as it goes to
  # stdout, you do not need a debug file for that. The profile output will be
  # be sent to stdout, if you provide a DEBUG_FILE the profiling data can be
  # inspected there.
  #
  # --profile will not output if --very-quiet is used, so avoid it.
  echo $SPFLAGS | egrep -e "--profile|--show-trying" 2>&1 > /dev/null
  if [ $? -eq 0 ]; then
  	FLAGS="--quiet"
  fi
9e3955507   Nicolas Palix   Coccinelle: Clean...
51
52
53
  # spatch only allows include directories with the syntax "-I include"
  # while gcc also allows "-Iinclude" and "-include include"
  COCCIINCLUDE=${LINUXINCLUDE//-I/-I }
5b1691085   Andrzej Hajda   Coccinelle: fix i...
54
  COCCIINCLUDE=${COCCIINCLUDE// -include/ --include}
9e3955507   Nicolas Palix   Coccinelle: Clean...
55

1e9dea2a6   Nicolas Palix   Add support for t...
56
57
  if [ "$C" = "1" -o "$C" = "2" ]; then
      ONLINE=1
9e3955507   Nicolas Palix   Coccinelle: Clean...
58
59
60
      # Take only the last argument, which is the C file to test
      shift $(( $# - 1 ))
      OPTIONS="$COCCIINCLUDE $1"
d7059ca01   Masahiro Yamada   coccinelle: fix p...
61
62
63
  
      # No need to parallelize Coccinelle since this mode takes one input file.
      NPROC=1
1e9dea2a6   Nicolas Palix   Add support for t...
64
65
  else
      ONLINE=0
d0bc1fb46   Greg Dietsche   coccicheck: add M...
66
      if [ "$KBUILD_EXTMOD" = "" ] ; then
93f144684   Nicolas Palix   Coccinelle: Updat...
67
          OPTIONS="--dir $srctree $COCCIINCLUDE"
d0bc1fb46   Greg Dietsche   coccicheck: add M...
68
      else
93f144684   Nicolas Palix   Coccinelle: Updat...
69
          OPTIONS="--dir $KBUILD_EXTMOD $COCCIINCLUDE"
d0bc1fb46   Greg Dietsche   coccicheck: add M...
70
      fi
d7059ca01   Masahiro Yamada   coccinelle: fix p...
71
72
73
74
75
76
  
      if [ -z "$J" ]; then
          NPROC=$(getconf _NPROCESSORS_ONLN)
      else
          NPROC="$J"
      fi
1e9dea2a6   Nicolas Palix   Add support for t...
77
  fi
bad6a4092   Nicolas Palix   Coccinelle: Fix p...
78
  if [ "$KBUILD_EXTMOD" != "" ] ; then
93f144684   Nicolas Palix   Coccinelle: Updat...
79
      OPTIONS="--patch $srctree $OPTIONS"
bad6a4092   Nicolas Palix   Coccinelle: Fix p...
80
  fi
c930a1b23   Luis R. Rodriguez   coccicheck: enabl...
81
82
83
84
85
86
87
88
89
  # You can override by using SPFLAGS
  if [ "$USE_JOBS" = "no" ]; then
  	trap kill_running SIGTERM SIGINT
  	declare -a SPATCH_PID
  elif [ "$NPROC" != "1" ]; then
  	# Using 0 should work as well, refer to _SC_NPROCESSORS_ONLN use on
  	# https://github.com/rdicosmo/parmap/blob/master/setcore_stubs.c
  	OPTIONS="$OPTIONS --jobs $NPROC --chunksize 1"
  fi
74425eee7   Nicolas Palix   Add a target to u...
90
  if [ "$MODE" = "" ] ; then
1e9dea2a6   Nicolas Palix   Add support for t...
91
      if [ "$ONLINE" = "0" ] ; then
1f0a6742d   Nicolas Palix   Coccinelle: Make ...
92
93
  	echo 'You have not explicitly specified the mode to use. Using default "report" mode.'
  	echo 'Available modes are the following: patch, report, context, org'
1e9dea2a6   Nicolas Palix   Add support for t...
94
  	echo 'You can specify the mode with "make coccicheck MODE=<mode>"'
1f0a6742d   Nicolas Palix   Coccinelle: Make ...
95
96
97
98
99
100
101
102
103
  	echo 'Note however that some modes are not implemented by some semantic patches.'
      fi
      MODE="report"
  fi
  
  if [ "$MODE" = "chain" ] ; then
      if [ "$ONLINE" = "0" ] ; then
  	echo 'You have selected the "chain" mode.'
  	echo 'All available modes will be tried (in that order): patch, report, context, org'
1e9dea2a6   Nicolas Palix   Add support for t...
104
      fi
03ee0c42a   Nicolas Palix   Coccinelle: Use t...
105
  elif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then
7a2358b38   Deepa Dinamani   coccicheck: Allow...
106
      FLAGS="--no-show-diff $FLAGS"
74425eee7   Nicolas Palix   Add a target to u...
107
  fi
1e9dea2a6   Nicolas Palix   Add support for t...
108
109
110
111
112
113
  if [ "$ONLINE" = "0" ] ; then
      echo ''
      echo 'Please check for false positives in the output before submitting a patch.'
      echo 'When using "patch" mode, carefully review the patch before submitting it.'
      echo ''
  fi
74425eee7   Nicolas Palix   Add a target to u...
114

c930a1b23   Luis R. Rodriguez   coccicheck: enabl...
115
116
117
118
  run_cmd_parmap() {
  	if [ $VERBOSE -ne 0 ] ; then
  		echo "Running ($NPROC in parallel): $@"
  	fi
9ed07ada0   Julia Lawall   Coccinelle: make ...
119
120
  	echo $@ >>$DEBUG_FILE
  	$@ 2>>$DEBUG_FILE
512ddf7d7   Denis Efremov   coccicheck: retur...
121
122
  	err=$?
  	if [[ $err -ne 0 ]]; then
c930a1b23   Luis R. Rodriguez   coccicheck: enabl...
123
  		echo "coccicheck failed"
512ddf7d7   Denis Efremov   coccicheck: retur...
124
  		exit $err
c930a1b23   Luis R. Rodriguez   coccicheck: enabl...
125
126
127
128
  	fi
  }
  
  run_cmd_old() {
90d06a468   Kees Cook   coccicheck: span ...
129
  	local i
5303265a4   Bernd Schubert   coccicheck: Allow...
130
  	if [ $VERBOSE -ne 0 ] ; then
90d06a468   Kees Cook   coccicheck: span ...
131
  		echo "Running ($NPROC in parallel): $@"
5303265a4   Bernd Schubert   coccicheck: Allow...
132
  	fi
90d06a468   Kees Cook   coccicheck: span ...
133
  	for i in $(seq 0 $(( NPROC - 1)) ); do
93f144684   Nicolas Palix   Coccinelle: Updat...
134
  		eval "$@ --max $NPROC --index $i &"
90d06a468   Kees Cook   coccicheck: span ...
135
136
137
138
139
140
  		SPATCH_PID[$i]=$!
  		if [ $VERBOSE -eq 2 ] ; then
  			echo "${SPATCH_PID[$i]} running"
  		fi
  	done
  	wait
5303265a4   Bernd Schubert   coccicheck: Allow...
141
  }
c930a1b23   Luis R. Rodriguez   coccicheck: enabl...
142
143
144
145
146
147
148
  run_cmd() {
  	if [ "$USE_JOBS" = "yes" ]; then
  		run_cmd_parmap $@
  	else
  		run_cmd_old $@
  	fi
  }
90d06a468   Kees Cook   coccicheck: span ...
149
  kill_running() {
2552a39fa   Kees Cook   coccicheck: Fix m...
150
  	for i in $(seq 0 $(( NPROC - 1 )) ); do
90d06a468   Kees Cook   coccicheck: span ...
151
152
153
154
155
156
  		if [ $VERBOSE -eq 2 ] ; then
  			echo "Killing ${SPATCH_PID[$i]}"
  		fi
  		kill ${SPATCH_PID[$i]} 2>/dev/null
  	done
  }
5303265a4   Bernd Schubert   coccicheck: Allow...
157

8e826ad52   Luis R. Rodriguez   coccicheck: make ...
158
159
  # You can override heuristics with SPFLAGS, these must always go last
  OPTIONS="$OPTIONS $SPFLAGS"
1e9dea2a6   Nicolas Palix   Add support for t...
160
  coccinelle () {
74425eee7   Nicolas Palix   Add a target to u...
161
      COCCI="$1"
74425eee7   Nicolas Palix   Add a target to u...
162

e0be348e4   Masahiro Yamada   coccinelle: grep ...
163
164
      OPT=`grep "Options:" $COCCI | cut -d':' -f2`
      REQ=`grep "Requires:" $COCCI | cut -d':' -f2 | sed "s| ||"`
a9e064c00   Luis R. Rodriguez   coccicheck: add s...
165
166
167
      REQ_NUM=$(echo $REQ | ${DIR}/scripts/ld-version.sh)
      if [ "$REQ_NUM" != "0" ] ; then
  	    if [ "$SPATCH_VERSION_NUM" -lt "$REQ_NUM" ] ; then
1640eea35   Julia Lawall   Coccinelle: cocci...
168
  		    echo "Skipping coccinelle SmPL patch: $COCCI"
a9e064c00   Luis R. Rodriguez   coccicheck: add s...
169
170
171
172
173
  		    echo "You have coccinelle:           $SPATCH_VERSION"
  		    echo "This SmPL patch requires:      $REQ"
  		    return
  	    fi
      fi
74425eee7   Nicolas Palix   Add a target to u...
174

93f144684   Nicolas Palix   Coccinelle: Updat...
175
  #   The option '--parse-cocci' can be used to syntactically check the SmPL files.
1e9dea2a6   Nicolas Palix   Add support for t...
176
177
  #
  #    $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null
74425eee7   Nicolas Palix   Add a target to u...
178

35d88a387   Nicolas Palix   Coccinelle: Resto...
179
      if [ $VERBOSE -ne 0 -a $ONLINE -eq 0 ] ; then
74425eee7   Nicolas Palix   Add a target to u...
180

cd1af7cfb   Masahiro Yamada   coccinelle: fix v...
181
  	FILE=${COCCI#$srctree/}
74425eee7   Nicolas Palix   Add a target to u...
182

3c9084176   Nicolas Palix   Coccinelle: Impro...
183
184
185
  	echo "Processing `basename $COCCI`"
  	echo "with option(s) \"$OPT\""
  	echo ''
1e9dea2a6   Nicolas Palix   Add support for t...
186
  	echo 'Message example to submit a patch:'
3c9084176   Nicolas Palix   Coccinelle: Impro...
187
  	sed -ne 's|^///||p' $COCCI
1e9dea2a6   Nicolas Palix   Add support for t...
188

062c1825a   Nicolas Palix   Coccinelle: Add c...
189
190
191
192
193
194
195
196
197
198
199
  	if [ "$MODE" = "patch" ] ; then
  	    echo ' The semantic patch that makes this change is available'
  	elif [ "$MODE" = "report" ] ; then
  	    echo ' The semantic patch that makes this report is available'
  	elif [ "$MODE" = "context" ] ; then
  	    echo ' The semantic patch that spots this code is available'
  	elif [ "$MODE" = "org" ] ; then
  	    echo ' The semantic patch that makes this Org report is available'
  	else
  	    echo ' The semantic patch that makes this output is available'
  	fi
1e9dea2a6   Nicolas Palix   Add support for t...
200
201
202
203
204
  	echo " in $FILE."
  	echo ''
  	echo ' More information about semantic patching is available at'
  	echo ' http://coccinelle.lip6.fr/'
  	echo ''
3c9084176   Nicolas Palix   Coccinelle: Impro...
205
206
207
208
209
  	if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then
  	    echo 'Semantic patch information:'
  	    sed -ne 's|^//#||p' $COCCI
  	    echo ''
  	fi
2c1160c87   Nicolas Palix   Coccinelle: Add a...
210
      fi
3c9084176   Nicolas Palix   Coccinelle: Impro...
211

2c1160c87   Nicolas Palix   Coccinelle: Add a...
212
      if [ "$MODE" = "chain" ] ; then
5303265a4   Bernd Schubert   coccicheck: Allow...
213
  	run_cmd $SPATCH -D patch   \
93f144684   Nicolas Palix   Coccinelle: Updat...
214
  		$FLAGS --cocci-file $COCCI $OPT $OPTIONS               || \
5303265a4   Bernd Schubert   coccicheck: Allow...
215
  	run_cmd $SPATCH -D report  \
93f144684   Nicolas Palix   Coccinelle: Updat...
216
  		$FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || \
5303265a4   Bernd Schubert   coccicheck: Allow...
217
  	run_cmd $SPATCH -D context \
93f144684   Nicolas Palix   Coccinelle: Updat...
218
  		$FLAGS --cocci-file $COCCI $OPT $OPTIONS               || \
5303265a4   Bernd Schubert   coccicheck: Allow...
219
  	run_cmd $SPATCH -D org     \
93f144684   Nicolas Palix   Coccinelle: Updat...
220
  		$FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || exit 1
c05cd6ddb   Nicolas Palix   coccicheck: Add t...
221
      elif [ "$MODE" = "rep+ctxt" ] ; then
5303265a4   Bernd Schubert   coccicheck: Allow...
222
  	run_cmd $SPATCH -D report  \
93f144684   Nicolas Palix   Coccinelle: Updat...
223
  		$FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff && \
5303265a4   Bernd Schubert   coccicheck: Allow...
224
  	run_cmd $SPATCH -D context \
93f144684   Nicolas Palix   Coccinelle: Updat...
225
  		$FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1
1e9dea2a6   Nicolas Palix   Add support for t...
226
      else
93f144684   Nicolas Palix   Coccinelle: Updat...
227
  	run_cmd $SPATCH -D $MODE   $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1
1e9dea2a6   Nicolas Palix   Add support for t...
228
      fi
74425eee7   Nicolas Palix   Add a target to u...
229

74425eee7   Nicolas Palix   Add a target to u...
230
  }
9ed07ada0   Julia Lawall   Coccinelle: make ...
231
232
233
234
235
236
237
238
  if [ "$DEBUG_FILE" != "/dev/null" -a "$DEBUG_FILE" != "" ]; then
  	if [ -f $DEBUG_FILE ]; then
  		echo "Debug file $DEBUG_FILE exists, bailing"
  		exit
  	fi
  else
  	DEBUG_FILE="/dev/null"
  fi
74425eee7   Nicolas Palix   Add a target to u...
239
240
  if [ "$COCCI" = "" ] ; then
      for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do
1e9dea2a6   Nicolas Palix   Add support for t...
241
  	coccinelle $f
74425eee7   Nicolas Palix   Add a target to u...
242
243
      done
  else
1e9dea2a6   Nicolas Palix   Add support for t...
244
      coccinelle $COCCI
74425eee7   Nicolas Palix   Add a target to u...
245
  fi