Skip to content

Commit bc8a939

Browse files
ElectricalPaulCommit Bot
authored and
Commit Bot
committed
ec: fix make coverage code coverage reporting
Fixed problems that were preventing us from building the unit tests with code coverage testing via `make coverage`. * Changed test_util so that programs will cleanly exit on SIGTERM. * Changed run_host_test to wait for the child process to exit, and only proc.kill() if it times out, so the child process will generate code coverage output files on exit. * Changed Makefile.toolchain to use the --coverage flag for both compile and link. * Changed build.mk and Makefile.rules to exclude certain tests from code coverage because they were causing failures either during the individual stage of code coverage, or generating the overall report. BUG=b:143065231 BRANCH=none TEST=`make coverage` produces results Signed-off-by: Paul Fagerburg <[email protected]> Change-Id: I8575013551ce1dba3fd249cd933a3cf6d110db8d Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2186853 Reviewed-by: Jack Rosenthal <[email protected]>
1 parent 12c3acd commit bc8a939

File tree

5 files changed

+40
-12
lines changed

5 files changed

+40
-12
lines changed

Makefile.rules

+3-1
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ buildfuzztests: $(fuzz-test-targets)
339339

340340
.PHONY: hosttests runhosttests runfuzztests runtests
341341
hosttests: $(host-test-targets)
342+
runhosttests: TEST_FLAG=TEST_HOSTTEST=y
342343
runhosttests: $(run-test-targets)
343344
runfuzztests: $(run-fuzz-test-targets)
344345
runtests: runhosttests runfuzztests
@@ -376,7 +377,7 @@ $(foreach b, $(cts_boards), \
376377
) \
377378
)
378379

379-
cov-test-targets=$(foreach t,$(test-list-host),build/host/$(t).info)
380+
cov-test-targets=$(foreach t,$(cov-test-list-host),build/host/$(t).info)
380381
bldversion=$(shell (./util/getversion.sh ; echo VERSION) | $(CPP) -P -)
381382

382383
# lcov fails when multiple instances run at the same time.
@@ -727,6 +728,7 @@ help:
727728
@echo " tests [BOARD=] - Build all unit tests for a specific board"
728729
@echo " hosttests - Build all host unit tests"
729730
@echo " runhosttests - Build and run all host unit tests"
731+
@echo " coverage - Build and run all host unit tests for code coverage"
730732
@echo " buildfuzztests - Build all host fuzzers"
731733
@echo " runfuzztests - Build and run all host fuzzers for one round"
732734
@echo ""

Makefile.toolchain

