Skip to content

Fix a SEGV from Numo::RObject#hton swapping the bytes of object references - #41

Merged
yoshoku merged 2 commits into
yoshoku:mainfrom
Watson1978:fix-robject-hton
Nov 29, 2026
Merged

yoshoku merged 2 commits into
yoshoku:mainfrom
Watson1978:fix-robject-hton

Conversation

@Watson1978

Copy link
Copy Markdown
Contributor

Purpose

Numo::RObject#hton swaps the bytes of each element. RObject elements are Ruby object references, so the result holds broken references, and the GC crashes when it marks them.

b = Numo::RObject[1, 2, 3].hton
GC.start  # [BUG] Segmentation fault

Summary of Changes

hton is an alias of swap_byte on Numo::NArray in little-endian builds. Numo::RObject undefines swap_byte, to_network and the other conversions, but undefining a method does not remove its aliases, so hton stayed reachable.

Numo::RObject now undefines hton too, and nary_swap_byte raises TypeError for RObject. The second part covers a bound method object, such as Numo::NArray.instance_method(:swap_byte).bind_call(a), as #30 did for binary I/O.

Cumo found and fixed the same in sonots/cumo#638.

Testing

  • Tests pass locally
  • Manual testing completed
Numo::RObject[1, 2, 3].hton                                                 # NoMethodError
Numo::NArray.instance_method(:swap_byte).bind_call(Numo::RObject[1, 2, 3])  # TypeError
Numo::DFloat[1.5, -2.0].hton.hton == Numo::DFloat[1.5, -2.0]                # true

Before the fix, hton on an RObject array, a view and a subclass, and swap_byte and hton through bind_call, all crashed in the GC when the result was kept. After the fix, they raise NoMethodError or TypeError. Through bind_call, to_network and to_swapped already raised NoMethodError, because they call swap_byte. to_vacs and to_host return the array unchanged. hton on DFloat, Int32, SComplex and Bit gives the same results as before.

I also checked every alias on Numo::NArray that points to a method Numo::RObject undefines. Only hton was left. to_string and from_string were already undefined by #30.

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

Related Issues

None.

🤖 Generated with Claude Code

In little-endian builds, hton is an alias of swap_byte on
Numo::NArray. RObject undefines swap_byte, but undefining a method does
not remove its aliases, so hton stayed reachable. It swapped the bytes
of each VALUE, and the GC crashed when it marked the result.

Undefine hton as well, and reject RObject in nary_swap_byte so a bound
method object cannot get around the undef.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@yoshoku

yoshoku commented Oct 27, 2026

Copy link
Copy Markdown
Owner

/opencode review

@github-actions

Copy link
Copy Markdown

Review: PR #41 — Fix Numo::RObject#hton SEGV

Verdict

Approve. The fix is correct, minimal, and consistent with the precedent set by PR #30 (ca62640). I built the extension and verified the fix behavior and the full test suite.

Correctness

The root-cause analysis in the description is accurate: hton is an alias of swap_byte (ext/numo/narray/narray.c:980), and rb_undef_method does not remove aliases — both names share the same method entry, so hton kept reaching nary_swap_byte and byte-swapped raw VALUE references.

The two-part fix is the right combination:

  • rb_undef_method(cT, "hton") (t_robject.c:413) gives the expected user-facing NoMethodError, consistent with the other undefined conversion methods.
  • The TypeError guard in nary_swap_byte (data.c:109-111) closes the bind_call hole. Since hton and swap_byte share a method entry, one guard covers both Numo::NArray.instance_method(:swap_byte) and instance_method(:hton).

Things I verified:

  • Built the extension and ran all suites: test_narray (141 runs), test_bit (17), test_extra (69), test_math (29), test_ractor (3) — all pass, including the new test.
  • Manually confirmed: Numo::RObject[1,2,3].hton → NoMethodError; both bind_call forms → TypeError; Numo::DFloat[1.5,-2.0].hton.hton round-trips correctly.
  • No remaining bypass paths: hton is the only alias of swap_byte anywhere in the extension; to_network/to_vacs/to_host/to_swapped dispatch through rb_funcall(self, id_swap_byte, 0), which safely raises NoMethodError for RObject; iter_swap_byte has no other callers. The guard's rb_obj_is_kind_of also covers views and subclasses, and numo_cRObject is properly declared in numo/narray.h:177.

Suggestions (non-blocking)

  1. Test coverage for the bound conversion methods. The new test covers swap_byte/hton via bind_call, but not the endian conversion methods. Two portable assertions could be added (portable across endianness, unlike to_network/to_vacs whose behavior through bind_call is endian-dependent):

    assert_raises(NoMethodError) { Numo::NArray.instance_method(:to_swapped).bind_call(a) }
    assert_same(a, Numo::NArray.instance_method(:to_host).bind_call(a))

    (to_swapped always dispatches to swap_byte for a fresh array on either endian; to_host always returns self for a host-ordered array.)

  2. Guard placement/cost — fine as is. The RObject check before the numo_cBit branch adds one class-walk per swap_byte call, negligible next to the ndloop, and an array can never be both kinds, so ordering is harmless.

  3. No CHANGELOG entry needed — this repo updates CHANGELOG.md at release time, matching recent fix PRs.

The commit message and PR description are thorough and accurately describe both the bug and the behavior after the fix.

github run

@yoshoku
yoshoku merged commit c781308 into yoshoku:main Nov 29, 2026
13 checks passed
@Watson1978
Watson1978 deleted the fix-robject-hton branch November 30, 2026 05:27
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