Repository navigation
Fix out-of-bounds reads and wrong values when storing a Ruby Array of NArrays - #44
Conversation
loop_store_subnarray reuses the iterator of argument 1 for each sub-narray. ndloop_set_stepidx skips an outer axis of length 1, so that axis kept the step and the index of the previous sub-narray. The store then read outside the new sub-narray and wrote heap data into the destination. The check for an outer axis that a sub-narray does not reach ran only when the loop entered that axis, and the flag was reset for every row. The later rows of that axis were read from past the end of the sub-narray instead of being zero-filled. The loop also read the index array of such an axis past its end. An empty sub-narray has no data pointer, so the store template took the flag for a value. It stored the flag, or raised TypeError or NoMethodError. Reset the iterator of argument 1 for each sub-narray. Track per axis whether the sub-narray reaches the row, and do not move the position where it does not. Pass the number of elements of the row through shape[0], and tell a sub-narray row by its value in the store template. Clear the value after the rows of a sub-narray. It can be a temporary array from a cast, and ndloop_release reads it if a later row raises. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
/opencode review |
|
I reviewed the diff (static analysis plus a local build and reproduction of both examples from the PR description, which now produce the correct values on Ruby 3.2). The extension compiles cleanly, and the three fixes in I traced the control/data flow of Findings1. (Low) 2. (Low) 3. (Nit) Reset range expression — ext/numo/narray/ndloop.c:1546 4. (Nit) The 5. (Info) Related limitation remains, pre-existing — verified against both 6. (Nit, tests) Two coverage gaps worth considering — test/test_narray.rb
The tests as written are good: they fail against pre-fix code (per the PR), cover all dtypes plus Bit explicitly, and the outer-axis-short cases give the |
Purpose
After #27,
storewith a Ruby Array of NArrays still reads outside a sub-narray in some cases.NArray.castandNArray[]take the same path. The result holds heap data or values from outside the sub-narray, or the call raises.Summary of Changes
All three come from how
loop_store_subnarrayhands each row to the store template.ndloop_set_stepidxskips an outer axis of length 1, so that axis kept the step and the index of the previous sub-narray. The iterator of argument 1 is now reset for each sub-narray.trueorfalseflag for a value. It stored the flag for RObject and could store 1 for Bit, and raisedTypeErrororNoMethodErrorfor the other types. The number of elements of the row now goes throughshape[0], and the template tells a sub-narray row by its value.The sub-narray is no longer left in the loop argument after its rows. It can be a temporary array from a cast, and
ndloop_releasewould read it after it was collected if a later row raised.Cumo fixed the same in sonots/cumo#382 to sonots/cumo#386. The restore of
shapeandelmszfrom sonots/cumo#385 is left out, because nothing reads them after the loop here.Testing
I compared
storewith expected values built from Ruby Arrays. The sweep covers 14 dtypes and destinations of 2 to 4 dimensions. Each case stores one or two sub-narrays that are plain, sliced, offset, indexed or stepped, and that are longer, shorter, empty, or have an axis of length 1. That is 48,160 cases. Before the fix, 8 of the 42 dtype and destination groups crashed. In the other groups, 3,322 of 39,014 cases gave wrong values and 2,928 raised. After the fix, all cases match. With Ruby 4.0.9 built with AddressSanitizer, the code before the fix reports a heap-buffer-overflow inloop_store_subnarray, and the fixed code reports nothing.The new tests fail on the code before the fix.
test_narray,test_bit,test_extra,test_mathandtest_ractorall pass on Ruby 4.0.9 and 4.1-dev.Related Issues
Follows #27.
🤖 Generated with Claude Code