Commit Graph

1 Commits

Author SHA1 Message Date
Jarek Prokop
7993206359 Undefine GC compaction and related methods for ppc64le.
The following falcon source:
<https://github.com/socketry/falcon/blob/v0.42.3/lib/falcon/command/serve.rb#L153-L155>
contains the following snippet:
~~~
if GC.respond_to?(:compact)
	GC.compact
end
~~~

The `if` guard for this feature is recommended from documentation since Ruby >= 3.2:
<https://docs.ruby-lang.org/en/3.2/GC.html#method-c-compact>:
```
 To test whether GC compaction is supported, use the idiom:
 ~~~
 GC.respond_to?(:compact)
 ~~~
```

Gems make use of this idiom in various situations. However on Ruby 3.0
without this patch the `GC.respond_to?` method will return true for
`GC.compact` and compaction related methods even though an attempt to call
them will raise "NotImplementedError" exception.

We observe this behavior due to the methods being implemented and the
NotImplementedError is not returned because methods are not implemented,
but because it is a runtime behavior from inside the called methods.

The applied patchset undefines the methods in C code to ensure even the
`GC.respond_to?` will return false and accomodate libraries that call
`GC.compact` like falcon does:
<https://github.com/socketry/falcon/blob/v0.42.3/lib/falcon/command/serve.rb#L153-L155>

The methods are undefined on build time on affected platforms that do
not support compaction. Currently this is only affecting ppc64le.

The important portion of the patch undefines the `GC.compact` and other
methods, so we can expect the following on ppc64le architecture:
Before:
~~~
$ ruby -e 'p GC.respond_to? :compact'
true
~~~

After:
~~~
$ ruby -e 'p GC.respond_to? :compact'
false
~~~

As long as Ruby gems guard the call to `GC.compact` correctly, there
should no longer be a situation where GC reports compaction methods
as implemented even though it will throw an exception when trying to use the methods.

Most importantly, this is only an issue with ppc64le. The other
architectures such as x86_64, aarch64, s390x should continue to be able
to compact. This is guarded on compile-time.

However the patches touch the source for pre-generated files.
Mainly meaning the file `gc.rb` that is the source for the
`gc.rbinc` and `miniprelude.c` need to be re-generated.
The files use Ruby to generate the C counterparts. To prevent cyclic dependency,
they are re-generated and patches are created against the old version.
The approach to regenerate the files for 3.0 is the same as for 3.1
with the exception of also having to first patch with the file
`ruby-3.1.0-Use-mmap-for-allocating-heap-pages-in-the-GC.patch`,
since the subsequent patches are made on top of that patch.

The mmap patch changes parts of gc.c, that the required patch
also changes, so it had to be backported on top of that patch.

The sequence of actions is as follows:
~~~
tar -Jxvf ./ruby-3.0.7.tar.xz
git clone https://github.com/ruby/ruby.git
cd ruby && git checkout v3_0_7
patch -p1 < ../ruby-3.1.0-Use-mmap-for-allocating-heap-pages-in-the-GC.patch
patch -p1 < ../ruby-3.2.0-define-unsupported-gc-compaction-methods-as-rb_f_notimplement.patch
./autogen.sh && ./configure
make gc.rbinc miniprelude.c
cd ..
diff -u {ruby-3.0.7,ruby}/gc.rbinc > ruby-3.2.0-define-unsupported-gc-compaction-methods_generated-files.patch
diff -u {ruby-3.0.7,ruby}/miniprelude.c >> ruby-3.2.0-define-unsupported-gc-compaction-methods_generated-files.patch
~~~
Firstly we unpact the upstream tar archive, which creates `ruby-3.0.7`
directory.
Then we clone the upstream git repository and check out the Ruby version
tag that is source for the tar archive which creates `ruby` repository.

The `ruby-3.0.7` directory contains the pre-generated files we will use as
the original for the patch.

In the `ruby` repository we apply the patches mentioned in the code
snippet.

Then, generate configure script with autogen.sh, and we create the
gc.rbinc and miniprelude.c files that are generated.

Then create the patch for the generated files. `diff` is used as the
generated files are not tracked via git in upstream, and the diff is
executed against the expanded upstream tar archive that does not ship a
git repository with it. While it would be possible to `git add` the
files before patching them, this ensures that the files are applicable
without a problem on top of the archive.

This commit is partially backported from Ruby 3.1's Fedora counterparts:
b7b5473796
ca94aff023
649a6e3083
instead of re-doing the entire patchset from 3.2 branch back to 3.0.

There is less disruption in GC related code when comparing 3.0 to 3.1
than there is 3.1 to 3.2. Therefore the upstream sources for the patches
were first applied in the Ruby upstream git repository on top of tag v3_1_2.
Namely, the following is the patch source:
https://github.com/ruby/ruby/pull/5934
0de1495f35
0c36ba5319
available in Fedora as part of:
649a6e3083

The version of Ruby that is the version when the mentioned Fedora commits
were introduced. And then cherry picked on top of patch
"ruby-3.1.0-Use-mmap-for-allocating-heap-pages-in-the-GC.patch"
applied in v3_0_7 tag that is present here in downstream as the
difference brought by the patch is significant enough to disrupt
patch application. Git allows easier resolution of the patches here.

In short,
1. Take the upstream compaction patches, cherry-pick them on top of v3_1_2 so that
the changes are present in the git tree, as they have not been backported
to 3.1 upstream.
2. Apply the "ruby-3.1.0-Use-mmap-for-allocating-heap-pages-in-the-GC.patch" on top of v3_0_7.
3. Cherry pick the 3.1 patches to v3_0_7 and resolve differences.

There might have been a few sections where git marked changes from both
patches as a conflict to resolve.
I preferred the approach where, despite the checks introduced
from the 3.1 branch patches should be enough to guard against bad
behavior around compaction, the "mmap" patch's checks are retained for safety.
One of the reasons are that the checks are also present outside the
compact methods in other parts of `gc.c` code.

References for the specific Fedora patches:
b7b5473796
649a6e3083
<b7b5473796>
<649a6e3083>
Uptream bug: <https://bugs.ruby-lang.org/issues/18779>
Upstream PR: <https://github.com/ruby/ruby/pull/5934>

ca94aff023
<ca94aff023>
Related upstream issue: <https://bugs.ruby-lang.org/issues/18829>
<https://github.com/ruby/ruby/pull/6019>
<2c19086323>

Resolves: RHEL-83136
2025-03-27 15:03:49 +01:00