Skip to content

Link the gcov runtime into coverage-instrumented unit tests - #601

Merged
fcostaoliveira merged 2 commits into
masterfrom
fix/coverage-make-check-link
Oct 14, 2026
Merged

fcostaoliveira merged 2 commits into
masterfrom
fix/coverage-make-check-link

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Oct 13, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

With ./configure --enable-code-coverage, make check fails at link time. socket_options_test, hdr_histogram_test and cluster_transaction_test all report undefined __gcov_init / __gcov_merge_add / __gcov_exit. The memtier_benchmark binary still builds, and CI never runs make check in a coverage build, so nobody noticed.

Root cause

The vendored m4/ax_code_coverage.m4 is #serial 15. It sets CODE_COVERAGE_LDFLAGS="-lgcov" and never defines CODE_COVERAGE_LIBS, so two things follow:

The three tests compile with $(memtier_benchmark_CPPFLAGS), which carries -fprofile-arcs -ftest-coverage. socket_options_test and cluster_transaction_test also link the instrumented memtier_benchmark-*.o objects. None of them gets memtier_benchmark_LDFLAGS.

Fix

  • In memtier_benchmark_LDADD and hdr_histogram_test_LDADD, replace the undefined $(CODE_COVERAGE_LIBS) with $(CODE_COVERAGE_LDFLAGS).
  • socket_options_test and cluster_transaction_test already reuse $(memtier_benchmark_LDADD), so they inherit it. Future tests that reuse the instrumented objects will too.
  • Drop the now-redundant memtier_benchmark_LDFLAGS. -lgcov now follows the objects whether or not libtool is present.
  • Add '*/tests/*' to CODE_COVERAGE_IGNORE_PATTERN so the unit test sources do not inflate the coverage report.
  • Add a make check step to the coverage-ubuntu CI job (Ubuntu 22.04, lcov 1.14) so this can't regress. The unit tests also add their gcov data to the captured report.

Alternatives considered:

  • Per-test <test>_LDFLAGS = $(CODE_COVERAGE_LDFLAGS). This works with libtool, but every new test has to remember it, and it keeps -lgcov before the objects.
  • Updating the vendored macro to the latest upstream ax_code_coverage.m4 (serial 37). This does not fix the bug. The maintainer reviewer tested it: its configure check looks for _gcov_init, but libgcov only exports __gcov_init, so CODE_COVERAGE_LIBS comes out empty. That serial also drops CODE_COVERAGE_LDFLAGS, so even memtier_benchmark fails to link.

Test evidence

Docker ubuntu:22.04 (gcc 11.4, lcov 1.14, same packages as the CI coverage job plus libtool), ./configure --enable-code-coverage && make -j8 && make check, repeated 3 times per tree:

Tree Runs Build make check
master e4d14e8 3/3 ok rc=2: socket_options_test, hdr_histogram_test, cluster_transaction_test fail to link (60 / 18 / 63 undefined __gcov refs)
this PR 3/3 ok rc=0, 5/5 PASS
this PR, no coverage (./configure) 3/3 ok rc=0, 5/5 PASS
this PR, coverage, no libtool 1/1 ok rc=0, 5/5 PASS (master fails to link memtier_benchmark itself here)

Coverage report (rebased on 6b6d6df, which includes #594): with '*/tests/*' added to CODE_COVERAGE_IGNORE_PATTERN, make code-coverage-capture lists 84 SF: entries, none under tests/, and make check still passes 5/5. Without it, the capture lists 87 entries, including tests/unit/{cluster_transaction,hdr_histogram,socket_options}_test.cpp.

The result is deterministic: 0 flaky runs. make code-coverage-capture also succeeds after make check. The new CI step is the regression test: it fails on master and passes with this change.

Fixes #595

Affected versions

2.5.2 and master. 2.5.1 and earlier are not affected.

Found during the build review of #583.

🤖 Generated with Claude Code


Note

Low Risk
Build/CI and coverage linking only; no runtime behavior changes to the benchmark binary.

Overview
Fixes coverage builds where make check failed to link instrumented unit tests (undefined reference to __gcov_*) because $(CODE_COVERAGE_LIBS) is empty with the vendored ax_code_coverage macro.

Makefile now links libgcov via $(CODE_COVERAGE_LDFLAGS) on memtier_benchmark_LDADD and hdr_histogram_test_LDADD (so tests that reuse those LDADD lines get the runtime after the objects), drops the redundant memtier_benchmark_LDFLAGS, and excludes tests/ from coverage capture via CODE_COVERAGE_IGNORE_PATTERN.

CI runs make check in the coverage-ubuntu job right after the instrumented build so this cannot regress.

Reviewed by Cursor Bugbot for commit 1d993ec. Bugbot is set up for automated code reviews on this repo. Configure here.

fcostaoliveira and others added 2 commits October 14, 2026 03:18
With --enable-code-coverage, make check failed to link socket_options_test,
hdr_histogram_test and cluster_transaction_test with undefined __gcov_init,
__gcov_merge_add and __gcov_exit. These tests compile with
memtier_benchmark_CPPFLAGS, which carries the coverage flags, and two of them
link the instrumented memtier_benchmark objects, but none links libgcov.

The vendored AX_CODE_COVERAGE (serial 15) puts -lgcov in
CODE_COVERAGE_LDFLAGS and never defines CODE_COVERAGE_LIBS. The
$(CODE_COVERAGE_LIBS) in memtier_benchmark_LDADD, and the one #564 added to
hdr_histogram_test_LDADD, therefore expand to nothing. Only the main binary
got the runtime, through memtier_benchmark_LDFLAGS. That put -lgcov before the
objects, so it also depended on libtool reordering the link line.

Use $(CODE_COVERAGE_LDFLAGS) in both LDADD lists in place of the undefined
variable. The tests that reuse memtier_benchmark_LDADD inherit it,
memtier_benchmark_LDFLAGS is no longer needed, and -lgcov now follows the
objects. Run make check in the coverage CI job so this can't regress.

Fixes #595

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Now that the coverage CI job runs make check, the instrumented unit test
sources (tests/unit/*.cpp) end up in the lcov capture and inflate the
reported coverage. Ignore them like the vendored HdrHistogram sources.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

make check fails to link unit tests in --enable-code-coverage builds (undefined __gcov_init)

1 participant