Skip to content
Snippets Groups Projects
Commit ac847a87405d authored by Robert Marshall's avatar Robert Marshall
Browse files

Merge branch 'sh-fix-nokogiri-segfaults' into 'master'

Backport Ruby upstream patch to fix seg faults in libxml2/Nokogiri

See merge request https://gitlab.com/gitlab-org/omnibus-gitlab/-/merge_requests/6938



Merged-by: default avatarRobert Marshall <rmarshall@gitlab.com>
Approved-by: default avatarRobert Marshall <rmarshall@gitlab.com>
Co-authored-by: default avatarStan Hu <stanhu@gmail.com>
No related branches found
No related tags found
1 merge request!100Upstream Merge of 16.1 CE branching point
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/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/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/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);
+ }
}
}
...@@ -105,16 +105,16 @@ ...@@ -105,16 +105,16 @@
# be fixed. # be fixed.
end end
# Enable custom patch created by ayufan that allows to count memory allocations # Two patches:
# per-thread. This is asked to be upstreamed as part of https://github.com/ruby/ruby/pull/3978 # 1. Enable custom patch created by ayufan that allows to count memory allocations
if version.start_with?('2.7') # per-thread. This is asked to be upstreamed as part of https://github.com/ruby/ruby/pull/3978
patch source: 'thread-memory-allocations-2.7.patch', plevel: 1, env: env # 2. Backport Ruby upstream patch to fix seg faults in libxml2/Nokogiri: https://bugs.ruby-lang.org/issues/19580
elsif version.start_with?('3.0') # This has been merged for Ruby 3.3 but not yet backported: https://github.com/ruby/ruby/pull/7663
patch source: 'thread-memory-allocations-3.0.patch', plevel: 1, env: env patches = %w[thread-memory-allocations fix-ruby-xfree-for-libxml2]
elsif version.start_with?('3.1') ruby_version = Gem::Version.new(version).canonical_segments[0..1].join('.')
patch source: 'thread-memory-allocations-3.1.patch', plevel: 1, env: env
elsif version.start_with?('3.2') patches.each do |patch_name|
patch source: 'thread-memory-allocations-3.2.patch', plevel: 1, env: env patch source: "#{patch_name}-#{ruby_version}.patch", plevel: 1, env: env
end end
# copy_file_range() has been disabled on recent RedHat kernels: # copy_file_range() has been disabled on recent RedHat kernels:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment