Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

feat(ci): MemorySanitizer lane — instrumented libc++/zlib image, full C++ coverage

Martin Vogelc9886d4f25ad
diff --git a/.github/workflows/_test.yml b/.github/workflows/_test.yml
index d453244bc..f635d6fa1 100644
--- a/.github/workflows/_test.yml
+++ b/.github/workflows/_test.yml
@@ -224,6 +224,36 @@ jobs:
path: ${{ github.workspace }}/.ccache
key: ccache-diag-${{ github.ref }}-${{ github.sha }}
+ # MemorySanitizer lane (stage 2; user decision 2026-08-03). Uninitialized
+ # READS are the one memory-error class no other lane covers dynamically.
+ # The image carries MSan-instrumented libc++/libc++abi/libunwind + zlib
+ # (vendored C deps instrument in-tree); scripts/msan.sh drives the build
+ # and the full suite. The image layers are cached via buildx local cache —
+ # a warm run skips the ~30-min libc++ build entirely.
+ test-msan:
+ runs-on: ubuntu-latest
+ timeout-minutes: 180
+ steps:
+ - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
+
+ - name: Buildx cache
+ uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
+ with:
+ path: /tmp/.buildx-msan
+ key: buildx-msan-${{ hashFiles('test-infrastructure/Dockerfile.msan') }}
+
+ - name: Build MSan image (cached layers)
+ run: |
+ docker buildx create --use --name msan-builder
+ docker buildx build \
+ --cache-from type=local,src=/tmp/.buildx-msan \
+ --cache-to type=local,dest=/tmp/.buildx-msan,mode=max \
+ -f test-infrastructure/Dockerfile.msan \
+ -t cbm-msan:ci --load test-infrastructure/
+
+ - name: MSan full suite
+ run: docker run --rm -v "$PWD:/src" -w /src cbm-msan:ci
+
test-tsan:
strategy:
fail-fast: false
diff --git a/Makefile.cbm b/Makefile.cbm
index db1fbfea4..f66b9e1df 100644
--- a/Makefile.cbm
+++ b/Makefile.cbm
@@ -100,7 +100,7 @@ INCREMENTAL_TEST_DEFINE = -DCBM_INCREMENTAL_TEST_API=1
CFLAGS_TEST = $(CFLAGS_COMMON) $(EDITOR_TEST_DEFINES) $(SANITIZED_DEFINE) \
$(KOTLIN_DEDUP_TEST_DEFINE) $(CALL_REFERENCE_LOOKUP_TEST_DEFINE) \
$(INCREMENTAL_TEST_DEFINE) -g -O1 $(SANITIZE)
-CXXFLAGS_TEST = $(CXXFLAGS_COMMON) $(SANITIZED_DEFINE) -g -O1 $(SANITIZE)
+CXXFLAGS_TEST = $(CXXFLAGS_COMMON) $(SANITIZED_DEFINE) -g -O1 $(SANITIZE) $(CXX_STDLIB_FLAGS)
# TSan (can't combine with ASan)
TSAN_SANITIZE = -fsanitize=thread -fno-omit-frame-pointer
@@ -171,9 +171,13 @@ endif
# and redirecting malloc into mimalloc underneath ASan's own interception mixes
# two allocators on the same pointers. Windows keeps its wrap flags everywhere,
# because there the shim is what makes mimalloc own the allocations at all.
-LDFLAGS = -lm -lstdc++ -lpthread -lz $(WIN32_LIBS) $(STATIC_FLAGS) $(MIMALLOC_WRAP_FLAGS) $(ELF_HARDENING_FLAGS)
-LDFLAGS_TEST = -lm -lstdc++ -lpthread -lz $(SANITIZE) $(WIN32_LIBS) $(MIMALLOC_WRAP_FLAGS) $(ELF_HARDENING_FLAGS)
-LDFLAGS_TSAN = -lm -lstdc++ -lpthread -lz $(TSAN_SANITIZE) $(WIN32_LIBS) $(MIMALLOC_WRAP_FLAGS) $(ELF_HARDENING_FLAGS)
+# C++ stdlib selection: MSan builds must link an MSan-instrumented libc++
+# instead of the system libstdc++ (scripts/msan.sh overrides both vars).
+CXX_STDLIB ?= -lstdc++
+CXX_STDLIB_FLAGS ?=
+LDFLAGS = -lm $(CXX_STDLIB) -lpthread -lz $(WIN32_LIBS) $(STATIC_FLAGS) $(MIMALLOC_WRAP_FLAGS) $(ELF_HARDENING_FLAGS)
+LDFLAGS_TEST = -lm $(CXX_STDLIB) -lpthread -lz $(SANITIZE) $(WIN32_LIBS) $(MIMALLOC_WRAP_FLAGS) $(ELF_HARDENING_FLAGS)
+LDFLAGS_TSAN = -lm $(CXX_STDLIB) -lpthread -lz $(TSAN_SANITIZE) $(WIN32_LIBS) $(MIMALLOC_WRAP_FLAGS) $(ELF_HARDENING_FLAGS)
# ── Source files ─────────────────────────────────────────────────
diff --git a/scripts/msan.sh b/scripts/msan.sh
new file mode 100644
index 000000000..06641fb22
--- /dev/null
+++ b/scripts/msan.sh
@@ -0,0 +1,44 @@
+#!/usr/bin/env bash
+# msan.sh — MemorySanitizer lane (stage 2: full coverage incl. the C++ paths).
+#
+# Runs inside the cbm-msan image (test-infrastructure/Dockerfile.msan), which
+# provides MSan-instrumented libc++/libc++abi/libunwind and zlib in /opt/msan.
+# Vendored C deps compile in-tree and are instrumented by the build itself.
+#
+# MSan detects uninitialized READS — the one memory-error class ASan/LSan and
+# the clang-analyzer lane do not cover dynamically. halt_on_error stays ON:
+# a finding is a bug (or an interceptor gap to triage), never board data.
+#
+# Usage: scripts/msan.sh [suite ...] (default: full suite)
+set -euo pipefail
+
+ROOT="$(cd "$(dirname "$0")/.." && pwd)"
+cd "$ROOT"
+
+MSAN_PREFIX="${MSAN_PREFIX:-/opt/msan}"
+if [ ! -d "$MSAN_PREFIX/lib" ]; then
+ echo "FATAL: MSan-instrumented runtime not found at $MSAN_PREFIX (build test-infrastructure/Dockerfile.msan)" >&2
+ exit 1
+fi
+
+# -isystem: the image deliberately has no system zlib (so the instrumented
+# one cannot be shadowed); its headers live under the MSan prefix.
+MSAN_SAN="-fsanitize=memory -fsanitize-memory-track-origins=2 -fno-omit-frame-pointer -isystem $MSAN_PREFIX/include"
+
+# Always clean: make does not encode flags into dependencies, so a build dir
+# populated under different stdlib/sanitizer flags silently mixes objects
+# (observed: a stage-1 probe's libstdc++ objects surviving into the libc++
+# lane and producing an unattributable report). Correctness over speed here.
+make -f Makefile.cbm clean-c BUILD_DIR=build/msan >/dev/null 2>&1 || true
+
+make -j"$(nproc)" -f Makefile.cbm build/msan/test-runner \
+ CC=clang CXX=clang++ BUILD_DIR=build/msan \
+ SANITIZE="$MSAN_SAN" \
+ CXX_STDLIB_FLAGS="-stdlib=libc++ -nostdinc++ -isystem $MSAN_PREFIX/include/c++/v1" \
+ CXX_STDLIB="-L$MSAN_PREFIX/lib -Wl,-rpath,$MSAN_PREFIX/lib -lc++ -lc++abi"
+
+export MSAN_OPTIONS="${MSAN_OPTIONS:-halt_on_error=1:print_stats=0}"
+export LD_LIBRARY_PATH="$MSAN_PREFIX/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
+
+echo "=== MSan lane: $(clang --version | head -1) ==="
+./build/msan/test-runner "$@"
diff --git a/test-infrastructure/Dockerfile.msan b/test-infrastructure/Dockerfile.msan
new file mode 100644
index 000000000..c5e7f9ae1
--- /dev/null
+++ b/test-infrastructure/Dockerfile.msan
@@ -0,0 +1,65 @@
+# MemorySanitizer lane image — stage-2 of the memory-diagnostics program.
+#
+# MSan requires EVERY linked library to be MSan-instrumented, or reads of
+# memory those libraries wrote report as uninitialized. The vendored deps
+# (sqlite3, lz4, zstd, tree-sitter runtime, grammars) compile in-tree and get
+# instrumented for free; the two external links -lstdc++/-lz do not. This
+# image provides an MSan-instrumented libc++/libc++abi/libunwind and zlib in
+# /opt/msan, and scripts/msan.sh points the build at them.
+#
+# Sources are pinned by release tag from their canonical upstreams (same
+# precedent as the cppcheck 2.20.0 build in _lint.yml).
+#
+# Build: docker build -f test-infrastructure/Dockerfile.msan -t cbm-msan test-infrastructure/
+# Run: see scripts/msan.sh (driven via docker-compose service test-msan)
+
+# Same pinned base as the primary test image — bump deliberately, never to a tag.
+FROM ubuntu:noble@sha256:4fbb8e6a8395de5a7550b33509421a2bafbc0aab6c06ba2cef9ebffbc7092d90
+
+RUN apt-get update && apt-get install -y --no-install-recommends \
+ clang \
+ cmake \
+ ninja-build \
+ make \
+ python3 \
+ git \
+ curl \
+ zsh \
+ ccache \
+ ca-certificates \
+ && rm -rf /var/lib/apt/lists/*
+
+# libc++ + libc++abi + libunwind with MemoryWithOrigins.
+# release/18.x matches the noble apt clang major (18); the runtimes build is
+# the LLVM-documented procedure for MSan'd C++ standard libraries.
+RUN git clone --depth 1 --branch llvmorg-18.1.8 \
+ https://github.com/llvm/llvm-project.git /tmp/llvm-project \
+ && cmake -G Ninja -S /tmp/llvm-project/runtimes -B /tmp/llvm-msan \
+ -DCMAKE_BUILD_TYPE=Release \
+ -DCMAKE_C_COMPILER=clang \
+ -DCMAKE_CXX_COMPILER=clang++ \
+ -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind" \
+ -DLLVM_USE_SANITIZER=MemoryWithOrigins \
+ -DCMAKE_INSTALL_PREFIX=/opt/msan \
+ && ninja -C /tmp/llvm-msan cxx cxxabi unwind \
+ && ninja -C /tmp/llvm-msan install-cxx install-cxxabi install-unwind \
+ && rm -rf /tmp/llvm-project /tmp/llvm-msan
+
+# zlib with MSan (static, so the runner needs no runtime path for it).
+RUN git clone --depth 1 --branch v1.3.1 https://github.com/madler/zlib.git /tmp/zlib \
+ && cd /tmp/zlib \
+ && CC=clang CFLAGS="-fsanitize=memory -fsanitize-memory-track-origins=2 -fno-omit-frame-pointer -O2" \
+ ./configure --prefix=/opt/msan --static \
+ && make -j"$(nproc)" install \
+ && rm -rf /tmp/zlib
+
+# Symbolizer + MSan runtime in their own (last) layer so adding tools never
+# invalidates the expensive libc++ build layers above. libclang-rt-18-dev is
+# a Recommends of clang on noble, which --no-install-recommends drops — the
+# link then fails to find libclang_rt.msan-*.a.
+RUN apt-get update && apt-get install -y --no-install-recommends llvm libclang-rt-18-dev \
+ && rm -rf /var/lib/apt/lists/*
+ENV MSAN_SYMBOLIZER_PATH=/usr/lib/llvm-18/bin/llvm-symbolizer
+
+WORKDIR /src
+ENTRYPOINT ["scripts/msan.sh"]
diff --git a/test-infrastructure/docker-compose.yml b/test-infrastructure/docker-compose.yml
index 21a18b8f5..0b6acfb34 100644
--- a/test-infrastructure/docker-compose.yml
+++ b/test-infrastructure/docker-compose.yml
@@ -47,6 +47,28 @@ services:
# Overrides the default test.sh entrypoint to build + run the widened TSan
# suite set. /usr/lib/ccache is first on PATH in the image, so CC=gcc is
# ccache-masqueraded automatically (same as the ASan legs).
+ # MemorySanitizer lane (stage 2): MSan-instrumented libc++/zlib image, full
+ # coverage including the C++ preprocessing paths. See scripts/msan.sh.
+ test-msan:
+ build:
+ context: ..
+ dockerfile: test-infrastructure/Dockerfile.msan
+ platform: linux/arm64
+ init: true
+ volumes:
+ - ..:/src
+ - cbm-build:/src/build
+ - cbm-fixture-cache:/root/.cache/cbm-test-fixtures
+ - cbm-ccache-arm64:/root/.ccache
+ environment:
+ CCACHE_DIR: /root/.ccache
+ CCACHE_MAXSIZE: 1500M
+ # MSan's shadow layout has the same aarch64 high-entropy-ASLR problem as
+ # TSan (personality() blocked by the default seccomp profile) — same
+ # remedy, same safety argument: the container runs only our own tests.
+ security_opt: ["seccomp=unconfined"]
+ entrypoint: ["setarch", "-R", "bash", "scripts/msan.sh"]
+
test-tsan:
build:
context: ..