Skip to content

Commit

Permalink
dep: libxml 2.12.0 upgrade (with libxml2 patch for weakref) (#3032)
Browse files Browse the repository at this point in the history
**What problem is this PR intended to solve?**

#3031

This approach uses the patches submitted upstream at:

- https://gitlab.gnome.org/GNOME/libxml2/-/merge_requests/229
- https://gitlab.gnome.org/GNOME/libxml2/-/merge_requests/230

and turns off libxml2's thread-local storage feature, which is an issue
when precompiling for musl on glibc systems. If and when we can build a
separate musl native gem, we can revisit this decision.
  • Loading branch information
flavorjones authored Nov 18, 2023
2 parents af3217e + 004e6a6 commit c7dca6a
Show file tree
Hide file tree
Showing 16 changed files with 64 additions and 31 deletions.
16 changes: 8 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ jobs:
if: matrix.sys == 'disable'
with:
path: ports
key: ports-ubuntu-${{matrix.ruby}}-${{hashFiles('dependencies.yml', 'patches/**/*.patch')}}
key: ports-ubuntu-${{matrix.ruby}}-${{hashFiles('dependencies.yml', 'patches/**/*.patch', 'ext/nokogiri/extconf.rb')}}
- run: bundle install --local || bundle install
- run: bundle exec rake compile -- --${{matrix.sys}}-system-libraries
- run: bundle exec rake test
Expand Down Expand Up @@ -141,7 +141,7 @@ jobs:
if: matrix.sys == 'disable'
with:
path: ports
key: ports-ubuntu-${{matrix.ruby}}-${{hashFiles('dependencies.yml', 'patches/**/*.patch')}}
key: ports-ubuntu-${{matrix.ruby}}-${{hashFiles('dependencies.yml', 'patches/**/*.patch', 'ext/nokogiri/extconf.rb')}}
- run: bundle install --local || bundle install
- run: bundle exec rake compile -- --${{matrix.sys}}-system-libraries
- run: bundle exec rake test:valgrind
Expand All @@ -151,7 +151,7 @@ jobs:
strategy:
fail-fast: false
matrix:
sys: ["enable"]
sys: ["enable", "disable"]
runs-on: ubuntu-latest
container:
image: ghcr.io/sparklemotion/nokogiri-test:alpine
Expand Down Expand Up @@ -202,7 +202,7 @@ jobs:
if: matrix.sys == 'disable'
with:
path: ports
key: ports-ubuntu-${{matrix.ruby}}-${{hashFiles('dependencies.yml', 'patches/**/*.patch')}}
key: ports-ubuntu-${{matrix.ruby}}-${{hashFiles('dependencies.yml', 'patches/**/*.patch', 'ext/nokogiri/extconf.rb')}}
- run: bundle install --local || bundle install
- run: bundle exec rake compile -- --${{matrix.sys}}-system-libraries
- run: bundle exec rake test
Expand All @@ -227,7 +227,7 @@ jobs:
if: matrix.sys == 'disable'
with:
path: ports
key: ports-ubuntu-${{matrix.ruby}}-${{hashFiles('dependencies.yml', 'patches/**/*.patch')}}
key: ports-ubuntu-${{matrix.ruby}}-${{hashFiles('dependencies.yml', 'patches/**/*.patch', 'ext/nokogiri/extconf.rb')}}
- run: bundle install --local || bundle install
- run: bundle exec rake compile -- --${{matrix.sys}}-system-libraries
- run: bundle exec rake test:valgrind
Expand All @@ -253,7 +253,7 @@ jobs:
if: matrix.sys == 'disable'
with:
path: ports
key: ports-macos-${{hashFiles('dependencies.yml', 'patches/**/*.patch')}}
key: ports-macos-${{hashFiles('dependencies.yml', 'patches/**/*.patch', 'ext/nokogiri/extconf.rb')}}
- run: bundle exec rake compile -- --${{matrix.sys}}-system-libraries
- run: bundle exec rake test

Expand Down Expand Up @@ -283,7 +283,7 @@ jobs:
if: matrix.sys == 'disable'
with:
path: ports
key: ports-windows-${{hashFiles('dependencies.yml', 'patches/**/*.patch')}}
key: ports-windows-${{hashFiles('dependencies.yml', 'patches/**/*.patch', 'ext/nokogiri/extconf.rb')}}
- run: bundle exec rake compile -- --${{matrix.sys}}-system-libraries
- run: bundle exec rake test

Expand Down Expand Up @@ -378,7 +378,7 @@ jobs:
if: matrix.sys == 'disable'
with:
path: ports
key: ports-ubuntu-${{matrix.ruby}}-${{hashFiles('dependencies.yml', 'patches/**/*.patch')}}
key: ports-ubuntu-${{matrix.ruby}}-${{hashFiles('dependencies.yml', 'patches/**/*.patch', 'ext/nokogiri/extconf.rb')}}
- run: bundle install --local || bundle install
- run: bundle exec rake compile -- --${{matrix.sys}}-system-libraries
- run: bundle exec rake test:memcheck
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/downstream.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ jobs:
- uses: actions/cache@v3
with:
path: ports
key: ports-ubuntu-${{matrix.ruby}}-${{hashFiles('dependencies.yml', 'patches/**/*.patch')}}
key: ports-ubuntu-${{matrix.ruby}}-${{hashFiles('dependencies.yml', 'patches/**/*.patch', 'ext/nokogiri/extconf.rb')}}
- if: matrix.precommand
run: ${{matrix.precommand}}
- run: gem install bundler -v ">= 2.3.22" # for "add --path"
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/upstream.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ jobs:
if: matrix.sys == 'disable'
with:
path: ports
key: ports-${{matrix.plat}}-${{hashFiles('dependencies.yml', 'patches/**/*.patch')}}
key: ports-${{matrix.plat}}-${{hashFiles('dependencies.yml', 'patches/**/*.patch', 'ext/nokogiri/extconf.rb')}}
- run: bundle exec rake compile -- --${{matrix.sys}}-system-libraries
- run: bundle exec rake test

Expand All @@ -142,7 +142,7 @@ jobs:
if: matrix.sys == 'disable'
with:
path: ports
key: ports-ubuntu-head-${{hashFiles('dependencies.yml', 'patches/**/*.patch')}}
key: ports-ubuntu-head-${{hashFiles('dependencies.yml', 'patches/**/*.patch', 'ext/nokogiri/extconf.rb')}}
- run: bundle exec rake compile -- --${{matrix.sys}}-system-libraries
- run: bundle exec rake test:valgrind
- run: bundle exec rake test:memcheck
Expand Down Expand Up @@ -172,7 +172,7 @@ jobs:
- uses: actions/cache@v3
with:
path: ports
key: ports-ubuntu-3.2-${{hashFiles('dependencies.yml', 'patches/**/*.patch')}}
key: ports-ubuntu-3.2-${{hashFiles('dependencies.yml', 'patches/**/*.patch', 'ext/nokogiri/extconf.rb')}}
- name: Update html5lib-tests
run: |
cd test/html5lib-tests
Expand Down
6 changes: 3 additions & 3 deletions dependencies.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

libxml2:
version: "2.11.6"
sha256: "c90eee7506764abbe07bb616b82da452529609815aefef423d66ef080eb0c300"
# sha-256 hash provided in https://download.gnome.org/sources/libxml2/2.11/libxml2-2.11.6.sha256sum
version: "2.12.0"
sha256: "431521c8e19ca396af4fa97743b5a6bfcccddbba90e16426a15e5374cd64fe0d"
# sha-256 hash provided in https://download.gnome.org/sources/libxml2/2.12/libxml2-2.12.0.sha256sum

libxslt:
version: "1.1.39"
Expand Down
1 change: 1 addition & 0 deletions ext/nokogiri/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,7 @@ def configure
"--with-c14n",
"--with-debug",
"--with-threads",
"--without-tls", # see https://github.com/sparklemotion/nokogiri/issues/3031
"CPPFLAGS=#{cppflags}",
"CFLAGS=#{cflags}",
]
Expand Down
2 changes: 1 addition & 1 deletion ext/nokogiri/html4_sax_push_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ native_write(VALUE self, VALUE _chunk, VALUE _last_chunk)

