Skip to content

eBPF-based security platform

Makefile, testing/vm: build the test bundle's target with coverage

John Fastabend93f5d7818c30
diff --git a/Makefile.shirudo b/Makefile.shirudo
index fd04f7a5..4f74ae89 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -149,9 +149,10 @@ CFLAGS_BASE := -O2 $(DEBUG_CFLAGS) -std=gnu11 $(WARNINGS) $(CC_WARNINGS) \
-DVMLINUX_PATH='"$(SHIRUDO_VMLINUX_PATH)"' \
-DCLANG_PATH='"$(SHIRUDO_CLANG_PATH)"'
-# GCC coverage is collected from the host build on a clean checkout.
+# GCC coverage is collected from a build on a clean checkout: the host's
+# counters from the checkout, the target's from the test VMs.
COVERAGE ?= 0
-ifeq ($(PROFILE):$(COVERAGE),operator:1)
+ifeq ($(COVERAGE),1)
CFLAGS_BASE += --coverage -fprofile-abs-path -fprofile-update=atomic
endif
diff --git a/testing/Makefile b/testing/Makefile
index db15d398..44fc4aaa 100644
--- a/testing/Makefile
+++ b/testing/Makefile
@@ -147,6 +147,14 @@ TEST_VM_BUNDLE_INPUTS := $(sort \
$(TEST_VM_BUNDLE_SOURCES) $(TEST_VM_BUNDLE_ARTIFACTS) \
$(if $(strip $(TEST_VM_PACKAGE_CERT)),$(TEST_VM_PACKAGE_CERT)))
+# A bundle carries coverage/ when its target was built with COVERAGE=1,
+# and one built the other way is built again.
+COVERAGE ?= 0
+TEST_VM_BUNDLE_COVERAGE := $(if $(wildcard $(TEST_VM_BUNDLE_DIR)/coverage),1,0)
+TEST_VM_BUNDLE_REBUILD := $(if $(filter $(COVERAGE),$(TEST_VM_BUNDLE_COVERAGE)),,test-vm-bundle-rebuild)
+.PHONY: test-vm-bundle-rebuild
+test-vm-bundle-rebuild:
+
$(ROOT)/config.mk: $(ROOT)/Makefile.config
+$(Q)$(MAKE) -C $(ROOT) config
@@ -154,10 +162,10 @@ $(ROOT)/config.mk: $(ROOT)/Makefile.config
test-vm-bundle: require-vm-kernel
+$(Q)$(MAKE) -f "$(ROOT)/testing/Makefile" "$(TEST_VM_BUNDLE_STAMP)"
-$(TEST_VM_BUNDLE_STAMP): $(TEST_VM_BUNDLE_INPUTS)
+$(TEST_VM_BUNDLE_STAMP): $(TEST_VM_BUNDLE_INPUTS) $(TEST_VM_BUNDLE_REBUILD)
$(Q)mkdir -p $(dir $(BUILD_LOCK))
$(Q)TEST_VM_MODE="$(TEST_VM_MODE)" TEST_VM_INITRD="$(TEST_VM_INITRD)" \
- BUILD_CONTAINER="$(BUILD_CONTAINER)" \
+ BUILD_CONTAINER="$(BUILD_CONTAINER)" COVERAGE="$(COVERAGE)" \
TEST_VM_HOST_BUNDLE="$(abspath $(TEST_VM_BUNDLE_DIR))" \
TEST_VM_WORK_DIR="$(abspath $(TEST_VM_WORK_DIR))" \
TEST_VM_HOST_PRIVATE_DIR="$(abspath $(TEST_VM_HOST_PRIVATE_DIR))" \
diff --git a/testing/vm/build-bundle-payload.sh b/testing/vm/build-bundle-payload.sh
index a6bd2dde..a43e414b 100755
--- a/testing/vm/build-bundle-payload.sh
+++ b/testing/vm/build-bundle-payload.sh
@@ -15,6 +15,9 @@ REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
# shellcheck source=testing/vm/arch.sh
. "${REPO_ROOT}/testing/vm/arch.sh"
PACKAGE_CERT="${TEST_VM_PACKAGE_CERT:-}"
+# The container is given TEST_VM_COVERAGE alone, so that only the target
+# is instrumented there; a build in the checkout follows the host's.
+COVERAGE="${TEST_VM_COVERAGE:-${COVERAGE:-0}}"
log()
{
@@ -74,11 +77,24 @@ make -C "$REPO_ROOT/testing" -j"$jobs" test-helpers
# t7108 reaches the tree walk limits: the guest runs a target that walks
# 4 operations back from a cursor past 0 and 6 from 0. t7110 reaches past
# the keepalive window, which the target lowers to the last 7 operations.
-make -C "$REPO_ROOT" -j"$jobs" \
- TREE_LIMIT_CFLAGS="-DTREE_MAX_WALK_OPS=4 -DTREE_MAX_JOIN_OPS=6 -DKEEPALIVE_WINDOW_OPS=7" \
- DESTDIR="$TMP_DIR/root" install-target
+tree_limits="-DTREE_MAX_WALK_OPS=4 -DTREE_MAX_JOIN_OPS=6 -DKEEPALIVE_WINDOW_OPS=7"
+make -C "$REPO_ROOT" -j"$jobs" TREE_LIMIT_CFLAGS="$tree_limits" \
+ COVERAGE="$COVERAGE" DESTDIR="$TMP_DIR/root" install-target
+# shirudo-image-fault links the objects of the target as they are: other
+# options would compile some again, and under gcov a guest would then have
+# two builds of one object write over each other's counters.
make -C "$REPO_ROOT" -j"$jobs" -f Makefile.shirudo \
+ TREE_LIMIT_CFLAGS="$tree_limits" COVERAGE="$COVERAGE" \
PROFILE=target CONFIG_MK=config-target.mk verity-test-helpers
+if [[ "$COVERAGE" == 1 ]]; then
+ # A guest's counters name the objects by the path they were built
+ # at, and gcov reads them beside the notes of that build.
+ install -d "$TMP_DIR/coverage"
+ (cd "$REPO_ROOT" && find build/target build/verity-tests \
+ -name '*.gcno' -print0 |
+ tar --null --files-from=- -cf "$TMP_DIR/coverage/gcno.tar")
+ printf '%s\n' "$REPO_ROOT" >"$TMP_DIR/coverage/srcdir"
+fi
install -d "$TMP_DIR/test-helpers/testing/helper" \
"$TMP_DIR/test-helpers/testing/fixtures" \
diff --git a/testing/vm/build-bundle.sh b/testing/vm/build-bundle.sh
index a55f16e3..934a7f81 100755
--- a/testing/vm/build-bundle.sh
+++ b/testing/vm/build-bundle.sh
@@ -338,7 +338,7 @@ prepare_runtime_image()
bundle_is_current()
{
- local input
+ local input built=0
[[ -s "$BUNDLE_DIR/test-mode" &&
"$(cat "$BUNDLE_DIR/test-mode")" == "$TEST_VM_MODE" ]] || return 1
@@ -351,6 +351,8 @@ bundle_is_current()
"$BUNDLE_DIR/fixtures/cert.der" || return 1
[[ "$(<"$BUNDLE_DIR/runtime-image-key")" == "$RUNTIME_KEY" ]] || return 1
[[ -s "$RUNTIME_IMAGE" ]] || return 1
+ [[ -d "$BUNDLE_DIR/coverage" ]] && built=1
+ [[ "$built" == "${COVERAGE:-0}" ]] || return 1
for input in $STAMP_INPUTS; do
[[ -e "$input" ]] || return 1
[[ "$input" -nt "$STAMP_FILE" ]] && return 1
@@ -365,7 +367,7 @@ bundle_is_current()
build_payload_in_container()
{
local container="$REPO_ROOT/testing/container/run.sh" deps name
- local -a pass=(-e TARGET_DEPS_DIR -e TEST_VM_ARCH)
+ local -a pass=(-e TARGET_DEPS_DIR -e TEST_VM_ARCH -e TEST_VM_COVERAGE)
deps="$WORK_DIR/cache/target-deps/$({
"$container" --image
@@ -382,7 +384,7 @@ build_payload_in_container()
pass+=(-e "$name")
done
log "building the payload in $("$container" --image)"
- TARGET_DEPS_DIR="$deps" "$container" -v "$TMP_DIR" -v "$FIXTURE_WORK" \
+ TARGET_DEPS_DIR="$deps" TEST_VM_COVERAGE="${COVERAGE:-0}" "$container" -v "$TMP_DIR" -v "$FIXTURE_WORK" \
-v "$deps" "${pass[@]}" -- \
"$REPO_ROOT/testing/vm/build-bundle-payload.sh" --from-copy \
"$FIXTURE_WORK/inputs/source-files" "$TMP_DIR" "$FIXTURE_WORK"