From 56ecc243e230e8e99761ec0ffc5116601f094bb0 Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Tue, 5 Nov 2024 10:45:39 -0500 Subject: [Bug #20868] Fix Method#hash to not change after compaction The hash value of a Method must remain constant after a compaction, otherwise it may not work as the key in a hash table. For example: def a; end # Need this method here because otherwise the iseq may be on the C stack # which would get pinned and not move during compaction def get_hash method(:a).hash end puts get_hash # => 2993401401091578131 GC.verify_compaction_references(expand_heap: true, toward: :empty) puts get_hash # => -2162775864511574135 --- test/ruby/test_method.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'test/ruby') diff --git a/test/ruby/test_method.rb b/test/ruby/test_method.rb index ebe711ddb4..0707d61f1b 100644 --- a/test/ruby/test_method.rb +++ b/test/ruby/test_method.rb @@ -209,6 +209,27 @@ class TestMethod < Test::Unit::TestCase assert_kind_of(String, o.method(:foo).hash.to_s) end + def test_hash_does_not_change_after_compaction + omit "compaction is not supported on this platform" unless GC.respond_to?(:compact) + + # iseq backed method + assert_separately([], <<~RUBY) + def a; end + + # Need this method here because otherwise the iseq may be on the C stack + # which would get pinned and not move during compaction + def get_hash + method(:a).hash + end + + hash = get_hash + + GC.verify_compaction_references(expand_heap: true, toward: :empty) + + assert_equal(hash, get_hash) + RUBY + end + def test_owner c = Class.new do def foo; end -- cgit v1.2.3