summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornagachika <nagachika@ruby-lang.org>2024-11-10 13:04:43 +0900
committernagachika <nagachika@ruby-lang.org>2024-11-10 13:04:43 +0900
commitee3428aa0e3a11ed1574b7d99222f6f08737f818 (patch)
treea468a4f8b6425c2635794d4af67841fd9dabead0
parent288e24b73ab20100b63ae7d30fb06a7d8a19de6e (diff)
merge revision(s) 56ecc243e230e8e99761ec0ffc5116601f094bb0: [Backport #20868]
[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
-rw-r--r--test/ruby/test_method.rb21
-rw-r--r--version.h2
-rw-r--r--vm_method.c2
3 files changed, 23 insertions, 2 deletions
diff --git a/test/ruby/test_method.rb b/test/ruby/test_method.rb
index a04666890e..aa564cde25 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
diff --git a/version.h b/version.h
index 27acd894cf..a82aed30ce 100644
--- a/version.h
+++ b/version.h
@@ -11,7 +11,7 @@
# define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR
#define RUBY_VERSION_TEENY 6
#define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR
-#define RUBY_PATCHLEVEL 235
+#define RUBY_PATCHLEVEL 236
#include "ruby/version.h"
#include "ruby/internal/abi.h"
diff --git a/vm_method.c b/vm_method.c
index 17aeb16a66..7b9deffe1e 100644
--- a/vm_method.c
+++ b/vm_method.c
@@ -2105,7 +2105,7 @@ rb_hash_method_definition(st_index_t hash, const rb_method_definition_t *def)
switch (def->type) {
case VM_METHOD_TYPE_ISEQ:
- return rb_hash_uint(hash, (st_index_t)def->body.iseq.iseqptr);
+ return rb_hash_uint(hash, (st_index_t)def->body.iseq.iseqptr->body);
case VM_METHOD_TYPE_CFUNC:
hash = rb_hash_uint(hash, (st_index_t)def->body.cfunc.func);
return rb_hash_uint(hash, def->body.cfunc.argc);