From abdc77a28859e1117c4bb99c23643c1b726323c1 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Thu, 30 Jul 2015 19:39:16 -0400 Subject: [PATCH] runtime: avoid reference to stale stack after GC shrinkstack Dangling pointer error. Unlikely to trigger in practice, but still. Found by running GODEBUG=efence=1 GOGC=1 trace.test. Change-Id: Ice474dedcf62dd33ab77526287a023ba3b166db9 Reviewed-on: https://go-review.googlesource.com/12991 Reviewed-by: Austin Clements --- src/runtime/mgc.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/runtime/mgc.go b/src/runtime/mgc.go index 614860e711..f0e05f61aa 100644 --- a/src/runtime/mgc.go +++ b/src/runtime/mgc.go @@ -1064,6 +1064,16 @@ func gc(mode int) { // need to switch to g0 so we can shrink the stack. systemstack(func() { gcMark(startTime) + // Must return immediately. + // The outer function's stack may have moved + // during gcMark (it shrinks stacks, including the + // outer function's stack), so we must not refer + // to any of its variables. Return back to the + // non-system stack to pick up the new addresses + // before continuing. + }) + + systemstack(func() { heap2 = work.bytesMarked if debug.gccheckmark > 0 { // Run a full stop-the-world mark using checkmark bits, -- 2.48.1