From 6d01b66764b6dd3fc61c297bd1ec973f8ea686aa Mon Sep 17 00:00:00 2001 From: Sutou Kouhei Date: Fri, 9 Sep 2022 23:59:41 +0900 Subject: [ruby/fiddle] test: ensure GC-ing closures MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GitHub: fix GH-102 We can't use Fiddle::Closure before we fork the process. If we do it, the process may be crashed with SELinux. See https://github.com/ruby/fiddle/issues/102#issuecomment-1241763091 for details. Reported by Vít Ondruch. Thanks!!! https://github.com/ruby/fiddle/commit/1343ac7a95 --- test/fiddle/test_closure.rb | 11 +++++++++++ test/fiddle/test_func.rb | 6 ++++++ test/fiddle/test_function.rb | 7 +++++++ test/fiddle/test_import.rb | 6 ------ 4 files changed, 24 insertions(+), 6 deletions(-) (limited to 'test/fiddle') diff --git a/test/fiddle/test_closure.rb b/test/fiddle/test_closure.rb index 9e748bf5ee..1a7c41fd95 100644 --- a/test/fiddle/test_closure.rb +++ b/test/fiddle/test_closure.rb @@ -6,6 +6,14 @@ end module Fiddle class TestClosure < Fiddle::TestCase + def teardown + super + # Ensure freeing all closures. + # See https://github.com/ruby/fiddle/issues/102#issuecomment-1241763091 . + GC.start + assert_equal(0, ObjectSpace.each_object(Fiddle::Closure) {}) + end + def test_argument_errors assert_raise(TypeError) do Closure.new(TYPE_INT, TYPE_INT) @@ -103,6 +111,9 @@ module Fiddle func = Function.new(clos, [t], t) assert_equal(v, func.call(v)) assert_equal(arg, v, n) + ensure + clos = nil + func = nil end end end diff --git a/test/fiddle/test_func.rb b/test/fiddle/test_func.rb index 44893017e8..45fed916ba 100644 --- a/test/fiddle/test_func.rb +++ b/test/fiddle/test_func.rb @@ -79,6 +79,12 @@ module Fiddle qsort.call(buff, buff.size, 1, cb) end assert_equal("1349", buff, bug4929) + ensure + # Ensure freeing all closures. + # See https://github.com/ruby/fiddle/issues/102#issuecomment-1241763091 . + cb = nil + GC.start + assert_equal(0, ObjectSpace.each_object(Fiddle::Closure) {}) end def test_snprintf diff --git a/test/fiddle/test_function.rb b/test/fiddle/test_function.rb index 8ac4f60aa3..0a1159b7bc 100644 --- a/test/fiddle/test_function.rb +++ b/test/fiddle/test_function.rb @@ -15,6 +15,13 @@ module Fiddle end end + def teardown + # Ensure freeing all closures. + # See https://github.com/ruby/fiddle/issues/102#issuecomment-1241763091 . + GC.start + assert_equal(0, ObjectSpace.each_object(Fiddle::Closure) {}) + end + def test_default_abi func = Function.new(@libm['sin'], [TYPE_DOUBLE], TYPE_DOUBLE) assert_equal Function::DEFAULT, func.abi diff --git a/test/fiddle/test_import.rb b/test/fiddle/test_import.rb index afa8df9e00..831c1d310f 100644 --- a/test/fiddle/test_import.rb +++ b/test/fiddle/test_import.rb @@ -22,7 +22,6 @@ module Fiddle extern "int fprintf(FILE*, char*)" rescue nil extern "int gettimeofday(timeval*, timezone*)" rescue nil - BoundQsortCallback = bind("void *bound_qsort_callback(void*, void*)"){|ptr1,ptr2| ptr1[0] <=> ptr2[0]} Timeval = struct [ "long tv_sec", "long tv_usec", @@ -59,11 +58,6 @@ module Fiddle ] } ] - - CallCallback = bind("void call_callback(void*, void*)"){ | ptr1, ptr2| - f = Function.new(ptr1.to_i, [TYPE_VOIDP], TYPE_VOID) - f.call(ptr2) - } end class TestImport < TestCase -- cgit v1.2.1