ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:71682] [Ruby trunk - Bug #11740] [Open] ObjectSpace.each_object exposes internal metaclasses
       [not found] <redmine.issue-11740.20151125102527@ruby-lang.org>
@ 2015-11-25 10:25 ` eregontp
  2015-12-07  7:33 ` [ruby-core:71886] [Ruby trunk - Bug #11740] " ko1
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 9+ messages in thread
From: eregontp @ 2015-11-25 10:25 UTC (permalink / raw)
  To: ruby-core

Issue #11740 has been reported by Benoit Daloze.

----------------------------------------
Bug #11740: ObjectSpace.each_object exposes internal metaclasses
https://bugs.ruby-lang.org/issues/11740

* Author: Benoit Daloze
* Status: Open
* Priority: Normal
* Assignee: 
* ruby -v: ruby 2.3.0dev (2015-11-19 trunk 52672) [x86_64-linux]
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
ObjectSpace.each_object exposes internal metaclasses and
this might result in assumptions being violated since the metaclass structure is not well preserved.

See the attached script for an example.
The #bla method should always be defined on the metaclass of "klass".

See https://bugs.ruby-lang.org/issues/11360#note-2 as well in which I warned against this problem ;)

---Files--------------------------------
objspace_expose_intern_meta.rb (413 Bytes)


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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [ruby-core:71886] [Ruby trunk - Bug #11740] ObjectSpace.each_object exposes internal metaclasses
       [not found] <redmine.issue-11740.20151125102527@ruby-lang.org>
  2015-11-25 10:25 ` [ruby-core:71682] [Ruby trunk - Bug #11740] [Open] ObjectSpace.each_object exposes internal metaclasses eregontp
@ 2015-12-07  7:33 ` ko1
  2015-12-16  8:06 ` [ruby-core:72180] " ko1
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 9+ messages in thread
From: ko1 @ 2015-12-07  7:33 UTC (permalink / raw)
  To: ruby-core

Issue #11740 has been updated by Koichi Sasada.

Assignee set to Koichi Sasada

I'll fix it.

----------------------------------------
Bug #11740: ObjectSpace.each_object exposes internal metaclasses
https://bugs.ruby-lang.org/issues/11740#change-55292

* Author: Benoit Daloze
* Status: Open
* Priority: Normal
* Assignee: Koichi Sasada
* ruby -v: ruby 2.3.0dev (2015-11-19 trunk 52672) [x86_64-linux]
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
ObjectSpace.each_object exposes internal metaclasses and
this might result in assumptions being violated since the metaclass structure is not well preserved.

See the attached script for an example.
The #bla method should always be defined on the metaclass of "klass".

See https://bugs.ruby-lang.org/issues/11360#note-2 as well in which I warned against this problem ;)

---Files--------------------------------
objspace_expose_intern_meta.rb (413 Bytes)


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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [ruby-core:72180] [Ruby trunk - Bug #11740] ObjectSpace.each_object exposes internal metaclasses
       [not found] <redmine.issue-11740.20151125102527@ruby-lang.org>
  2015-11-25 10:25 ` [ruby-core:71682] [Ruby trunk - Bug #11740] [Open] ObjectSpace.each_object exposes internal metaclasses eregontp
  2015-12-07  7:33 ` [ruby-core:71886] [Ruby trunk - Bug #11740] " ko1
@ 2015-12-16  8:06 ` ko1
  2015-12-16 14:35 ` [ruby-core:72187] " eregontp
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 9+ messages in thread
From: ko1 @ 2015-12-16  8:06 UTC (permalink / raw)
  To: ruby-core

Issue #11740 has been updated by Koichi Sasada.


```diff
Index: class.c
===================================================================
--- class.c	(revision 53150)
+++ class.c	(working copy)
@@ -442,6 +442,12 @@ rb_singleton_class_attached(VALUE klass,
  */
 #define META_CLASS_OF_CLASS_CLASS_P(k)  (METACLASS_OF(k) == (k))
 
+int
+rb_singleton_class_has_metaclass_p(VALUE sklass)
+{
+    return rb_ivar_get(METACLASS_OF(sklass), id_attached) == sklass;
+}
+
 /*!
  * whether k has a metaclass
  * @retval 1 if \a k has a metaclass
@@ -449,7 +455,13 @@ rb_singleton_class_attached(VALUE klass,
  */
 #define HAVE_METACLASS_P(k) \
     (FL_TEST(METACLASS_OF(k), FL_SINGLETON) && \
-     rb_ivar_get(METACLASS_OF(k), id_attached) == (k))
+     rb_singleton_class_has_metaclass_p(k))
+
+int
+rb_class_has_metaclass_p(VALUE klass)
+{
+    return HAVE_METACLASS_P(klass);
+}
 
 /*!
  * ensures \a klass belongs to its own eigenclass.
Index: gc.c
===================================================================
--- gc.c	(revision 53150)
+++ gc.c	(working copy)
@@ -2400,6 +2400,13 @@ internal_object_p(VALUE obj)
 	  case T_NODE:
 	  case T_ZOMBIE:
 	    break;
+	  case T_CLASS:
+	    {
+		if (FL_TEST(obj, FL_SINGLETON)) {
+		    int rb_singleton_class_has_metaclass_p(VALUE sklass);
+		    return rb_singleton_class_has_metaclass_p(obj) == 0;
+		}
+	    }
 	  default:
 	    if (!p->as.basic.klass) break;
 	    return 0;
```

This patch solves this problem.
Could you check it?


----------------------------------------
Bug #11740: ObjectSpace.each_object exposes internal metaclasses
https://bugs.ruby-lang.org/issues/11740#change-55594

* Author: Benoit Daloze
* Status: Open
* Priority: Normal
* Assignee: Koichi Sasada
* ruby -v: ruby 2.3.0dev (2015-11-19 trunk 52672) [x86_64-linux]
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
ObjectSpace.each_object exposes internal metaclasses and
this might result in assumptions being violated since the metaclass structure is not well preserved.

See the attached script for an example.
The #bla method should always be defined on the metaclass of "klass".

See https://bugs.ruby-lang.org/issues/11360#note-2 as well in which I warned against this problem ;)

---Files--------------------------------
objspace_expose_intern_meta.rb (413 Bytes)


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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [ruby-core:72187] [Ruby trunk - Bug #11740] ObjectSpace.each_object exposes internal metaclasses
       [not found] <redmine.issue-11740.20151125102527@ruby-lang.org>
                   ` (2 preceding siblings ...)
  2015-12-16  8:06 ` [ruby-core:72180] " ko1
@ 2015-12-16 14:35 ` eregontp
  2015-12-17  2:57 ` [ruby-core:72202] " nobu
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 9+ messages in thread
From: eregontp @ 2015-12-16 14:35 UTC (permalink / raw)
  To: ruby-core

Issue #11740 has been updated by Benoit Daloze.


Thanks for looking at this!

I tried, but I observed a few issues.

First, when running the test above with -v:

~~~
ruby 2.3.0dev (2015-12-16 trunk 53152) [x86_64-linux]
#<Class:0x0055cb4c0f3a58>
[#<Class:0x0055cb4c0f3a58>, Array, Enumerable, Object, Kernel, BasicObject]
objspace_expose_intern_meta.rb:10: warning: instance variable __attached__ not initialized
objspace_expose_intern_meta.rb:10: warning: instance variable __attached__ not initialized
nil
~~~

I think it would be better to hide this warning from the user.

Second, while the patch now hides the internal class, it seems to also hide sclass exposed to the user.

~~~
p c = Class.new
p i = c.new
p s = i.singleton_class
p s.is_a? c.singleton_class
p ObjectSpace.each_object(c.singleton_class).to_a
s.singleton_class
p ObjectSpace.each_object(c.singleton_class).to_a
~~~

outputs:

~~~
...
[#<Class:0x00558227ca5f90>] # c.singleton_class
[#<Class:#<#<Class:0x00558227ca5f90>:0x00558227ca5748>>, #<Class:0x00558227ca5f90>] # s, c.singleton_class
~~~

So `s` is not shown by each_object, even though the user already has a reference to it (with `s`, and it has a metaclass I believe).
So it would seem the check is not correct, but I found no obvious fix.
There is a difference between `HAVE_METACLASS_P()` and the check in gc.c though.

This failure can also be found by running RubySpec core/objectspace/each_object_spec.rb.

PS: How should I apply the patch? I tried `patch < x.patch` but I got a rejected hunk in gc.c

----------------------------------------
Bug #11740: ObjectSpace.each_object exposes internal metaclasses
https://bugs.ruby-lang.org/issues/11740#change-55599

* Author: Benoit Daloze
* Status: Open
* Priority: Normal
* Assignee: Koichi Sasada
* ruby -v: ruby 2.3.0dev (2015-11-19 trunk 52672) [x86_64-linux]
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
ObjectSpace.each_object exposes internal metaclasses and
this might result in assumptions being violated since the metaclass structure is not well preserved.

See the attached script for an example.
The #bla method should always be defined on the metaclass of "klass".

See https://bugs.ruby-lang.org/issues/11360#note-2 as well in which I warned against this problem ;)

---Files--------------------------------
objspace_expose_intern_meta.rb (413 Bytes)


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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [ruby-core:72202] [Ruby trunk - Bug #11740] ObjectSpace.each_object exposes internal metaclasses
       [not found] <redmine.issue-11740.20151125102527@ruby-lang.org>
                   ` (3 preceding siblings ...)
  2015-12-16 14:35 ` [ruby-core:72187] " eregontp
@ 2015-12-17  2:57 ` nobu
  2015-12-21 10:34 ` [ruby-core:72420] [Ruby trunk - Bug #11740] [Assigned] " ko1
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 9+ messages in thread
From: nobu @ 2015-12-17  2:57 UTC (permalink / raw)
  To: ruby-core

Issue #11740 has been updated by Nobuyoshi Nakada.


Koichi Sasada wrote:
> ```diff
> +int
> +rb_singleton_class_has_metaclass_p(VALUE sklass)
> +{
> +    return rb_ivar_get(METACLASS_OF(sklass), id_attached) == sklass;
> +}

Should be `rb_attr_get` instead.

Benoit Daloze wrote:
> PS: How should I apply the patch? I tried `patch < x.patch` but I got a rejected hunk in gc.c

Redmine seems expand tabs, you can see markdown source from a pencil icon.


----------------------------------------
Bug #11740: ObjectSpace.each_object exposes internal metaclasses
https://bugs.ruby-lang.org/issues/11740#change-55613

* Author: Benoit Daloze
* Status: Open
* Priority: Normal
* Assignee: Koichi Sasada
* ruby -v: ruby 2.3.0dev (2015-11-19 trunk 52672) [x86_64-linux]
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
ObjectSpace.each_object exposes internal metaclasses and
this might result in assumptions being violated since the metaclass structure is not well preserved.

See the attached script for an example.
The #bla method should always be defined on the metaclass of "klass".

See https://bugs.ruby-lang.org/issues/11360#note-2 as well in which I warned against this problem ;)

---Files--------------------------------
objspace_expose_intern_meta.rb (413 Bytes)


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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [ruby-core:72420] [Ruby trunk - Bug #11740] [Assigned] ObjectSpace.each_object exposes internal metaclasses
       [not found] <redmine.issue-11740.20151125102527@ruby-lang.org>
                   ` (4 preceding siblings ...)
  2015-12-17  2:57 ` [ruby-core:72202] " nobu
@ 2015-12-21 10:34 ` ko1
  2015-12-22  2:36 ` [ruby-core:72425] [Ruby trunk - Bug #11740] " shugo
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 9+ messages in thread
From: ko1 @ 2015-12-21 10:34 UTC (permalink / raw)
  To: ruby-core

Issue #11740 has been updated by Koichi Sasada.

Status changed from Closed to Assigned

This patch breaks rubyspec.

```
1)
ObjectSpace.each_object walks a class and its normal descendants when passed the class's singleton class FAILED
Expected [#<Class:0x00000001860d98>,
 #<Class:0x00000001860de8>,
 #<Class:0x00000001860e38>,
 #<Class:0x00000001860e88>]
to equal [#<Class:#<#<Class:0x00000001860de8>:0x00000001860d20>>,
 #<Class:0x00000001860d98>,
 #<Class:0x00000001860de8>,
 #<Class:0x00000001860e38>,
 #<Class:0x00000001860e88>]

/mnt/sdb1/ruby/trunk/spec/rubyspec/core/objectspace/each_object_spec.rb:222:in `block (2 levels) in <top (required)>'
/mnt/sdb1/ruby/trunk/spec/rubyspec/core/objectspace/each_object_spec.rb:4:in `<top (required)>'

2)
ObjectSpace.each_object on singleton classes walks singleton classes FAILED
Expected [#<Class:0x0000000189a958>]
```

Maybe I missed some points.
Any idea?

----------------------------------------
Bug #11740: ObjectSpace.each_object exposes internal metaclasses
https://bugs.ruby-lang.org/issues/11740#change-55710

* Author: Benoit Daloze
* Status: Assigned
* Priority: Normal
* Assignee: Koichi Sasada
* ruby -v: ruby 2.3.0dev (2015-11-19 trunk 52672) [x86_64-linux]
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
ObjectSpace.each_object exposes internal metaclasses and
this might result in assumptions being violated since the metaclass structure is not well preserved.

See the attached script for an example.
The #bla method should always be defined on the metaclass of "klass".

See https://bugs.ruby-lang.org/issues/11360#note-2 as well in which I warned against this problem ;)

---Files--------------------------------
objspace_expose_intern_meta.rb (413 Bytes)


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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [ruby-core:72425] [Ruby trunk - Bug #11740] ObjectSpace.each_object exposes internal metaclasses
       [not found] <redmine.issue-11740.20151125102527@ruby-lang.org>
                   ` (5 preceding siblings ...)
  2015-12-21 10:34 ` [ruby-core:72420] [Ruby trunk - Bug #11740] [Assigned] " ko1
@ 2015-12-22  2:36 ` shugo
  2015-12-24 11:09 ` [ruby-core:72471] " eregontp
  2016-02-03 10:56 ` [ruby-core:73681] [Ruby trunk Bug#11740] " usa
  8 siblings, 0 replies; 9+ messages in thread
From: shugo @ 2015-12-22  2:36 UTC (permalink / raw)
  To: ruby-core

Issue #11740 has been updated by Shugo Maeda.


Koichi Sasada wrote:
> Maybe I missed some points.
> Any idea?

You shouldn't hide singleton classes of non-class objects.

```diff
diff --git a/gc.c b/gc.c
index 12be1ea..a574656 100644
--- a/gc.c
+++ b/gc.c
@@ -2400,6 +2400,14 @@ internal_object_p(VALUE obj)
 	  case T_NODE:
 	  case T_ZOMBIE:
 	    break;
+	  case T_CLASS:
+	    {
+		if (FL_TEST(obj, FL_SINGLETON)) {
+		    int rb_singleton_class_has_metaclass_p(VALUE sklass);
+		    return RB_TYPE_P(rb_attr_get(obj, id__attached__), T_CLASS) &&
+			rb_singleton_class_has_metaclass_p(obj) == 0;
+		}
+	    }
 	  default:
 	    if (!p->as.basic.klass) break;
 	    return 0;
```



----------------------------------------
Bug #11740: ObjectSpace.each_object exposes internal metaclasses
https://bugs.ruby-lang.org/issues/11740#change-55718

* Author: Benoit Daloze
* Status: Assigned
* Priority: Normal
* Assignee: Koichi Sasada
* ruby -v: ruby 2.3.0dev (2015-11-19 trunk 52672) [x86_64-linux]
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
ObjectSpace.each_object exposes internal metaclasses and
this might result in assumptions being violated since the metaclass structure is not well preserved.

See the attached script for an example.
The #bla method should always be defined on the metaclass of "klass".

See https://bugs.ruby-lang.org/issues/11360#note-2 as well in which I warned against this problem ;)

---Files--------------------------------
objspace_expose_intern_meta.rb (413 Bytes)


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

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [ruby-core:72471] [Ruby trunk - Bug #11740] ObjectSpace.each_object exposes internal metaclasses
       [not found] <redmine.issue-11740.20151125102527@ruby-lang.org>
                   ` (6 preceding siblings ...)
  2015-12-22  2:36 ` [ruby-core:72425] [Ruby trunk - Bug #11740] " shugo
@ 2015-12-24 11:09 ` eregontp
  2016-02-03 10:56 ` [ruby-core:73681] [Ruby trunk Bug#11740] " usa
  8 siblings, 0 replies; 9+ messages in thread
From: eregontp @ 2015-12-24 11:09 UTC (permalink / raw)
  To: ruby-core

Issue #11740 has been updated by Benoit Daloze.


Looks good now, thanks!

----------------------------------------
Bug #11740: ObjectSpace.each_object exposes internal metaclasses
https://bugs.ruby-lang.org/issues/11740#change-55758

* Author: Benoit Daloze
* Status: Closed
* Priority: Normal
* Assignee: Koichi Sasada
* ruby -v: ruby 2.3.0dev (2015-11-19 trunk 52672) [x86_64-linux]
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
ObjectSpace.each_object exposes internal metaclasses and
this might result in assumptions being violated since the metaclass structure is not well preserved.

See the attached script for an example.
The #bla method should always be defined on the metaclass of "klass".

See https://bugs.ruby-lang.org/issues/11360#note-2 as well in which I warned against this problem ;)

---Files--------------------------------
objspace_expose_intern_meta.rb (413 Bytes)


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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [ruby-core:73681] [Ruby trunk Bug#11740] ObjectSpace.each_object exposes internal metaclasses
       [not found] <redmine.issue-11740.20151125102527@ruby-lang.org>
                   ` (7 preceding siblings ...)
  2015-12-24 11:09 ` [ruby-core:72471] " eregontp
@ 2016-02-03 10:56 ` usa
  8 siblings, 0 replies; 9+ messages in thread
From: usa @ 2016-02-03 10:56 UTC (permalink / raw)
  To: ruby-core

Issue #11740 has been updated by Usaku NAKAMURA.

Backport changed from 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN to 2.0.0: DONTNEED, 2.1: DONTNEED, 2.2: DONTNEED

----------------------------------------
Bug #11740: ObjectSpace.each_object exposes internal metaclasses
https://bugs.ruby-lang.org/issues/11740#change-56876

* Author: Benoit Daloze
* Status: Closed
* Priority: Normal
* Assignee: Koichi Sasada
* ruby -v: ruby 2.3.0dev (2015-11-19 trunk 52672) [x86_64-linux]
* Backport: 2.0.0: DONTNEED, 2.1: DONTNEED, 2.2: DONTNEED
----------------------------------------
ObjectSpace.each_object exposes internal metaclasses and
this might result in assumptions being violated since the metaclass structure is not well preserved.

See the attached script for an example.
The #bla method should always be defined on the metaclass of "klass".

See https://bugs.ruby-lang.org/issues/11360#note-2 as well in which I warned against this problem ;)

---Files--------------------------------
objspace_expose_intern_meta.rb (413 Bytes)


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

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2016-02-03 10:22 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <redmine.issue-11740.20151125102527@ruby-lang.org>
2015-11-25 10:25 ` [ruby-core:71682] [Ruby trunk - Bug #11740] [Open] ObjectSpace.each_object exposes internal metaclasses eregontp
2015-12-07  7:33 ` [ruby-core:71886] [Ruby trunk - Bug #11740] " ko1
2015-12-16  8:06 ` [ruby-core:72180] " ko1
2015-12-16 14:35 ` [ruby-core:72187] " eregontp
2015-12-17  2:57 ` [ruby-core:72202] " nobu
2015-12-21 10:34 ` [ruby-core:72420] [Ruby trunk - Bug #11740] [Assigned] " ko1
2015-12-22  2:36 ` [ruby-core:72425] [Ruby trunk - Bug #11740] " shugo
2015-12-24 11:09 ` [ruby-core:72471] " eregontp
2016-02-03 10:56 ` [ruby-core:73681] [Ruby trunk Bug#11740] " usa

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).