runtime: make iface/eface handling more type safe

Change compiler-invoked interface functions to directly take
iface/eface parameters instead of fInterface/interface{} to avoid
needing to always convert.

For the handful of functions that legitimately need to take an
interface{} parameter, add efaceOf to type-safely convert *interface{}
to *eface.

Change-Id: I8928761a12fd3c771394f36adf93d3006a9fcf39
Reviewed-on: https://go-review.googlesource.com/16166
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Matthew Dempsky 2015-10-21 12:12:25 -07:00
parent 03b0065204
commit 84afa1be76
9 changed files with 99 additions and 142 deletions

View file

@ -226,18 +226,12 @@ func strequal(p, q unsafe.Pointer) bool {
return *(*string)(p) == *(*string)(q) return *(*string)(p) == *(*string)(q)
} }
func interequal(p, q unsafe.Pointer) bool { func interequal(p, q unsafe.Pointer) bool {
return ifaceeq(*(*interface { return ifaceeq(*(*iface)(p), *(*iface)(q))
f()
})(p), *(*interface {
f()
})(q))
} }
func nilinterequal(p, q unsafe.Pointer) bool { func nilinterequal(p, q unsafe.Pointer) bool {
return efaceeq(*(*interface{})(p), *(*interface{})(q)) return efaceeq(*(*eface)(p), *(*eface)(q))
} }
func efaceeq(p, q interface{}) bool { func efaceeq(x, y eface) bool {
x := (*eface)(unsafe.Pointer(&p))
y := (*eface)(unsafe.Pointer(&q))
t := x._type t := x._type
if t != y._type { if t != y._type {
return false return false
@ -254,11 +248,7 @@ func efaceeq(p, q interface{}) bool {
} }
return eq(x.data, y.data) return eq(x.data, y.data)
} }
func ifaceeq(p, q interface { func ifaceeq(x, y iface) bool {
f()
}) bool {
x := (*iface)(unsafe.Pointer(&p))
y := (*iface)(unsafe.Pointer(&q))
xtab := x.tab xtab := x.tab
if xtab != y.tab { if xtab != y.tab {
return false return false

View file

@ -4,8 +4,6 @@
package runtime package runtime
import "unsafe"
// The Error interface identifies a run time error. // The Error interface identifies a run time error.
type Error interface { type Error interface {
error error
@ -57,7 +55,7 @@ type stringer interface {
} }
func typestring(x interface{}) string { func typestring(x interface{}) string {
e := (*eface)(unsafe.Pointer(&x)) e := efaceOf(&x)
return *e._type._string return *e._type._string
} }

View file

@ -127,7 +127,7 @@ var BigEndian = _BigEndian
// For benchmarking. // For benchmarking.
func BenchSetType(n int, x interface{}) { func BenchSetType(n int, x interface{}) {
e := *(*eface)(unsafe.Pointer(&x)) e := *efaceOf(&x)
t := e._type t := e._type
var size uintptr var size uintptr
var p unsafe.Pointer var p unsafe.Pointer

View file

@ -379,7 +379,7 @@ func dumpgoroutine(gp *g) {
dumpint(tagPanic) dumpint(tagPanic)
dumpint(uint64(uintptr(unsafe.Pointer(p)))) dumpint(uint64(uintptr(unsafe.Pointer(p))))
dumpint(uint64(uintptr(unsafe.Pointer(gp)))) dumpint(uint64(uintptr(unsafe.Pointer(gp))))
eface := (*eface)(unsafe.Pointer(&p.arg)) eface := efaceOf(&p.arg)
dumpint(uint64(uintptr(unsafe.Pointer(eface._type)))) dumpint(uint64(uintptr(unsafe.Pointer(eface._type))))
dumpint(uint64(uintptr(unsafe.Pointer(eface.data)))) dumpint(uint64(uintptr(unsafe.Pointer(eface.data))))
dumpint(0) // was p->defer, no longer recorded dumpint(0) // was p->defer, no longer recorded

View file

@ -15,13 +15,6 @@ var (
hash [hashSize]*itab hash [hashSize]*itab
) )
// fInterface is our standard non-empty interface. We use it instead
// of interface{f()} in function prototypes because gofmt insists on
// putting lots of newlines in the otherwise concise interface{f()}.
type fInterface interface {
f()
}
func getitab(inter *interfacetype, typ *_type, canfail bool) *itab { func getitab(inter *interfacetype, typ *_type, canfail bool) *itab {
if len(inter.mhdr) == 0 { if len(inter.mhdr) == 0 {
throw("internal error - misuse of itab") throw("internal error - misuse of itab")
@ -128,17 +121,16 @@ func typ2Itab(t *_type, inter *interfacetype, cache **itab) *itab {
return tab return tab
} }
func convT2E(t *_type, elem unsafe.Pointer, x unsafe.Pointer) (e interface{}) { func convT2E(t *_type, elem unsafe.Pointer, x unsafe.Pointer) (e eface) {
if raceenabled { if raceenabled {
raceReadObjectPC(t, elem, getcallerpc(unsafe.Pointer(&t)), funcPC(convT2E)) raceReadObjectPC(t, elem, getcallerpc(unsafe.Pointer(&t)), funcPC(convT2E))
} }
if msanenabled { if msanenabled {
msanread(elem, t.size) msanread(elem, t.size)
} }
ep := (*eface)(unsafe.Pointer(&e))
if isDirectIface(t) { if isDirectIface(t) {
ep._type = t e._type = t
typedmemmove(t, unsafe.Pointer(&ep.data), elem) typedmemmove(t, unsafe.Pointer(&e.data), elem)
} else { } else {
if x == nil { if x == nil {
x = newobject(t) x = newobject(t)
@ -146,13 +138,13 @@ func convT2E(t *_type, elem unsafe.Pointer, x unsafe.Pointer) (e interface{}) {
// TODO: We allocate a zeroed object only to overwrite it with // TODO: We allocate a zeroed object only to overwrite it with
// actual data. Figure out how to avoid zeroing. Also below in convT2I. // actual data. Figure out how to avoid zeroing. Also below in convT2I.
typedmemmove(t, x, elem) typedmemmove(t, x, elem)
ep._type = t e._type = t
ep.data = x e.data = x
} }
return return
} }
func convT2I(t *_type, inter *interfacetype, cache **itab, elem unsafe.Pointer, x unsafe.Pointer) (i fInterface) { func convT2I(t *_type, inter *interfacetype, cache **itab, elem unsafe.Pointer, x unsafe.Pointer) (i iface) {
if raceenabled { if raceenabled {
raceReadObjectPC(t, elem, getcallerpc(unsafe.Pointer(&t)), funcPC(convT2I)) raceReadObjectPC(t, elem, getcallerpc(unsafe.Pointer(&t)), funcPC(convT2I))
} }
@ -164,17 +156,16 @@ func convT2I(t *_type, inter *interfacetype, cache **itab, elem unsafe.Pointer,
tab = getitab(inter, t, false) tab = getitab(inter, t, false)
atomicstorep(unsafe.Pointer(cache), unsafe.Pointer(tab)) atomicstorep(unsafe.Pointer(cache), unsafe.Pointer(tab))
} }
pi := (*iface)(unsafe.Pointer(&i))
if isDirectIface(t) { if isDirectIface(t) {
pi.tab = tab i.tab = tab
typedmemmove(t, unsafe.Pointer(&pi.data), elem) typedmemmove(t, unsafe.Pointer(&i.data), elem)
} else { } else {
if x == nil { if x == nil {
x = newobject(t) x = newobject(t)
} }
typedmemmove(t, x, elem) typedmemmove(t, x, elem)
pi.tab = tab i.tab = tab
pi.data = x i.data = x
} }
return return
} }
@ -187,9 +178,8 @@ func panicdottype(have, want, iface *_type) {
panic(&TypeAssertionError{*iface._string, haveString, *want._string, ""}) panic(&TypeAssertionError{*iface._string, haveString, *want._string, ""})
} }
func assertI2T(t *_type, i fInterface, r unsafe.Pointer) { func assertI2T(t *_type, i iface, r unsafe.Pointer) {
ip := (*iface)(unsafe.Pointer(&i)) tab := i.tab
tab := ip.tab
if tab == nil { if tab == nil {
panic(&TypeAssertionError{"", "", *t._string, ""}) panic(&TypeAssertionError{"", "", *t._string, ""})
} }
@ -198,16 +188,15 @@ func assertI2T(t *_type, i fInterface, r unsafe.Pointer) {
} }
if r != nil { if r != nil {
if isDirectIface(t) { if isDirectIface(t) {
writebarrierptr((*uintptr)(r), uintptr(ip.data)) writebarrierptr((*uintptr)(r), uintptr(i.data))
} else { } else {
typedmemmove(t, r, ip.data) typedmemmove(t, r, i.data)
} }
} }
} }
func assertI2T2(t *_type, i fInterface, r unsafe.Pointer) bool { func assertI2T2(t *_type, i iface, r unsafe.Pointer) bool {
ip := (*iface)(unsafe.Pointer(&i)) tab := i.tab
tab := ip.tab
if tab == nil || tab._type != t { if tab == nil || tab._type != t {
if r != nil { if r != nil {
memclr(r, uintptr(t.size)) memclr(r, uintptr(t.size))
@ -216,27 +205,26 @@ func assertI2T2(t *_type, i fInterface, r unsafe.Pointer) bool {
} }
if r != nil { if r != nil {
if isDirectIface(t) { if isDirectIface(t) {
writebarrierptr((*uintptr)(r), uintptr(ip.data)) writebarrierptr((*uintptr)(r), uintptr(i.data))
} else { } else {
typedmemmove(t, r, ip.data) typedmemmove(t, r, i.data)
} }
} }
return true return true
} }
func assertE2T(t *_type, e interface{}, r unsafe.Pointer) { func assertE2T(t *_type, e eface, r unsafe.Pointer) {
ep := (*eface)(unsafe.Pointer(&e)) if e._type == nil {
if ep._type == nil {
panic(&TypeAssertionError{"", "", *t._string, ""}) panic(&TypeAssertionError{"", "", *t._string, ""})
} }
if ep._type != t { if e._type != t {
panic(&TypeAssertionError{"", *ep._type._string, *t._string, ""}) panic(&TypeAssertionError{"", *e._type._string, *t._string, ""})
} }
if r != nil { if r != nil {
if isDirectIface(t) { if isDirectIface(t) {
writebarrierptr((*uintptr)(r), uintptr(ep.data)) writebarrierptr((*uintptr)(r), uintptr(e.data))
} else { } else {
typedmemmove(t, r, ep.data) typedmemmove(t, r, e.data)
} }
} }
} }
@ -244,101 +232,89 @@ func assertE2T(t *_type, e interface{}, r unsafe.Pointer) {
var testingAssertE2T2GC bool var testingAssertE2T2GC bool
// The compiler ensures that r is non-nil. // The compiler ensures that r is non-nil.
func assertE2T2(t *_type, e interface{}, r unsafe.Pointer) bool { func assertE2T2(t *_type, e eface, r unsafe.Pointer) bool {
if testingAssertE2T2GC { if testingAssertE2T2GC {
GC() GC()
} }
ep := (*eface)(unsafe.Pointer(&e)) if e._type != t {
if ep._type != t {
memclr(r, uintptr(t.size)) memclr(r, uintptr(t.size))
return false return false
} }
if isDirectIface(t) { if isDirectIface(t) {
writebarrierptr((*uintptr)(r), uintptr(ep.data)) writebarrierptr((*uintptr)(r), uintptr(e.data))
} else { } else {
typedmemmove(t, r, ep.data) typedmemmove(t, r, e.data)
} }
return true return true
} }
func convI2E(i fInterface) (r interface{}) { func convI2E(i iface) (r eface) {
ip := (*iface)(unsafe.Pointer(&i)) tab := i.tab
tab := ip.tab
if tab == nil { if tab == nil {
return return
} }
rp := (*eface)(unsafe.Pointer(&r)) r._type = tab._type
rp._type = tab._type r.data = i.data
rp.data = ip.data
return return
} }
func assertI2E(inter *interfacetype, i fInterface, r *interface{}) { func assertI2E(inter *interfacetype, i iface, r *eface) {
ip := (*iface)(unsafe.Pointer(&i)) tab := i.tab
tab := ip.tab
if tab == nil { if tab == nil {
// explicit conversions require non-nil interface value. // explicit conversions require non-nil interface value.
panic(&TypeAssertionError{"", "", *inter.typ._string, ""}) panic(&TypeAssertionError{"", "", *inter.typ._string, ""})
} }
rp := (*eface)(unsafe.Pointer(r)) r._type = tab._type
rp._type = tab._type r.data = i.data
rp.data = ip.data
return return
} }
// The compiler ensures that r is non-nil. // The compiler ensures that r is non-nil.
func assertI2E2(inter *interfacetype, i fInterface, r *interface{}) bool { func assertI2E2(inter *interfacetype, i iface, r *eface) bool {
ip := (*iface)(unsafe.Pointer(&i)) tab := i.tab
tab := ip.tab
if tab == nil { if tab == nil {
return false return false
} }
rp := (*eface)(unsafe.Pointer(r)) r._type = tab._type
rp._type = tab._type r.data = i.data
rp.data = ip.data
return true return true
} }
func convI2I(inter *interfacetype, i fInterface) (r fInterface) { func convI2I(inter *interfacetype, i iface) (r iface) {
ip := (*iface)(unsafe.Pointer(&i)) tab := i.tab
tab := ip.tab
if tab == nil { if tab == nil {
return return
} }
rp := (*iface)(unsafe.Pointer(&r))
if tab.inter == inter { if tab.inter == inter {
rp.tab = tab r.tab = tab
rp.data = ip.data r.data = i.data
return return
} }
rp.tab = getitab(inter, tab._type, false) r.tab = getitab(inter, tab._type, false)
rp.data = ip.data r.data = i.data
return return
} }
func assertI2I(inter *interfacetype, i fInterface, r *fInterface) { func assertI2I(inter *interfacetype, i iface, r *iface) {
ip := (*iface)(unsafe.Pointer(&i)) tab := i.tab
tab := ip.tab
if tab == nil { if tab == nil {
// explicit conversions require non-nil interface value. // explicit conversions require non-nil interface value.
panic(&TypeAssertionError{"", "", *inter.typ._string, ""}) panic(&TypeAssertionError{"", "", *inter.typ._string, ""})
} }
rp := (*iface)(unsafe.Pointer(r))
if tab.inter == inter { if tab.inter == inter {
rp.tab = tab r.tab = tab
rp.data = ip.data r.data = i.data
return return
} }
rp.tab = getitab(inter, tab._type, false) r.tab = getitab(inter, tab._type, false)
rp.data = ip.data r.data = i.data
} }
func assertI2I2(inter *interfacetype, i fInterface, r *fInterface) bool { func assertI2I2(inter *interfacetype, i iface, r *iface) bool {
ip := (*iface)(unsafe.Pointer(&i)) tab := i.tab
tab := ip.tab
if tab == nil { if tab == nil {
if r != nil { if r != nil {
*r = nil *r = iface{}
} }
return false return false
} }
@ -346,68 +322,62 @@ func assertI2I2(inter *interfacetype, i fInterface, r *fInterface) bool {
tab = getitab(inter, tab._type, true) tab = getitab(inter, tab._type, true)
if tab == nil { if tab == nil {
if r != nil { if r != nil {
*r = nil *r = iface{}
} }
return false return false
} }
} }
if r != nil { if r != nil {
rp := (*iface)(unsafe.Pointer(r)) r.tab = tab
rp.tab = tab r.data = i.data
rp.data = ip.data
} }
return true return true
} }
func assertE2I(inter *interfacetype, e interface{}, r *fInterface) { func assertE2I(inter *interfacetype, e eface, r *iface) {
ep := (*eface)(unsafe.Pointer(&e)) t := e._type
t := ep._type
if t == nil { if t == nil {
// explicit conversions require non-nil interface value. // explicit conversions require non-nil interface value.
panic(&TypeAssertionError{"", "", *inter.typ._string, ""}) panic(&TypeAssertionError{"", "", *inter.typ._string, ""})
} }
rp := (*iface)(unsafe.Pointer(r)) r.tab = getitab(inter, t, false)
rp.tab = getitab(inter, t, false) r.data = e.data
rp.data = ep.data
} }
var testingAssertE2I2GC bool var testingAssertE2I2GC bool
func assertE2I2(inter *interfacetype, e interface{}, r *fInterface) bool { func assertE2I2(inter *interfacetype, e eface, r *iface) bool {
if testingAssertE2I2GC { if testingAssertE2I2GC {
GC() GC()
} }
ep := (*eface)(unsafe.Pointer(&e)) t := e._type
t := ep._type
if t == nil { if t == nil {
if r != nil { if r != nil {
*r = nil *r = iface{}
} }
return false return false
} }
tab := getitab(inter, t, true) tab := getitab(inter, t, true)
if tab == nil { if tab == nil {
if r != nil { if r != nil {
*r = nil *r = iface{}
} }
return false return false
} }
if r != nil { if r != nil {
rp := (*iface)(unsafe.Pointer(r)) r.tab = tab
rp.tab = tab r.data = e.data
rp.data = ep.data
} }
return true return true
} }
//go:linkname reflect_ifaceE2I reflect.ifaceE2I //go:linkname reflect_ifaceE2I reflect.ifaceE2I
func reflect_ifaceE2I(inter *interfacetype, e interface{}, dst *fInterface) { func reflect_ifaceE2I(inter *interfacetype, e eface, dst *iface) {
assertE2I(inter, e, dst) assertE2I(inter, e, dst)
} }
func assertE2E(inter *interfacetype, e interface{}, r *interface{}) { func assertE2E(inter *interfacetype, e eface, r *eface) {
ep := (*eface)(unsafe.Pointer(&e)) if e._type == nil {
if ep._type == nil {
// explicit conversions require non-nil interface value. // explicit conversions require non-nil interface value.
panic(&TypeAssertionError{"", "", *inter.typ._string, ""}) panic(&TypeAssertionError{"", "", *inter.typ._string, ""})
} }
@ -415,28 +385,25 @@ func assertE2E(inter *interfacetype, e interface{}, r *interface{}) {
} }
// The compiler ensures that r is non-nil. // The compiler ensures that r is non-nil.
func assertE2E2(inter *interfacetype, e interface{}, r *interface{}) bool { func assertE2E2(inter *interfacetype, e eface, r *eface) bool {
ep := (*eface)(unsafe.Pointer(&e)) if e._type == nil {
if ep._type == nil { *r = eface{}
*r = nil
return false return false
} }
*r = e *r = e
return true return true
} }
func ifacethash(i fInterface) uint32 { func ifacethash(i iface) uint32 {
ip := (*iface)(unsafe.Pointer(&i)) tab := i.tab
tab := ip.tab
if tab == nil { if tab == nil {
return 0 return 0
} }
return tab._type.hash return tab._type.hash
} }
func efacethash(e interface{}) uint32 { func efacethash(e eface) uint32 {
ep := (*eface)(unsafe.Pointer(&e)) t := e._type
t := ep._type
if t == nil { if t == nil {
return 0 return 0
} }

View file

@ -1623,7 +1623,7 @@ func getgcmaskcb(frame *stkframe, ctxt unsafe.Pointer) bool {
//go:linkname reflect_gcbits reflect.gcbits //go:linkname reflect_gcbits reflect.gcbits
func reflect_gcbits(x interface{}) []byte { func reflect_gcbits(x interface{}) []byte {
ret := getgcmask(x) ret := getgcmask(x)
typ := (*ptrtype)(unsafe.Pointer((*eface)(unsafe.Pointer(&x))._type)).elem typ := (*ptrtype)(unsafe.Pointer(efaceOf(&x)._type)).elem
nptr := typ.ptrdata / ptrSize nptr := typ.ptrdata / ptrSize
for uintptr(len(ret)) > nptr && ret[len(ret)-1] == 0 { for uintptr(len(ret)) > nptr && ret[len(ret)-1] == 0 {
ret = ret[:len(ret)-1] ret = ret[:len(ret)-1]
@ -1633,7 +1633,7 @@ func reflect_gcbits(x interface{}) []byte {
// Returns GC type info for object p for testing. // Returns GC type info for object p for testing.
func getgcmask(ep interface{}) (mask []byte) { func getgcmask(ep interface{}) (mask []byte) {
e := *(*eface)(unsafe.Pointer(&ep)) e := *efaceOf(&ep)
p := e.data p := e.data
t := e._type t := e._type
// data or bss // data or bss

View file

@ -187,7 +187,7 @@ func runfinq() {
if len(ityp.mhdr) != 0 { if len(ityp.mhdr) != 0 {
// convert to interface with methods // convert to interface with methods
// this conversion is guaranteed to succeed - we checked in SetFinalizer // this conversion is guaranteed to succeed - we checked in SetFinalizer
assertE2I(ityp, *(*interface{})(frame), (*fInterface)(frame)) assertE2I(ityp, *(*eface)(frame), (*iface)(frame))
} }
default: default:
throw("bad kind in runfinq") throw("bad kind in runfinq")
@ -264,7 +264,7 @@ func SetFinalizer(obj interface{}, finalizer interface{}) {
// (and we don't have the data structures to record them). // (and we don't have the data structures to record them).
return return
} }
e := (*eface)(unsafe.Pointer(&obj)) e := efaceOf(&obj)
etyp := e._type etyp := e._type
if etyp == nil { if etyp == nil {
throw("runtime.SetFinalizer: first argument is nil") throw("runtime.SetFinalizer: first argument is nil")
@ -313,7 +313,7 @@ func SetFinalizer(obj interface{}, finalizer interface{}) {
} }
} }
f := (*eface)(unsafe.Pointer(&finalizer)) f := efaceOf(&finalizer)
ftyp := f._type ftyp := f._type
if ftyp == nil { if ftyp == nil {
// switch to system stack and remove finalizer // switch to system stack and remove finalizer
@ -347,7 +347,7 @@ func SetFinalizer(obj interface{}, finalizer interface{}) {
// ok - satisfies empty interface // ok - satisfies empty interface
goto okarg goto okarg
} }
if assertE2I2(ityp, obj, nil) { if assertE2I2(ityp, *efaceOf(&obj), nil) {
goto okarg goto okarg
} }
} }

View file

@ -220,12 +220,10 @@ func printslice(s []byte) {
printpointer(unsafe.Pointer(sp.array)) printpointer(unsafe.Pointer(sp.array))
} }
func printeface(e interface{}) { func printeface(e eface) {
ep := (*eface)(unsafe.Pointer(&e)) print("(", e._type, ",", e.data, ")")
print("(", ep._type, ",", ep.data, ")")
} }
func printiface(i fInterface) { func printiface(i iface) {
ip := (*iface)(unsafe.Pointer(&i)) print("(", i.tab, ",", i.data, ")")
print("(", ip.tab, ",", ip.data, ")")
} }

View file

@ -82,6 +82,10 @@ type eface struct {
data unsafe.Pointer data unsafe.Pointer
} }
func efaceOf(ep *interface{}) *eface {
return (*eface)(unsafe.Pointer(ep))
}
// The guintptr, muintptr, and puintptr are all used to bypass write barriers. // The guintptr, muintptr, and puintptr are all used to bypass write barriers.
// It is particularly important to avoid write barriers when the current P has // It is particularly important to avoid write barriers when the current P has
// been released, because the GC thinks the world is stopped, and an // been released, because the GC thinks the world is stopped, and an