+6-5
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ CFLAGS_TEST=$(if $(TEST_BUILD),-DTEST_BUILD=$(EMPTY) \
6868
$(if $(TEST_UBSAN),$(UBSAN_FLAGS)) \
6969
$(if $(TEST_FUZZ),-fsanitize=fuzzer-no-link \
7070
-fno-experimental-new-pass-manager -DTEST_FUZZ=$(EMPTY))
71-
CFLAGS_COVERAGE=$(if $(TEST_COVERAGE),-fprofile-arcs -ftest-coverage \
71+
CFLAGS_COVERAGE=$(if $(TEST_COVERAGE),--coverage \
7272
-DTEST_COVERAGE=$(EMPTY),)
73+
CFLAGS_HOSTTEST=$(if $(TEST_HOSTTEST),-DTEST_HOSTTEST=$(EMPTY),)
7374
CFLAGS_DEFINE=-DOUTDIR=$(out)/$(BLD) -DCHIP=$(CHIP) -DBOARD_TASKFILE=$(_tsk_lst_file) \
7475
-DBOARD=$(BOARD) -DCORE=$(CORE) -DPROJECT=$(PROJECT) \
7576
-DCHIP_VARIANT=$(CHIP_VARIANT) -DCHIP_FAMILY=$(UC_CHIP_FAMILY) \
@@ -82,13 +83,13 @@ CFLAGS_DEFINE=-DOUTDIR=$(out)/$(BLD) -DCHIP=$(CHIP) -DBOARD_TASKFILE=$(_tsk_lst_
8283
-DPROTOBUF_MIN_PROTOC_VERSION=0 \
8384
$(CFLAGS_BASEBOARD)
8485
CPPFLAGS=$(CFLAGS_DEFINE) $(CFLAGS_INCLUDE) $(CFLAGS_TEST) \
85-
$(EXTRA_CFLAGS) $(CFLAGS_COVERAGE) $(LATE_CFLAGS_DEFINE) \
86+
$(EXTRA_CFLAGS) $(CFLAGS_COVERAGE) $(CFLAGS_HOSTTEST) $(LATE_CFLAGS_DEFINE) \
8687
-DSECTION_IS_$(BLD)=$(EMPTY) -DSECTION=$(BLD) $(CPPFLAGS_$(BLD))
8788
BUILD_CPPFLAGS=$(CFLAGS_DEFINE) -Icore/host $(CFLAGS_INCLUDE) $(CFLAGS_TEST) \
88-
$(EXTRA_CFLAGS) $(CFLAGS_COVERAGE) $(LATE_CFLAGS_DEFINE) \
89+
$(EXTRA_CFLAGS) $(CFLAGS_COVERAGE) $(CFLAGS_HOSTTEST) $(LATE_CFLAGS_DEFINE) \
8990
-DSECTION_IS_$(BLD)=$(EMPTY) -DSECTION=$(BLD) $(CPPFLAGS_$(BLD))
9091
HOST_CPPFLAGS=$(CFLAGS_DEFINE) $(CFLAGS_INCLUDE) $(CFLAGS_TEST) \
91-
$(EXTRA_CFLAGS) $(CFLAGS_COVERAGE) $(LATE_CFLAGS_DEFINE) \
92+
$(EXTRA_CFLAGS) $(CFLAGS_COVERAGE) $(CFLAGS_HOSTTEST) $(LATE_CFLAGS_DEFINE) \
9293
-DSECTION_IS_$(BLD)=$(EMPTY) -DSECTION=$(BLD) $(CPPFLAGS_$(BLD))
9394
ifneq ($(BOARD),host)
9495
CPPFLAGS+=-ffreestanding -fno-builtin -nostdinc -nostdlib
@@ -142,7 +143,7 @@ BUILD_LDFLAGS=$(LIBFTDIUSB_LDLIBS)
142143
HOST_LDFLAGS=$(LIBFTDIUSB_LDLIBS)
143144
HOST_TEST_LDFLAGS=-Wl,-T core/host/host_exe.lds -lrt -pthread -rdynamic -lm\
144145
-fuse-ld=bfd \
145-
$(if $(TEST_COVERAGE),-fprofile-arcs,) \
146+
$(if $(TEST_COVERAGE), --coverage,) \
146147
$(if $(TEST_ASAN), -fsanitize=address) \
147148
$(if $(TEST_MSAN), -fsanitize=memory) \
148149
$(if $(TEST_UBSAN), ${UBSAN_FLAGS}) \

common/test_util.c

+9-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
* Test utilities.
66
*/
77

8-
#ifdef TEST_COVERAGE
8+
#if defined(TEST_COVERAGE) || defined(TEST_HOSTTEST)
9+
/* We need signal() and exit() only when building to run on the host. */
910
#include <signal.h>
1011
#include <stdlib.h>
1112
#endif
@@ -46,7 +47,14 @@ void emulator_flush(void)
4647
{
4748
__gcov_flush();
4849
}
50+
#else
51+
void emulator_flush(void)
52+
{
53+
}
54+
#endif
4955

56+
#if defined(TEST_HOSTTEST) || defined(TEST_COVERAGE)
57+
/* Host-based unit tests need to exit(0) when they receive a SIGTERM. */
5058
void test_end_hook(int sig)
5159
{
5260
emulator_flush();
@@ -58,10 +66,6 @@ void register_test_end_hook(void)
5866
signal(SIGTERM, test_end_hook);
5967
}
6068
#else
61-
void emulator_flush(void)
62-
{
63-
}
64-
6569
void register_test_end_hook(void)
6670
{
6771
}

test/build.mk

+15
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,21 @@ test-list-host += x25519
9595
test-list-host += stillness_detector
9696
endif
9797

98+
# Build up the list of coverage test targets based on test-list-host, but
99+
# with some tests excluded because they cause code coverage to fail.
100+
101+
# is_enabled_error is a shell script that does not produce coverage results
102+
cov-dont-test = is_enabled_error
103+
# static_if_error is a shell script that does not produce coverage results
104+
cov-dont-test += static_if_error
105+
# fpsensor: genhtml looks for build/host/fpsensor/cryptoc/util.c
106+
cov-dont-test += fpsensor
107+
# fpsensor_crypto: genhtml looks for build/host/fpsensor_crypto/cryptoc/util.c
108+
cov-dont-test += fpsensor_crypto
109+
# fpsensor_state: genhtml looks for build/host/fpsensor_state/cryptoc/util.c
110+
cov-dont-test += fpsensor_state
111+
cov-test-list-host = $(filter-out $(cov-dont-test), $(test-list-host))
112+
98113
accel_cal-y=accel_cal.o
99114
aes-y=aes.o
100115
base32-y=base32.o

util/run_host_test

+7-1
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,14 @@ def run_test(path, timeout=10):
8181
if proc.poll():
8282
return TestResult.UNEXPECTED_TERMINATION, output_log
8383
finally:
84+
# Check if the process has exited. If not, send it a SIGTERM, wait for it
85+
# to exit, and if it times out, kill the process directly.
8486
if not proc.poll():
85-
proc.kill()
87+
try:
88+
proc.terminate()
89+
proc.wait(timeout)
90+
except subprocess.TimeoutExpired:
91+
proc.kill()
8692

8793

8894
def host_test(test_name):

0 commit comments

Comments
 (0)