From 24ef4133edb701d1dbf33ae8526b0b7546ea459e Mon Sep 17 00:00:00 2001 From: tannevaled Date: Fri, 21 Aug 2026 15:54:17 +0200 Subject: [PATCH] Array#<=>, #hash and #flatten: remember where they have been MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit a = [] a << a #inspect and #== already guard against this; #<=>, #hash and #flatten did not, and each cost about 1.1 GB and never finished. core/array/comparison_spec.rb, hash_spec.rb and flatten_spec.rb all build one through ArraySpecs.empty_recursive_array, which is why all three read exactly 1.08 GB. They read 78 MB now. Each takes MRI's answer rather than a convenient one: #<=> a pair already being compared is equal at that point, so a <=> a is 0 while a <=> [] is still 1. #hash a repeat contributes a constant rather than being skipped, so [a] and [a, a] still hash differently. #flatten a ring has no flattening, and raises ArgumentError, "tried to flatten recursive array" — including flatten!. #<=>'s path lives on the VM rather than in an argument, because that recursion leaves the builtin and comes back through method dispatch; #hash and #flatten recurse directly and carry theirs. Checked against ruby 3, and so is everything ordinary that had to keep working: depth-limited flatten, an array repeating a sibling (not a ring, still flattens), the shorter/longer comparison rules, an incomparable argument, and two equal arrays hashing alike. --- internal/vm/builtins.go | 45 ++++++++++++++- internal/vm/recursive_array_internal_test.go | 61 ++++++++++++++++++++ internal/vm/vm.go | 39 +++++++++++-- 3 files changed, 137 insertions(+), 8 deletions(-) create mode 100644 internal/vm/recursive_array_internal_test.go diff --git a/internal/vm/builtins.go b/internal/vm/builtins.go index 7ccaf884..e76ef14e 100644 --- a/internal/vm/builtins.go +++ b/internal/vm/builtins.go @@ -3241,11 +3241,27 @@ func (vm *VM) bootstrap() { return object.NewArrayFromSlice(out) }) vm.cArray.define("<=>", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { - a := self.(*object.Array).Elems + sa := self.(*object.Array) b, ok := args[0].(*object.Array) if !ok { return object.NilV } + // Comparing a pair already being compared means the two descend into + // each other, and MRI calls that equal at this point rather than going + // round again: `a = []; a << a; a <=> a` is 0. The path is on the VM + // because the recursion leaves here and comes back through #<=> + // dispatch, so it cannot be carried in an argument. + pair := [2]*object.Array{sa, b} + if vm.cmpPath[pair] { + return object.IntValue(0) + } + if vm.cmpPath == nil { + vm.cmpPath = map[[2]*object.Array]bool{} + } + vm.cmpPath[pair] = true + defer delete(vm.cmpPath, pair) + + a := sa.Elems be := b.Elems n := len(a) if len(be) < n { @@ -6278,10 +6294,22 @@ func sign(n int) int { // flattenDepth flattens nested arrays up to depth levels (-1 = fully). func flattenDepth(elems []object.Value, depth int) []object.Value { + return flattenDepthIn(elems, depth, map[*object.Array]bool{}) +} + +// flattenDepthIn is flattenDepth remembering the arrays it is inside. A +// self-referential array has no flattening, and MRI says so rather than trying: +// without this it allocated 1.1 GB and did not finish. +func flattenDepthIn(elems []object.Value, depth int, path map[*object.Array]bool) []object.Value { var out []object.Value for _, e := range elems { if sub, ok := e.(*object.Array); ok && depth != 0 { - out = append(out, flattenDepth(sub.Elems, depth-1)...) + if path[sub] { + raise("ArgumentError", "tried to flatten recursive array") + } + path[sub] = true + out = append(out, flattenDepthIn(sub.Elems, depth-1, path)...) + delete(path, sub) } else { out = append(out, e) } @@ -6294,12 +6322,23 @@ func flattenDepth(elems []object.Value, depth int) []object.Value { // bang form must return nil when nothing changed, which length alone cannot // detect (e.g. [[1]] flattens to [1] with the same length). func flattenDepthChanged(elems []object.Value, depth int) ([]object.Value, bool) { + return flattenDepthChangedIn(elems, depth, map[*object.Array]bool{}) +} + +// flattenDepthChangedIn carries the same cycle path flattenDepthIn does: the +// bang form must refuse a self-referential array for the same reason. +func flattenDepthChangedIn(elems []object.Value, depth int, path map[*object.Array]bool) ([]object.Value, bool) { var out []object.Value changed := false for _, e := range elems { if sub, ok := e.(*object.Array); ok && depth != 0 { + if path[sub] { + raise("ArgumentError", "tried to flatten recursive array") + } changed = true - inner, _ := flattenDepthChanged(sub.Elems, depth-1) + path[sub] = true + inner, _ := flattenDepthChangedIn(sub.Elems, depth-1, path) + delete(path, sub) out = append(out, inner...) } else { out = append(out, e) diff --git a/internal/vm/recursive_array_internal_test.go b/internal/vm/recursive_array_internal_test.go new file mode 100644 index 00000000..09f7727f --- /dev/null +++ b/internal/vm/recursive_array_internal_test.go @@ -0,0 +1,61 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +package vm + +import "testing" + +// TestAnArrayThatContainsItself covers Array#<=>, Array#hash and Array#flatten +// on a self-referential array. +// +// a = [] +// a << a +// +// #inspect and #== already remembered where they had been; these three did not, +// and each cost about 1.1 GB and never finished. core/array/comparison_spec.rb, +// hash_spec.rb and flatten_spec.rb all build one through +// ArraySpecs.empty_recursive_array, which is why all three read 1.08 GB. +// +// Verified against ruby 3, including that flatten *raises* rather than +// answering. +func TestAnArrayThatContainsItself(t *testing.T) { + cases := []struct{ src, want string }{ + // Comparing a pair already being compared is equal at that point. + {`a = []; a << a; p(a <=> a)`, `0`}, + {`a = []; a << a; b = []; b << b; p(a <=> b)`, `0`}, + {`c = [1, [2]]; c << c; p(c <=> c)`, `0`}, + // The spec's own three, which need the shorter/longer rules to survive + // the guard. + {`e = []; e << e; p(e <=> [])`, `1`}, + {`e = []; e << e; p([] <=> e)`, `-1`}, + // Hashing ends, and is still a hash: a repeat contributes a constant + // rather than being dropped, so these two differ. + {`a = []; a << a; p a.hash.class`, `Integer`}, + {`a = []; a << a; b = [a]; c = [a, a]; p(b.hash == c.hash)`, `false`}, + // Flattening a ring has no answer, and MRI says so. + {`a = []; a << a +begin; a.flatten; rescue ArgumentError => e; p e.message; end`, + `"tried to flatten recursive array"`}, + {`a = []; a << a +begin; a.flatten!; rescue ArgumentError => e; p e.message; end`, + `"tried to flatten recursive array"`}, + // An array that merely repeats a sibling is not a ring, and still + // flattens. + {`x = [1]; p [x, x].flatten`, `[1, 1]`}, + // None of the ordinary behaviour moves. + {`p([1, [2, [3]]].flatten)`, `[1, 2, 3]`}, + {`p([1, [2, [3]]].flatten(1))`, `[1, 2, [3]]`}, + {`p([1, 2] <=> [1, 3])`, `-1`}, + {`p([1, 2] <=> [1, 2])`, `0`}, + {`p([1, 2] <=> [1])`, `1`}, + {`p([1, 2] <=> "x")`, `nil`}, + {`p([[1]].hash == [[1]].hash)`, `true`}, + {`p([1].hash == [2].hash)`, `false`}, + } + 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 61fe8117..17476311 100644 --- a/internal/vm/vm.go +++ b/internal/vm/vm.go @@ -195,6 +195,12 @@ func (vm *VM) uncaughtBacktrace(e RubyError) []object.Value { // VM holds I/O, the top-level self, the constant table and the base classes. type VM struct { + // cmpPath holds the array pairs Array#<=> is in the middle of comparing, so + // two arrays that descend into each other are called equal instead of + // compared for ever. It lives here rather than in an argument because the + // recursion leaves the builtin and returns through method dispatch. + cmpPath map[[2]*object.Array]bool + out io.Writer errOut io.Writer // $stderr/STDERR sink; defaults to out (no separate stream) main object.Value @@ -664,11 +670,7 @@ func (vm *VM) hashValue(self object.Value) int64 { // while Complex(1, 2) and Complex(2, 1) differ. return vm.hashValue(v.Re)*31 + vm.hashValue(v.Im) case *object.Array: - h := int64(1) - for _, e := range v.Elems { - h = h*31 + vm.hashValue(e) - } - return h + return vm.hashArray(v, nil) } // Any other object: a stable hash derived from its identity id. return vm.refID(self) @@ -689,6 +691,33 @@ func (vm *VM) refID(self object.Value) int64 { return vm.nextObjID } +// hashArray folds an array's elements, remembering the arrays it is inside so a +// self-referential one ends instead of recursing for ever. `a = []; a << a` +// costs 1.1 GB and never finishes without this; MRI answers an Integer. +// +// A repeat contributes a constant rather than being skipped, so [a] and [a, a] +// still differ. +func (vm *VM) hashArray(a *object.Array, path map[*object.Array]bool) int64 { + if path[a] { + return 9 + } + if path == nil { + path = map[*object.Array]bool{} + } + path[a] = true + defer delete(path, a) + + h := int64(1) + for _, e := range a.Elems { + if sub, ok := e.(*object.Array); ok { + h = h*31 + vm.hashArray(sub, path) + continue + } + h = h*31 + vm.hashValue(e) + } + return h +} + // fnvHash is a small deterministic 64-bit FNV-1a fold over s, used by hashValue // for the content-addressed value types. func fnvHash(s string) int64 {