Skip to content

Fix out-of-bounds reads in Numo::Bit#mean, var, stddev and rms - #50

Open
Watson1978 wants to merge 3 commits into
yoshoku:mainfrom
Watson1978:fix-bit-stat-view
Open

Watson1978 wants to merge 3 commits into
yoshoku:mainfrom
Watson1978:fix-bit-stat-view

Conversation

@Watson1978

@Watson1978 Watson1978 commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Purpose

Numo::Bit#mean, var, stddev and rms read past the array for some views and for some axes. They return values that change between runs, or crash.

Numo::Bit.new(1000, 1000).fill(1).transpose.mean  # [BUG] Segmentation fault
Numo::Bit.new(1, 2**27).fill(1).mean              # [BUG] Segmentation fault
Numo::Bit[1, 0, 1, 0][[3, 1, 2, 0]].mean           # 0.0, 0.25, 0.5, 0.75 or 1.0 (expected 0.5)

This happens for an index view, for a view that is not contiguous and is reduced over more than one axis, for axes that are not next to each other (axis: [0, 2]), and for an array with an axis of length 1, such as a row vector.

Summary of Changes

When the iterator cannot step through an argument, ndloop copies it to a buffer first. The copy used the positions, steps, indices and element size as bytes, but for Numo::Bit they count bits. These four methods are the only ones that pass a Bit argument through this copy.

The buffer now records whether its argument is a Numo::Bit. A Bit buffer is sized in words and copied bit by bit. The copy for other types does not change. The copy back from a buffer handles Bit the same way, although no current method passes a Bit output through a buffer.

Cumo avoids the copy for these methods with an indexer loop and a Bit copy of index views in its bit_stat.c. That does not cover axis: [0, 2] on a contiguous array, so this change fixes the copy itself.

Testing

  • Tests pass locally
  • Manual testing completed
Numo::Bit.new(1000, 1000).fill(1).transpose.mean  # 1.0
Numo::Bit.new(1, 2**27).fill(1).mean              # 1.0
Numo::Bit[1, 0, 1, 0][[3, 1, 2, 0]].mean           # 0.5

I compared the four methods with the same calculation on Numo::DFloat for random shapes, views (transpose, index arrays, steps, ranges, reverse) and axes. Before the fix, 360 of 1,600 results differed. After the fix, none of 9,600 differ. With Ruby 4.0.9 built with AddressSanitizer, 3,600 results match and nothing is reported.

The new test fails on the code before the fix. test_narray, test_bit, test_extra, test_math and test_ractor all pass on Ruby 4.0.10.

Related Issues

None.

🤖 Generated with Claude Code

Watson1978 and others added 2 commits December 2, 2026 19:30
When an argument cannot be stepped through by the iterator, ndloop
copies it to a buffer. The copy used the positions and the element
size as bytes, but for Numo::Bit they count bits. Numo::Bit#mean, var,
stddev and rms take this path for an index view, for a view reduced
over more than one axis that is not contiguous, for axes that are not
next to each other, and for an array with an axis of length 1 such as
a row vector. They read past the array and returned values that
changed between runs, or crashed, as in
Numo::Bit.new(1000, 1000).fill(1).transpose.mean and
Numo::Bit.new(1, 2**27).fill(1).mean.

Copy a Bit argument bit by bit and size its buffer in words.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Watson1978 Watson1978 changed the title Fix out-of-bounds reads in Numo::Bit#mean, var, stddev and rms on views Fix out-of-bounds reads in Numo::Bit#mean, var, stddev and rms Dec 2, 2026
The previous pattern gave the same counts in every slice of axis 0,
so the test still passed when the index arrays were ignored. Use a
pattern whose slices differ, and add a reversed view and a transposed
view reduced over two axes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant