Skip to content

Commit

Permalink
test: ensure GC-ing closures
Browse files Browse the repository at this point in the history
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 #102 (comment)
for details.

Reported by Vít Ondruch. Thanks!!!
  • Loading branch information
kou committed Sep 9, 2022
1 parent 4a71246 commit 1343ac7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
11 changes: 11 additions & 0 deletions test/fiddle/test_closure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@

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)
Expand Down Expand Up @@ -103,6 +111,9 @@ def test_memsize
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
Expand Down
6 changes: 6 additions & 0 deletions test/fiddle/test_func.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ def call(x, y)
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
Expand Down
7 changes: 7 additions & 0 deletions test/fiddle/test_function.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ def setup
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
Expand Down
6 changes: 0 additions & 6 deletions test/fiddle/test_import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ module LIBC
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",
Expand Down Expand Up @@ -59,11 +58,6 @@ module LIBC
]
}
]

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
Expand Down

0 comments on commit 1343ac7

Please sign in to comment.