reflect: make Value.Pointer panic on bad notinheap pointers

Same as CL 350153 did for Value.Elem to panic on bad notinheap pointers.
While at it, also add more tests for notinheap deref.

Change-Id: Id7d9d12ad8467de5926b6a7e8f9d659fea5fedb5
Reviewed-on: https://go-review.googlesource.com/c/go/+/357630
Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
This commit is contained in:
Cuong Manh Le 2021-10-21 23:35:05 +07:00
parent 3beab0cd71
commit 23e57e5955
2 changed files with 9 additions and 5 deletions

View file

@ -7721,6 +7721,8 @@ func TestNotInHeapDeref(t *testing.T) {
v = ValueOf((*nih)(unsafe.Pointer(new(int)))) v = ValueOf((*nih)(unsafe.Pointer(new(int))))
shouldPanic("reflect: reflect.Value.Elem on an invalid notinheap pointer", func() { v.Elem() }) shouldPanic("reflect: reflect.Value.Elem on an invalid notinheap pointer", func() { v.Elem() })
shouldPanic("reflect: reflect.Value.Pointer on an invalid notinheap pointer", func() { v.Pointer() })
shouldPanic("reflect: reflect.Value.UnsafePointer on an invalid notinheap pointer", func() { v.UnsafePointer() })
} }
func TestMethodCallValueCodePtr(t *testing.T) { func TestMethodCallValueCodePtr(t *testing.T) {

View file

@ -1940,11 +1940,13 @@ func (v Value) Pointer() uintptr {
switch k { switch k {
case Ptr: case Ptr:
if v.typ.ptrdata == 0 { if v.typ.ptrdata == 0 {
// Handle pointers to go:notinheap types directly, val := *(*uintptr)(v.ptr)
// so we never materialize such pointers as an // Since it is a not-in-heap pointer, all pointers to the heap are
// unsafe.Pointer. (Such pointers are always indirect.) // forbidden! See comment in Value.Elem and issue #48399.
// See issue 42076. if !verifyNotInHeapPtr(val) {
return *(*uintptr)(v.ptr) panic("reflect: reflect.Value.Pointer on an invalid notinheap pointer")
}
return val
} }
fallthrough fallthrough
case Chan, Map, UnsafePointer: case Chan, Map, UnsafePointer: