Project

General

Profile

Actions

Bug #22640

open

Thread stack size is one guard page too small on glibc 2.27+, and running into the guard is a segmentation fault

Bug #22640: Thread stack size is one guard page too small on glibc 2.27+, and running into the guard is a segmentation fault

Added by runlevel5 (Trung Le) about 4 hours ago.

Status:
Open
Assignee:
-
Target version:
-
ruby -v:
ruby 4.1.0dev (2026-11-26) +PRISM [powerpc64le-linux]
[ruby-core:127726]

Description

Summary

On Linux with glibc 2.27 or later, get_stack() in thread_pthread.c takes the guard size off the size of a thread's stack, although glibc no longer counts the guard in that size. For every thread but the main one this has two effects:

  1. Ruby believes the machine stack is one guard page smaller than it is, and raises SystemStackError earlier than it has to.
  2. ruby_stack_overflowed_p() looks for the guard one page above where it is. Where the SIGSEGV handler relies on it to recognise a stack overflow, a thread that runs into its guard page is reported as [BUG] Segmentation fault instead of raising SystemStackError.

The crash reproduces on arm64 and on ppc64le. The lost stack is one page: 4KB is hard to notice, but with 64KB pages, the default on ppc64le and an option on arm64, a thread loses 64KB.

Environment

master as of 40abfd3322, built with --disable-yjit --disable-zjit, on two machines:

ruby 4.1.0dev (2026-11-26) +PRISM [powerpc64le-linux]
ruby 4.1.0dev (2026-11-26) +PRISM [aarch64-linux]
  • ppc64le: Fedora 45, glibc 2.44, POWER10, 64KB pages.
  • arm64: Ubuntu 24.04 in Docker, glibc 2.39, 4KB pages.

Steps to reproduce

1. Segmentation fault instead of SystemStackError

# join.rb
a = []
300_000.times { a = [a] }
Thread.new do
  a.join
rescue SystemStackError => e
  puts "#{e.class}: #{e.message}"
end.join

Array#join recurses in C on the nested array until the thread runs into the guard page of its stack. Marshal.dump(a) does the same.

Actual, on ppc64le (arm64 is the same, at 0x0000ffffa38fffc0):

$ ./miniruby join.rb
join.rb:4: [BUG] Segmentation fault at 0x00007fff904fff00
ruby 4.1.0dev (2026-11-26) +PRISM [powerpc64le-linux]

-- Control frame information -----------------------------------------------
c:0003 p:---- s:0011 e:000010 l:y b:0001 r:(nil) CFUNC  :join
c:0002 p:0004 s:0007 e:000006 l:n b:---- r:(nil) BLOCK  join.rb:4 [FINISH]
c:0001 p:---- s:0003 e:000002 l:y b:---- r:(nil) DUMMY  [FINISH]

Expected:

$ ./miniruby join.rb
SystemStackError: stack level too deep

Both fault addresses are just below a page boundary, on the guard page right under the stack.

2. Less usable stack

# depth.rb
def dive(n) = [n].each { |i| $depth = i; dive(i + 1) }
Thread.new do
  dive(0)
rescue SystemStackError
  puts "depth #{$depth}"
end.join
actual expected
ppc64le, 64KB pages ./miniruby depth.rb depth 519 depth 555
RUBY_THREAD_MACHINE_STACK_SIZE=131072 ./miniruby depth.rb depth 20 depth 56
arm64, 4KB pages ./miniruby depth.rb depth 885 depth 888
RUBY_THREAD_MACHINE_STACK_SIZE=131072 ./miniruby depth.rb depth 88 depth 92

The 128KB rows are the stack that the test suite gives its threads.

Cause

static int
get_stack(void **addr, size_t *size)
{
    ...
    CHECK_ERR(pthread_attr_getstack(&attr, addr, size));
    STACK_DIR_UPPER((void)0, (void)(*addr = (char *)*addr + *size));
    ...
    CHECK_ERR(pthread_attr_getguardsize(&attr, &guard));
    ...
    *size -= guard;

Up to glibc 2.26 the guard was the lowest part of the area pthread_attr_getstack() reports, so subtracting it was right. Since glibc 2.27 (the fix for https://sourceware.org/bugzilla/show_bug.cgi?id=22637) the guard is allocated below that area and is not part of the reported size, so it gets taken off a second time.

I checked where the guard is by comparing pthread_attr_getstack() with /proc/self/maps in containers on ppc64le:

glibc guard
2.23 inside the reported stack
2.27, 2.40, 2.42, 2.44 below the reported stack

I did not test 2.24 to 2.26. The glibc fix was also backported to the 2.24, 2.25 and 2.26 stable branches, so a distribution's build of those may behave either way.

For 2: ruby_stack_overflowed_p() works out where the guard is from th->ec->machine.stack_maxsize, which comes from the size above. With the size one page short, the range it accepts is one page above the real guard, and a fault on the real guard is not taken for a stack overflow. From reading check_stack_overflow() in signal.c, x86 is not affected by this part because there the stack pointer in the signal context is used as well. I have not run it on x86.

Proposed fix

Patch attached: 0001-Do-not-take-the-guard-off-the-thread-stack-size-on-g.patch. It skips the subtraction when __GLIBC_PREREQ(2, 27) holds and leaves every other libc as it is, including glibc 2.24 to 2.26 with the backport, which keep today's behaviour.

With it applied to master, on both machines above, join.rb raises SystemStackError and depth.rb gives the expected numbers.

The test suites give the same results on master with and without the patch, so they neither catch the bug nor regress:

master master + patch
arm64, make btest pass pass
arm64, make test-all 39453 tests, 0 failures 39444 tests, 0 failures
arm64, make test-spec 33752 examples, 0 failures 33752 examples, 0 failures
ppc64le, make btest pass pass
ppc64le, make test-all 39447 tests, 1 failure 39484 tests, 1 failure
ppc64le, make test-spec 33761 examples, 0 failures 33761 examples, 0 failures

The ppc64le runs were on a second machine (POWER9, Fedora 45, glibc 2.44, 64KB pages). The one failure there is TestThread#test_handle_interrupt_masks_sigint on both sides, which fails for me whenever the suite is started detached over ssh.


Files

No data to display

Actions

Also available in: PDF Atom