Go interface principle analysis -- type conversion

hi, everyone, my name is haohongfan.

You may have read many articles on interface analysis, and these articles basically analyze type conversion or dynamic forwarding from the perspective of assembly. However, with the upgrade of the Go version, the corresponding Go assembly has also undergone tremendous changes. It is very difficult to analyze the interface from the perspective of assembly alone. In this article, I will cut into the interface from the perspective of memory allocation + assembly to understand the interface. principle.

Due to space limitations interface For dynamic forwarding and reflection, please pay attention to the follow-up articles. This article is mainly about type conversions.

eface
iface

eface

func main() {
   var ti interface{}
   var a int = 100
   ti = a
   fmt.Println(ti)
}

This most common code, now raises some questions:

  • How to check if ti is eface or iface?
  • Where is the value 100 stored?
  • How to see the type of the real value of ti?

Most source code analysis starts from assembly, and the corresponding assembly is also posted here.

0x0040 00064 (main.go:44)	MOVQ	$100, (SP)
0x0048 00072 (main.go:44) CALL runtime.convT64(SB)
0x004d 00077 (main.go:44) MOVQ 8(SP), AX
0x0052 00082 (main.go:44) MOVQ AX, ""..autotmp_3+64(SP)
0x0057 00087 (main.go:44) LEAQ type.int(SB), CX
0x005e 00094 (main.go:44) MOVQ CX, "".ti+72(SP)
0x0063 00099 (main.go:44) MOVQ AX, "".ti+80(SP)

This compilation has the following characteristics:

  • CALL runtime.convT64(SB): Take 100 as the parameter of runtime.convT64, the function applies for a memory and puts 100 into this memory
  • Put the type type.int in the SP+72 location
  • Put the pointer of the memory containing 100 into the position of SP + 80

Intuitively speaking, the conversion of interface to eface cannot be seen in this assembly. How to observe this? This requires the help of gdb.

Continue to investigate further, how to use memory distribution to verify that it is an eface? Additional code needs to be added.

type eface struct {
    _type *_type
    data  unsafe.Pointer
}

type _type struct {
    size       uintptr
    ptrdata    uintptr // size of memory prefix holding all pointers
    hash       uint32
    tflag      tflag
    align      uint8
    fieldAlign uint8
    kind       uint8
    equal      func(unsafe.Pointer, unsafe.Pointer) bool
    gcdata     *byte
    str        nameOff
    ptrToThis  typeOff
}

func main() {
    var ti interface{}
    var a int = 100
    ti = a

    fmt.Println("type:", *(*eface)(unsafe.Pointer(&ti))._type)
    fmt.Println("data:", *(*int)((*eface)(unsafe.Pointer(&ti)).data))
    fmt.Println((*eface)(unsafe.Pointer(&ti)))
}

output:

type: {8 0 4149441018 15 8 8 2 0x10032e0 0x10e6b60 959 27232}
data: 100
&{0x10ade20 0x1155bc0}

From this result, it can be seen that

  • eface.kind = 2, corresponding to runtime.kindInt
  • eface.data = 100

从内存上分配上看,我们基本看出来了 eface 的内存布局及对应的最终的 eface 的类型转换结果。

iface

package main

type Person interface {
   Say() string
}

type Man struct {
}

func (m *Man) Say() string {
   return "Man"
}

func main() {
    var p Person

    m := &Man{}
    p = m
    println(p.Say())
}

iface 我们也看下汇编:

0x0029 00041 (main.go:24)	LEAQ	runtime.zerobase(SB), AX
0x0030 00048 (main.go:24) MOVQ AX, ""..autotmp_6+48(SP)
0x0035 00053 (main.go:24) MOVQ AX, "".m+32(SP)
0x003a 00058 (main.go:25) MOVQ AX, ""..autotmp_3+64(SP)
0x003f 00063 (main.go:25) LEAQ go.itab.*"".Man,"".Person(SB), CX
0x0046 00070 (main.go:25) MOVQ CX, "".p+72(SP)
0x004b 00075 (main.go:25) MOVQ AX, "".p+80(SP)

