From 3fd4d3a10a53ab0cf2eae9826c0589531d1d850c Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Fri, 2 Sep 2022 13:01:48 +0200 Subject: [PATCH] Add spec and fix exception for Fiddle::Handle.new with a missing library * Fixes https://github.com/oracle/truffleruby/issues/2714 --- CHANGELOG.md | 1 + lib/truffle/truffle/fiddle_backend.rb | 7 ++++--- spec/ruby/library/fiddle/handle/initialize_spec.rb | 10 ++++++++++ 3 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 spec/ruby/library/fiddle/handle/initialize_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index e2559fdb4ff6..60067596489e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,7 @@ Compatibility: * Implement `rb_eval_cmd_kw` to support the `tk` gem (#2556, @aardvark179). * Fix `rb_class2name` to call `inspect` on anonymous classes like in CRuby (#2701, @aardvark179). * Implement `rb_ivar_foreach` to iterate over instance and class variables like in CRuby (#2701, @aardvark179). +* Fix exception for `Fiddle::Handle.new` with a missing library (#2714, @eregon). Performance: diff --git a/lib/truffle/truffle/fiddle_backend.rb b/lib/truffle/truffle/fiddle_backend.rb index 4bd5aebaa394..a577b4ee3e54 100644 --- a/lib/truffle/truffle/fiddle_backend.rb +++ b/lib/truffle/truffle/fiddle_backend.rb @@ -253,15 +253,16 @@ def self.[](*args) RTLD_GLOBAL = Truffle::Config['platform.dlopen.RTLD_GLOBAL'] def initialize(library = nil, flags = RTLD_LAZY | RTLD_GLOBAL) - raise DLError, 'unsupported dlopen flags' if flags != RTLD_LAZY | RTLD_GLOBAL + raise DLError, 'unsupported dlopen flags' if flags != (RTLD_LAZY | RTLD_GLOBAL) + if library == Truffle::FiddleBackend::RTLD_NEXT @handle = :rtld_next else library = nil if library == Truffle::FiddleBackend::RTLD_DEFAULT begin @handle = Primitive.interop_eval_nfi(library ? "load '#{library}'" : 'default') - rescue RuntimeError - raise DLError, "#{library}: cannot open shared object file: No such file or directory" + rescue Polyglot::ForeignException => e + raise DLError, "#{e.message}" end end end diff --git a/spec/ruby/library/fiddle/handle/initialize_spec.rb b/spec/ruby/library/fiddle/handle/initialize_spec.rb new file mode 100644 index 000000000000..51c2470efdf3 --- /dev/null +++ b/spec/ruby/library/fiddle/handle/initialize_spec.rb @@ -0,0 +1,10 @@ +require_relative '../../../spec_helper' +require 'fiddle' + +describe "Fiddle::Handle#initialize" do + it "raises Fiddle::DLError if the library cannot be found" do + -> { + Fiddle::Handle.new("doesnotexist.doesnotexist") + }.should raise_error(Fiddle::DLError) + end +end