暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Go语言之unsafe包介绍及使用

梦中不知岁已老 2018-03-24
203

unsafe内容介绍

  1. type ArbitraryType int

  2. type Pointer *ArbitraryType

  3. func Sizeof(x ArbitraryType) uintptr

  4. func Offsetof(x ArbitraryType) uintptr

  5. func Alignof(x ArbitraryType) uintptr

复制

unsafe包只有两个类型,三个函数,但是功能很强大。

unsafe 库让 golang 可以像C语言一样操作计算机内存,但这并不是golang推荐使用的,能不用尽量不用,就像它的名字所表达的一样,它绕过了golang的内存安全原则,是不安全的,容易使你的程序出现莫名其妙的问题,不利于程序的扩展与维护。

先简单介绍下Golang指针

  1. *类型:普通指针,用于传递对象地址,不能进行指针运算。


  2. unsafe.Pointer:通用指针类型,用于转换不同类型的指针,不能进行指针运算。


  3. uintptr:用于指针运算,GC 不把 uintptr 当指针,uintptr 无法持有对象。uintptr 类型的目标会被回收

      unsafe.Pointer 可以和 普通指针 进行相互转换。

      unsafe.Pointer 可以和 uintptr 进行相互转换。


也就是说 unsafe.Pointer 是桥梁,可以让任意类型的指针实现相互转换,也可以将任意类型的指针转换为 uintptr 进行指针运算。

uintptr这个类型,在golang中,字节长度也是与int一致。通常Pointer不能参与运算,比如你要在某个指针地址上加上一个偏移量,Pointer是不能做这个运算的,那么谁可以呢?就是uintptr类型了,只要将Pointer类型转换成uintptr类型,做完加减法后,转换成Pointer,通过*操作,取值,修改值,随意。

两个类型简介

  1. // ArbitraryType is here for the purposes of documentation only and is not actually

  2. // part of the unsafe package. It represents the type of an arbitrary Go expression.

  3. type ArbitraryType int


  4. type Pointer *ArbitraryType

复制

ArbitraryType是int的一个别名,在Go中对ArbitraryType赋予特殊的意义。代表一个任意Go表达式类型。

Pointer 是int指针类型的一个别名,在Go中可以把Pointer类型,理解成任何指针的父类型。

下面为官方文档中对Pointer的使用场景介绍

  1. Pointer represents a pointer to an arbitrary type. There are four special operationsavailable for type Pointer that are not available for other types:

  2. - A pointer value of any type can be converted to a Pointer.

  3. - A Pointer can be converted to a pointer value of any type.

  4. - A uintptr can be converted to a Pointer.

  5. - A Pointer can be converted to a uintptr.


  6. Pointer therefore allows a program to defeat the type system and read and write arbitrary memory. It should be used with extreme care.

复制

golang的指针类型长度与int类型长度,在内存中占用的字节数是一样的.

ArbitraryType类型的变量也可以是指针。所以,千万不要死磕type后边的那个int

三个函数简介

  1. // Sizeof takes an expression x of any type and returns the size in bytes

  2. // of a hypothetical variable v as if v was declared via var v = x.

  3. // The size does not include any memory possibly referenced by x.

  4. // For instance, if x is a slice,  Sizeof returns the size of the slice

  5. // descriptor, not the size of the memory referenced by the slice.

  6. func Sizeof(x ArbitraryType) uintptr


  7. // Offsetof returns the offset within the struct of the field represented by x,

  8. // which must be of the form structValue.field. In other words, it returns the

  9. // number of bytes between the start of the struct and the start of the field.

  10. func Offsetof(x ArbitraryType) uintptr


  11. // Alignof takes an expression x of any type and returns the required alignment

  12. // of a hypothetical variable v as if v was declared via var v = x.

  13. // It is the largest value m such that the address of v is always zero mod m.

  14. // It is the same as the value returned by reflect.TypeOf(x).Align().

  15. // As a special case, if a variable s is of struct type and f is a field

  16. // within that struct, then Alignof(s.f) will return the required alignment

  17. // of a field of that type within a struct. This case is the same as the

  18. // value returned by reflect.TypeOf(s.f).FieldAlign().

  19. func Alignof(x ArbitraryType) uintptr

复制

通过分析发现,这三个函数的参数均是ArbitraryType类型,就是接受任何类型的变量。

  1. Alignof返回变量对齐字节数量

  2. Offsetof返回变量指定属性的偏移量,这个函数虽然接收的是任何类型的变量,但是有一个前提,就是变量要是一个struct类型,且还不能直接将这个struct类型的变量当作参数,只能将这个struct类型变量的属性当作参数。

  3. Sizeof 返回变量在内存中占用的字节数,切记,如果是slice,则不会返回这个slice在内存中的实际占用长度

