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 { ...@@ -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 { ...@@ -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
......
...@@ -4,8 +4,6 @@ ...@@ -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 { ...@@ -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
} }
......
...@@ -127,7 +127,7 @@ var BigEndian = _BigEndian ...@@ -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
......
...@@ -379,7 +379,7 @@ func dumpgoroutine(gp *g) { ...@@ -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
......
This diff is collapsed.
...@@ -1623,7 +1623,7 @@ func getgcmaskcb(frame *stkframe, ctxt unsafe.Pointer) bool { ...@@ -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 { ...@@ -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
......
...@@ -187,7 +187,7 @@ func runfinq() { ...@@ -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{}) { ...@@ -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{}) { ...@@ -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{}) { ...@@ -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
} }
} }
......
...@@ -220,12 +220,10 @@ func printslice(s []byte) { ...@@ -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, ")")
} }
...@@ -82,6 +82,10 @@ type eface struct { ...@@ -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
......
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