Commit 84afa1be authored by Matthew Dempsky's avatar Matthew Dempsky

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: 's avatarIan Lance Taylor <iant@golang.org>
parent 03b00652
......@@ -226,18 +226,12 @@ func strequal(p, q unsafe.Pointer) bool {
return *(*string)(p) == *(*string)(q)
}
func interequal(p, q unsafe.Pointer) bool {
return ifaceeq(*(*interface {
f()
})(p), *(*interface {
f()
})(q))
return ifaceeq(*(*iface)(p), *(*iface)(q))
}
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 {
x := (*eface)(unsafe.Pointer(&p))
y := (*eface)(unsafe.Pointer(&q))
func efaceeq(x, y eface) bool {
t := x._type
if t != y._type {
return false
......@@ -254,11 +248,7 @@ func efaceeq(p, q interface{}) bool {
}
return eq(x.data, y.data)
}
func ifaceeq(p, q interface {
f()
}) bool {
x := (*iface)(unsafe.Pointer(&p))
y := (*iface)(unsafe.Pointer(&q))
func ifaceeq(x, y iface) bool {
xtab := x.tab
if xtab != y.tab {
return false
......
......@@ -4,8 +4,6 @@
package runtime
import "unsafe"
// The Error interface identifies a run time error.
type Error interface {
error
......@@ -57,7 +55,7 @@ type stringer interface {
}
func typestring(x interface{}) string {
e := (*eface)(unsafe.Pointer(&x))
e := efaceOf(&x)
return *e._type._string
}
......
......@@ -127,7 +127,7 @@ var BigEndian = _BigEndian
// For benchmarking.
func BenchSetType(n int, x interface{}) {
e := *(*eface)(unsafe.Pointer(&x))
e := *efaceOf(&x)
t := e._type
var size uintptr
var p unsafe.Pointer
......
......@@ -379,7 +379,7 @@ func dumpgoroutine(gp *g) {
dumpint(tagPanic)
dumpint(uint64(uintptr(unsafe.Pointer(p))))
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.data))))
dumpint(0) // was p->defer, no longer recorded
......
This diff is collapsed.
......@@ -1623,7 +1623,7 @@ func getgcmaskcb(frame *stkframe, ctxt unsafe.Pointer) bool {
//go:linkname reflect_gcbits reflect.gcbits
func reflect_gcbits(x interface{}) []byte {
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
for uintptr(len(ret)) > nptr && ret[len(ret)-1] == 0 {
ret = ret[:len(ret)-1]
......@@ -1633,7 +1633,7 @@ func reflect_gcbits(x interface{}) []byte {
// Returns GC type info for object p for testing.
func getgcmask(ep interface{}) (mask []byte) {
e := *(*eface)(unsafe.Pointer(&ep))
e := *efaceOf(&ep)
p := e.data
t := e._type
// data or bss
......
......@@ -187,7 +187,7 @@ func runfinq() {
if len(ityp.mhdr) != 0 {
// convert to interface with methods
// this conversion is guaranteed to succeed - we checked in SetFinalizer
assertE2I(ityp, *(*interface{})(frame), (*fInterface)(frame))
assertE2I(ityp, *(*eface)(frame), (*iface)(frame))
}
default:
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).
return
}
e := (*eface)(unsafe.Pointer(&obj))
e := efaceOf(&obj)
etyp := e._type
if etyp == 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
if ftyp == nil {
// switch to system stack and remove finalizer
......@@ -347,7 +347,7 @@ func SetFinalizer(obj interface{}, finalizer interface{}) {
// ok - satisfies empty interface
goto okarg
}
if assertE2I2(ityp, obj, nil) {
if assertE2I2(ityp, *efaceOf(&obj), nil) {
goto okarg
}
}
......
......@@ -220,12 +220,10 @@ func printslice(s []byte) {
printpointer(unsafe.Pointer(sp.array))
}
func printeface(e interface{}) {
ep := (*eface)(unsafe.Pointer(&e))
print("(", ep._type, ",", ep.data, ")")
func printeface(e eface) {
print("(", e._type, ",", e.data, ")")
}
func printiface(i fInterface) {
ip := (*iface)(unsafe.Pointer(&i))
print("(", ip.tab, ",", ip.data, ")")
func printiface(i iface) {
print("(", i.tab, ",", i.data, ")")
}
......@@ -82,6 +82,10 @@ type eface struct {
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.
// 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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment