From c60e0134f1e45bd5c748dc4edac4da953251f18b Mon Sep 17 00:00:00 2001 From: Erik Rigtorp Date: Tue, 17 Nov 2026 18:53:02 -0500 Subject: [PATCH] Measure and enforce MC/DC coverage with GCC and CTest --- .github/workflows/ci.yml | 16 +++++++ CMakeLists.txt | 37 ++++++++++++++++ CMakePresets.json | 19 ++++++++ README.md | 10 +++++ src/MPMCQueueTest.cpp | 30 +++++++++++-- src/check_mcdc.py | 93 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 201 insertions(+), 4 deletions(-) create mode 100644 src/check_mcdc.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ad60b37..c7fbd84 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,3 +23,19 @@ jobs: with: python-version: "3.12" - uses: pre-commit/action@v3.0.1 + + coverage: + runs-on: ubuntu-24.04 + steps: + - uses: actions/checkout@v4 + - name: Install GCC 14 + run: | + sudo apt-get update + sudo apt-get install -y --no-install-recommends g++-14 + - name: Configure, build, and test with MC/DC coverage + env: + CXX: g++-14 + run: | + cmake --preset coverage + cmake --build --preset coverage + ctest --preset coverage diff --git a/CMakeLists.txt b/CMakeLists.txt index a413831..c2062ce 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,6 +46,43 @@ if(PROJECT_IS_TOP_LEVEL AND BUILD_TESTING) mpmcqueue_executable(MPMCQueueExample src/MPMCQueueExample.cpp) mpmcqueue_executable(MPMCQueueTest src/MPMCQueueTest.cpp) add_test(NAME MPMCQueueTest COMMAND MPMCQueueTest) + + option(MPMCQueue_COVERAGE "Check MC/DC coverage with gcov" OFF) + if(MPMCQueue_COVERAGE) + if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_VERSION VERSION_LESS 14) + message(FATAL_ERROR "MPMCQueue_COVERAGE requires GCC 14 or newer") + endif() + if(CMAKE_CONFIGURATION_TYPES) + message(FATAL_ERROR "MPMCQueue_COVERAGE requires a single-config generator") + endif() + + target_compile_options(MPMCQueueTest PRIVATE + -O0 --coverage -fcondition-coverage -fprofile-update=atomic + ) + target_link_options(MPMCQueueTest PRIVATE --coverage) + + find_package(Python3 COMPONENTS Interpreter REQUIRED) + get_filename_component(compiler_dir "${CMAKE_CXX_COMPILER}" DIRECTORY) + if(CMAKE_CXX_COMPILER MATCHES "g[+][+](-[0-9]+)$") + set(gcov_name "gcov${CMAKE_MATCH_1}") + else() + set(gcov_name gcov) + endif() + find_program(GCOV_EXECUTABLE NAMES "${gcov_name}" HINTS "${compiler_dir}" REQUIRED) + set(profile "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/MPMCQueueTest.dir/src/MPMCQueueTest.cpp") + add_test(NAME MPMCQueueCoverageReset COMMAND "${CMAKE_COMMAND}" -E rm -f "${profile}.gcda") + add_test(NAME MPMCQueueMCDC + COMMAND ${Python3_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}/src/check_mcdc.py" + --gcno "${profile}.gcno" + --gcov "${GCOV_EXECUTABLE}" + ) + set_tests_properties(MPMCQueueCoverageReset PROPERTIES FIXTURES_SETUP queue_coverage_reset) + set_tests_properties(MPMCQueueTest PROPERTIES + FIXTURES_REQUIRED queue_coverage_reset + FIXTURES_SETUP queue_coverage + ) + set_tests_properties(MPMCQueueMCDC PROPERTIES FIXTURES_REQUIRED queue_coverage DEPENDS MPMCQueueTest) + endif() endif() # ----------------------------------------------------------------------------- diff --git a/CMakePresets.json b/CMakePresets.json index ead7bc8..9d9fca2 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -46,6 +46,15 @@ "cacheVariables": { "CMAKE_CXX_FLAGS": "-fsanitize=thread -fno-omit-frame-pointer" } + }, + { + "name": "coverage", + "displayName": "Debug (GCC MC/DC)", + "inherits": "debug", + "cacheVariables": { + "CMAKE_CXX_STANDARD": "20", + "MPMCQueue_COVERAGE": "ON" + } } ], "buildPresets": [ @@ -72,6 +81,11 @@ "name": "tsan", "inherits": "base", "configurePreset": "tsan" + }, + { + "name": "coverage", + "inherits": "base", + "configurePreset": "coverage" } ], "testPresets": [ @@ -101,6 +115,11 @@ "name": "tsan", "inherits": "base", "configurePreset": "tsan" + }, + { + "name": "coverage", + "inherits": "base", + "configurePreset": "coverage" } ] } diff --git a/README.md b/README.md index 5b4101b..d0ce1e5 100644 --- a/README.md +++ b/README.md @@ -155,6 +155,16 @@ implementation: - A multithreaded fuzz test that all elements are enqueued and dequeued correctly under heavy contention. +To check MC/DC coverage of the queue header, use GCC 14 or newer with its +matching gcov and a single-config CMake generator: + + CXX=g++-14 cmake --preset coverage + cmake --build --preset coverage + ctest --preset coverage + +CTest clears the previous profile before each run. The check combines condition +outcomes across template instantiations and fails on uncovered code or outcomes. + ## TODO - [X] Add allocator supports so that the queue could be used with huge pages and diff --git a/src/MPMCQueueTest.cpp b/src/MPMCQueueTest.cpp index 9fe07f9..d3c2dea 100644 --- a/src/MPMCQueueTest.cpp +++ b/src/MPMCQueueTest.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -271,9 +272,20 @@ int main(int argc, char *argv[]) { assert(throws && state.count == 0); } - // Fuzz test { - const std::size_t numOps = 1000; + bool throws = false; + try { + MPMCQueue q(std::numeric_limits::max()); + } catch (const std::bad_array_new_length &) { + throws = true; + } + assert(throws); + } + + // Exercise both ticket paths under contention, including try-operation + // retries when another producer or consumer claims a ticket first. + for (bool tryOperations : {false, true}) { + const std::size_t numOps = 10000; const std::size_t numThreads = 10; MPMCQueue q(numThreads); std::atomic flag(false); @@ -284,7 +296,12 @@ int main(int argc, char *argv[]) { while (!flag) ; for (auto j = i; j < numOps; j += numThreads) { - q.push(static_cast(j)); + if (tryOperations) { + while (!q.try_push(static_cast(j))) + ; + } else { + q.push(static_cast(j)); + } } })); } @@ -295,7 +312,12 @@ int main(int argc, char *argv[]) { std::uint64_t threadSum = 0; for (auto j = i; j < numOps; j += numThreads) { std::uint64_t v; - q.pop(v); + if (tryOperations) { + while (!q.try_pop(v)) + ; + } else { + q.pop(v); + } threadSum += v; } sum += threadSum; diff --git a/src/check_mcdc.py b/src/check_mcdc.py new file mode 100644 index 0000000..5f5fbc3 --- /dev/null +++ b/src/check_mcdc.py @@ -0,0 +1,93 @@ +#!/usr/bin/env python3 +"""Verify MC/DC coverage of include/rigtorp/MPMCQueue.h using gcov -g.""" + +import argparse +import collections +import re +import subprocess +import sys +import tempfile +from pathlib import Path + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--gcno", type=Path, required=True, help="Path to MPMCQueueTest.cpp.gcno") + parser.add_argument("--gcov", default="gcov", help="gcov executable") + args = parser.parse_args() + + gcno = args.gcno.resolve() + if not gcno.with_suffix(".gcda").is_file(): + parser.error(f"missing profile data for {gcno}; run MPMCQueueTest first") + with tempfile.TemporaryDirectory(prefix="mpmc-mcdc-") as tmpdir: + subprocess.run( + [args.gcov, "-g", str(gcno)], + cwd=tmpdir, + stdout=subprocess.DEVNULL, + check=True, + ) + gcov_file = Path(tmpdir) / "MPMCQueue.h.gcov" + if not gcov_file.exists(): + raise SystemExit(f"Missing {gcov_file}") + lines = gcov_file.read_text().splitlines() + + src = {} + exec_counts = collections.defaultdict(int) + inst_conds = collections.defaultdict(list) + cur_ln = None + cur_inst = None + + for line in lines: + match = re.match(r"^\s*([^:]+):\s*(\d+):(.*)$", line) + if match and not line.startswith("condition"): + cnt, ln, code = match.group(1).strip(), int(match.group(2)), match.group(3) + cur_ln = ln + src.setdefault(ln, code) + if cnt not in ("-", "#####", "====="): + exec_counts[ln] += int(cnt.rstrip("*")) + cur_inst = None + elif line.startswith("condition outcomes covered"): + match2 = re.match(r"condition outcomes covered (\d+)/(\d+)", line) + if match2 and cur_ln is not None: + cur_inst = [int(match2.group(2)), set()] + inst_conds[cur_ln].append(cur_inst) + elif line.startswith("condition ") and cur_inst is not None: + missing = re.fullmatch(r"condition\s+(\d+) not covered \((true|false|true false)\)", line) + if not missing: + raise SystemExit(f"Unexpected gcov output: {line}") + for outcome in missing[2].split(): + cur_inst[1].add((int(missing[1]), outcome)) + + never_exec = { + int(m.group(1)) + for line in lines + for m in [re.match(r"^\s*(?:#####|=====):\s*(\d+):", line)] + if m and exec_counts[int(m.group(1))] == 0 + } + + gaps = {} + total_outcomes = 0 + covered_outcomes = 0 + for ln, insts in sorted(inst_conds.items()): + tot = max(t for t, _ in insts) + common_uncov = set.intersection(*(u for _, u in insts)) + cov = tot - len(common_uncov) + total_outcomes += tot + covered_outcomes += cov + if common_uncov: + gaps[ln] = (cov, tot, src[ln].strip(), sorted(common_uncov)) + + print(f"MPMCQueue.h MC/DC condition outcomes: {covered_outcomes}/{total_outcomes}") + for ln in sorted(never_exec): + print(f"UNEXECUTED L{ln}: {src[ln]}") + for ln, (cov, tot, code, uncov) in gaps.items(): + print(f"CONDITION GAP L{ln} ({cov}/{tot}): {code} -> {uncov}") + + if not total_outcomes: + print("No instrumented conditions found", file=sys.stderr) + if not total_outcomes or never_exec or gaps: + sys.exit(1) + + +if __name__ == "__main__": + main()