示例

Sizeof

unsafe.Sizeof函数返回的就是uintptr类型的值(表达式,即值的大小):

  1. var p float64 = 99

  2. fmt.Println(reflect.TypeOf(unsafe.Sizeof(p)))

  3. fmt.Println(unsafe.Sizeof(p))

复制

results:

  1. uintptr

  2. 8

复制

unsafe.Sizeof接受任意类型的值(表达式),返回其占用的字节数,在上面的例子中float64的大小是8bytes。

如果传入一个指针类型的对象会返回多少呢?

  1. type W struct {

  2.    a byte

  3.    b int32

  4.    c int64

  5. }


  6. var w *W

  7. fmt.Println(unsafe.Sizeof(w)) //4 or 8

复制

一般情况下,可能是4或8,因为w是指针类型uintptr,而uintptr是平台相关的,在32位系统下大小是4bytes,在64位系统下是8bytes。

要获取值类型的大小,需要对指针变量进行取值:

  1. fmt.Println(unsafe.Sizeof(*w)) //16

复制

对齐

在上面的例子中,*w的大小为16,按照常理来说,byte占用1字节,int32占用4字节,int64占用8字节,大小应该是13才对。这是因为发生了对齐,unsafe.Alignof可以计算对齐值:

  1. unsafe.Alignof(w.a)   // type byte

  2. unsafe.Alignof(w.b)   // type int32

  3. unsafe.Alignof(w.c)   // type int64

复制

分别是1、4、8,因为int32类型的对齐值是4bytes,必须是4的倍数,故byte类型要填充3个字节。而填充后,两者的大小和为8bytes,int64对齐值是8bytes,不需要填充,所以用unsafe.Sizeof获取到结构的大小为4+4+8=16。

反射包的对齐方法

反射包也有某些方法可用于计算对齐值:

unsafe.Alignof(w)等价于reflect.TypeOf(w).Align。

unsafe.Alignof(w.i)等价于reflect.Typeof(w.i).FieldAlign()。

结构体的对齐值

如果我们计算的是结构体的对齐值而不是某个字段或者基本类型,那么值会是多少呢?

  1. type W struct {

  2.    a byte

  3.    b int32

  4.    c int64

  5. }


  6. var w *W

  7. var w2 W


  8. fmt.Println(unsafe.Alignof(w))

  9. fmt.Println(unsafe.Alignof(w2))

  10. fmt.Println(reflect.TypeOf(w).Elem().Align())    

复制

results:

  1. 8

  2. 8

  3. 8

复制

64位机器下,指针对象的对齐值是8,因为指针类型是uintptr。而结构体的值类型却是8bytes的对齐值,这是因为会先进行字段的对齐,字段最大的对齐值是8bytes,因此结构体值类型的对齐值也是8。

更改结构,验证一下:

  1. type W struct {

  2.    a byte

  3.    b int32

  4.    c int32

  5. }

  6. var w W

  7. fmt.Println(unsafe.Alignof(w)) //4

复制

综合示例

  1. type T struct {

  2.    t1 byte

  3.    t2 int32

  4.    t3 int64

  5.    t4 string

  6.    t5 bool

  7. }


  8. fmt.Println("----------unsafe.Pointer---------")

  9. t := &T{1, 2, 3, "this is a example", true}

  10. ptr := unsafe.Pointer(t)

  11. t1 := (*byte)(ptr)

  12. fmt.Println(*t1)

  13. t2 := (*int32)(unsafe.Pointer(uintptr(ptr) + unsafe.Offsetof(t.t2)))

  14. *t2 = 99

  15. fmt.Println(t)

  16. t3 := (*int64)(unsafe.Pointer(uintptr(ptr) + unsafe.Offsetof(t.t3)))

  17. fmt.Println(*t3)

  18. *t3 = 123

  19. fmt.Println(t)

复制

results:

  1. ----------unsafe.Pointer---------

  2. 1

  3. &{1 99 3 this is a example true}

  4. 3

  5. &{1 99 123 this is a example true}

复制

借助于 unsafe.Pointer,我们实现了像 C 语言中的指针偏移操作。可以看出,这种不安全的操作使得我们可以在任何地方直接访问结构体中未公开的成员,只要能得到这个结构体变量的地址。

参考资料:

  1. https://blog.csdn.net/libing_thinking/article/details/49907815

  2. https://studygolang.com/articles/6903

  3. https://blog.csdn.net/hzwy23/article/details/60893765

  4. https://www.cnblogs.com/golove/p/5909968.html

  5. https://studygolang.com/articles/1414


文章转载自梦中不知岁已老,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论