From d4f51a8b9ef36e05e2a92c43a9a5169c439212c4 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Fri, 21 Aug 2026 10:31:12 +0200 Subject: [PATCH] A class bound to a constant in its own body is not its own lexical parent MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit assignConstIn gives an unnamed class the qualified name of the constant it is bound to, and the lexical parent of the scope it is bound in. In class << o CONST = self end the scope and the value are the same singleton class, and a singleton class is unnamed — so it was recorded as its own lexical parent. Nothing is nested in itself, and every later bare-constant read in that scope then walked a ring: class << o x = CONST # 17 GB. ruby: instant end language/singleton_class_spec.rb allocated 71.7 GB, which is the largest of the five reasons the rubyspec-ratchet lane kept dying, and the one still killing it after the other four were fixed. Two changes, because two things were wrong: - assignConstIn does not record a lexical parent that would make the class its own ancestor. - nesting stops at a repeat. The lexical chain is shaped by the program and cannot be assumed acyclic; ancestors has guarded itself this way all along, and this is the same guard for the same reason. A ring from any other route now costs a short walk rather than the machine. before 71.7 GB after 78 MB, and the spec reports pass=40 fail=9 error=4 Checked against ruby 3: the four shapes the spec reads the constant in, the same shape one level in on a real class's singleton, a non-class constant, a class bound outside its own body (which still takes its lexical parent and qualified name), and a missing constant still raising. --- internal/vm/object_model.go | 9 +- internal/vm/singleton_const_internal_test.go | 91 ++++++++++++++++++++ internal/vm/vm.go | 27 +++++- 3 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 internal/vm/singleton_const_internal_test.go diff --git a/internal/vm/object_model.go b/internal/vm/object_model.go index e1117c10..d686d2b3 100644 --- a/internal/vm/object_model.go +++ b/internal/vm/object_model.go @@ -541,7 +541,14 @@ func (vm *VM) scopedConst(cls *RClass, name string) object.Value { // terminates the lexParent chain) is not part of nesting. func (vm *VM) nesting(cref *RClass) []*RClass { var out []*RClass - for c := cref; c != nil && c != vm.cObject; c = c.lexParent { + // Stop at a repeat as well as at the top. A lexical chain is shaped by the + // program — `class << o; CONST = self; end` used to make a class its own + // lexical parent — and a walk with no end here allocated 17 GB and killed a + // CI runner. ancestors has guarded itself this way all along; this is the + // same guard for the same reason. + seen := map[*RClass]bool{} + for c := cref; c != nil && c != vm.cObject && !seen[c]; c = c.lexParent { + seen[c] = true out = append(out, c) } return out diff --git a/internal/vm/singleton_const_internal_test.go b/internal/vm/singleton_const_internal_test.go new file mode 100644 index 00000000..5090861b --- /dev/null +++ b/internal/vm/singleton_const_internal_test.go @@ -0,0 +1,91 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +package vm + +import "testing" + +// TestAConstantBoundToItsOwnScope covers a class assigned to a constant inside +// its own body. +// +// class << o +// CONST = self +// end +// +// The scope and the value are the same singleton class, and a singleton class +// is unnamed, so assignConstIn used to record it as its own lexical parent. +// Every later bare-constant read in that scope then walked a ring: 17 GB for +// six lines, and a CI runner killed under language/singleton_class_spec.rb. +// +// Verified against ruby 3. +func TestAConstantBoundToItsOwnScope(t *testing.T) { + cases := []struct{ src, want string }{ + // The reading that used to take the machine, in the four shapes + // language/singleton_class_spec.rb reads it. + {`o = Object.new +class << o + CONST = self +end +class << o + p CONST == self +end`, `true`}, + {`o = Object.new +class << o + CONST = self + p CONST.equal?(self) +end`, `true`}, + {`o = Object.new +class << o + CONST = self +end +class << o + p self::CONST == self +end`, `true`}, + {`o = Object.new +class << o + CONST = self +end +class << o + p const_get(:CONST) == self +end`, `true`}, + // The same shape one level in, where the scope is a real class's + // singleton rather than an object's. + {`class Outer + class << self + INNER = self + end + def self.read; class << self; INNER; end; end +end +p Outer.read == Outer.singleton_class`, `true`}, + // A constant that is not a class is unaffected, and so is a class bound + // somewhere that is not its own body — it still takes the lexical parent + // and the qualified name it always did. + {`o = Object.new +class << o + CONST = 42 + p CONST +end`, `42`}, + {`module M + Inner = Class.new +end +p M::Inner.name`, `"M::Inner"`}, + {`module Outer2 + module Inner2 + THING = 7 + end + p Inner2::THING +end`, `7`}, + // A missing constant still raises rather than being swallowed by any of + // this. + {`o = Object.new +class << o + begin; NOPE; rescue NameError; p :raised; end +end`, `:raised`}, + } + for _, c := range cases { + if got := eval(t, c.src); got != c.want+"\n" { + t.Errorf("src=%q got=%q want=%q", c.src, got, c.want+"\n") + } + } +} diff --git a/internal/vm/vm.go b/internal/vm/vm.go index 3a05c89d..61fe8117 100644 --- a/internal/vm/vm.go +++ b/internal/vm/vm.go @@ -2108,10 +2108,35 @@ func (vm *VM) assignConstIn(scope *RClass, name string, val object.Value) { if c, ok := val.(*RClass); ok && !c.named { c.name = scopedNameFor(scope, name) c.named = true - c.lexParent = lexParentFor(scope) + // Unless that would make c its own lexical ancestor. A class bound to a + // constant inside its own body does exactly that: + // + // class << o + // CONST = self # scope and value are the same singleton class + // end + // + // Nothing is nested in itself, and recording it that way made every + // later bare-constant read in that scope walk a ring. + if p := lexParentFor(scope); !lexicallyReaches(p, c) { + c.lexParent = p + } } } +// lexicallyReaches reports whether c is start or one of its lexical parents — +// i.e. whether recording start as c's lexParent would close a ring. It stops at +// a repeat, because it may be asked about a chain that already has one. +func lexicallyReaches(start, c *RClass) bool { + seen := map[*RClass]bool{} + for s := start; s != nil && !seen[s]; s = s.lexParent { + if s == c { + return true + } + seen[s] = true + } + return false +} + // lexParentFor returns the lexParent to record for a class/module nested in // scope: scope itself, except Object (the top level), which terminates the // chain as nil.