Project

General

Profile

Actions

Bug #21992

closed

Defining BasicObject#initialize causes segmentation fault

Bug #21992: Defining BasicObject#initialize causes segmentation fault

Added by petercooper (Peter Cooper) 2 days ago. Updated about 14 hours ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:125237]

Description

Simplest reproduction:

class BasicObject
  def initialize
  end
end

In Ruby HEAD, 4.0.2, 3.4.7, and 3.3.11 on macOS, this leads to an immediate segmentation fault. I also got this on Ruby 3.4.1 on Linux.

What I would expect, a warning similar to that raised if you do the same on Object.

I couldn't find a previous mention of this. It's not something you would ever write yourself, but I guess it could arise during metaprogramming or dynamic code generation with the right (wrong) constant name in play.


Related issues 1 (0 open1 closed)

Related to Ruby - Bug #21694: Crash when looking up super method from BasicObjectClosedActions

Updated by nobu (Nobuyoshi Nakada) 1 day ago Actions #1 [ruby-core:125239]

  • Backport changed from 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN to 3.2: REQUIRED, 3.3: REQUIRED, 3.4: REQUIRED, 4.0: REQUIRED

petercooper (Peter Cooper) wrote:

In Ruby HEAD, 4.0.2, 3.4.7, and 3.3.11 on macOS, this leads to an immediate segmentation fault. I also got this on Ruby 3.4.1 on Linux.

It looks like since 3.2 (9c5e3671ebd9c07c178ca5dac08ad15ad1eae411).

What I would expect, a warning similar to that raised if you do the same on Object.

BasicObject#initialize is not defined, so "redefining" may be questionable.
Rather it might to be OK to prohibit the definition of BasicObject#initialize, I think.

Updated by Eregon (Benoit Daloze) 1 day ago Actions #2 [ruby-core:125240]

nobu (Nobuyoshi Nakada) wrote in #note-1:

BasicObject#initialize is not defined, so "redefining" may be questionable.

It seems it is defined:

$ ruby -ve 'p Class.new.instance_method :initialize' 
ruby 3.4.9 (2026-03-11 revision 76cca827ab) +PRISM [x86_64-linux]
#<UnboundMethod: BasicObject#initialize()>

Updated by byroot (Jean Boussier) 1 day ago Actions #3 [ruby-core:125241]

The fix is pretty trivial: https://github.com/ruby/ruby/pull/16725

Not the first time we fail to handle the fact that RClass.superclass may be Qnil, e.g. [Bug #21694]

Updated by byroot (Jean Boussier) 1 day ago Actions #4

  • Related to Bug #21694: Crash when looking up super method from BasicObject added

Updated by byroot (Jean Boussier) 1 day ago Actions #5

  • Status changed from Open to Closed

Applied in changeset git|6593cc52d6153c5535968a8faff6cf4ac83a11fe.


iseq.c: rb_estimate_iv_count handle no superclass

[Bug #21992]

When redefining BasicObject#initialize there's no super class to
access.

Updated by byroot (Jean Boussier) 1 day ago Actions #6 [ruby-core:125242]

3.2: REQUIRED, 3.3: REQUIRED, 3.4: REQUIRED, 4.0: REQUIRED

I don't know if this bug actually qualify for 3.3 (and more so 3.2) backport, but in doubt I made the PRs:

Updated by hsbt (Hiroshi SHIBATA) about 14 hours ago Actions #7

  • Backport changed from 3.2: REQUIRED, 3.3: REQUIRED, 3.4: REQUIRED, 4.0: REQUIRED to 3.3: WONTFIX, 3.4: REQUIRED, 4.0: REQUIRED
Actions

Also available in: PDF Atom