【Go】内存中的接口类型

开发 后端
本文完整地、详细地、深入地剖析了Go语言接口的类型结构、对象结构、实现类型、方法调用、继承扩展等等的各个方面的底层原理。

[[432303]]

前言

抽象来讲,接口,是一种约定,是一种约束,是一种协议。

在Go语言中,接口是一种语法类型,用来定义一种编程规范。

在Go语言中,接口主要有两类:

没有方法定义的空接口

有方法定义的非空接口

之前,有两篇图文详细介绍了空接口对象及其类型:

  • 【Go】内存中的空接口
  • 【Go】再谈空接口

本文将深入探究包含方法的非空接口,以下简称接口。

环境

  1. OS : Ubuntu 20.04.2 LTS; x86_64 
  2. Go : go version go1.16.2 linux/amd64 

声明

操作系统、处理器架构、Go版本不同,均有可能造成相同的源码编译后运行时的寄存器值、内存地址、数据结构等存在差异。

本文仅包含 64 位系统架构下的 64 位可执行程序的研究分析。

本文仅保证学习过程中的分析数据在当前环境下的准确有效性。

代码清单

  1. // interface_in_memory.go 
  2. package main 
  3.  
  4. import "fmt" 
  5. import "reflect" 
  6. import "strconv" 
  7.  
  8. type foo interface { 
  9.   fmt.Stringer 
  10.   Foo() 
  11.   ree() 
  12.  
  13. type fooImpl int 
  14.  
  15. //go:noinline 
  16. func (i fooImpl) Foo() { 
  17.   println("hello foo"
  18.  
  19. //go:noinline 
  20. func (i fooImpl) ree() { 
  21.   println("hello ree"
  22.  
  23. //go:noinline 
  24. func (i fooImpl) String() string { 
  25.   return strconv.Itoa(int(i)) 
  26.  
  27. func main() { 
  28.   impl := fooImpl(123) 
  29.   impl.Foo() 
  30.   impl.ree() 
  31.   fmt.Println(impl.String()) 
  32.   typeOf(impl) 
  33.   exec(impl) 
  34.  
  35. //go:noinline 
  36. func exec(foo foo) { 
  37.   foo.Foo() 
  38.   foo.ree() 
  39.   fmt.Println(foo.String()) 
  40.   typeOf(foo) 
  41.   fmt.Printf("exec 参数类型地址:%p\n", reflect.TypeOf(exec).In(0)) 
  42.  
  43. //go:noinline 
  44. func typeOf(i interface{}) { 
  45.   v := reflect.ValueOf(i) 
  46.   t := v.Type() 
  47.   fmt.Printf("类型:%s\n", t.String()) 
  48.   fmt.Printf("地址:%p\n", t) 
  49.   fmt.Printf("值  :%d\n", v.Int()) 
  50.   fmt.Println() 

以上代码,定义了一个包含3个方法的接口类型foo,还定义了一个fooImpl类型。在语法上,我们称fooImpl类型实现了foo接口。

运行结果

程序结构

数据结构介绍

接口数据类型的结构定义在reflect/type.go源文件中,如下所示:

  1. // 表示一个接口方法 
  2. type imethod struct { 
  3.   name nameOff // 方法名称相对程序 .rodata 节的偏移量 
  4.   typ  typeOff // 方法类型相对程序 .rodata 节的偏移量 
  5.  
  6. // 表示一个接口数据类型 
  7. type interfaceType struct { 
  8.   rtype             // 基础信息 
  9.   pkgPath name      // 包路径信息 
  10.   methods []imethod // 接口方法 

其实这只是一个表象,完整的接口数据类型结构如下伪代码所示:

  1. // 表示一个接口类型 
  2. type interfaceType struct { 
  3.   rtype             // 基础信息 
  4.   pkgPath name      // 包路径信息 
  5.   methods []imethod // 接口方法的 slice,实际指向 array 字段 
  6.   u uncommonType    // 占位 
  7.   array [len(methods)]imethod // 实际的接口方法数据 

完整的结构分布图如下:

另外两个需要了解的结构体,之前文章已经多次介绍过,也在reflect/type.go源文件中,定义如下:

  1. type uncommonType struct { 
  2.     pkgPath nameOff  // 包路径名称偏移量 
  3.     mcount  uint16   // 方法的数量 
  4.     xcount  uint16   // 公共导出方法的数量 
  5.     moff    uint32   // [mcount]method 相对本对象起始地址的偏移量 
  6.     _       uint32   // unused 

reflect.uncommonType结构体用于描述一个数据类型的包名和方法信息。对于接口类型,意义不是很大。

  1. // 非接口类型的方法 
  2. type method struct { 
  3.     name nameOff // 方法名称偏移量 
  4.     mtyp typeOff // 方法类型偏移量 
  5.     ifn  textOff // 通过接口调用时的地址偏移量;接口类型本文不介绍 
  6.     tfn  textOff // 直接类型调用时的地址偏移量 

reflect.method结构体用于描述一个非接口类型的方法,它是一个压缩格式的结构,每个字段的值都是一个相对偏移量。

  1. type nameOff int32 // offset to a name 
  2. type typeOff int32 // offset to an *rtype 
  3. type textOff int32 // offset from top of text section 
  • nameOff 是相对程序 .rodata 节起始地址的偏移量。
  • typeOff 是相对程序 .rodata 节起始地址的偏移量。
  • textOff 是相对程序 .text 节起始地址的偏移量。

接口实现类型

从以上“运行结果”可以看到,fooImpl的类型信息位于0x4a9be0内存地址处。

关于fooImpl类型,【Go】再谈整数类型一文曾进行过非常详细的介绍,此处仅分析其方法相关内容。

查看fooImpl类型的内存数据如下:

绘制成图表如下:

fooImpl类型有3个方法,我们以Foo方法来说明接口相关的底层原理。

Foo方法的相关数据如下:

  1. var Foo = reflect.method { 
  2.   name: 0x00000172, // 方法名称相对程序 `.rodata` 节起始地址的偏移量 
  3.   mtyp: 0x00009960, // 方法类型相对程序 `.rodata` 节起始地址的偏移量 
  4.   ifn:  0x000989a0, // 接口调用的指令相对程序 `.text` 节起始地址的偏移量 
  5.   tfn:  0x00098160, // 正常调用的指令相对程序 `.text` 节起始地址的偏移量 

方法名称

method.name用于定位方法的名称,即一个reflect.name对象。

Foo方法的reflect.name对象位于 0x49a172(0x00000172 + 0x49a000)地址处,毫无疑问,解析结果是Foo。

  1. (gdb) p /x 0x00000172 + 0x49a000 
  2. $3 = 0x49a172 
  3. (gdb) x /3bd 0x49a172 
  4. 0x49a172:  1  0  3 
  5. (gdb) x /3c 0x49a172 + 3 
  6. 0x49a175:  70 'F'  111 'o'  111 'o' 
  7. (gdb) 

方法类型

method.mtyp用于定位方法的数据类型,即一个reflect.funcType对象。

Foo方法的reflect.funcType对象,其位于0x4a3960(0x00009960 + 0x49a000)地址处。

Foo方法的数据类型的字符串表示形式是func()。

  1. (gdb) x /56bx 0x4a3960 
  2. 0x4a3960:  0x08  0x00  0x00  0x00  0x00  0x00  0x00  0x00 
  3. 0x4a3968:  0x08  0x00  0x00  0x00  0x00  0x00  0x00  0x00 
  4. 0x4a3970:  0xf6  0xbc  0x82  0xf6  0x02  0x08  0x08  0x33 
  5. 0x4a3978:  0x00  0x00  0x00  0x00  0x00  0x00  0x00  0x00 
  6. 0x4a3980:  0xa0  0x4a  0x4c  0x00  0x00  0x00  0x00  0x00 
  7. 0x4a3988:  0x34  0x11  0x00  0x00  0x00  0x00  0x00  0x00 
  8. 0x4a3990:  0x00  0x00  0x00  0x00  0x00  0x00  0x00  0x00 
  9. (gdb) x /wx 0x4a3988 
  10. 0x4a3988:  0x00001134 
  11. (gdb) x /s 0x00001134 + 0x49a000 + 3 
  12. 0x49b137:  "*func()" 
  13. (gdb) 

想要深入了解函数类型,请阅读【Go】内存中的函数。

接口方法

method.ifn字段的英文注释为function used in interface call,即调用接口方法时使用的函数。

在本例中,就是通过foo接口调用fooImpl类型的Foo函数时需要执行的指令集合。

具体来讲就是,代码清单中的exec函数内调用Foo方法需要执行的指令集合。

Foo函数的method.ifn = 0x000989a0,计算出其指令集合位于地址0x4999a0(0x000989a0 + 0x401000)处。

通过内存数据可以清楚地看到,接口方法的符号是main.(*fooImpl).Foo。该函数主要做了两件事:

检查panic

在0x4999d7地址处调用另一个函数main.fooImpl.Foo。

类型方法

method.tfn字段的英文注释为function used for normal method call,即正常方法调用时使用的函数。

在本例中,就是通过fooImpl类型的对象调用Foo函数时需要执行的指令集合。

具体来讲就是,代码清单中的main函数内调用Foo方法需要执行的指令集合。

Foo函数的method.tfn = 0x00098160,计算出其指令集合位于地址0x499160(0x00098160 + 0x401000)处。

通过内存数据可以清楚地看到,类型方法的符号是main.fooImpl.Foo。

调用堆栈

通过上述分析,已经能够对method.ifn和method.tfn两个字段的含义建立起基本的认知。

实践是检验真理的唯一标准。能动手尽量别吵吵。

在main.(*fooImpl).Foo和main.fooImpl.Foo两个函数的入口处设置断点,通过行动巩固我们对接口类型的认识。

通过动态调试,我们清晰地看到:

  • main函数调用了main.fooImpl.Foo函数
  • exec函数调用了main.(*fooImpl).Foo函数
  • main.(*fooImpl).Foo函数调用了main.fooImpl.Foo函数
  • main.(*fooImpl).Foo函数的调试信息显示autogenerated,表示其是由编译器生成的

对比本文“代码清单”,你是否对Go语言的方法调用有了全新的认识。

几乎每种编程语言都会存在编译器自动生成代码的情况,用来实现某些通用逻辑的处理。本例中自动生成的main.(*fooImpl).Foo函数中增加了panic检查逻辑,不过, 乍看起来这像是某种设计缺陷导致不能直接调用main.fooImpl.Foo函数,而是必须经过一个"中间人"才行。

接口类型

从以上“运行结果”可以看到,exec函数的参数类型的地址是0x4aa5c0,也就是foo接口的类型信息存储位置。查看类型数据如下:

将以上内存数据绘制成图表如下:

  • rtype.size = 16
  • rtype.ptrdata = 16
  • rtype.hash = 0x187f135e
  • rtype.tflag = 0xf = reflect.tflagUncommon | reflect.tflagExtraStar | reflect.tflagNamed
  • rtype.align = 8
  • rtype.fieldAlign = 8
  • rtype.kind = 0x14 = 20 = reflect.Interface
  • rtype.equal = 0x4c4d38 -> runtime.interequal
  • rtype.str = 0x000003e3 -> *main.foo
  • rtype.ptrToThis = 0x00006a20 -> *foo
  • interfaceType.pkgPath = 0x49a34c -> main
  • interfaceType.methods.Data = 0x4aa620
  • interfaceType.methods.Len = 3
  • interfaceType.methods.Cap = 3
  • uncommonType.pkgPath = 0x0000034c
  • uncommonType.mcount = 0
  • uncommonType.xcount = 0
  • uncommonType.moff = 0x28
  • interfaceType.methods[0].name = 0x00000172 -> Foo
  • interfaceType.methods[0].typ = 0x00009960 -> func()
  • interfaceType.methods[1].name = 0x00000d7a -> String
  • interfaceType.methods[1].typ = 0x0000a140 -> func() string
  • interfaceType.methods[2].name = 0x000002ce -> ree
  • interfaceType.methods[2].typ = 0x00009960 -> func()

对象大小

接口类型的对象大小(rtype.size)是16字节,指针数据(rtype.ptrdata)占16字节;也就是说,接口类型的对象由2个指针组成,与空接口(interface{})对象大小一样。

比较函数

内存数据显示,接口类型的对象使用runtime.interequal进行相等性比较,该函数定义在runtime/alg.go源文件中:

  1. func interequal(p, q unsafe.Pointer) bool { 
  2.   x := *(*iface)(p) 
  3.   y := *(*iface)(q) 
  4.   return x.tab == y.tab && ifaceeq(x.tab, x.data, y.data) 
  5.  
  6. func ifaceeq(tab *itab, x, y unsafe.Pointer) bool { 
  7.   if tab == nil { 
  8.     return true 
  9.   } 
  10.   t := tab._type 
  11.   eq := t.equal 
  12.   if eq == nil { 
  13.     panic(errorString("comparing uncomparable type " + t.string())) 
  14.   } 
  15.   if isDirectIface(t) { 
  16.     // See comment in efaceeq. 
  17.     return x == y 
  18.   } 
  19.   return eq(x, y) 

该函数的执行逻辑是:

  1. 接口类型不同返回 false
  2. 接口类型为空返回 true
  3. 实现类型不可比较立即 panic
  4. 比较两个实现类型的对象并返回结果

uncommonType

在接口类型数据中,包路径信息可以通过interfaceType.pkgPath字段获取,方法信息通过interfaceType.methods字段获取, 因此uncommonType数据几乎没什么意义,只不过保持一致性罢了。

在本例中,可执行程序.rodata节的起始地址是0x49a000, interfaceType.pkgPath=uncommonType.pkgPath+0x49a000。

接口方法

接口方法(reflect.imethod)只有名称和类型信息,没有可执行指令,所以相对普通方法(reflect.method)缺少两个字段。

foo接口的方法的名称和类型,与fooImpl类型的方法的名称和类型完全一致,此处不再赘述。如有需要请阅读上文中方法相关的内容。

接口对象

runtime.interequal函数源码清晰地显示,其比较的是两个runtime.iface对象。

runtime.iface结构体定义在runtime/runtime2.go源码文件中,包含两个指针字段,大小是16个字节(rtype.size)。

  1. type iface struct { 
  2.   tab  *itab 
  3.   data unsafe.Pointer 
  4.  
  5. type itab struct { 
  6.   inter *interfacetype // 接口类型 
  7.   _type *_type         // 具体实现类型 
  8.   hash  uint32 // copy of _type.hash. Used for type switches. 
  9.   _     [4]byte 
  10.   fun   [1]uintptr // variable sized. fun[0]==0 means _type does not implement inter. 

该结构体与reflect/value.go源文件中定义的nonEmptyInterface结构体是等价的:

  1. type nonEmptyInterface struct { 
  2.   itab *struct { 
  3.     ityp *rtype // 接口类型 
  4.     typ  *rtype // 具体实现类型 
  5.     hash uint32 // 实现类型哈希种子 
  6.     _    [4]byte // 内存对齐 
  7.     fun  [100000]unsafe.Pointer // 方法数组,编译器控制数组长度 
  8.   } 
  9.   word unsafe.Pointer // 具体实现类型对象 

没错,接口对象就是iface对象,接口对象就是nonEmptyInterface对象。

源码清单中的exec函数接受一个foo接口类型的参数,在该函数入口处设置断点,即可查看其参数:

内存数据显示,exec函数的参数foo的值如下伪代码所示:

  1. foo := runtime.iface { 
  2.   tab:  0x4dcbb8, 
  3.   data: 0x543ad8, // 指向整数 123 

iface.data指针指向的内存数据是整数123,关于整数和runtime.staticuint64s,请阅读【Go】内存中的整数。

iface.tab指针指向一个全局符号go.itab.main.fooImpl,main.foo。该符号可以被视为一个全局常量,它是由Go编译器生成的,保存在可执行程序的.rodata节,其值如下伪代码所示:

  1. go.itab.main.fooImpl,main.foo = & runtime.itab { 
  2.     inter: 0x4aa5c0,    // foo 接口类型的地址,上文已经详细分析 
  3.     _type: 0x4a9be0,    // fooImpl 实现类型的地址,上文已经详细分析 
  4.     hash:  0xb597252a,  // fooImpl 类型的哈希种子拷贝 
  5.     fun:   [0x4999a0, 0x499a20, 0x499aa0] // 方法数组 

在本例中,runtime.iface.tab.fun字段值包含三个指针,分别指向以下三个函数:

  • main.(*fooImpl).Foo (0x4999a0)
  • main.(*fooImpl).String (0x499a20)
  • main.(*fooImpl).ree (0x499aa0)

当exec函数调用foo接口的方法时,实际是从runtime.iface.tab.fun字段的数组中获得方法地址;

所以,在本例中,exec`函数只能寻址以上三个方法,而无法寻址以下三个方法:

  • main.fooImpl.Foo
  • main.fooImpl.String
  • main.fooImpl.ree

如果定义新的类型实现了foo接口,作为参数传递给exec函数,Go编译器就会生成新的runtime.itab对象,并命名为go.itab.${pkg}.${type},main.foo格式,也是以相同的方式进行调用和执行。

在Go语言中,接口方法的调用逻辑是一致的。

接口扩展(继承)

在源码清单中,foo接口继承了fmt.Stringer接口,并扩展了两个方法。

  1. type foo interface { 
  2.   fmt.Stringer 
  3.   Foo() 
  4.   ree() 

而在程序运行时的内存数据中,在动态调试过程中,根本就没有fmt.Stringer接口什么事,连根毛都没看见。

实际上,Go编译器把foo接口的定义调整为以下代码,这就是接口继承和扩展的本质。

  1. type foo interface { 
  2.   String() string 
  3.   Foo() 
  4.   ree() 

总结

本文完整地、详细地、深入地剖析了Go语言接口的类型结构、对象结构、实现类型、方法调用、继承扩展等等的各个方面的底层原理。

相信这是对Go接口类型的一次重新认识。

本文转载自微信公众号「Golang In Memory」

 

责任编辑:姜华 来源: Golang In Memory
相关推荐

2021-10-31 14:14:33

内存接口协议

2010-03-22 10:56:26

各种交换机

2019-07-21 08:13:21

2012-11-19 10:29:51

路由器WANLAN

2010-02-22 16:39:08

千兆交换机

2010-01-13 10:27:49

交换机接口类型

2010-01-18 14:20:15

交换机接口类型

2010-01-04 15:36:52

以太网交换机

2010-01-13 10:27:45

交换机接口类型

2010-03-29 15:54:36

PCMCIA无线上网卡

2023-10-26 11:19:21

指针Go

2021-09-10 08:18:31

Go语言字符串

2023-10-26 11:56:18

VLAN接口

2023-10-29 16:18:26

Go接口

2023-11-21 08:03:43

语言架构偏移量

2021-09-15 07:56:33

函数类型Go

2021-12-20 07:59:07

Go语言结构体

2014-01-14 09:10:53

GoHTTP内存泄漏

2021-07-02 20:46:06

Go接口动态

2023-06-26 00:03:55

Go语言类型
点赞
收藏

51CTO技术栈公众号