Add patch to make WeakMap write-barrier protected, and a write barrier for WeakMap finalizer #7669
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Subset of patch in Ruby 3.3
Motivation
WeakMap
defines a finalizer on every value inserted into it. When this finalizer runs, it removes the correpsonding entry from the map. However, if this finalizer does not run we may have a dangling pointer. That is, the VM may allocate the empty slot corresponding to the value to a new object. Then, on performing a WeakMap lookup , we might get some random Ruby value.This change adds a write barrier to the finalizer defined by WeakMap, which is Ruby lambda (and hence which might get garbage collected since there's no other reference to it)
The following is a minimal repro:
output:
To run yourself, put
foo.c
andtest.rb
in a single directory, and add the followingextconf.rb
file as wellruby extconf.rb
make
ruby test.rb
Test plan
See included automated tests.