diff --git a/patches/ruby/2.7/fix-ruby-xfree-for-libxml2.patch b/patches/ruby/2.7/fix-ruby-xfree-for-libxml2.patch new file mode 100644 index 0000000..6e468a6 --- /dev/null +++ b/patches/ruby/2.7/fix-ruby-xfree-for-libxml2.patch @@ -0,0 +1,23 @@ +diff --git a/gc.c b/gc.c +index 67a709ff79..98785901f4 100644 +--- a/gc.c ++++ b/gc.c +@@ -10174,8 +10174,16 @@ ruby_xrealloc2_body(void *ptr, size_t n, size_t size) + void + ruby_sized_xfree(void *x, size_t size) + { +- if (x) { +- objspace_xfree(&rb_objspace, x, size); ++ if (LIKELY(x)) { ++ /* It's possible for a C extension's pthread destructor function set by pthread_key_create ++ * to be called after ruby_vm_destruct and attempt to free memory. Fall back to mimfree in ++ * that case. */ ++ if (LIKELY(GET_VM())) { ++ objspace_xfree(&rb_objspace, x, size); ++ } ++ else { ++ ruby_mimfree(x); ++ } + } + } + diff --git a/patches/ruby/3.0/fix-ruby-xfree-for-libxml2.patch b/patches/ruby/3.0/fix-ruby-xfree-for-libxml2.patch new file mode 100644 index 0000000..f6015e4 --- /dev/null +++ b/patches/ruby/3.0/fix-ruby-xfree-for-libxml2.patch @@ -0,0 +1,23 @@ +diff --git a/gc.c b/gc.c +index 5d0c342206..2bfff21004 100644 +--- a/gc.c ++++ b/gc.c +@@ -10905,8 +10905,16 @@ ruby_xrealloc2_body(void *ptr, size_t n, size_t size) + void + ruby_sized_xfree(void *x, size_t size) + { +- if (x) { +- objspace_xfree(&rb_objspace, x, size); ++ if (LIKELY(x)) { ++ /* It's possible for a C extension's pthread destructor function set by pthread_key_create ++ * to be called after ruby_vm_destruct and attempt to free memory. Fall back to mimfree in ++ * that case. */ ++ if (LIKELY(GET_VM())) { ++ objspace_xfree(&rb_objspace, x, size); ++ } ++ else { ++ ruby_mimfree(x); ++ } + } + } + diff --git a/patches/ruby/3.1/fix-ruby-xfree-for-libxml2.patch b/patches/ruby/3.1/fix-ruby-xfree-for-libxml2.patch new file mode 100644 index 0000000..18b3d90 --- /dev/null +++ b/patches/ruby/3.1/fix-ruby-xfree-for-libxml2.patch @@ -0,0 +1,23 @@ +diff --git a/gc.c b/gc.c +index 030a4627bd..1c96f6401f 100644 +--- a/gc.c ++++ b/gc.c +@@ -11763,8 +11763,16 @@ ruby_xrealloc2_body(void *ptr, size_t n, size_t size) + void + ruby_sized_xfree(void *x, size_t size) + { +- if (x) { +- objspace_xfree(&rb_objspace, x, size); ++ if (LIKELY(x)) { ++ /* It's possible for a C extension's pthread destructor function set by pthread_key_create ++ * to be called after ruby_vm_destruct and attempt to free memory. Fall back to mimfree in ++ * that case. */ ++ if (LIKELY(GET_VM())) { ++ objspace_xfree(&rb_objspace, x, size); ++ } ++ else { ++ ruby_mimfree(x); ++ } + } + } + diff --git a/patches/ruby/3.2/fix-ruby-xfree-for-libxml2.patch b/patches/ruby/3.2/fix-ruby-xfree-for-libxml2.patch new file mode 100644 index 0000000..c2e93b6 --- /dev/null +++ b/patches/ruby/3.2/fix-ruby-xfree-for-libxml2.patch @@ -0,0 +1,23 @@ +diff --git a/gc.c b/gc.c +index 54400e4f6b..96d43ac8ac 100644 +--- a/gc.c ++++ b/gc.c +@@ -12614,8 +12614,16 @@ ruby_xrealloc2_body(void *ptr, size_t n, size_t size) + void + ruby_sized_xfree(void *x, size_t size) + { +- if (x) { +- objspace_xfree(&rb_objspace, x, size); ++ if (LIKELY(x)) { ++ /* It's possible for a C extension's pthread destructor function set by pthread_key_create ++ * to be called after ruby_vm_destruct and attempt to free memory. Fall back to mimfree in ++ * that case. */ ++ if (LIKELY(GET_VM())) { ++ objspace_xfree(&rb_objspace, x, size); ++ } ++ else { ++ ruby_mimfree(x); ++ } + } + } + diff --git a/patches/ruby/mandatory_patches b/patches/ruby/mandatory_patches index fb13254..752a2e7 100644 --- a/patches/ruby/mandatory_patches +++ b/patches/ruby/mandatory_patches @@ -1 +1,2 @@ thread-memory-allocations +fix-ruby-xfree-for-libxml2