Blame view

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

a9e064c00   Luis R. Rodriguez   coccicheck: add s...
9
  DIR="$(dirname $(readlink -f $0))/.."
74425eee7   Nicolas Palix   Add a target to u...
10
  SPATCH="`which ${SPATCH:=spatch}`"
13d948653   Luis R. Rodriguez   coccicheck: move ...
11
12
13
14
  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...
15
16
  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...
17
18
  USE_JOBS="no"
  $SPATCH --help | grep "\-\-jobs" > /dev/null && USE_JOBS="yes"
90d06a468   Kees Cook   coccicheck: span ...
19

26e567209   Bernd Schubert   coccicheck: Allow...
20
21
22
23
  # 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 ...
24
  	VERBOSE="$V"
26e567209   Bernd Schubert   coccicheck: Allow...
25
26
27
  else
  	VERBOSE=0
  fi
90d06a468   Kees Cook   coccicheck: span ...
28
29
30
31
32
  if [ -z "$J" ]; then
  	NPROC=$(getconf _NPROCESSORS_ONLN)
  else
  	NPROC="$J"
  fi
8e826ad52   Luis R. Rodriguez   coccicheck: make ...
33
  FLAGS="--very-quiet"
9e3955507   Nicolas Palix   Coccinelle: Clean...
34

5c384dba9   Luis R. Rodriguez   coccicheck: repla...
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  # 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...
55
56
57
  # 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...
58
  COCCIINCLUDE=${COCCIINCLUDE// -include/ --include}
9e3955507   Nicolas Palix   Coccinelle: Clean...
59

1e9dea2a6   Nicolas Palix   Add support for t...
60
61
  if [ "$C" = "1" -o "$C" = "2" ]; then
      ONLINE=1
9e3955507   Nicolas Palix   Coccinelle: Clean...
62
63
64
      # Take only the last argument, which is the C file to test
      shift $(( $# - 1 ))
      OPTIONS="$COCCIINCLUDE $1"
1e9dea2a6   Nicolas Palix   Add support for t...
65
66
  else
      ONLINE=0
d0bc1fb46   Greg Dietsche   coccicheck: add M...
67
      if [ "$KBUILD_EXTMOD" = "" ] ; then
93f144684   Nicolas Palix   Coccinelle: Updat...
68
          OPTIONS="--dir $srctree $COCCIINCLUDE"
d0bc1fb46   Greg Dietsche   coccicheck: add M...
69
      else
93f144684   Nicolas Palix   Coccinelle: Updat...
70
          OPTIONS="--dir $KBUILD_EXTMOD $COCCIINCLUDE"
d0bc1fb46   Greg Dietsche   coccicheck: add M...
71
      fi
1e9dea2a6   Nicolas Palix   Add support for t...
72
  fi
bad6a4092   Nicolas Palix   Coccinelle: Fix p...
73
  if [ "$KBUILD_EXTMOD" != "" ] ; then
93f144684   Nicolas Palix   Coccinelle: Updat...
74
      OPTIONS="--patch $srctree $OPTIONS"
bad6a4092   Nicolas Palix   Coccinelle: Fix p...
75
  fi
c930a1b23   Luis R. Rodriguez   coccicheck: enabl...
76
77
78
79
80
81
82
83
84
  # 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...
85
  if [ "$MODE" = "" ] ; then
1e9dea2a6   Nicolas Palix   Add support for t...
86
      if [ "$ONLINE" = "0" ] ; then
1f0a6742d   Nicolas Palix   Coccinelle: Make ...
87
88
  	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...
89
  	echo 'You can specify the mode with "make coccicheck MODE=<mode>"'
1f0a6742d   Nicolas Palix   Coccinelle: Make ...
90
91
92
93
94
95
96
97
98
  	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...
99
      fi
03ee0c42a   Nicolas Palix   Coccinelle: Use t...
100
  elif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then
7a2358b38   Deepa Dinamani   coccicheck: Allow...
101
      FLAGS="--no-show-diff $FLAGS"
74425eee7   Nicolas Palix   Add a target to u...
102
  fi
1e9dea2a6   Nicolas Palix   Add support for t...
103
104
105
106
107
108
  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...
109

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

8e826ad52   Luis R. Rodriguez   coccicheck: make ...
159
160
  # You can override heuristics with SPFLAGS, these must always go last
  OPTIONS="$OPTIONS $SPFLAGS"
1e9dea2a6   Nicolas Palix   Add support for t...
161
  coccinelle () {
74425eee7   Nicolas Palix   Add a target to u...
162
      COCCI="$1"
74425eee7   Nicolas Palix   Add a target to u...
163
164
  
      OPT=`grep "Option" $COCCI | cut -d':' -f2`
a9e064c00   Luis R. Rodriguez   coccicheck: add s...
165
166
167
168
169
170
171
172
173
174
      REQ=`grep "Requires" $COCCI | cut -d':' -f2 | sed "s| ||"`
      REQ_NUM=$(echo $REQ | ${DIR}/scripts/ld-version.sh)
      if [ "$REQ_NUM" != "0" ] ; then
  	    if [ "$SPATCH_VERSION_NUM" -lt "$REQ_NUM" ] ; then
  		    echo "Skipping coccinele SmPL patch: $COCCI"
  		    echo "You have coccinelle:           $SPATCH_VERSION"
  		    echo "This SmPL patch requires:      $REQ"
  		    return
  	    fi
      fi
74425eee7   Nicolas Palix   Add a target to u...
175

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

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

1e9dea2a6   Nicolas Palix   Add support for t...
182
  	FILE=`echo $COCCI | sed "s|$srctree/||"`
74425eee7   Nicolas Palix   Add a target to u...
183

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

062c1825a   Nicolas Palix   Coccinelle: Add c...
190
191
192
193
194
195
196
197
198
199
200
  	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...
201
202
203
204
205
  	echo " in $FILE."
  	echo ''
  	echo ' More information about semantic patching is available at'
  	echo ' http://coccinelle.lip6.fr/'
  	echo ''
3c9084176   Nicolas Palix   Coccinelle: Impro...
206
207
208
209
210
  	if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then
  	    echo 'Semantic patch information:'
  	    sed -ne 's|^//#||p' $COCCI
  	    echo ''
  	fi
2c1160c87   Nicolas Palix   Coccinelle: Add a...
211
      fi
3c9084176   Nicolas Palix   Coccinelle: Impro...
212

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

74425eee7   Nicolas Palix   Add a target to u...
231
232
233
234
  }
  
  if [ "$COCCI" = "" ] ; then
      for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do
1e9dea2a6   Nicolas Palix   Add support for t...
235
  	coccinelle $f
74425eee7   Nicolas Palix   Add a target to u...
236
237
      done
  else
1e9dea2a6   Nicolas Palix   Add support for t...
238
      coccinelle $COCCI
74425eee7   Nicolas Palix   Add a target to u...
239
  fi