<feed xmlns='http://www.w3.org/2005/Atom'>
<title>ruby.git/gc.c, branch v3_2_11</title>
<subtitle>The Ruby Programming Language</subtitle>
<link rel='alternate' type='text/html' href='https://git.ruby-lang.org/ruby.git/'/>
<entry>
<title>merge revision(s) bccec7fb468ad977be75e7e4c2644b4ea845ab0c, 5f8ebcada099351acbc22db264e7cd3773c2bdc4, e13575bb7938e9e5b6a79bfca1b3793123f479da, 4adcfc8cd7a17593a6590025da2b03eebf4fd63c: [Backport #19584]</title>
<updated>2025-03-13T05:27:06+00:00</updated>
<author>
<name>Hiroshi SHIBATA</name>
<email>hsbt@ruby-lang.org</email>
</author>
<published>2025-03-13T04:26:38+00:00</published>
<link rel='alternate' type='text/html' href='https://git.ruby-lang.org/ruby.git/commit/?id=f89a334b555d9f91f0bb5a7c58b3097960dd7fb8'/>
<id>f89a334b555d9f91f0bb5a7c58b3097960dd7fb8</id>
<content type='text'>
	Fix crash in rb_gc_register_address

	[Bug #19584]

	Some C extensions pass a pointer to a global variable to
	rb_gc_register_address. However, if a GC is triggered inside of
	rb_gc_register_address, then the object could get swept since it does
	not exist on the stack.

	[Bug #19584] Register global variable address before assignment

	[Bug #19584] Register global variables before assignment

	[Bug #19584] [DOC] Tweek description of `rb_gc_register_address`
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
	Fix crash in rb_gc_register_address

	[Bug #19584]

	Some C extensions pass a pointer to a global variable to
	rb_gc_register_address. However, if a GC is triggered inside of
	rb_gc_register_address, then the object could get swept since it does
	not exist on the stack.

	[Bug #19584] Register global variable address before assignment

	[Bug #19584] Register global variables before assignment

	[Bug #19584] [DOC] Tweek description of `rb_gc_register_address`
</pre>
</div>
</content>
</entry>
<entry>
<title>merge revision(s) a1758fbd7fe7406601b0eb1617752db59b83586a:</title>
<updated>2024-12-21T04:31:11+00:00</updated>
<author>
<name>nagachika</name>
<email>nagachika@ruby-lang.org</email>
</author>
<published>2024-12-21T04:31:11+00:00</published>
<link rel='alternate' type='text/html' href='https://git.ruby-lang.org/ruby.git/commit/?id=1d57924d693534b10699a375cb8400e57a67031c'/>
<id>1d57924d693534b10699a375cb8400e57a67031c</id>
<content type='text'>
	Crash when malloc during GC

	This feature was introduced in commit 2ccf6e5, but I realized that
	using rb_warn is a bad idea because it allocates objects, which causes
	a different crash ("object allocation during garbage collection phase").
	We should just hard crash here instead.
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
	Crash when malloc during GC

	This feature was introduced in commit 2ccf6e5, but I realized that
	using rb_warn is a bad idea because it allocates objects, which causes
	a different crash ("object allocation during garbage collection phase").
	We should just hard crash here instead.
</pre>
</div>
</content>
</entry>
<entry>
<title>merge revision(s) f9a48548cf3ef54fc0a385ccd78c708737055ecc: [Backport #20042]</title>
<updated>2024-01-18T02:48:46+00:00</updated>
<author>
<name>nagachika</name>
<email>nagachika@ruby-lang.org</email>
</author>
<published>2024-01-18T02:48:46+00:00</published>
<link rel='alternate' type='text/html' href='https://git.ruby-lang.org/ruby.git/commit/?id=0cc0e43745ffc13a596441adccee295274d99a0b'/>
<id>0cc0e43745ffc13a596441adccee295274d99a0b</id>
<content type='text'>
	restore the stack pointer on finalizer

	When error on finalizer, the exception will be ignored.
	To restart the code, we need to restore the stack pointer.

	fix [Bug #20042]
	---
	 gc.c                 | 4 ++++
	 test/ruby/test_gc.rb | 9 +++++++++
	 2 files changed, 13 insertions(+)
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
	restore the stack pointer on finalizer

	When error on finalizer, the exception will be ignored.
	To restart the code, we need to restore the stack pointer.

	fix [Bug #20042]
	---
	 gc.c                 | 4 ++++
	 test/ruby/test_gc.rb | 9 +++++++++
	 2 files changed, 13 insertions(+)
</pre>
</div>
</content>
</entry>
<entry>
<title>merge revision(s) 8b236e0c66da8f92e9fc33de66cfbc8e4b0c0763: [Backport #19896]</title>
<updated>2023-09-30T04:46:29+00:00</updated>
<author>
<name>nagachika</name>
<email>nagachika@ruby-lang.org</email>
</author>
<published>2023-09-30T04:46:29+00:00</published>
<link rel='alternate' type='text/html' href='https://git.ruby-lang.org/ruby.git/commit/?id=97b7070ebd5493de8d3a6a9b0ecc91bef5068d3f'/>
<id>97b7070ebd5493de8d3a6a9b0ecc91bef5068d3f</id>
<content type='text'>
	[Bug #19896]

	fix memory leak in vm_method

	This introduces a unified reference_count to clarify who is referencing a method.
	This also allows us to treat the refinement method as the def owner since it counts itself as a reference

	Co-authored-by: Peter Zhu &lt;peter@peterzhu.ca&gt;
	---
	 gc.c                     |   4 +-
	 method.h                 |   6 +--
	 rjit_c.rb                |   6 +--
	 test/ruby/test_module.rb |   4 +-
	 vm_insnhelper.c          |   2 +-
	 vm_method.c              | 105 +++++++++++++++++++----------------------------
	 6 files changed, 54 insertions(+), 73 deletions(-)
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
	[Bug #19896]

	fix memory leak in vm_method

	This introduces a unified reference_count to clarify who is referencing a method.
	This also allows us to treat the refinement method as the def owner since it counts itself as a reference

	Co-authored-by: Peter Zhu &lt;peter@peterzhu.ca&gt;
	---
	 gc.c                     |   4 +-
	 method.h                 |   6 +--
	 rjit_c.rb                |   6 +--
	 test/ruby/test_module.rb |   4 +-
	 vm_insnhelper.c          |   2 +-
	 vm_method.c              | 105 +++++++++++++++++++----------------------------
	 6 files changed, 54 insertions(+), 73 deletions(-)
</pre>
</div>
</content>
</entry>
<entry>
<title>merge revision(s) 54dbd8bea8a79bfcdefa471c1717c6cd28022f33: [Backport #19535]</title>
<updated>2023-07-22T04:24:55+00:00</updated>
<author>
<name>nagachika</name>
<email>nagachika@ruby-lang.org</email>
</author>
<published>2023-07-22T04:24:55+00:00</published>
<link rel='alternate' type='text/html' href='https://git.ruby-lang.org/ruby.git/commit/?id=fa72ba72f8c64fd0fa87c8f68cbc31f2e7b94b00'/>
<id>fa72ba72f8c64fd0fa87c8f68cbc31f2e7b94b00</id>
<content type='text'>
	Use an st table for "too complex" objects

	st tables will maintain insertion order so we can marshal dump / load
	objects with instance variables in the same order they were set on that
	particular instance

	[ruby-core:112926] [Bug #19535]

	Co-Authored-By: Jemma Issroff &lt;jemmaissroff@gmail.com&gt;
	---
	 gc.c                     | 10 ++++------
	 include/ruby/st.h        |  2 ++
	 object.c                 |  2 +-
	 ractor.c                 | 43 ++++++++++++++++++++++---------------------
	 shape.h                  |  6 +++---
	 st.c                     |  6 ++++++
	 test/ruby/test_shapes.rb | 21 +++++++++++++++++++++
	 variable.c               | 28 ++++++++++++++--------------
	 vm_insnhelper.c          |  2 +-
	 9 files changed, 74 insertions(+), 46 deletions(-)
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
	Use an st table for "too complex" objects

	st tables will maintain insertion order so we can marshal dump / load
	objects with instance variables in the same order they were set on that
	particular instance

	[ruby-core:112926] [Bug #19535]

	Co-Authored-By: Jemma Issroff &lt;jemmaissroff@gmail.com&gt;
	---
	 gc.c                     | 10 ++++------
	 include/ruby/st.h        |  2 ++
	 object.c                 |  2 +-
	 ractor.c                 | 43 ++++++++++++++++++++++---------------------
	 shape.h                  |  6 +++---
	 st.c                     |  6 ++++++
	 test/ruby/test_shapes.rb | 21 +++++++++++++++++++++
	 variable.c               | 28 ++++++++++++++--------------
	 vm_insnhelper.c          |  2 +-
	 9 files changed, 74 insertions(+), 46 deletions(-)
</pre>
</div>
</content>
</entry>
<entry>
<title>merge revision(s) 3592b24cdc07ed89eecb39161f21fe721a89a5de: [Backport #19531]</title>
<updated>2023-07-22T02:44:54+00:00</updated>
<author>
<name>nagachika</name>
<email>nagachika@ruby-lang.org</email>
</author>
<published>2023-07-22T02:44:54+00:00</published>
<link rel='alternate' type='text/html' href='https://git.ruby-lang.org/ruby.git/commit/?id=46b62f44ce30bf234a76114c8249081e47ce3da4'/>
<id>46b62f44ce30bf234a76114c8249081e47ce3da4</id>
<content type='text'>
	ObjectSpace::WeakMap: clean inverse reference when an entry is
	 re-assigned

	[Bug #19531]

	```ruby
	wmap[1] = "A"
	wmap[1] = "B"
	```

	In the example above, we need to remove the `"A" =&gt; 1` inverse reference
	so that when `"A"` is GCed the `1` key isn't deleted.
	---
	 test/ruby/test_weakmap.rb | 17 +++++++++
	 weakmap.c                 | 91 ++++++++++++++++++++++++++++++++++++++---------
	 2 files changed, 91 insertions(+), 17 deletions(-)
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
	ObjectSpace::WeakMap: clean inverse reference when an entry is
	 re-assigned

	[Bug #19531]

	```ruby
	wmap[1] = "A"
	wmap[1] = "B"
	```

	In the example above, we need to remove the `"A" =&gt; 1` inverse reference
	so that when `"A"` is GCed the `1` key isn't deleted.
	---
	 test/ruby/test_weakmap.rb | 17 +++++++++
	 weakmap.c                 | 91 ++++++++++++++++++++++++++++++++++++++---------
	 2 files changed, 91 insertions(+), 17 deletions(-)
</pre>
</div>
</content>
</entry>
<entry>
<title>merge revision(s) 52e571fa72debcd764765775bd1b76ee87e36d2d: [Backport #19580]</title>
<updated>2023-07-17T00:51:53+00:00</updated>
<author>
<name>nagachika</name>
<email>nagachika@ruby-lang.org</email>
</author>
<published>2023-07-17T00:51:53+00:00</published>
<link rel='alternate' type='text/html' href='https://git.ruby-lang.org/ruby.git/commit/?id=62763658d53a66ad624e1c730742cb3811d18329'/>
<id>62763658d53a66ad624e1c730742cb3811d18329</id>
<content type='text'>
	Ensure ruby_xfree won't segfault if called after vm_destruct

	[Bug #19580]

	The real-world scenario motivating this change is libxml2's pthread
	code which uses `pthread_key_create` to set up a destructor that is
	called at thread exit to free thread-local storage.

	There is a small window of time -- after ruby_vm_destruct but before
	the process exits -- in which a pthread may exit and the destructor is
	called, leading to a segfault.

	Please note that this window of time may be relatively large if
	`atexit` is being used.
	---
	 gc.c | 12 ++++++++++--
	 1 file changed, 10 insertions(+), 2 deletions(-)
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
	Ensure ruby_xfree won't segfault if called after vm_destruct

	[Bug #19580]

	The real-world scenario motivating this change is libxml2's pthread
	code which uses `pthread_key_create` to set up a destructor that is
	called at thread exit to free thread-local storage.

	There is a small window of time -- after ruby_vm_destruct but before
	the process exits -- in which a pthread may exit and the destructor is
	called, leading to a segfault.

	Please note that this window of time may be relatively large if
	`atexit` is being used.
	---
	 gc.c | 12 ++++++++++--
	 1 file changed, 10 insertions(+), 2 deletions(-)
</pre>
</div>
</content>
</entry>
<entry>
<title>merge revision(s) 417b1a36447cb2c650de55b433ba623541fb8bb3: [Backport #19550]</title>
<updated>2023-07-17T00:29:04+00:00</updated>
<author>
<name>nagachika</name>
<email>nagachika@ruby-lang.org</email>
</author>
<published>2023-07-17T00:29:04+00:00</published>
<link rel='alternate' type='text/html' href='https://git.ruby-lang.org/ruby.git/commit/?id=9fb94407b97bb85ea344e67606f8ae9ba0bdbd48'/>
<id>9fb94407b97bb85ea344e67606f8ae9ba0bdbd48</id>
<content type='text'>
	Fix memory leak for iclass

	[Bug #19550]

	If !RCLASS_EXT_EMBEDDED (e.g. 32 bit systems) then the rb_classext_t is
	allocated throug malloc so it must be freed.

	The issue can be seen in the following script:

	```
	20.times do
	  100_000.times do
	    mod = Module.new
	    Class.new do
	      include mod
	    end
	  end

	  # Output the Resident Set Size (memory usage, in KB) of the current Ruby process
	  puts `ps -o rss= -p #{$$}`
	end
	```

	Before this fix, the max RSS is 280MB, while after this change, it's
	30MB.
	---
	 gc.c                     |  2 +-
	 test/ruby/test_module.rb | 15 +++++++++++++++
	 2 files changed, 16 insertions(+), 1 deletion(-)
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
	Fix memory leak for iclass

	[Bug #19550]

	If !RCLASS_EXT_EMBEDDED (e.g. 32 bit systems) then the rb_classext_t is
	allocated throug malloc so it must be freed.

	The issue can be seen in the following script:

	```
	20.times do
	  100_000.times do
	    mod = Module.new
	    Class.new do
	      include mod
	    end
	  end

	  # Output the Resident Set Size (memory usage, in KB) of the current Ruby process
	  puts `ps -o rss= -p #{$$}`
	end
	```

	Before this fix, the max RSS is 280MB, while after this change, it's
	30MB.
	---
	 gc.c                     |  2 +-
	 test/ruby/test_module.rb | 15 +++++++++++++++
	 2 files changed, 16 insertions(+), 1 deletion(-)
</pre>
</div>
</content>
</entry>
<entry>
<title>merge revision(s) e1bd45624c85e8a80991bda20801f50967ac77a1: [Backport #19482]</title>
<updated>2023-07-16T03:58:21+00:00</updated>
<author>
<name>nagachika</name>
<email>nagachika@ruby-lang.org</email>
</author>
<published>2023-07-16T03:58:21+00:00</published>
<link rel='alternate' type='text/html' href='https://git.ruby-lang.org/ruby.git/commit/?id=141402d11c09fa641eebd8f4841f81e7bbf3518c'/>
<id>141402d11c09fa641eebd8f4841f81e7bbf3518c</id>
<content type='text'>
	Fix crash when allocating classes with newobj hook

	We need to zero out the whole slot when running the newobj hook for a
	newly allocated class because the slot could be filled with garbage,
	which would cause a crash if a GC runs inside of the newobj hook.

	For example, the following script crashes:

	```
	require "objspace"

	GC.stress = true

	ObjectSpace.trace_object_allocations {
	  100.times do
	    Class.new
	  end
	}
	```

	[Bug #19482]
	---
	 gc.c                           | 8 +++++++-
	 test/objspace/test_objspace.rb | 7 +++++++
	 2 files changed, 14 insertions(+), 1 deletion(-)
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
	Fix crash when allocating classes with newobj hook

	We need to zero out the whole slot when running the newobj hook for a
	newly allocated class because the slot could be filled with garbage,
	which would cause a crash if a GC runs inside of the newobj hook.

	For example, the following script crashes:

	```
	require "objspace"

	GC.stress = true

	ObjectSpace.trace_object_allocations {
	  100.times do
	    Class.new
	  end
	}
	```

	[Bug #19482]
	---
	 gc.c                           | 8 +++++++-
	 test/objspace/test_objspace.rb | 7 +++++++
	 2 files changed, 14 insertions(+), 1 deletion(-)
</pre>
</div>
</content>
</entry>
<entry>
<title>Fix cvar caching when class is cloned</title>
<updated>2023-07-01T05:17:30+00:00</updated>
<author>
<name>eileencodes</name>
<email>eileencodes@gmail.com</email>
</author>
<published>2023-02-07T20:46:50+00:00</published>
<link rel='alternate' type='text/html' href='https://git.ruby-lang.org/ruby.git/commit/?id=8a3d57971c99680d4baec84553247b9c6ee41080'/>
<id>8a3d57971c99680d4baec84553247b9c6ee41080</id>
<content type='text'>
The class variable cache that was added in
https://github.com/ruby/ruby/pull/4544 changed the behavior of class
variables on cloned classes. As reported when a class is cloned AND a
class variable was set, and the class variable was read from the
original class, reading a class variable from the cloned class would
return the value from the original class.

This was happening because the IC (inline cache) is stored on the ISEQ
which is shared between the original and cloned class, therefore they
share the cache too.

To fix this we are now storing the `cref` in the cache so that we can
check if it's equal to the current `cref`. If it's different we don't
want to read from the cache. If it's the same we do. Cloned classes
don't share the same cref with their original class.

This will need to be backported to 3.1 in addition to 3.2 since the bug
exists in both versions.

We also added a marking function which was missing.

Fixes [Bug #19379]

Co-authored-by: Aaron Patterson &lt;tenderlove@ruby-lang.org&gt;
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
The class variable cache that was added in
https://github.com/ruby/ruby/pull/4544 changed the behavior of class
variables on cloned classes. As reported when a class is cloned AND a
class variable was set, and the class variable was read from the
original class, reading a class variable from the cloned class would
return the value from the original class.

This was happening because the IC (inline cache) is stored on the ISEQ
which is shared between the original and cloned class, therefore they
share the cache too.

To fix this we are now storing the `cref` in the cache so that we can
check if it's equal to the current `cref`. If it's different we don't
want to read from the cache. If it's the same we do. Cloned classes
don't share the same cref with their original class.

This will need to be backported to 3.1 in addition to 3.2 since the bug
exists in both versions.

We also added a marking function which was missing.

Fixes [Bug #19379]

Co-authored-by: Aaron Patterson &lt;tenderlove@ruby-lang.org&gt;
</pre>
</div>
</content>
</entry>
</feed>
