Skip to content

Fix LIL values counter drift and set_fill_value data loss - #285

Merged
vkutuev merged 3 commits into
mainfrom
cpp-fixes
Nov 28, 2026
Merged

vkutuev merged 3 commits into
mainfrom
cpp-fixes

Conversation

@vkutuev

@vkutuev vkutuev commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

Short description

Fixes two silent data-corruption bugs in the C++ core. Both were found while
building the Python test suite for the graph-algorithms work (PR #282), which
exercised code paths the existing C++ tests never covered.

1. Out-of-bounds write (heap corruption / segfault) after mxm,
mxmT_masked, or COO→LIL conversion.

CpuLil<T> tracks the total number of stored entries in a values counter.
The kernels Algo_mxm_cpu and Algo_mxmT_masked_cpu and the
cpu_coo_to_lil converter append entries to LIL rows without updating this
counter
. The Lil→Coo and Lil→Csr conversions size their output buffers from
the counter (cpu_coo_resize(lil->values, ...),
cpu_csr_resize(n_rows, lil->values, ...)), so a stale (too small) counter
makes them write past the end of the allocation.

Repro: exec_mxm(R, A, B, ...) followed by any read-back of R
(R->read(...), to_lists() from Python, ...) crashes with a segfault.

2. set_fill_value silently discards all explicit entries.

Both TVector<T>::set_fill_value and TMatrix<T>::set_fill_value called
m_storage.invalidate(), which marks every storage format invalid. The next
read rebuilds a representation from scratch — starting empty — so all data is
lost:

v.set(0, 10);
v.set_fill_value(-1);
v.get(0); // returns -1 instead of 10

Only dense representations actually materialize the fill value; sparse ones
(Lil/Dok/Coo/Csr) store explicit entries and are unaffected by it, so
invalidating them loses data for no reason.

List of changes

  • src/cpu/cpu_mxm.hpp, src/cpu/cpu_mxmT_masked.hpp,
    src/cpu/cpu_format_coo.hpp — keep the LIL values counter in sync with
    the actual row contents (increment per appended entry, assign before
    return), the same pattern already used by the kron, eadd, emult, transpose,
    and vxm kernels. Side effect: kron (computes the counter arithmetically)
    and transpose (copies it) now also produce correct counters when fed
    affected inputs.
  • src/core/tvector.hpp, src/core/tdecoration.hpp —
    TVector::set_fill_value now invalidates only the dense representations
    (CpuDense, plus AccDense in OpenCL builds), converting the data to COO
    first when no sparse representation is valid. Adds per-format
    invalidate(F) to TDecorationStorage.
  • src/core/tmatrix.hpp — TMatrix::set_fill_value no longer invalidates any
    representation: all matrix formats are sparse and the fill value is applied
    on read, so nothing can be lost.
  • tests/test_mxm.cpp, tests/test_mxmT.cpp, tests/test_vector.cpp,
    tests/test_matrix.cpp — four regression tests (see Testing strategy).

Type of changes

  • bug-fix (change which fixes some issue)
  • new-feature (change which proposes new feature or functionality)
  • breaking-change (change which brakes compatibility of api or etc.)

Changes aspect

  • unit-tests for C/C++ code added
  • benchmark for C/C++ code added
  • workflow tests to .github scripts added
  • C bindings to C++ code added
  • Python bindings to C code added
  • documentation to C/C++ code added
  • documentation to Python code added

Testing strategy

Four regression tests, each failing (crash or wrong value) before the fix and
passing after:

  • mxm.read_after_exec — strict nnz check after reading back an mxm result
    (segfaults pre-fix).
  • mxmT_masked.read_after_exec — entry-wise check, tolerant of explicit
    zeros kept by the OpenCL kernel.
  • vector.set_fill_value_preserves_data — entries survive
    set_fill_value and a round trip through the dense representation.
  • matrix.set_fill_value_preserves_data — entries survive
    set_fill_value and round trips through Lil/Dok/Coo/Csr.

Full C++ test suite verified locally on three backends: CPU (no OpenCL),
Intel iGPU (OpenCL), and PoCL (CPU OpenCL) — all green.

Background

The stale-counter bug means any code that multiplies matrices on the CPU
backend and then reads the result back was already crashing or corrupting
memory; the set_fill_value bug means any code that changed the fill value of
a populated vector/matrix silently lost its data.

Addition context

This PR is a prerequisite for #282 (Python graph algorithms): that PR's tests
exercise mxm read-back and fill-value semantics, so they fail without these
fixes.

The mxm and mxmT_masked CPU kernels append entries to LIL rows directly
without updating the 'values' counter, and the Coo -> Lil conversion has
the same flaw. Downstream consumers size their output from the counter
(e.g. Lil -> Coo in Matrix::read), so a stale counter leads to
out-of-bounds writes and segfaults when reading back the result.

Maintain the counter in all three places, following the pattern already
used by the kron, eadd, emult and transpose kernels. Add regression
tests that read the mxm/mxmT results back through MemView.
TVector::set_fill_value invalidated every storage format, so the next
read rebuilt an empty representation and silently discarded all explicit
entries. Only dense representations materialize the fill value; sparse
ones (Coo, Dok) store explicit entries and are unaffected. Invalidate
just the dense formats, converting the data to COO first when no sparse
representation is valid.
TMatrix::set_fill_value invalidated every storage format, so the next
read rebuilt a representation from scratch and silently lost all
explicit entries. All matrix formats are sparse and never materialize
the fill value in storage (it is applied on read), so no
representation needs to be invalidated.

Regression test: matrix.set_fill_value_preserves_data verifies entries
survive the call and round trips through Lil/Dok/Coo/Csr.
@vkutuev
vkutuev merged commit b7d2159 into main Nov 28, 2026
14 checks passed
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.

2 participants