From 457b8d688a8fe707d6a9b5af0bb6d77324f14d76 Mon Sep 17 00:00:00 2001 From: Alan Wu Date: Thu, 29 Nov 2018 17:26:41 -0500 Subject: [PATCH] Avoid GCing dead stack after switching away from a fiber Fibers save execution contextes, and execution contexts include a native stack pointer. It may happen that a Fiber outlive the native thread it executed on. Consider the following code adapted from Bug #14561: ```ruby enum = Enumerator.new { |y| y << 1 } thread = Thread.new { enum.peek } # fiber constructed inside the # block and saved inside `enum` thread.join sleep 5 # thread finishes and thread cache wait time runs out. # Native thread exits, possibly freeing its stack. GC.start # segfault because GC tires to mark the dangling stack pointer # inside `enum`'s fiber ``` The problem is masked by FIBER_USE_COROUTINE and FIBER_USE_NATIVE, as those implementations already do what this commit does. Generally on Linux systems, FIBER_USE_NATIVE is 1 even when one uses `./configure --disable-fiber-coroutine`, since most Linux systems have getcontext() and setcontext() which turns on FIBER_USE_NATIVE. (compile with `make DEFS="-DFIBER_USE_NATIVE=0" to explicitly disable it) Furthermore, when both FIBER_USE_COROUTINE and FIBER_USE_NATIVE are off, and the GC reads from the stack of a dead native thread, MRI does not segfault on Linux. This is probably due to libpthread not marking the page where the dead stack lives as unreadable. Nevertheless, this use-after-free is visible through Valgrind. On ruby_2_5, this is an acute problem, since it doesn't have FIBER_USE_COROUTINE. Thread cache is also unavailable for 2.5.x, triggering this issue more often. (thread cache gives this bug a grace period since it makes native threads wait a little before exiting) This issue is very visible on MacOS on 2.5.x since libpthread marks the dead stack as unreadable, consistently turning this use-after-free into a segfault. Fixes Bug #14561 * cont.c: Set saved_ec.machine.stack_end to NULL when switching away from a fiber to keep the GC marking it. `saved_ec` gets rehydrated with a stack pointer if/when the fiber runs again. --- cont.c | 1 + 1 file changed, 1 insertion(+) diff --git a/cont.c b/cont.c index 142713ad84..da469b6cd5 100644 --- a/cont.c +++ b/cont.c @@ -1747,6 +1747,7 @@ fiber_store(rb_fiber_t *next_fib, rb_thread_t *th) return fib->cont.value; #else /* FIBER_USE_NATIVE */ + fib->cont.saved_ec.machine.stack_end = NULL; if (ruby_setjmp(fib->cont.jmpbuf)) { /* restored */ fib = th->ec->fiber_ptr; -- 2.19.1