1
0
mirror of https://github.com/golang/go synced 2024-11-26 20:51:23 -07:00
go/test/fixedbugs/issue16331.go
Austin Clements d5bd797ee5 runtime: fix getArgInfo for deferred reflection calls
getArgInfo for reflect.makeFuncStub and reflect.methodValueCall is
necessarily special. These have dynamically determined argument maps
that are stored in their context (that is, their *funcval). These
functions are written to store this context at 0(SP) when called, and
getArgInfo retrieves it from there.

This technique works if getArgInfo is passed an active call frame for
one of these functions. However, getArgInfo is also used in
tracebackdefers, where the "call" is not a true call with an active
stack frame, but a deferred call. In this situation, getArgInfo
currently crashes because tracebackdefers passes a frame with sp set
to 0. However, the entire approach used by getArgInfo is flawed in
this situation because the wrapper has not actually executed, and
hence hasn't saved this metadata to any stack frame.

In the defer case, we know the *funcval from the _defer itself, so we
can fix this by teaching getArgInfo to use the *funcval context
directly when its available, and otherwise get it from the active call
frame.

While we're here, this commit simplifies getArgInfo a bit by making it
play more nicely with the type system. Rather than decoding the
*reflect.methodValue that is the wrapper's context as a *[2]uintptr,
just write out a copy of the reflect.methodValue type in the runtime.

Fixes #16331. Fixes #17471.

Change-Id: I81db4d985179b4a81c68c490cceeccbfc675456a
Reviewed-on: https://go-review.googlesource.com/31138
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
2016-10-17 18:57:01 +00:00

49 lines
839 B
Go

// run
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Perform tracebackdefers with a deferred reflection method.
package main
import "reflect"
type T struct{}
func (T) M() {
}
func F(args []reflect.Value) (results []reflect.Value) {
return nil
}
func main() {
done := make(chan bool)
go func() {
// Test reflect.makeFuncStub.
t := reflect.TypeOf((func())(nil))
f := reflect.MakeFunc(t, F).Interface().(func())
defer f()
growstack(10000)
done <- true
}()
<-done
go func() {
// Test reflect.methodValueCall.
f := reflect.ValueOf(T{}).Method(0).Interface().(func())
defer f()
growstack(10000)
done <- true
}()
<-done
}
func growstack(x int) {
if x == 0 {
return
}
growstack(x - 1)
}