if ((status != 0) && !(ctx->options & XML_PARSE_RECOVER)) {
// TODO: there appear to be no tests for this block
xmlErrorPtr e = xmlCtxtGetLastError(ctx);
xmlErrorConstPtr e = xmlCtxtGetLastError(ctx);
Nokogiri_error_raise(NULL, e);
}

Expand Down
9 changes: 6 additions & 3 deletions ext/nokogiri/nokogiri.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ xmlNodePtr xmlLastElementChild(xmlNodePtr parent);
#define XMLNS_PREFIX "xmlns"
#define XMLNS_PREFIX_LEN 6 /* including either colon or \0 */

#ifndef xmlErrorConstPtr
# define xmlErrorConstPtr const xmlError *
#endif

#include <ruby.h>
#include <ruby/st.h>
Expand Down Expand Up @@ -227,9 +230,9 @@ void Nokogiri_structured_error_func_save(libxmlStructuredErrorHandlerState *hand
void Nokogiri_structured_error_func_save_and_set(libxmlStructuredErrorHandlerState *handler_state, void *user_data,
xmlStructuredErrorFunc handler);
void Nokogiri_structured_error_func_restore(libxmlStructuredErrorHandlerState *handler_state);
VALUE Nokogiri_wrap_xml_syntax_error(xmlErrorPtr error);
void Nokogiri_error_array_pusher(void *ctx, xmlErrorPtr error);
NORETURN_DECL void Nokogiri_error_raise(void *ctx, xmlErrorPtr error);
VALUE Nokogiri_wrap_xml_syntax_error(xmlErrorConstPtr error);
void Nokogiri_error_array_pusher(void *ctx, xmlErrorConstPtr error);
NORETURN_DECL void Nokogiri_error_raise(void *ctx, xmlErrorConstPtr error);
void Nokogiri_marshal_xpath_funcall_and_return_values(xmlXPathParserContextPtr ctx, int nargs, VALUE handler,
const char *function_name) ;

Expand Down
2 changes: 1 addition & 1 deletion ext/nokogiri/test_global_handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
static VALUE foreign_error_handler_block = Qnil;

static void
foreign_error_handler(void *user_data, xmlErrorPtr c_error)
foreign_error_handler(void *user_data, xmlErrorConstPtr c_error)
{
rb_funcall(foreign_error_handler_block, rb_intern("call"), 0);
}
Expand Down
6 changes: 3 additions & 3 deletions ext/nokogiri/xml_document.c
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ read_io(VALUE klass,
xmlSetStructuredErrorFunc(NULL, NULL);

if (doc == NULL) {
xmlErrorPtr error;
xmlErrorConstPtr error;

xmlFreeDoc(doc);

Expand Down Expand Up @@ -383,7 +383,7 @@ read_memory(VALUE klass,
xmlSetStructuredErrorFunc(NULL, NULL);

if (doc == NULL) {
xmlErrorPtr error;
xmlErrorConstPtr error;

xmlFreeDoc(doc);

Expand Down Expand Up @@ -537,7 +537,7 @@ create_entity(int argc, VALUE *argv, VALUE self)
);

if (NULL == ptr) {
xmlErrorPtr error = xmlGetLastError();
xmlErrorConstPtr error = xmlGetLastError();
if (error) {
rb_exc_raise(Nokogiri_wrap_xml_syntax_error(error));
} else {
Expand Down
2 changes: 1 addition & 1 deletion ext/nokogiri/xml_node.c
Original file line number Diff line number Diff line change
Expand Up @@ -2138,7 +2138,7 @@ process_xincludes(VALUE self, VALUE options)
xmlSetStructuredErrorFunc(NULL, NULL);

if (rcode < 0) {
xmlErrorPtr error;
xmlErrorConstPtr error;

error = xmlGetLastError();
if (error) {
Expand Down
2 changes: 1 addition & 1 deletion ext/nokogiri/xml_reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ static VALUE
read_more(VALUE self)
{
xmlTextReaderPtr reader;
xmlErrorPtr error;
xmlErrorConstPtr error;
VALUE error_list;
int ret;

Expand Down
2 changes: 1 addition & 1 deletion ext/nokogiri/xml_relax_ng.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ xml_relax_ng_parse_schema(
xmlRelaxNGFreeParserCtxt(c_parser_context);

if (NULL == c_schema) {
xmlErrorPtr error = xmlGetLastError();
xmlErrorConstPtr error = xmlGetLastError();
if (error) {
Nokogiri_error_raise(NULL, error);
} else {
Expand Down
2 changes: 1 addition & 1 deletion ext/nokogiri/xml_sax_push_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ native_write(VALUE self, VALUE _chunk, VALUE _last_chunk)

if (xmlParseChunk(ctx, chunk, size, Qtrue == _last_chunk ? 1 : 0)) {
if (!(ctx->options & XML_PARSE_RECOVER)) {
xmlErrorPtr e = xmlCtxtGetLastError(ctx);
xmlErrorConstPtr e = xmlCtxtGetLastError(ctx);
Nokogiri_error_raise(NULL, e);
}
}
Expand Down
2 changes: 1 addition & 1 deletion ext/nokogiri/xml_schema.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ xml_schema_parse_schema(
xmlSchemaFreeParserCtxt(c_parser_context);

if (NULL == c_schema) {
xmlErrorPtr error = xmlGetLastError();
xmlErrorConstPtr error = xmlGetLastError();
if (error) {
Nokogiri_error_raise(NULL, error);
} else {
Expand Down
6 changes: 3 additions & 3 deletions ext/nokogiri/xml_syntax_error.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,21 @@ Nokogiri_structured_error_func_restore(libxmlStructuredErrorHandlerState *handle
}

void
Nokogiri_error_array_pusher(void *ctx, xmlErrorPtr error)
Nokogiri_error_array_pusher(void *ctx, xmlErrorConstPtr error)
{
VALUE list = (VALUE)ctx;
Check_Type(list, T_ARRAY);
rb_ary_push(list, Nokogiri_wrap_xml_syntax_error(error));
}

void
Nokogiri_error_raise(void *ctx, xmlErrorPtr error)
Nokogiri_error_raise(void *ctx, xmlErrorConstPtr error)
{
rb_exc_raise(Nokogiri_wrap_xml_syntax_error(error));
}

VALUE
Nokogiri_wrap_xml_syntax_error(xmlErrorPtr error)
Nokogiri_wrap_xml_syntax_error(xmlErrorConstPtr error)
{
VALUE msg, e, klass;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
diff --git a/globals.c b/globals.c
index a786a4b9..1b2b9ad0 100644
--- a/globals.c
+++ b/globals.c
@@ -118,6 +118,13 @@ static XML_THREAD_LOCAL xmlGlobalState globalState;
defined(__GLIBC__) && \
__GLIBC__ * 100 + __GLIBC_MINOR__ < 234

+#pragma weak pthread_getspecific
+#pragma weak pthread_setspecific
+#pragma weak pthread_key_create
+#pragma weak pthread_key_delete
+#pragma weak pthread_equal
+#pragma weak pthread_self
+
#define XML_PTHREAD_WEAK

static int libxml_is_threaded = -1;
@@ -566,7 +573,9 @@ void xmlInitGlobalsInternal(void) {
(pthread_getspecific != NULL) &&
(pthread_setspecific != NULL) &&
(pthread_key_create != NULL) &&
- (pthread_key_delete != NULL);
+ (pthread_key_delete != NULL) &&
+ (pthread_equal != NULL) &&
+ (pthread_self != NULL);
if (libxml_is_threaded == 0)
return;
#endif /* XML_PTHREAD_WEAK */

0 comments on commit c7dca6a

Please sign in to comment.