3 # Upload a created tarball to Coverity Scan, as per
4 # https://scan.coverity.com/projects/qemu/builds/new
6 # This work is licensed under the terms of the GNU GPL version 2,
7 # or (at your option) any later version.
8 # See the COPYING file in the top-level directory.
10 # Copyright (c) 2017-2020 Linaro Limited
11 # Written by Peter Maydell
13 # Note that this script will automatically download and
14 # run the (closed-source) coverity build tools, so don't
15 # use it if you don't trust them!
17 # This script assumes that you're running it from a QEMU source
18 # tree, and that tree is a fresh clean one, because we do an in-tree
19 # build. (This is necessary so that the filenames that the Coverity
20 # Scan server sees are relative paths that match up with the component
21 # regular expressions it uses; an out-of-tree build won't work for this.)
22 # The host machine should have as many of QEMU's dependencies
23 # installed as possible, for maximum coverity coverage.
25 # To do an upload you need to be a maintainer in the Coverity online
26 # service, and you will need to know the "Coverity token", which is a
27 # secret 8 digit hex string. You can find that from the web UI in the
28 # project settings, if you have maintainer access there.
30 # Command line options:
31 # --check-upload-only : return success if upload is possible
32 # --dry-run : run the tools, but don't actually do the upload
33 # --docker : create and work inside a container
34 # --docker-engine : specify the container engine to use (docker/podman/auto);
36 # --update-tools-only : update the cached copy of the tools, but don't run them
37 # --no-update-tools : do not update the cached copy of the tools
38 # --tokenfile : file to read Coverity token from
39 # --version ver : specify version being analyzed (default: ask git)
40 # --description desc : specify description of this version (default: ask git)
41 # --srcdir : QEMU source tree to analyze (default: current working dir)
42 # --results-tarball : path to copy the results tarball to (default: don't
43 # copy it anywhere, just upload it)
44 # --src-tarball : tarball to untar into src dir (default: none); this
45 # is intended mainly for internal use by the Docker support
47 # User-specifiable environment variables:
48 # COVERITY_TOKEN -- Coverity token (default: looks at your
49 # coverity.token config)
50 # COVERITY_EMAIL -- the email address to use for uploads (default:
51 # looks at your git coverity.email or user.email config)
52 # COVERITY_BUILD_CMD -- make command (default: 'make -jN' where N is
53 # number of CPUs as determined by 'nproc')
54 # COVERITY_TOOL_BASE -- set to directory to put coverity tools
55 # (default: /tmp/coverity-tools)
57 # You must specify the token, either by environment variable or by
58 # putting it in a file and using --tokenfile. Everything else has
59 # a reasonable default if this is run from a git tree.
62 # Check whether we can do an upload to the server; will exit *the script*
63 # with status 99 if the check failed (usually a bad token);
64 # will return from the function with status 1 if the check indicated
65 # that we can't upload yet (ie we are at quota)
66 # Assumes that COVERITY_TOKEN and PROJNAME have been initialized.
68 echo "Checking upload permissions..."
70 if ! up_perm
="$(wget https://scan.coverity.com/api/upload_permitted --post-data "token
=$COVERITY_TOKEN&project
=$PROJNAME" -q -O -)"; then
71 echo "Coverity Scan API access denied: bad token?"
75 # Really up_perm is a JSON response with either
76 # {upload_permitted:true} or {next_upload_permitted_at:<date>}
77 # We do some hacky string parsing instead of properly parsing it.
79 *upload_permitted
*true
*)
82 *next_upload_permitted_at
*)
86 echo "Coverity Scan upload check: unexpected result $up_perm"
93 check_upload_permissions
() {
94 # Check whether we can do an upload to the server; will exit the script
95 # with status 99 if the check failed (usually a bad token);
96 # will exit the script with status 0 if the check indicated that we
97 # can't upload yet (ie we are at quota)
98 # Assumes that COVERITY_TOKEN, PROJNAME and DRYRUN have been initialized.
100 if upload_permitted
; then
101 echo "Coverity Scan: upload permitted"
103 if [ "$DRYRUN" = yes ]; then
104 echo "Coverity Scan: upload quota reached, continuing dry run"
106 echo "Coverity Scan: upload quota reached; stopping here"
107 # Exit success as this isn't a build error.
114 build_docker_image
() {
115 # build docker container including the coverity-scan tools
116 echo "Building docker container..."
117 # TODO: This re-unpacks the tools every time, rather than caching
118 # and reusing the image produced by the COPY of the .tgz file.
120 tests
/docker
/docker.py
--engine ${DOCKER_ENGINE} build \
121 -t coverity-scanner
-f scripts
/coverity-scan
/coverity-scan.docker \
122 --extra-files scripts
/coverity-scan
/run-coverity-scan \
123 "$COVERITY_TOOL_BASE"/coverity_tool.tgz
126 update_coverity_tools
() {
127 # Check for whether we need to download the Coverity tools
128 # (either because we don't have a copy, or because it's out of date)
129 # Assumes that COVERITY_TOOL_BASE, COVERITY_TOKEN and PROJNAME are set.
131 mkdir
-p "$COVERITY_TOOL_BASE"
132 cd "$COVERITY_TOOL_BASE"
134 echo "Checking for new version of coverity build tools..."
135 wget https
://scan.coverity.com
/download
/cxx
/linux64
--post-data "token=$COVERITY_TOKEN&project=$PROJNAME&md5=1" -O coverity_tool.md5.new
137 if ! cmp -s coverity_tool.md5 coverity_tool.md5.new
; then
138 # out of date md5 or no md5: download new build tool
139 # blow away the old build tool
140 echo "Downloading coverity build tools..."
141 rm -rf coverity_tool coverity_tool.tgz
142 wget https
://scan.coverity.com
/download
/cxx
/linux64
--post-data "token=$COVERITY_TOKEN&project=$PROJNAME" -O coverity_tool.tgz
143 if ! (cat coverity_tool.md5.new
; echo " coverity_tool.tgz") |
md5sum -c --status; then
144 echo "Downloaded tarball didn't match md5sum!"
148 if [ "$DOCKER" != yes ]; then
149 # extract the new one, keeping it corralled in a 'coverity_tool' directory
150 echo "Unpacking coverity build tools..."
151 mkdir
-p coverity_tool
153 tar xf ..
/coverity_tool.tgz
155 mv coverity_tool.md5.new coverity_tool.md5
158 rm -f coverity_tool.md5.new
161 if [ "$DOCKER" = yes ]; then
167 # Check user-provided environment variables and arguments
173 while [ "$#" -ge 1 ]; do
193 if [ $# -eq 0 ]; then
194 echo "--version needs an argument"
202 if [ $# -eq 0 ]; then
203 echo "--description needs an argument"
211 if [ $# -eq 0 ]; then
212 echo "--tokenfile needs an argument"
215 COVERITY_TOKEN
="$(cat "$1")"
220 if [ $# -eq 0 ]; then
221 echo "--srcdir needs an argument"
229 if [ $# -eq 0 ]; then
230 echo "--results-tarball needs an argument"
238 if [ $# -eq 0 ]; then
239 echo "--src-tarball needs an argument"
252 if [ $# -eq 0 ]; then
253 echo "--docker-engine needs an argument"
261 echo "Unexpected argument '$1'"
267 if [ -z "$COVERITY_TOKEN" ]; then
268 COVERITY_TOKEN
="$(git config coverity.token)"
270 if [ -z "$COVERITY_TOKEN" ]; then
271 echo "COVERITY_TOKEN environment variable not set"
275 if [ "$DRYRUN" = check
]; then
280 if [ -z "$COVERITY_BUILD_CMD" ]; then
282 COVERITY_BUILD_CMD
="make -j$NPROC"
283 echo "COVERITY_BUILD_CMD: using default '$COVERITY_BUILD_CMD'"
286 if [ -z "$COVERITY_TOOL_BASE" ]; then
287 echo "COVERITY_TOOL_BASE: using default /tmp/coverity-tools"
288 COVERITY_TOOL_BASE
=/tmp
/coverity-tools
291 if [ -z "$SRCDIR" ]; then
295 TARBALL
=cov-int.
tar.xz
297 if [ "$UPDATE" = only
]; then
298 # Just do the tools update; we don't need to check whether
299 # we are in a source tree or have upload rights for this,
300 # so do it before some of the command line and source tree checks.
302 if [ "$DOCKER" = yes ] && [ ! -z "$SRCTARBALL" ]; then
303 echo --update-tools-only --docker is incompatible with
--src-tarball.
307 update_coverity_tools
311 if [ ! -e "$SRCDIR" ]; then
317 if [ ! -z "$SRCTARBALL" ]; then
318 echo "Untarring source tarball into $SRCDIR..."
319 tar xvf
"$SRCTARBALL"
322 echo "Checking this is a QEMU source tree..."
323 if ! [ -e "$SRCDIR/VERSION" ]; then
324 echo "Not in a QEMU source tree?"
328 # Fill in defaults used by the non-update-only process
329 if [ -z "$VERSION" ]; then
330 VERSION
="$(git describe --always HEAD)"
333 if [ -z "$DESCRIPTION" ]; then
334 DESCRIPTION
="$(git rev-parse HEAD)"
337 if [ -z "$COVERITY_EMAIL" ]; then
338 COVERITY_EMAIL
="$(git config coverity.email)"
340 if [ -z "$COVERITY_EMAIL" ]; then
341 COVERITY_EMAIL
="$(git config user.email)"
344 # Otherwise, continue with the full build and upload process.
346 check_upload_permissions
348 if [ "$UPDATE" != no
]; then
349 update_coverity_tools
352 # Run ourselves inside docker if that's what the user wants
353 if [ "$DOCKER" = yes ]; then
354 # Put the Coverity token into a temporary file that only
355 # we have read access to, and then pass it to docker build
356 # using a volume. A volume is enough for the token not to
357 # leak into the Docker image.
359 SECRETDIR
=$
(mktemp
-d)
360 if [ -z "$SECRETDIR" ]; then
361 echo "Failed to create temporary directory"
364 trap 'rm -rf "$SECRETDIR"' INT TERM EXIT
365 echo "Created temporary directory $SECRETDIR"
366 SECRET
="$SECRETDIR/token"
367 echo "$COVERITY_TOKEN" > "$SECRET"
368 echo "Archiving sources to be analyzed..."
369 .
/scripts
/archive-source.sh
"$SECRETDIR/qemu-sources.tgz"
370 ARGS
="--no-update-tools"
371 if [ "$DRYRUN" = yes ]; then
372 ARGS
="$ARGS --dry-run"
374 echo "Running scanner..."
375 # If we need to capture the output tarball, get the inner run to
376 # save it to the secrets directory so we can copy it out before the
377 # directory is cleaned up.
378 if [ ! -z "$RESULTSTARBALL" ]; then
379 ARGS
="$ARGS --results-tarball /work/cov-int.tar.xz"
381 # Arrange for this docker run to get access to the sources with -v.
382 # We pass through all the configuration from the outer script to the inner.
383 export COVERITY_EMAIL COVERITY_BUILD_CMD
384 tests
/docker
/docker.py run
-it --env COVERITY_EMAIL
--env COVERITY_BUILD_CMD \
385 -v "$SECRETDIR:/work" coverity-scanner \
386 .
/run-coverity-scan
--version "$VERSION" \
387 --description "$DESCRIPTION" $ARGS --tokenfile /work
/token \
388 --srcdir /qemu
--src-tarball /work
/qemu-sources.tgz
389 if [ ! -z "$RESULTSTARBALL" ]; then
390 echo "Copying results tarball to $RESULTSTARBALL..."
391 cp "$SECRETDIR/cov-int.tar.xz" "$RESULTSTARBALL"
393 echo "Docker work complete."
397 TOOLBIN
="$(cd "$COVERITY_TOOL_BASE" && echo $PWD/coverity_tool/cov-analysis-*/bin)"
399 if ! test -x "$TOOLBIN/cov-build"; then
400 echo "Couldn't find cov-build in the coverity build-tool directory??"
404 export PATH
="$TOOLBIN:$PATH"
408 echo "Nuking build directory..."
413 echo "Configuring..."
414 # We configure with a fixed set of enables here to ensure that we don't
415 # accidentally reduce the scope of the analysis by doing the build on
416 # the system that's missing a dependency that we need to build part of
418 ..
/configure
--disable-modules --enable-sdl --enable-gtk \
419 --enable-opengl --enable-vte --enable-gnutls \
420 --enable-nettle --enable-curses --enable-curl \
421 --audio-drv-list=oss
,alsa
,sdl
,pa
--enable-virtfs \
422 --enable-vnc --enable-vnc-sasl --enable-vnc-jpeg --enable-png \
423 --enable-xen --enable-brlapi \
424 --enable-linux-aio --enable-attr \
425 --enable-cap-ng --enable-trace-backends=log
--enable-spice --enable-rbd \
426 --enable-libusb --enable-usb-redir \
427 --enable-libiscsi --enable-libnfs --enable-seccomp \
428 --enable-tpm --enable-libssh --enable-lzo --enable-snappy --enable-bzip2 \
429 --enable-numa --enable-rdma --enable-smartcard --enable-virglrenderer \
430 --enable-mpath --enable-glusterfs \
431 --enable-virtfs --enable-zstd
433 echo "Running cov-build..."
436 cov-build
--dir cov-int
$COVERITY_BUILD_CMD
438 echo "Creating results tarball..."
439 tar cvf
- cov-int | xz
> "$TARBALL"
441 if [ ! -z "$RESULTSTARBALL" ]; then
442 echo "Copying results tarball to $RESULTSTARBALL..."
443 cp "$TARBALL" "$RESULTSTARBALL"
446 echo "Uploading results tarball..."
448 if [ "$DRYRUN" = yes ]; then
449 echo "Dry run only, not uploading $TARBALL"
453 curl
--form token
="$COVERITY_TOKEN" --form email
="$COVERITY_EMAIL" \
454 --form file=@
"$TARBALL" --form version
="$VERSION" \
455 --form description
="$DESCRIPTION" \
456 https
://scan.coverity.com
/builds?project
="$PROJNAME"