Skip to content

Commit 3e6c171

Browse files
randall77andybons
authored andcommitted
runtime: when using explicit argmap, also use arglen
When we set an explicit argmap, we may want only a prefix of that argmap. Argmap is set when the function is reflect.makeFuncStub or reflect.methodValueCall. In this case, arglen specifies how much of the args section is actually live. (It could be either all the args + results, or just the args.) Fixes #28752 Change-Id: Idf060607f15a298ac591016994e58e22f7f92d83 Reviewed-on: https://go-review.googlesource.com/c/149217 Run-TryBot: Keith Randall <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Austin Clements <[email protected]> (cherry picked from commit 0098f8a) Reviewed-on: https://go-review.googlesource.com/c/149457
1 parent c92a208 commit 3e6c171

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

src/runtime/stack.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,7 +1221,14 @@ func getStackMap(frame *stkframe, cache *pcvalueCache, debug bool) (locals, args
12211221
// Arguments.
12221222
if frame.arglen > 0 {
12231223
if frame.argmap != nil {
1224+
// argmap is set when the function is reflect.makeFuncStub or reflect.methodValueCall.
1225+
// In this case, arglen specifies how much of the args section is actually live.
1226+
// (It could be either all the args + results, or just the args.)
12241227
args = *frame.argmap
1228+
n := int32(frame.arglen / sys.PtrSize)
1229+
if n < args.n {
1230+
args.n = n // Don't use more of the arguments than arglen.
1231+
}
12251232
} else {
12261233
stackmap := (*stackmap)(funcdata(f, _FUNCDATA_ArgsPointerMaps))
12271234
if stackmap == nil || stackmap.n <= 0 {

test/fixedbugs/issue27695c.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// run
2+
3+
// Copyright 2018 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
// Make sure return values aren't scanned until they
8+
// are initialized, when calling functions and methods
9+
// via reflect.
10+
11+
package main
12+
13+
import (
14+
"io"
15+
"reflect"
16+
"runtime"
17+
"unsafe"
18+
)
19+
20+
var badPtr uintptr
21+
22+
var sink []byte
23+
24+
func init() {
25+
// Allocate large enough to use largeAlloc.
26+
b := make([]byte, 1<<16-1)
27+
sink = b // force heap allocation
28+
// Any space between the object and the end of page is invalid to point to.
29+
badPtr = uintptr(unsafe.Pointer(&b[len(b)-1])) + 1
30+
}
31+
32+
func f(d func(error) error) error {
33+
// Initialize callee args section with a bad pointer.
34+
g(badPtr, badPtr, badPtr, badPtr)
35+
36+
// Then call a function which returns a pointer.
37+
// That return slot starts out holding a bad pointer.
38+
return d(io.EOF)
39+
}
40+
41+
//go:noinline
42+
func g(x, y, z, w uintptr) {
43+
}
44+
45+
type T struct {
46+
}
47+
48+
func (t *T) Foo(e error) error {
49+
runtime.GC()
50+
return e
51+
}
52+
53+
func main() {
54+
// Functions
55+
d := reflect.MakeFunc(reflect.TypeOf(func(e error) error { return e }),
56+
func(args []reflect.Value) []reflect.Value {
57+
runtime.GC()
58+
return args
59+
}).Interface().(func(error) error)
60+
f(d)
61+
62+
// Methods
63+
x := reflect.ValueOf(&T{}).Method(0).Interface().(func(error) error)
64+
f(x)
65+
}

0 commit comments

Comments
 (0)