这段汇编上,能够看出来是有 itab 的,但是是否真的是转成了 iface,汇编上仍然反应不出来。

同样,我们继续用 gdb 查看 Person interface 确实被转换成了 iface。

关于 iface 内存布局,我们仍然加点代码来查看

type itab struct {
    inter *interfacetype
    _type *_type
    hash  uint32
    _     [4]byte
    fun   [1]uintptr
}

type iface struct {
    tab  *itab
    data unsafe.Pointer
}

type Person interface {
    Say() string
}

type Man struct {
    Name string
}

func (m *Man) Say() string {
    return "Man"
}

func main() {
    var p Person

    m := &Man{Name: "hhf"}
    p = m
    println(p.Say())

    fmt.Println("itab:", *(*iface)(unsafe.Pointer(&p)).tab)
    fmt.Println("data:", *(*Man)((*iface)(unsafe.Pointer(&p)).data))
}

output:

Man
itab: {0x10b3ba0 0x10b1900 1224794265 [0 0 0 0] [17445152]}
data: {hhf}

关于想继续探究 eface, iface 的内存布局的同学,可以基于上面的代码,利用 unsafe 的相关函数去看对应的内存位置上的值。

类型断言

type Person interface {
   Say() string
}

type Man struct {
   Name string
}

func (m *Man) Say() string {
   return "Man"
}

func main() {
   var p Person

    m := &Man{Name: "hhf"}
    p = m

    if m1, ok := p.(*Man); ok {
      fmt.Println(m1.Name)
    }
}

我们仅关注类型断言那块内容,贴出对应的汇编

0x0087 00135 (main.go:23)	MOVQ	"".p+104(SP), AX
0x008c 00140 (main.go:23) MOVQ "".p+112(SP), CX
0x0091 00145 (main.go:23) LEAQ go.itab.*"".Man,"".Person(SB), DX
0x0098 00152 (main.go:23) CMPQ DX, AX

能够看出来的是:将 iface.itab 放入了 AX,将 go.itab.*"".Man,"".Person(SB) 放入了 DX,比较两者是否相等,来判断 Person 的真实类型是否是 Man。

另外一个类型断言的方式就是 switch 了,其实两者本质上没啥区别。

interface 最著名的坑的,应该就是下面这个了。

func main() {
    var a interface{} = nil
    var b *int = nil
    
    isNil(a)
    isNil(b)
}

func isNil(x interface{}) {
    if x == nil {
      fmt.Println("empty interface")
      return
    }
    fmt.Println("non-empty interface")
}

output:

empty interface
non-empty interface

为什么会这样呢?这就涉及到 interface == nil 的判断方式了。一般情况只有 eface 的 type 和 data 都为 nil 时,interface == nil 才是 true。

当我们把 b 复制给 interface 时,x._type.Kind = kindPtr。虽说 x.data = nil,但是不符合 interface == nil 的判断条件了。

关于 interface 源码阅读的一点建议

关于 interface 源码阅读的一点建议,如果想利用汇编看源码的话,尽量选择 go1.14.x。

选择 Go 汇编来看 interface,基本上也是为了查看 interface 最终被转换成 eface 还是 iface,调用了 runtime 的哪些函数,以及对应的函数栈分布。如果 Go 版本选择的太高的话,go 汇编变化太大了,可能汇编上就看不到对应的内容了。

欢迎关注公众号。更多学习学习资料分享,关注公众号回复指令:

  • 回复 0,获取 《Go 面经》

  • 回复 1,获取 《Go 源码流程图》


本文分享自微信公众号 - HHFCodeRv(hhfcodearts)。
如有侵权,请联系 [email protected] 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/3162806/blog/5174384