Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upstream truffleruby patches needed when using system libraries #2663

Merged
merged 5 commits into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,30 @@ jobs:
- run: bundle exec rake test
- run: bundle exec rake test:bench

truffleruby-head:
needs: ["basic"]
strategy:
fail-fast: false
matrix:
flags:
- "--disable-system-libraries --disable-static"
- "--disable-system-libraries --enable-static"
- "--enable-system-libraries"
runs-on: ubuntu-latest
container:
image: ghcr.io/sparklemotion/nokogiri-test:truffle-nightly
steps:
- uses: actions/checkout@v2
with:
submodules: true
- uses: actions/cache@v2
with:
path: ports/archives
key: tarballs-ubuntu-${{hashFiles('dependencies.yml', 'patches/**/*.patch')}}
- run: bundle install --local || bundle install
- run: bundle exec rake compile -- ${{matrix.flags}}
- run: bundle exec rake test

bsd:
continue-on-error: true # we're seeing VMs hang and fail the whole workflow
needs: ["basic"]
Expand Down
40 changes: 0 additions & 40 deletions .github/workflows/truffle.yml

This file was deleted.

3 changes: 3 additions & 0 deletions ext/nokogiri/nokogiri.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ xmlNodePtr xmlLastElementChild(xmlNodePtr parent);
# endif
#endif

#if defined(TRUFFLERUBY) && !defined(NOKOGIRI_PACKAGED_LIBRARIES)
# define TRUFFLERUBY_NOKOGIRI_SYSTEM_LIBRARIES
#endif

NOKOPUBVAR VALUE mNokogiri ;
NOKOPUBVAR VALUE mNokogiriGumbo ;
Expand Down
12 changes: 12 additions & 0 deletions ext/nokogiri/xml_sax_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,16 @@ warning_func(void *ctx, const char *msg, ...)
VALUE doc = rb_iv_get(self, "@document");
VALUE rb_message;

#ifdef TRUFFLERUBY_NOKOGIRI_SYSTEM_LIBRARIES
/* It is not currently possible to pass var args from native
functions to sulong, so we work around the issue here. */
Copy link
Contributor Author

@eregon eregon Oct 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've asked the TruffleNFI/Sulong team about that (@rschatz), and it sounds like a limitation of libffi (can't read varargs from a libffi callback), so probably nothing we can do about it in the short term.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rb_message = rb_sprintf("warning_func: %s", msg);
#else
va_list args;
va_start(args, msg);
rb_message = rb_vsprintf(msg, args);
va_end(args);
#endif

rb_funcall(doc, id_warning, 1, rb_message);
}
Expand All @@ -219,10 +225,16 @@ error_func(void *ctx, const char *msg, ...)
VALUE doc = rb_iv_get(self, "@document");
VALUE rb_message;

#ifdef TRUFFLERUBY_NOKOGIRI_SYSTEM_LIBRARIES
/* It is not currently possible to pass var args from native
functions to sulong, so we work around the issue here. */
rb_message = rb_sprintf("error_func: %s", msg);
#else
va_list args;
va_start(args, msg);
rb_message = rb_vsprintf(msg, args);
va_end(args);
#endif

rb_funcall(doc, id_error, 1, rb_message);
}
Expand Down
17 changes: 17 additions & 0 deletions ext/nokogiri/xml_xpath_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,16 @@ xpath_generic_exception_handler(void *ctx, const char *msg, ...)
{
VALUE rb_message;

#ifdef TRUFFLERUBY_NOKOGIRI_SYSTEM_LIBRARIES
/* It is not currently possible to pass var args from native
functions to sulong, so we work around the issue here. */
rb_message = rb_sprintf("xpath_generic_exception_handler: %s", msg);
#else
va_list args;
va_start(args, msg);
rb_message = rb_vsprintf(msg, args);
va_end(args);
#endif

rb_exc_raise(rb_exc_new3(rb_eRuntimeError, rb_message));
}
Expand Down Expand Up @@ -336,7 +342,12 @@ evaluate(int argc, VALUE *argv, VALUE self)
}

xmlResetLastError();
#ifdef TRUFFLERUBY_NOKOGIRI_SYSTEM_LIBRARIES
VALUE errors = rb_ary_new();
xmlSetStructuredErrorFunc(errors, Nokogiri_error_array_pusher);
#else
xmlSetStructuredErrorFunc(NULL, Nokogiri_error_raise);
#endif

/* For some reason, xmlXPathEvalExpression will blow up with a generic error */
/* when there is a non existent function. */
Expand All @@ -346,6 +357,12 @@ evaluate(int argc, VALUE *argv, VALUE self)
xmlSetStructuredErrorFunc(NULL, NULL);
xmlSetGenericErrorFunc(NULL, NULL);

