Skip to content

eBPF-based security platform

ci: merge target coverage into the report

John Fastabend0e595d884623
diff --git a/.github/scripts/coverage.sh b/.github/scripts/coverage.sh
index b23683e6..fccadfee 100755
--- a/.github/scripts/coverage.sh
+++ b/.github/scripts/coverage.sh
@@ -1,10 +1,16 @@
#!/bin/sh
# SPDX-License-Identifier: BSD-2-Clause
# Copyright (c) 2026 Authors of shirudo
+#
+# .github/scripts/coverage.sh -- after the tests of a COVERAGE=1 build,
+# write coverage/shirudo.lcov from the counters of the host's binaries
+# and, where the test VMs ran, of the target in each guest.
+#
+# .github/scripts/coverage.sh
set -eu
-cd "$(git rev-parse --show-toplevel)"
+cd "$(dirname "$0")/../.."
mkdir -p coverage
capture() {
@@ -31,14 +37,64 @@ capture() {
lcov --capture --no-recursion "$@"
}
+# The counters of the guests of one bundle, as one tracefile. gcov reads
+# counters beside the notes of their build, with the gcov of the compiler
+# that wrote them and the sources where the build had them: this runs in
+# the build container, which mounts the checkout at its own path.
+capture_target() {
+ bundle=$1
+ srcdir=$(cat "$bundle/coverage/srcdir")
+ work=$(mktemp -d)
+ n=0
+
+ [ -e "$srcdir" ] || ln -s "$PWD" "$srcdir"
+ mkdir -p "$work/notes$srcdir"
+ tar -C "$work/notes$srcdir" -xf "$bundle/coverage/gcno.tar"
+ lcov --capture --initial --directory "$work/notes" \
+ --output-file "$work/notes.lcov"
+ set -- --add-tracefile "$work/notes.lcov"
+ for counters in build/test-vm/coverage/*.tar; do
+ [ -f "$counters" ] || continue
+ n=$((n + 1))
+ cp -a "$work/notes" "$work/$n"
+ tar -C "$work/$n" -xf "$counters"
+ lcov --capture --ignore-errors inconsistent \
+ --directory "$work/$n" --output-file "$work/$n.lcov"
+ rm -rf "$work/$n"
+ set -- "$@" --add-tracefile "$work/$n.lcov"
+ done
+ [ "$n" -gt 0 ] || {
+ echo "coverage.sh: no guest left counters in build/test-vm/coverage" >&2
+ exit 1
+ }
+ lcov --ignore-errors inconsistent "$@" --output-file "$work/target.lcov"
+ sed "s#^SF:$srcdir/#SF:$PWD/#" "$work/target.lcov" >coverage/target.lcov
+ rm -rf "$work"
+}
+
+if [ "${1:-}" = target ]; then
+ capture_target "$2"
+ exit 0
+fi
+
capture gcno --initial --output-file coverage/baseline.lcov
capture gcda --output-file coverage/run.lcov
-# Optimized code can give gcov different hit counts for a function and its lines.
-lcov --ignore-errors inconsistent \
- --add-tracefile coverage/baseline.lcov --add-tracefile coverage/run.lcov \
- --output-file coverage/combined.lcov
+set -- --add-tracefile coverage/baseline.lcov --add-tracefile coverage/run.lcov
+
+for bundle in build/test-vm/host-bundle-*; do
+ [ -d "$bundle/coverage" ] || continue
+ testing/container/run.sh -v "$PWD/coverage" -- \
+ "$PWD/.github/scripts/coverage.sh" target "$PWD/$bundle"
+ set -- "$@" --add-tracefile coverage/target.lcov
+ break
+done
+
+# Optimized code can give gcov different hit counts for a function and its
+# lines, and the host and the target compile one source with other options.
+lcov --ignore-errors inconsistent "$@" --output-file coverage/combined.lcov
lcov --ignore-errors inconsistent \
--extract coverage/combined.lcov "$PWD/*" --output-file coverage/repo.lcov
lcov --ignore-errors inconsistent,unused \
- --remove coverage/repo.lcov '*/testing/*' '*/vendor/*' '*/build/*' \
- '*/util/version_id.c' --output-file coverage/host.lcov
+ --remove coverage/repo.lcov "$PWD/testing/*" "$PWD/vendor/*" \
+ "$PWD/build/*" "$PWD/util/version_id.c" \
+ --output-file coverage/shirudo.lcov
diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml
index f2a498ca..8bbb0d79 100644
--- a/.github/workflows/coverage.yml
+++ b/.github/workflows/coverage.yml
@@ -92,7 +92,7 @@ jobs:
if: ${{ !cancelled() && steps.setup.outcome == 'success' }}
run: .github/scripts/tests.sh run end2end
- - name: Collect host userspace coverage
+ - name: Merge the counters of the host and the guests
id: collect
if: ${{ !cancelled() && steps.setup.outcome == 'success' }}
run: .github/scripts/coverage.sh
@@ -101,5 +101,5 @@ jobs:
if: ${{ !cancelled() && steps.collect.outcome == 'success' }}
uses: coverallsapp/github-action@cfd0633edbd2411b532b808ba7a8b5e04f76d2c8 # v2.3.4
with:
- file: coverage/host.lcov
+ file: coverage/shirudo.lcov
format: lcov
diff --git a/Documentation/contribute/testing/testing.rst b/Documentation/contribute/testing/testing.rst
index 90b19a8d..c58dbcf9 100644
--- a/Documentation/contribute/testing/testing.rst
+++ b/Documentation/contribute/testing/testing.rst
@@ -77,18 +77,37 @@ Land kernel input changes first. After publishing their bundles, update
the pin in the same change as any shirudo code that requires the new
kernel.
-Host userspace coverage
------------------------
+Coverage
+--------
The ``Coverage`` workflow, :src:`.github/workflows/coverage.yml`, runs after
a push to main, never on a pull request: the ``CI suite`` tests the binaries
as they ship, and no pull request waits on a coverage figure. It builds the
-host userspace C binaries with coverage instrumentation, runs every tier and
-sends an LCOV report to Coveralls, also when a tier fails. Reports exclude
-test code, vendored code, QEMU guests and BPF programs.
+userspace C code with coverage instrumentation, the host's binaries and the
+target the test VMs boot, runs every tier and sends one LCOV report to
+Coveralls, also when a tier fails.
Start the workflow by hand on a branch for the figure of a change before it
-lands.
+lands, or make the report on a host with ``lcov``, from a clean checkout:
+
+.. code-block:: console
+
+ $ make tests COVERAGE=1
+ $ .github/scripts/coverage.sh
+
+``coverage/shirudo.lcov`` is the report. With ``COVERAGE=1`` the bundle
+carries a target built with gcov and the notes of that build, and every
+shirudo process in a guest, from ``init`` in the initrd on, writes its
+counters under ``/run/shirudo-gcov`` when it exits. A guest packs them
+into its result directory before it powers off, a node of the end-to-end
+test when ``run.sh --stop`` asks it to, and :src:`testing/vm/run.sh` keeps
+them in ``build/test-vm/coverage/``. :src:`.github/scripts/coverage.sh`
+reads them in the build container, whose gcov matches the compiler of the
+target, and merges them with the host's.
+
+The report leaves out test code, vendored code and BPF programs, and counts
+nothing for a process that is killed, as the loads that ``t6143`` and
+``t6144`` interrupt are, since gcov writes its counters at exit.
Check the quickstart and quick guides
-------------------------------------
diff --git a/testing/container/packages b/testing/container/packages
index 895690e8..3f85a884 100644
--- a/testing/container/packages
+++ b/testing/container/packages
@@ -10,6 +10,7 @@ e2fsprogs
file
git
gpg
+lcov
libcurl4-openssl-dev
libelf-dev
libltdl-dev