Repository navigation
Fix out-of-bounds reads in Numo::Bit#mean, var, stddev and rms - #50
Open
Watson1978 wants to merge 3 commits into
Open
Watson1978 wants to merge 3 commits into
Watson1978 wants to merge 3 commits into
Conversation
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>
Numo::Bit#mean, var, stddev and rms on viewsNumo::Bit#mean, var, stddev and rms
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Purpose
Numo::Bit#mean,var,stddevandrmsread past the array for some views and for some axes. They return values that change between runs, or crash.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::Bitthey 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 coveraxis: [0, 2]on a contiguous array, so this change fixes the copy itself.Testing
I compared the four methods with the same calculation on
Numo::DFloatfor 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_mathandtest_ractorall pass on Ruby 4.0.10.Related Issues
None.
🤖 Generated with Claude Code