https://github.com/ruby/fiddle/commit/e08c4c635e

Co-authored-by: Sutou Kouhei <kou@clear-code.com>
This commit is contained in:
Hiroshi SHIBATA 2021-09-05 04:54:25 +09:00 коммит произвёл Nobuyoshi Nakada
Родитель afa33da72b
Коммит 8f752c95d2
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 7CD2805BFA3770C6
8 изменённых файлов: 79 добавлений и 37 удалений

Просмотреть файл

@ -58,9 +58,5 @@ Gem::Specification.new do |spec|
spec.required_ruby_version = ">= 2.5.0"
spec.add_development_dependency "bundler"
spec.add_development_dependency "rake"
spec.add_development_dependency "rake-compiler"
spec.metadata["msys2_mingw_dependencies"] = "libffi"
end

Просмотреть файл

@ -1,4 +1,6 @@
# frozen_string_literal: true
require 'rbconfig/sizeof'
require 'test/unit'
require 'fiddle'
@ -165,5 +167,12 @@ module Fiddle
GC.start
end
end
def under_gc_stress
stress, GC.stress = GC.stress, true
yield
ensure
GC.stress = stress
end
end
end

Просмотреть файл

@ -326,7 +326,7 @@ module Fiddle
def test_signature_variadic_arguments
unless Fiddle.const_defined?("TYPE_VARIADIC")
skip "libffi doesn't support variadic arguments"
omit "libffi doesn't support variadic arguments"
end
assert_equal([
"printf",

Просмотреть файл

@ -15,7 +15,7 @@ module Fiddle
begin
f = Function.new(@libm['sinf'], [TYPE_FLOAT], TYPE_FLOAT)
rescue Fiddle::DLError
skip "libm may not have sinf()"
omit "libm may not have sinf()"
end
assert_in_delta 1.0, f.call(90 * Math::PI / 180), 0.0001
end
@ -26,14 +26,13 @@ module Fiddle
end
def test_string
stress, GC.stress = GC.stress, true
f = Function.new(@libc['strcpy'], [TYPE_VOIDP, TYPE_VOIDP], TYPE_VOIDP)
buff = +"000"
str = f.call(buff, "123")
assert_equal("123", buff)
assert_equal("123", str.to_s)
ensure
GC.stress = stress
under_gc_stress do
f = Function.new(@libc['strcpy'], [TYPE_VOIDP, TYPE_VOIDP], TYPE_VOIDP)
buff = +"000"
str = f.call(buff, "123")
assert_equal("123", buff)
assert_equal("123", str.to_s)
end
end
def test_isdigit
@ -76,13 +75,15 @@ module Fiddle
bug4929 = '[ruby-core:37395]'
buff = "9341"
EnvUtil.under_gc_stress {qsort.call(buff, buff.size, 1, cb)}
under_gc_stress do
qsort.call(buff, buff.size, 1, cb)
end
assert_equal("1349", buff, bug4929)
end
def test_snprintf
unless Fiddle.const_defined?("TYPE_VARIADIC")
skip "libffi doesn't support variadic arguments"
omit "libffi doesn't support variadic arguments"
end
if Fiddle::WINDOWS
snprintf_name = "_snprintf"
@ -92,7 +93,7 @@ module Fiddle
begin
snprintf_pointer = @libc[snprintf_name]
rescue Fiddle::DLError
skip "Can't find #{snprintf_name}: #{$!.message}"
omit "Can't find #{snprintf_name}: #{$!.message}"
end
snprintf = Function.new(snprintf_pointer,
[

Просмотреть файл

@ -164,7 +164,7 @@ module Fiddle
begin
poll = @libc['poll']
rescue Fiddle::DLError
skip 'poll(2) not available'
omit 'poll(2) not available'
end
f = Function.new(poll, [TYPE_VOIDP, TYPE_INT, TYPE_INT], TYPE_INT)
@ -180,9 +180,37 @@ module Fiddle
end
def test_no_memory_leak
prep = 'r = Fiddle::Function.new(Fiddle.dlopen(nil)["rb_obj_frozen_p"], [Fiddle::TYPE_UINTPTR_T], Fiddle::TYPE_UINTPTR_T); a = "a"'
code = 'begin r.call(a); rescue TypeError; end'
assert_no_memory_leak(%w[-W0 -rfiddle], "#{prep}\n1000.times{#{code}}", "10_000.times {#{code}}", limit: 1.2)
if respond_to?(:assert_nothing_leaked_memory)
rb_obj_frozen_p_symbol = Fiddle.dlopen(nil)["rb_obj_frozen_p"]
rb_obj_frozen_p = Fiddle::Function.new(rb_obj_frozen_p_symbol,
[Fiddle::TYPE_UINTPTR_T],
Fiddle::TYPE_UINTPTR_T)
a = "a"
n_tries = 100_000
n_tries.times do
begin
a + 1
rescue TypeError
end
end
n_arguments = 1
sizeof_fiddle_generic = Fiddle::SIZEOF_VOIDP # Rough
size_per_try =
(sizeof_fiddle_generic * n_arguments) +
(Fiddle::SIZEOF_VOIDP * (n_arguments + 1))
assert_nothing_leaked_memory(size_per_try * n_tries) do
n_tries.times do
begin
rb_obj_frozen_p.call(a)
rescue TypeError
end
end
end
else
prep = 'r = Fiddle::Function.new(Fiddle.dlopen(nil)["rb_obj_frozen_p"], [Fiddle::TYPE_UINTPTR_T], Fiddle::TYPE_UINTPTR_T); a = "a"'
code = 'begin r.call(a); rescue TypeError; end'
assert_no_memory_leak(%w[-W0 -rfiddle], "#{prep}\n1000.times{#{code}}", "10_000.times {#{code}}", limit: 1.2)
end
end
private

Просмотреть файл

@ -185,7 +185,16 @@ module Fiddle
end if /freebsd/=~ RUBY_PLATFORM
def test_no_memory_leak
assert_no_memory_leak(%w[-W0 -rfiddle.so], '', '100_000.times {Fiddle::Handle.allocate}; GC.start', rss: true)
if respond_to?(:assert_nothing_leaked_memory)
n_tries = 100_000
assert_nothing_leaked_memory(SIZEOF_VOIDP * (n_tries / 100)) do
n_tries.times do
Fiddle::Handle.allocate
end
end
else
assert_no_memory_leak(%w[-W0 -rfiddle.so], '', '100_000.times {Fiddle::Handle.allocate}; GC.start', rss: true)
end
end
if /cygwin|mingw|mswin/ =~ RUBY_PLATFORM

Просмотреть файл

@ -14,7 +14,7 @@ end
module Fiddle
class TestMemoryView < TestCase
def setup
skip "MemoryView is unavailable" unless defined? Fiddle::MemoryView
omit "MemoryView is unavailable" unless defined? Fiddle::MemoryView
end
def test_null_ptr
@ -49,7 +49,7 @@ module Fiddle
end
def test_memory_view_multi_dimensional
skip "MemoryViewTestUtils is unavailable" unless defined? MemoryViewTestUtils
omit "MemoryViewTestUtils is unavailable" unless defined? MemoryViewTestUtils
buf = [ 1, 2, 3, 4,
5, 6, 7, 8,
@ -71,7 +71,7 @@ module Fiddle
end
def test_memory_view_multi_dimensional_with_strides
skip "MemoryViewTestUtils is unavailable" unless defined? MemoryViewTestUtils
omit "MemoryViewTestUtils is unavailable" unless defined? MemoryViewTestUtils
buf = [ 1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14, 15, 16 ].pack("l!*")
@ -93,7 +93,7 @@ module Fiddle
end
def test_memory_view_multi_dimensional_with_multiple_members
skip "MemoryViewTestUtils is unavailable" unless defined? MemoryViewTestUtils
omit "MemoryViewTestUtils is unavailable" unless defined? MemoryViewTestUtils
buf = [ 1, 2, 3, 4, 5, 6, 7, 8,
-1, -2, -3, -4, -5, -6, -7, -8].pack("s*")

Просмотреть файл

@ -179,16 +179,6 @@ module Fiddle
end
def test_free=
assert_normal_exit(<<-"End", '[ruby-dev:39269]')
require 'fiddle'
include Fiddle
free = Fiddle::Function.new(Fiddle::RUBY_FREE, [TYPE_VOIDP], TYPE_VOID)
ptr = Fiddle::Pointer.malloc(4)
ptr.free = free
free.ptr
ptr.free.ptr
End
free = Function.new(Fiddle::RUBY_FREE, [TYPE_VOIDP], TYPE_VOID)
ptr = Pointer.malloc(4)
ptr.free = free
@ -282,7 +272,16 @@ module Fiddle
end
def test_no_memory_leak
assert_no_memory_leak(%w[-W0 -rfiddle.so], '', '100_000.times {Fiddle::Pointer.allocate}', rss: true)
if respond_to?(:assert_nothing_leaked_memory)
n_tries = 100_000
assert_nothing_leaked_memory(SIZEOF_VOIDP * (n_tries / 100)) do
n_tries.times do
Fiddle::Pointer.allocate
end
end
else
assert_no_memory_leak(%w[-W0 -rfiddle.so], '', '100_000.times {Fiddle::Pointer.allocate}', rss: true)
end
end
end
end if defined?(Fiddle)