#ifdef TRUFFLERUBY_NOKOGIRI_SYSTEM_LIBRARIES
if (RARRAY_LEN(errors) > 0) {
rb_exc_raise(rb_ary_entry(errors, 0));
}
#endif

if (xpath == NULL) {
xmlErrorPtr error = xmlGetLastError();
rb_exc_raise(Nokogiri_wrap_xml_syntax_error(error));
Expand Down
6 changes: 6 additions & 0 deletions ext/nokogiri/xslt_stylesheet.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,16 @@ xslt_generic_error_handler(void *ctx, const char *msg, ...)
{
VALUE message;

#ifdef TRUFFLERUBY_NOKOGIRI_SYSTEM_LIBRARIES
/* It is not currently possible to pass var args from native
functions to sulong, so we work around the issue here. */
message = rb_sprintf("xslt_generic_error_handler: %s", msg);
#else
va_list args;
va_start(args, msg);
message = rb_vsprintf(msg, args);
va_end(args);
#endif

rb_str_concat((VALUE)ctx, message);
}
Expand Down
10 changes: 9 additions & 1 deletion test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ def skip_unless_libxml2_patch(patch_name)
def skip_unless_jruby(msg = "this test should only run with jruby")
skip(msg) unless Nokogiri.jruby?
end

def truffleruby_system_libraries?
RUBY_ENGINE == "truffleruby" && !Nokogiri::PACKAGED_LIBRARIES
end
end

class TestBenchmark < Minitest::BenchSpec
Expand Down Expand Up @@ -151,8 +155,12 @@ def initialize_nokogiri_test_gc_level
"compact"
elsif (ENV["NOKOGIRI_TEST_GC_LEVEL"] == "verify") && defined?(GC.verify_compaction_references)
"verify"
elsif RUBY_ENGINE == "truffleruby"
"normal"
elsif defined?(GC.compact)
"compact"
else
defined?(GC.compact) ? "compact" : "major"
"major"
end
if ["compact", "verify"].include?(@@gc_level)
# the only way of detecting an unsupported platform is actually
Expand Down
15 changes: 11 additions & 4 deletions test/test_xslt_transforms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,17 @@ def test_non_html_xslt_transform
exception = assert_raises(RuntimeError) do
xslt.transform(doc)
end
assert_match(
/xmlXPathCompOpEval: function decimal not found|java.lang.NoSuchMethodException.*decimal/,
exception.message,
)
if truffleruby_system_libraries?
assert_equal(
"xslt_generic_error_handler: xmlXPathCompOpEval: function %s not found\nxslt_generic_error_handler: %s",
exception.message
)
else
assert_match(
/xmlXPathCompOpEval: function decimal not found|java.lang.NoSuchMethodException.*decimal/,
exception.message
)
end
end

describe "DEFAULT_XSLT parse options" do
Expand Down
6 changes: 5 additions & 1 deletion test/xml/sax/test_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,11 @@ def call_parse_io_with_encoding(encoding)
XML
parser.parse(xml)
refute_empty(parser.document.warnings)
assert_match(/URI .* is not absolute/, parser.document.warnings.first)
if truffleruby_system_libraries?
assert_equal("warning_func: %s", parser.document.warnings.first)
else
assert_match(/URI .* is not absolute/, parser.document.warnings.first)
end
end
end
end
Expand Down
12 changes: 10 additions & 2 deletions test/xml/test_entity_reference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,11 @@ def setup
html = File.read(xml_document)
@parser.parse(html)
refute_nil @parser.document.errors
assert_equal ["Entity 'bar' not defined"], @parser.document.errors.map(&:to_s).map(&:strip)
if truffleruby_system_libraries?
assert_equal ["error_func: %s"], @parser.document.errors.map(&:to_s).map(&:strip)
else
assert_equal ["Entity 'bar' not defined"], @parser.document.errors.map(&:to_s).map(&:strip)
end
end

test_relative_and_absolute_path :test_more_sax_entity_reference do
Expand All @@ -206,7 +210,11 @@ def setup
]
@parser.parse(html)
refute_nil @parser.document.errors
assert_equal ["Entity 'bar' not defined"], @parser.document.errors.map(&:to_s).map(&:strip)
if truffleruby_system_libraries?
assert_equal ["error_func: %s"], @parser.document.errors.map(&:to_s).map(&:strip)
else
assert_equal ["Entity 'bar' not defined"], @parser.document.errors.map(&:to_s).map(&:strip)
end
end
end

Expand Down