Skip to content

Commit f75a45c

Browse files
danscalesianlancetaylor
authored andcommitted
[release-branch.go1.14] runtime: fix code so defer record is not added to g0 defer list during panic
newdefer() actually adds the new defer to the current g's defer chain. That happens even if we are on the system stack, in which case the g will be the g0 stack. For open-coded defers, we call newdefer() (only during panic processing) while on the system stack, so the new defer is unintentionally added to the g0._defer defer list. The code later correctly adds the defer to the user g's defer list. The g0._defer list is never used. However, that pointer on the g0._defer list can keep a defer struct alive that is intended to be garbage-collected (smaller defers use a defer pool, but larger-sized defer records are just GC'ed). freedefer() does not zero out pointers when it intends that a defer become garbage-collected. So, we can have the pointers in a defer that is held alive by g0._defer become invalid (in particular d.link). This is the cause of the bad pointer bug in this issue The fix is to change newdefer (only used in two places) to not add the new defer to the gp._defer list. We just do it after the call with the correct gp pointer. (As mentioned above, this code was already there after the newdefer in addOneOpenDeferFrame.) That ensures that defers will be correctly garbage-collected and eliminate the bad pointer. This fix definitely fixes the original repro. I added a test and tried hard to reproduce the bug (based on the original repro code), but awasn't actually able to cause the bug. However, the test is still an interesting mix of heap-allocated, stack-allocated, and open-coded defers. For #37688 Fixes #37968 Fixes #37688 Change-Id: I1a481b9d9e9b9ba4e8726ef718a1f4512a2d6faf Reviewed-on: https://go-review.googlesource.com/c/go/+/224581 Run-TryBot: Dan Scales <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Keith Randall <[email protected]> (cherry picked from commit 825ae71) Reviewed-on: https://go-review.googlesource.com/c/go/+/225279 Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: Dan Scales <[email protected]>
1 parent ab9d037 commit f75a45c

File tree

2 files changed

+72
-4
lines changed

2 files changed

+72
-4
lines changed

src/runtime/defer_test.go

+66
Original file line numberDiff line numberDiff line change
@@ -335,3 +335,69 @@ func recurseFn(level int, maxlevel int) {
335335
panic("recurseFn panic")
336336
}
337337
}
338+
339+
// Try to reproduce issue #37688, where a pointer to an open-coded defer struct is
340+
// mistakenly held, and that struct keeps a pointer to a stack-allocated defer
341+
// struct, and that stack-allocated struct gets overwritten or the stack gets
342+
// moved, so a memory error happens on GC.
343+
func TestIssue37688(t *testing.T) {
344+
for j := 0; j < 10; j++ {
345+
g2()
346+
g3()
347+
}
348+
}
349+
350+
type foo struct {
351+
}
352+
353+
func (f *foo) method1() {
354+
fmt.Fprintln(os.Stderr, "method1")
355+
}
356+
357+
func (f *foo) method2() {
358+
fmt.Fprintln(os.Stderr, "method2")
359+
}
360+
361+
func g2() {
362+
var a foo
363+
ap := &a
364+
// The loop forces this defer to be heap-allocated and the remaining two
365+
// to be stack-allocated.
366+
for i := 0; i < 1; i++ {
367+
defer ap.method1()
368+
}
369+
defer ap.method2()
370+
defer ap.method1()
371+
ff1(ap, 1, 2, 3, 4, 5, 6, 7, 8, 9)
372+
// Try to get the stack to be be moved by growing it too large, so
373+
// existing stack-allocated defer becomes invalid.
374+
rec1(2000)
375+
}
376+
377+
func g3() {
378+
// Mix up the stack layout by adding in an extra function frame
379+
g2()
380+
}
381+
382+
func ff1(ap *foo, a, b, c, d, e, f, g, h, i int) {
383+
defer ap.method1()
384+
385+
// Make a defer that has a very large set of args, hence big size for the
386+
// defer record for the open-coded frame (which means it won't use the
387+
// defer pool)
388+
defer func(ap *foo, a, b, c, d, e, f, g, h, i int) {
389+
if v := recover(); v != nil {
390+
fmt.Fprintln(os.Stderr, "did recover")
391+
}
392+
fmt.Fprintln(os.Stderr, "debug", ap, a, b, c, d, e, f, g, h)
393+
}(ap, a, b, c, d, e, f, g, h, i)
394+
panic("ff1 panic")
395+
}
396+
397+
func rec1(max int) {
398+
if max > 0 {
399+
rec1(max - 1)
400+
} else {
401+
fmt.Fprintln(os.Stderr, "finished recursion", max)
402+
}
403+
}

src/runtime/panic.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ func panicmem() {
216216
// The compiler turns a defer statement into a call to this.
217217
//go:nosplit
218218
func deferproc(siz int32, fn *funcval) { // arguments of fn follow fn
219-
if getg().m.curg != getg() {
219+
gp := getg()
220+
if gp.m.curg != gp {
220221
// go code on the system stack can't defer
221222
throw("defer on system stack")
222223
}
@@ -234,6 +235,8 @@ func deferproc(siz int32, fn *funcval) { // arguments of fn follow fn
234235
if d._panic != nil {
235236
throw("deferproc: d.panic != nil after newdefer")
236237
}
238+
d.link = gp._defer
239+
gp._defer = d
237240
d.fn = fn
238241
d.pc = callerpc
239242
d.sp = sp
@@ -374,7 +377,8 @@ func init() {
374377
}
375378

376379
// Allocate a Defer, usually using per-P pool.
377-
// Each defer must be released with freedefer.
380+
// Each defer must be released with freedefer. The defer is not
381+
// added to any defer chain yet.
378382
//
379383
// This must not grow the stack because there may be a frame without
380384
// stack map information when this is called.
@@ -424,8 +428,6 @@ func newdefer(siz int32) *_defer {
424428
}
425429
d.siz = siz
426430
d.heap = true
427-
d.link = gp._defer
428-
gp._defer = d
429431
return d
430432
}
431433

0 commit comments

Comments
 (0)