You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
cmd/compile, runtime, reflect: pointers to go:notinheap types must be stored indirectly in interfaces #42076
(This bug is a consequence of the fixes for issue #40954.)
package main
import "reflect"
//go:notinheap
type T struct {
x int
}
func main() {
reflect.ValueOf((*T)(nil)).Pointer()
}
This program panics with:
panic: can't call pointer on a non-pointer Value
goroutine 1 [running]:
reflect.Value.pointer(...)
/Users/khr/sandbox/ro/src/reflect/value.go:95
reflect.Value.Pointer(0x1081e40, 0x0, 0x16, 0x0)
/Users/khr/sandbox/ro/src/reflect/value.go:1457 +0x1b6
main.main()
/Users/khr/gowork/tmp1.go:11 +0x9b
reflect is confused by a type that claims to be a pointer, but has no pointer bits. It has no pointer bits because pointers to go:notinheap types are treated as uintptrs by the compiler.
More generally, we can't ever convert *T to unsafe.Pointer, as that's the equivalent of converting a uintptr, potentially containing a bad pointer value, to a pointer. Particularly, we can't store a *T in the data field of an interface, or in reflect.Value.ptr. We have to store them indirectly instead.
This issue originally came up with the a call to reflect.DeepEqual which ended up calling Pointer on one of these types.
Note that reflect.DeepEqual fundamentally doesn't work on incomplete types like this. They are represented as struct{}, so pointer equality doesn't really work. This program:
package main
import (
"reflect"
"unsafe"
)
//go:notinheap
type T struct {
}
var a [2]int
func main() {
x := (*T)(unsafe.Pointer(&a[0]))
y := (*T)(unsafe.Pointer(&a[1]))
println(reflect.DeepEqual(x, y))
}
Will print true after this issue is fixed, although one would probably expect it to return false. Something about incomplete types and deepequal just don't play well together.
I know this is ugly, but would it make sense to add an annotation, eg, :opaque, to use for marking such opaque pointers and having the reflect package understand it - i.e. a pointer that's ignored by gc, but used by reflect for direct pointer comparison? This would allow the opaque pointer to be wrapped by a go type and a finalizer can be used to free the underlying type in C or whatever other language is being used. I guess it would require an extra field in reflect.Value which is an overhead.
A complication in the runtime. pollDesc is marked as go:notinheap. It has a field, wt, which is a timer. That has a field arg which is an interface{}. Then at netpoll.go:290, we do (where pd is a *pollDesc):
pd.wt.arg = pd
That's writing a pointer to a go:notinheap type into an interface{}. Normally, that's fine, as we're just writing a pointer to the data field of an interface{}. But when I try to treat pointers to go:notinheap types as scalars, I can't store that scalar in the interface data field directly. I need to allocate some storage so I can store it indirectly. Allocation here is bad. The malloc call itself works, but then we're writing a pointer to that allocation to a location which isn't in the heap and won't get scanned.
Not sure why the prohibition on implicit allocation in the runtime didn't catch this.
This kind of implicit allocation isn't caught by the runtime prohibition. I added a check to see what happens, and it seems to trigger quite a bit, all on conversions to the argument of panic. Maybe we should make those explicit somehow? Maybe later.
Assuming they're mostly a few common data types (ints, strings, etc), seems like with some helpers this would go pretty fast. And if it'd catch bugs, seems worthwhile. Alternatively, we could teach the compiler (and runtime backtracing) to use specialized panic handlers for common panic arg types, thereby avoiding the code weight associated with allocations in everyone's code.
Activity
bartle-stripe commentedon Oct 19, 2020
Would you expect this to also manifest as a segmentation fault? From running
go test github.com/bsm/go-sparkey/...:randall77 commentedon Oct 19, 2020
@bartle-stripe I don't think that would be related, no.
bartle-stripe commentedon Oct 19, 2020
Hm, so git bisect is implicating 9698372 and I can repro this pretty readily.
randall77 commentedon Oct 20, 2020
@bartle-stripe Interesting. Can you open a new issue with all the details?
cosnicolaou commentedon Oct 20, 2020
I know this is ugly, but would it make sense to add an annotation, eg, :opaque, to use for marking such opaque pointers and having the reflect package understand it - i.e. a pointer that's ignored by gc, but used by reflect for direct pointer comparison? This would allow the opaque pointer to be wrapped by a go type and a finalizer can be used to free the underlying type in C or whatever other language is being used. I guess it would require an extra field in reflect.Value which is an overhead.
randall77 commentedon Oct 22, 2020
A complication in the runtime.
pollDescis marked asgo:notinheap. It has a field,wt, which is atimer. That has a fieldargwhich is aninterface{}. Then atnetpoll.go:290, we do (wherepdis a*pollDesc):That's writing a pointer to a
go:notinheaptype into aninterface{}. Normally, that's fine, as we're just writing a pointer to the data field of aninterface{}. But when I try to treat pointers togo:notinheaptypes as scalars, I can't store that scalar in the interface data field directly. I need to allocate some storage so I can store it indirectly. Allocation here is bad. The malloc call itself works, but then we're writing a pointer to that allocation to a location which isn't in the heap and won't get scanned.Not sure why the prohibition on implicit allocation in the runtime didn't catch this.
randall77 commentedon Oct 22, 2020
This kind of implicit allocation isn't caught by the runtime prohibition. I added a check to see what happens, and it seems to trigger quite a bit, all on conversions to the argument of
panic. Maybe we should make those explicit somehow? Maybe later.josharian commentedon Oct 22, 2020
Assuming they're mostly a few common data types (ints, strings, etc), seems like with some helpers this would go pretty fast. And if it'd catch bugs, seems worthwhile. Alternatively, we could teach the compiler (and runtime backtracing) to use specialized panic handlers for common panic arg types, thereby avoiding the code weight associated with allocations in everyone's code.
gopherbot commentedon Oct 22, 2020
Change https://golang.org/cl/264480 mentions this issue:
cmd/compile, runtime: store pointers to go:notinheap types indirectlyrandall77 commentedon Oct 23, 2020
@gopherbot Please open a backport issue for 1.15.
gopherbot commentedon Oct 23, 2020
Backport issue(s) opened: #42169 (for 1.15).
Remember to create the cherry-pick CL(s) as soon as the patch is submitted to master, according to https://golang.org/wiki/MinorReleases.
gopherbot commentedon Oct 27, 2020
Change https://golang.org/cl/265757 mentions this issue:
cmd/compile: print pointers to go:notinheap types without converting to unsafe.Pointercmd/compile: print pointers to go:notinheap types without converting …
gopherbot commentedon Apr 9, 2021
Change https://golang.org/cl/308970 mentions this issue:
reflect: make go:notinheap pointers indirect in Newreflect: panic on New of go:notinheap type
gopherbot commentedon Aug 9, 2021
Change https://golang.org/cl/340950 mentions this issue:
test: add test case for CL 340609gopherbot commentedon Aug 10, 2021
Change https://golang.org/cl/340609 mentions this issue:
compiler: store pointers to go:notinheap types indirectlycompiler: store pointers to go:notinheap types indirectly
compiler: store pointers to go:notinheap types indirectly
test: add test case for CL 340609
cmd/compile, runtime: store pointers to go:notinheap types indirectly