[ruby-core:127562] [Ruby Bug#22562] Type confusion SEGV: Using `super`, `instance_exec`, and `bind_call`, it is possible to run Array#first method on an instance of Integer
Issue #22562 has been reported by alanwu (Alan Wu). ---------------------------------------- Bug #22562: Type confusion SEGV: Using `super`, `instance_exec`, and `bind_call`, it is possible to run Array#first method on an instance of Integer https://bugs.ruby-lang.org/issues/22562 * Author: alanwu (Alan Wu) * Status: Open * Backport: 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN ---------------------------------------- The following crashes 3.3.0 through current trunk (commit:caf4f279d1): ```ruby module M # fixnum: (0x6f56<<1)+1 = 0xdead def first = 0x6f56.instance_exec { super } end M.instance_method(:first).bind_call([]) # <internal:array>:132: [BUG] Segmentation fault at 0x000000000000dead ``` Generally, the body of methods don't type-check `self`, relying on method dispatch to uphold the is-a relationship between `self` and the class/module defining the method. This funky use of `super` breaks that invariant. -- https://bugs.ruby-lang.org/
Issue #22562 has been updated by alanwu (Alan Wu). @matz To fix this bug, we'll need to amend how `self` is type-checked when `super` happens after switching `self` (through e.g. `instance_exec`, `eval`). I have found two options, please decide which one you would prefer so we can have a fix for the next release. This issue pertains to only `super` in a method from a module. The current rule checks if the switched-in `self` contains the module that defines the `super`. It is too loose, because having a common module in the ancestry says nothing about how the class (and thus the layout) of `self` relates to the ancestry chain containing the `super`. For example, `Kernel` is very popular: ```ruby module Kernel def first = 0x6f56.instance_exec { super } end class K < Array; prepend Kernel; end # Kernel, K, Array p K.new.first # Array#first with an Integer as `self` ``` Also, the current check rejects safe situations. The switched-in `self` is subject to module membership check even when the `super` would land in yet another method from a module. Because of method transplant with UnboundMethod#bind and bind_call, methods from modules must handle `self` of any type. The simplest (and most permissive) rule that still ensures safety is: in case `super` lands in a method from a class, check self is-a that class. This rule is analogous to the UnboundMethod method transplant rules: classes check is-a, modules allow everything. I prefer this option for its simplicity. The catch is, this check has to run for all `super` within a module. In contrast, the current is-a check only happens in self-switch situations. A more conservative rule that still limits to self-switch situations, and still checks before looking up the super method, is: let C be the first class higher in the ancestry chain than the iclass of the module enclosing the `super`; check self is-a C. It's less intuitive. It allows for a skewed performance profile where `super` is slower in contexes where self-switch is possible (perhaps a dubious advantage because blocks can self-switch and loops use blocks). Being more conservative, this may also mask buggy native methods in modules that also don't transplant properly with UnboundMethod. Either of the two options supercede the current module membership rule, but we could keep the current membership rule and let it continue to reject safe situations, for compatibility. Delete or keep the old rule? I prefer not having an unnecessary rule which complicates the language and implementation. So, which of the two new checks do you like, and is it okay to delete the old check? Please advice. ---------------------------------------- Bug #22562: Type confusion SEGV: Using `super`, `instance_exec`, and `bind_call`, it is possible to run Array#first on an instance of Integer https://bugs.ruby-lang.org/issues/22562#change-120171 * Author: alanwu (Alan Wu) * Status: Open * Backport: 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN ---------------------------------------- The following crashes 3.3.0 through current trunk (commit:caf4f279d1): ```ruby module M # fixnum: (0x6f56<<1)+1 = 0xdead def first = 0x6f56.instance_exec { super } end M.instance_method(:first).bind_call([]) # <internal:array>:132: [BUG] Segmentation fault at 0x000000000000dead ``` Generally, the body of methods don't type-check `self`, relying on method dispatch to uphold the is-a relationship between `self` and the class/module defining the method. This funky use of `super` breaks that invariant. -- https://bugs.ruby-lang.org/
Issue #22562 has been updated by alanwu (Alan Wu). Subject changed from Type confusion SEGV: Using `super`, `instance_exec`, and `bind_call`, it is possible to run Array#first on an instance of Integer to Type confusion SEGV: Using `super` and `instance_exec` it is possible to run Array#first on an instance of Integer Renaming ticket because during development I found cases that don't involve `bind_call`. ---------------------------------------- Bug #22562: Type confusion SEGV: Using `super` and `instance_exec` it is possible to run Array#first on an instance of Integer https://bugs.ruby-lang.org/issues/22562#change-120172 * Author: alanwu (Alan Wu) * Status: Open * Backport: 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN ---------------------------------------- The following crashes 3.3.0 through current trunk (commit:caf4f279d1): ```ruby module M # fixnum: (0x6f56<<1)+1 = 0xdead def first = 0x6f56.instance_exec { super } end M.instance_method(:first).bind_call([]) # <internal:array>:132: [BUG] Segmentation fault at 0x000000000000dead ``` Generally, the body of methods don't type-check `self`, relying on method dispatch to uphold the is-a relationship between `self` and the class/module defining the method. This funky use of `super` breaks that invariant. -- https://bugs.ruby-lang.org/
participants (1)
-
alanwu (Alan Wu)