8000 test: ensure GC-ing closures · ruby/fiddle@1343ac7 · GitHub
[go: up one dir, main page]

Skip to content
65FA

Commit 1343ac7

Browse files
committed
test: ensure GC-ing closures
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!!!
1 parent 4a71246 commit 1343ac7

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

test/fiddle/test_closure.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66

77
module Fiddle
88
class TestClosure < Fiddle::TestCase
9+
def teardown
10+
super
11+
# Ensure freeing all closures.
12+
# See https://github.com/ruby/fiddle/issues/102#issuecomment-1241763091 .
13+
GC.start
14+
assert_equal(0, ObjectSpace.each_object(Fiddle::Closure) {})
15+
end
16+
917
def test_argument_errors
1018
assert_raise(TypeError) do
1119
Closure.new(TYPE_INT, TYPE_INT)
@@ -103,6 +111,9 @@ def test_memsize
103111
func = Function.new(clos, [t], t)
104112
assert_equal(v, func.call(v))
105113
assert_equal(arg, v, n)
114+
ensure
115+
clos = nil
116+
func = nil
106117
end
107118
end
108119
end

test/fiddle/test_func.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ def call(x, y)
7979
qsort.call(buff, buff.size, 1, cb)
8080
end
8181
assert_equal("1349", buff, bug4929)
82+
ensure
83+
# Ensure freeing all closures.
84+
# See https://github.com/ruby/fiddle/issues/102#issuecomment-1241763091 .
85+
cb = nil
86+
GC.start
87+
assert_equal(0, ObjectSpace.each_object(Fiddle::Closure) {})
8288
end
8389

8490
def test_snprintf

test/fiddle/test_function.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ def setup
1515
end
1616
end
1717

18+
def teardown
19+
# Ensure freeing all closures.
20+
# See https://github.com/ruby/fiddle/issues/102#issuecomment-1241763091 .
21+
GC.start
22+
assert_equal(0, ObjectSpace.each_object(Fiddle::Closure) {})
23+
end
24+
1825
def test_default_abi
1926
func = Function.new(@libm['sin'], [TYPE_DOUBLE], TYPE_DOUBLE)
2027
assert_equal Function::DEFAULT, func.abi

test/fiddle/test_import.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ module LIBC
2222
extern "int fprintf(FILE*, char*)" rescue nil
2323
extern "int gettimeofday(timeval*, timezone*)" rescue nil
2424

25-
BoundQsortCallback = bind("void *bound_qsort_callback(void*, void*)"){|ptr1,ptr2| ptr1[0] <=> ptr2[0]}
2625
Timeval = struct [
2726
"long tv_sec",
2827
"long tv_usec",
@@ -59,11 +58,6 @@ module LIBC
5958
]
6059
}
6160
]
62-
63-
CallCallback = bind("void call_callback(void*, void*)"){ | ptr1, ptr2|
64-
f = Function.new(ptr1.to_i, [TYPE_VOIDP], TYPE_VOID)
65-
f.call(ptr2)
66-
}
6761
end
6862

6963
class TestImport < TestCase

0 commit comments

Comments
 (0)
0