Disable Fiddle test cases making use of FFI closure.

This commit was cherry-picked from Fedora rawhide
<c2026da175>.
In our observation, the libffi issue may happen randomly.

Fiddle::Closure object is making use of FFI closure
from libffi. When such object is created (instantiated) in Ruby,
and then the process forks on an SELinux-enabled system, the memory
will become corrupted. That is usually not a problem until the
The garbage collector sweeps the object and tries to free it, in which case the
Ruby process will fail with signal SIGABRT.

Tests in test/fiddle/test_closure.rb, test/fiddle/test_func.rb,
and test/fiddle/test_function.rb use the `Fiddle::Closure` class
directly and fiddle/test_import.rb use the class indirectly through
`bind_function` method, therefore they are disabled to prevent
introducing the problematic object into the Ruby GC during test suite
execution instead of relying on that fork and subsequent
garbage collection will not happen.

If an FFI closure object is allocated in Ruby
and the `fork` function is used afterward, the memory
pointing to the closure gets corrupted, and if Ruby GC
tries to collect the object in that state, a SIGABRT
error occurs.

The minimal Ruby reproducer for the issue is the following:
~~~
$ cat fiddle_fork.rb
require 'fiddle/closure'
require 'fiddle/struct'

Fiddle::Closure.new(Fiddle::TYPE_VOID, [])

fork { }

GC.start
~~~

We allocate an unused Closure object,
so it is free for the GC to pick up. Before we call `GC.start`
we fork the process as that corrupts the memory.

Running this with ruby-3.1.2-167.fc37.x86_64 on SELinux enabled system:
~~~
$ ruby fiddle_fork.rb
Aborted (core dumped)
~~~

Such issues may appear at random (depending on the use of forking and GC)
in larger applications that use Fiddle::Closure but can be spotted by the
following functions appearing in the coredump backtrace:
~~~
0x00007f6284d3e5b3 in dlfree (mem=<optimized out>) at ../src/dlmalloc.c:4350
0x00007f6284d6d0b1 in dealloc () from /usr/lib64/ruby/fiddle.so
0x00007f6295e432ec in finalize_list () from /lib64/libruby.so.3.1
0x00007f6295e43420 in finalize_deferred.lto_priv () from /lib64/libruby.so.3.1
0x00007f6295e4ff1c in gc_start_internal.lto_priv () from /lib64/libruby.so.3.1
~~~

Possible solutions to prevent Ruby from crashing:
* Do not use Fiddle::Closure.
* Use the Fiddle::Closure object only in isolated subprocess that
  will not fork further.
* Enable static trampolines in libffi as noted in bugzilla comment:
  <https://bugzilla.redhat.com/show_bug.cgi?id=2040380#c9>

See related discussion on
<https://bugzilla.redhat.com/show_bug.cgi?id=2040380>

Ruby upstream ticket:
<https://bugs.ruby-lang.org/issues/18914>

Ruby Fiddle ticket:
<https://github.com/ruby/fiddle/issues/102>

Related: RHEL-5590
This commit is contained in:
Jun Aruga 2023-12-03 21:59:36 +09:00
parent 59242d8ce8
commit 03fd963481

View File

@ -1014,6 +1014,9 @@ DISABLE_TESTS="$DISABLE_TESTS \
# other components are fixed. # other components are fixed.
# https://bugzilla.redhat.com/show_bug.cgi?id=2040380 # https://bugzilla.redhat.com/show_bug.cgi?id=2040380
mv test/fiddle/test_import.rb{,.disable} mv test/fiddle/test_import.rb{,.disable}
mv test/fiddle/test_closure.rb{,.disable}
DISABLE_TESTS="$DISABLE_TESTS -n !/Fiddle::TestFunc#test_qsort1/"
DISABLE_TESTS="$DISABLE_TESTS -n !/Fiddle::TestFunction#test_argument_count/"
# Give an option to increase the timeout in tests. # Give an option to increase the timeout in tests.
# https://bugs.ruby-lang.org/issues/16921 # https://bugs.ruby-lang.org/issues/16921
@ -1574,7 +1577,7 @@ make runruby TESTRUN_SCRIPT=" \
%changelog %changelog
* Mon Oct 09 2023 Jun Aruga <jaruga@redhat.com> - 3.1.2-142 * Sun Dec 03 2023 Jun Aruga <jaruga@redhat.com> - 3.1.2-142
- Bypass git submodule test failure on Git >= 2.38.1. - Bypass git submodule test failure on Git >= 2.38.1.
- Fix tests with Europe/Amsterdam pre-1970 time on tzdata version 2022b. - Fix tests with Europe/Amsterdam pre-1970 time on tzdata version 2022b.
- Fix for tzdata-2022g. - Fix for tzdata-2022g.
@ -1582,6 +1585,8 @@ make runruby TESTRUN_SCRIPT=" \
Resolves: RHEL-5590 Resolves: RHEL-5590
- ssl: use ffdhe2048 from RFC 7919 as the default DH group parameters - ssl: use ffdhe2048 from RFC 7919 as the default DH group parameters
Related: RHEL-5590 Related: RHEL-5590
- Disable fiddle tests that use FFI closures.
Related: RHEL-5590
* Fri Jun 03 2022 Jarek Prokop <jprokop@redhat.com> - 3.1.2-141 * Fri Jun 03 2022 Jarek Prokop <jprokop@redhat.com> - 3.1.2-141
- Upgrade to Ruby 3.1.2 by merging Fedora Rawhide branch (commit: b7b5473). - Upgrade to Ruby 3.1.2 by merging Fedora Rawhide branch (commit: b7b5473).