ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: XrXr@users.noreply.github.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:99317] [Ruby master Bug#17048] Calling initialize_copy on live modules leads to crashes
Date: Fri, 24 Jul 2020 22:29:47 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-86712.20200724222946.16806@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-17048.20200724160919.16806@ruby-lang.org

Issue #17048 has been updated by alanwu (Alan Wu).


In principal I agree that allowing initialization only once is a good way to go, but the way `Module.allocate` is currently setup makes implementing this a bit complicated. At the moment there is not really a pre-init state for modules and the result from `Module.allocate` is the same as `Module.new`. If we want to do this we would have to implement a pre-init state for modules, and make sure that all the operations on modules (adding methods, constants, etc) are aware of this state so they can do the initialization in case they receive a pre-init module.

It's doable, but I don't know if the extra consistency is worth the added complexity. It would slow things down a small bit for operations like method addition and has the potential to introduce crashes if we miss places where we need the initialization check.

----------------------------------------
Bug #17048: Calling initialize_copy on live modules leads to crashes
https://bugs.ruby-lang.org/issues/17048#change-86712

* Author: alanwu (Alan Wu)
* Status: Open
* Priority: Normal
* ruby -v: ruby 2.8.0dev (2020-07-23T14:44:25Z master 098e8c2873) [x86_64-linux]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------


Here's a repro script
```ruby
loop do
  m = Module.new do
    prepend Module.new
    def hello
    end
  end

  klass = Class.new { include m }
  m.send(:initialize_copy, Module.new)
  GC.start

  klass.new.hello rescue nil
end
```

Here's a script that shows that it has broken semantics even
when it happens to not crash.

```ruby
module A
end

class B
  include A
end

module C
  Const = :C
end

module D
  Const = :D
end

A.send(:initialize_copy, C)
p B::Const # :C, makes sense
A.send(:initialize_copy, D)
p B::Const # :D, makes sense
A.send(:initialize_copy, Module.new)
p (begin B::Const rescue NameError; 'NameError' end) # NameError, makes sense
A.send(:initialize_copy, C)
p B::Const # still NameErorr. Weird
```
This example shows that the problem exists [as far back as 2.0.0](https://wandbox.org/permlink/4dVDY9sNXJ803jh8).

I think the easiest way to fix this is to forbid calling `:initialize_copy`
on modules that have children. Another option is to try to decide on
the semantics of this. Though I don't think it's worth the effort as this
has been broken for a long time and people don't seem to to be using it.
Thoughts?




-- 
https://bugs.ruby-lang.org/

  parent reply	other threads:[~2020-07-24 22:29 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-24 16:09 [ruby-core:99311] [Ruby master Bug#17048] Calling initialize_copy on live modules leads to crashes XrXr
2020-07-24 16:40 ` [ruby-core:99312] " merch-redmine
2020-07-24 17:23 ` [ruby-core:99313] " XrXr
2020-07-24 17:52 ` [ruby-core:99314] " merch-redmine
2020-07-24 22:29 ` XrXr [this message]
2020-07-25  2:35 ` [ruby-core:99321] " nobu
2020-07-25 10:32 ` [ruby-core:99331] " eregontp
2020-07-25 18:33 ` [ruby-core:99336] " XrXr
2020-07-26  6:21 ` [ruby-core:99343] " nobu
2020-07-26 16:23 ` [ruby-core:99349] " eregontp
2020-07-28 23:45 ` [ruby-core:99379] " XrXr
2020-08-12  7:30 ` [ruby-core:99567] " ko1
2020-08-26 20:27 ` [ruby-core:99715] " XrXr
2021-07-02 17:11 ` [ruby-core:104475] " merch-redmine
2021-09-16  7:18 ` [ruby-core:105288] " nobu (Nobuyoshi Nakada)
2021-09-16 16:03 ` [ruby-core:105312] " jeremyevans0 (Jeremy Evans)
2021-09-16 21:00 ` [ruby-core:105314] " alanwu (Alan Wu)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-86712.20200724222946.16806@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).