From 229c041f057f24b8e5c0c911738e17dfb3da4515 Mon Sep 17 00:00:00 2001 From: nagachika Date: Thu, 1 Oct 2020 07:53:55 +0900 Subject: merge revision(s) d732bc51bdbfe7d66038731d42e01a511d13b5f8,633a1f15d8228236094ddee12e4e169d655ec49e,95f387f61a4a4ea92635da760b7de5b1e09bb84e,528a3a17977aa1843a26630c96635c3cb161e729,261569d4aac440f25de588cca365163ecf1124a2,e4a9e926f0fe0acf2fbe61da6e075a95d34be066,318be1cb2f9c6f04403c063a5618c6267012ee51,e1855100e46040e73630b378974c17764e0cccee,9cfa811b0f209d714f89fe0de6778c71f77556c7,b68dab866724aacc1cbc6b7d5e1f555dec092346: [Backport #17202] Revert "Revert "Revert "[ruby/fiddle] Use ffi_closure_free by default. (#20)""" This reverts commit 87f6154bb4c67ca77ee353bb1fe25a922036c0e5. It turned out that the change fails to build on macOS https://rubyci.org/logs/rubyci.s3.amazonaws.com/osx1014/ruby-master/log/20200304T074503Z.fail.html.gz ``` + make 'TESTS=--hide-skip -v fiddle' RUBYOPT=-w test-all dyld: lazy symbol binding failed: Symbol not found: _ffi_closure_alloc Referenced from: /Users/hsbt/Documents/cb/tmp/build/20200304T074503Z/ruby/.ext/x86_64-darwin18/fiddle.bundle Expected in: flat namespace dyld: Symbol not found: _ffi_closure_alloc Referenced from: /Users/hsbt/Documents/cb/tmp/build/20200304T074503Z/ruby/.ext/x86_64-darwin18/fiddle.bundle Expected in: flat namespace make: *** [yes-test-all] Abort trap: 6 ``` [ruby/fiddle] Use ffi_closure_free if available [ruby/fiddle] ffi_closure_free is available in the bundled libffi [ruby/fiddle] use ffi_closure_alloc only with 3.2 or later [ruby/fiddle] always use ffi_closure_alloc on Windows Fixed a typo Show libffi version only if set ext/fiddle/extconf.rb: check if ffi_closure_alloc is available to define HAVE_FFI_CLOSURE_ALLOC. The macro is used in closure.c, so have_func check is needed. If pkg-config is not installed, extconf.rb fails to detect the version of libffi, and does not add "-DUSE_FFI_CLOSURE_ALLOC=1" even when system libffi version is >= 3.2. If USE_FFI_CLOSURE_ALLOC is not defined, closure.c attempts to check if HAVE_FFI_CLOSURE_ALLOC is defined or not, but have_func was removed with 528a3a17977aa1843a26630c96635c3cb161e729, so the macro is always not defined. This resulted in this deprecation warning: https://rubyci.org/logs/rubyci.s3.amazonaws.com/ubuntu2004/ruby-master/log/20200512T123003Z.log.html.gz ``` compiling closure.c closure.c: In function 'initialize': closure.c:265:5: warning: 'ffi_prep_closure' is deprecated: use ffi_prep_closure_loc instead [-Wdeprecated-declarations] 265 | result = ffi_prep_closure(pcl, cif, callback, (void *)self); | ^~~~~~ In file included from ./fiddle.h:42, from closure.c:1: /usr/include/x86_64-linux-gnu/ffi.h:334:1: note: declared here 334 | ffi_prep_closure (ffi_closure*, | ^~~~~~~~~~~~~~~~ ``` Do not try ffi_closure_alloc if libffi is <= 3.1 Maybe due to e1855100e46040e73630b378974c17764e0cccee, CentOS, RHEL, and Fedora CIs have started failing with SEGV. Try to avoid ffi_closure_alloc on those environments. https://rubyci.org/logs/rubyci.s3.amazonaws.com/centos8/ruby-master/log/20200512T183004Z.fail.html.gz https://rubyci.org/logs/rubyci.s3.amazonaws.com/fedora32/ruby-master/log/20200512T183004Z.fail.html.gz https://rubyci.org/logs/rubyci.s3.amazonaws.com/rhel8/ruby-master/log/20200512T183003Z.fail.html.gz ext/fiddle/extconf.rb: Fix the condition of libffi <= 3.1 ver is [3, 1, 0] which is not less then or equal to [3, 1] --- ext/fiddle/closure.c | 25 +++++++++++++++++++++++++ ext/fiddle/extconf.rb | 33 +++++++++++++++++++++++++-------- 2 files changed, 50 insertions(+), 8 deletions(-) (limited to 'ext/fiddle') diff --git a/ext/fiddle/closure.c b/ext/fiddle/closure.c index b997e23f45..2044c836ea 100644 --- a/ext/fiddle/closure.c +++ b/ext/fiddle/closure.c @@ -13,11 +13,22 @@ typedef struct { ffi_type **argv; } fiddle_closure; +#if defined(USE_FFI_CLOSURE_ALLOC) +#elif !defined(HAVE_FFI_CLOSURE_ALLOC) +# define USE_FFI_CLOSURE_ALLOC 0 +#else +# define USE_FFI_CLOSURE_ALLOC 1 +#endif + static void dealloc(void * ptr) { fiddle_closure * cls = (fiddle_closure *)ptr; +#if USE_FFI_CLOSURE_ALLOC ffi_closure_free(cls->pcl); +#else + munmap(cls->pcl, sizeof(*cls->pcl)); +#endif if (cls->argv) xfree(cls->argv); xfree(cls); } @@ -191,7 +202,12 @@ allocate(VALUE klass) VALUE i = TypedData_Make_Struct(klass, fiddle_closure, &closure_data_type, closure); +#if USE_FFI_CLOSURE_ALLOC closure->pcl = ffi_closure_alloc(sizeof(ffi_closure), &closure->code); +#else + closure->pcl = mmap(NULL, sizeof(ffi_closure), PROT_READ | PROT_WRITE, + MAP_ANON | MAP_PRIVATE, -1, 0); +#endif return i; } @@ -238,8 +254,17 @@ initialize(int rbargc, VALUE argv[], VALUE self) if (FFI_OK != result) rb_raise(rb_eRuntimeError, "error prepping CIF %d", result); +#if USE_FFI_CLOSURE_ALLOC result = ffi_prep_closure_loc(pcl, cif, callback, (void *)self, cl->code); +#else + result = ffi_prep_closure(pcl, cif, callback, (void *)self); + cl->code = (void *)pcl; + i = mprotect(pcl, sizeof(*pcl), PROT_READ | PROT_EXEC); + if (i) { + rb_sys_fail("mprotect"); + } +#endif if (FFI_OK != result) rb_raise(rb_eRuntimeError, "error prepping closure %d", result); diff --git a/ext/fiddle/extconf.rb b/ext/fiddle/extconf.rb index 5525b2c808..f8a94d41e7 100644 --- a/ext/fiddle/extconf.rb +++ b/ext/fiddle/extconf.rb @@ -7,12 +7,13 @@ bundle = enable_config('bundled-libffi') if ! bundle dir_config 'libffi' - pkg_config("libffi") + pkg_config("libffi") and + ver = pkg_config("libffi", "modversion") if have_header(ffi_header = 'ffi.h') true elsif have_header(ffi_header = 'ffi/ffi.h') - $defs.push(format('-DUSE_HEADER_HACKS')) + $defs.push('-DUSE_HEADER_HACKS') true end and (have_library('ffi') || have_library('libffi')) end or @@ -27,20 +28,20 @@ begin Dir.glob("#{$srcdir}/libffi-*/").each{|dir| FileUtils.rm_rf(dir)} extlibs.run(["--cache=#{cache_dir}", ext_dir]) end - libffi_dir = bundle != false && + ver = bundle != false && Dir.glob("#{$srcdir}/libffi-*/") .map {|n| File.basename(n)} .max_by {|n| n.scan(/\d+/).map(&:to_i)} - unless libffi_dir + unless ver raise "missing libffi. Please install libffi." end - srcdir = "#{$srcdir}/#{libffi_dir}" + srcdir = "#{$srcdir}/#{ver}" ffi_header = 'ffi.h' libffi = Struct.new(*%I[dir srcdir builddir include lib a cflags ldflags opt arch]).new - libffi.dir = libffi_dir + libffi.dir = ver if $srcdir == "." - libffi.builddir = "#{libffi_dir}/#{RUBY_PLATFORM}" + libffi.builddir = "#{ver}/#{RUBY_PLATFORM}" libffi.srcdir = "." else libffi.builddir = libffi.dir @@ -51,6 +52,7 @@ begin libffi.a = "#{libffi.lib}/libffi_convenience.#{$LIBEXT}" nowarn = CONFIG.merge("warnflags"=>"") libffi.cflags = RbConfig.expand("$(CFLAGS)".dup, nowarn) + ver = ver[/libffi-(.*)/, 1] FileUtils.mkdir_p(libffi.dir) libffi.opt = CONFIG['configure_args'][/'(-C)'/, 1] @@ -110,6 +112,21 @@ begin $INCFLAGS << " -I" << libffi.include end +if ver + ver = ver.gsub(/-rc\d+/, '') # If ver contains rc version, just ignored. + ver = (ver.split('.').map(&:to_i) + [0,0])[0,3] + $defs.push(%{-DRUBY_LIBFFI_MODVERSION=#{ '%d%03d%03d' % ver }}) + warn "libffi_version: #{ver.join('.')}" +end + +case +when $mswin, $mingw, (ver && (ver <=> [3, 2]) >= 0) + $defs << "-DUSE_FFI_CLOSURE_ALLOC=1" +when (ver && (ver <=> [3, 2]) < 0) +else + have_func('ffi_closure_alloc', ffi_header) +end + have_header 'sys/mman.h' if have_header "dlfcn.h" @@ -134,7 +151,7 @@ types.each do |type, signed| if /^\#define\s+SIZEOF_#{type}\s+(SIZEOF_(.+)|\d+)/ =~ config if size = $2 and size != 'VOIDP' size = types.fetch(size) {size} - $defs << format("-DTYPE_%s=TYPE_%s", signed||type, size) + $defs << "-DTYPE_#{signed||type}=TYPE_#{size}" end if signed check_signedness(type.downcase, "stddef.h") -- cgit v1.2.3