Depth of decryption unsafe Go Language

Previous article we analyze in detail the implementation of the underlying map, if you followed the reading of the source code, it must be for unsafe.Pointerno stranger when the map of the key positioning, extensive use.

unsafe.PointerLocated in unsafe 包this article, we take a thorough research unsafe package. First explain, this is not so long before, you can be more easily read, so not too much time.

When the last published article, including code words than 5w, background editor of experience is very poor, so I suspect that once life. As I said before, as long as the text map, estimated to finish no more than 1 %. Like several students following this evaluation, and rare.

WeChat

Personally believe that learning itself is not a pleasant thing, entertaining is wishful thinking. Want to deeply understand, you have to pay out of sight of the effort. Learning is never an easy thing, boring is normal. Bide, in-depth study of a problem, reading, see the article, write a blog can be, impetuous era to be a focus of people!

Pointer types

Prior to the formal introduction unsafe package, highlighting the need pointer type Go language.

When I began to learn the undergraduate program, the first language is C. And later gradually learned C ++, Java, Python, these languages ​​are quite strong, but not the C language so "simple." Until I came into contact with the Go language, I have found that feeling. One of the authors of the Go language Ken Thompson is the author of the C language. So, Go can be seen as C-based language, and many of its features are similar to C, the pointer is one of them.

However, there are many pointers Go language restrictions compared to the C pointer. This course is for security reasons, you know, like Java / Python these modern languages, for fear of programmer error, so how pointer (here refers to explicit pointer)? Let alone the C / C ++ programmers also need to clean up their own "junk." Go for it, there pointer has been very good, only whether it has many limitations.

Why do we need a pointer type it? References cited in this go101.org an example:

package main

import "fmt"

func double(x int) {
    x += x
}

func main() {
    var a = 3
    double(a)
    fmt.Println(a) // 3
}

Very simple, I think there will be a double double function, but function in the example can not. why? Go language because the function parameters are passed 值传递. double function in the argument x is only one copy of a, in operation of the internal function of x can not be returned to the argument a.

If then, there is a pointer can solve the problem! This is our common "trick."

package main

import "fmt"

func double(x *int) {
    *x += *x
    x = nil
}

func main() {
    var a = 3
    double(&a)
    fmt.Println(a) // 6
    
    p := &a
    double(p)
    fmt.Println(a, p == nil) // 12 false
}

Very routine operation, do not explain. The only possible some doubts in this sentence:

x = nil

It was a little to think about in order to conclude that the code does not affect this line. Because it is passed by value, so that only one copy of the x & a a.

*x += *x

This is a point of the value x (i.e., & a point value, i.e., the variable a) becomes 2 times the original. However, the operation itself of x (a pointer) without affecting the outer layer a, so the x = nillift can not afford any storms.

This following chart can be "a witness innocent":

pointer copy

However, compared to the flexible C language pointer, Go pointer a few more restrictions. But it can be considered Go's success: not only can enjoy the convenience brought by the pointer, but also to avoid the risk of pointers.

A Go 的指针不能进行数学运算limit: .

Look at a simple example:

a := 5
p := &a

p++
p = &a + 3

The above code will not compile, compile-time error will be reported: invalid operationthat is can not do math pointer.

Limit 不同类型的指针不能相互转换two: .

For example, the following short example:

func main() {
    a := int(100)
    var f *float64
    
    f = &a
}

It will report compilation errors:

cannot use &a (type *int) as type *float64 in assignment

About two pointers can be interchangeable, Resources go 101 related articles written in very fine, and here I do not want to start. Remember these personally think does not make sense, there are a perfectionist students can go to read the original. Of course, I also have a perfectionist, but I sometimes restraint, hey.

限制三:不同类型的指针不能使用 == 或 != 比较

只有在两个指针类型相同或者可以相互转换的情况下,才可以对两者进行比较。另外,指针可以通过 ==!= 直接和 nil 作比较。

限制四:不同类型的指针变量不能相互赋值

这一点同限制三。

什么是 unsafe

前面所说的指针是类型安全的,但它有很多限制。Go 还有非类型安全的指针,这就是 unsafe 包提供的 unsafe.Pointer。在某些情况下,它会使代码更高效,当然,也更危险。

unsafe 包用于 Go 编译器,在编译阶段使用。从名字就可以看出来,它是不安全的,官方并不建议使用。我在用 unsafe 包的时候会有一种不舒服的感觉,可能这也是语言设计者的意图吧。

但是高阶的 Gopher,怎么能不会使用 unsafe 包呢?它可以绕过 Go 语言的类型系统,直接操作内存。例如,一般我们不能操作一个结构体的未导出成员,但是通过 unsafe 包就能做到。unsafe 包让我可以直接读写内存,还管你什么导出还是未导出。

为什么有 unsafe

Go 语言类型系统是为了安全和效率设计的,有时,安全会导致效率低下。有了 unsafe 包,高阶的程序员就可以利用它绕过类型系统的低效。因此,它就有了存在的意义,阅读 Go 源码,会发现有大量使用 unsafe 包的例子。

unsafe 实现原理

我们来看源码:

type ArbitraryType int

type Pointer *ArbitraryType

从命名来看,Arbitrary 是任意的意思,也就是说 Pointer 可以指向任意类型,实际上它类似于 C 语言里的 void*

unsafe 包还有其他三个函数:

func Sizeof(x ArbitraryType) uintptr
func Offsetof(x ArbitraryType) uintptr
func Alignof(x ArbitraryType) uintptr

Sizeof 返回类型 x 所占据的字节数,但不包含 x 所指向的内容的大小。例如,对于一个指针,函数返回的大小为 8 字节(64位机上),一个 slice 的大小则为 slice header 的大小。

Offsetof 返回结构体成员在内存中的位置离结构体起始处的字节数,所传参数必须是结构体的成员。

Alignof 返回 m,m 是指当类型进行内存对齐时,它分配到的内存地址能整除 m。

注意到以上三个函数返回的结果都是 uintptr 类型,这和 unsafe.Pointer 可以相互转换。三个函数都是在编译期间执行,它们的结果可以直接赋给 const 型变量。另外,因为三个函数执行的结果和操作系统、编译器相关,所以是不可移植的。

综上所述,unsafe 包提供了 2 点重要的能力:

  1. 任何类型的指针和 unsafe.Pointer 可以相互转换。
  2. uintptr 类型和 unsafe.Pointer 可以相互转换。

type pointer uintptr

pointer 不能直接进行数学运算,但可以把它转换成 uintptr,对 uintptr 类型进行数学运算,再转换成 pointer 类型。

// uintptr 是一个整数类型,它足够大,可以存储
type uintptr uintptr

还有一点要注意的是,uintptr 并没有指针的语义,意思就是 uintptr 所指向的对象会被 gc 无情地回收。而 unsafe.Pointer 有指针语义,可以保护它所指向的对象在“有用”的时候不会被垃圾回收。

unsafe 包中的几个函数都是在编译期间执行完毕,毕竟,编译器对内存分配这些操作“了然于胸”。在 /usr/local/go/src/cmd/compile/internal/gc/unsafe.go 路径下,可以看到编译期间 Go 对 unsafe 包中函数的处理。

更深层的原理需要去研究编译器的源码,这里就不去深究了。我们重点关注它的用法,接着往下看。

unsafe 如何使用

获取 slice 长度

通过前面关于 slice 的文章,我们知道了 slice header 的结构体定义:

// runtime/slice.go
type slice struct {
    array unsafe.Pointer // 元素指针
    len   int // 长度 
    cap   int // 容量
}

调用 make 函数新建一个 slice,底层调用的是 makeslice 函数,返回的是 slice 结构体:

func makeslice(et *_type, len, cap int) slice

因此我们可以通过 unsafe.Pointer 和 uintptr 进行转换,得到 slice 的字段值。

func main() {
    s := make([]int, 9, 20)
    var Len = *(*int)(unsafe.Pointer(uintptr(unsafe.Pointer(&s)) + uintptr(8)))
    fmt.Println(Len, len(s)) // 9 9

    var Cap = *(*int)(unsafe.Pointer(uintptr(unsafe.Pointer(&s)) + uintptr(16)))
    fmt.Println(Cap, cap(s)) // 20 20
}

Len,cap 的转换流程如下:

Len: &s => pointer => uintptr => poiter => *int => int
Cap: &s => pointer => uintptr => poiter => *int => int

获取 map 长度

再来看一下上篇文章我们讲到的 map:

type hmap struct {
    count     int
    flags     uint8
    B         uint8
    noverflow uint16
    hash0     uint32

    buckets    unsafe.Pointer
    oldbuckets unsafe.Pointer
    nevacuate  uintptr

    extra *mapextra
}

和 slice 不同的是,makemap 函数返回的是 hmap 的指针,注意是指针:

func makemap(t *maptype, hint int64, h *hmap, bucket unsafe.Pointer) *hmap

我们依然能通过 unsafe.Pointer 和 uintptr 进行转换,得到 hamp 字段的值,只不过,现在 count 变成二级指针了:

func main() {
    mp := make(map[string]int)
    mp["qcrao"] = 100
    mp["stefno"] = 18

    count := **(**int)(unsafe.Pointer(&mp))
    fmt.Println(count, len(mp)) // 2 2
}

count 的转换过程:

&mp => pointer => **int => int

map 源码中的应用

在 map 源码中,mapaccess1、mapassign、mapdelete 函数中,需要定位 key 的位置,会先对 key 做哈希运算。

例如:

b := (*bmap)(unsafe.Pointer(uintptr(h.buckets) + (hash&m)*uintptr(t.bucketsize)))

h.buckets 是一个 unsafe.Pointer,将它转换成 uintptr,然后加上 (hash&m)*uintptr(t.bucketsize),二者相加的结果再次转换成 unsafe.Pointer,最后,转换成 bmap 指针,得到 key 所落入的 bucket 位置。如果不熟悉这个公式,可以看看上一篇文章,浅显易懂。

上面举的例子相对简单,来看一个关于赋值的更难一点的例子:

// store new key/value at insert position
if t.indirectkey {
    kmem := newobject(t.key)
    *(*unsafe.Pointer)(insertk) = kmem
    insertk = kmem
}
if t.indirectvalue {
    vmem := newobject(t.elem)
    *(*unsafe.Pointer)(val) = vmem
}

typedmemmove(t.key, insertk, key)

这段代码是在找到了 key 要插入的位置后,进行“赋值”操作。insertk 和 val 分别表示 key 和 value 所要“放置”的地址。如果 t.indirectkey 为真,说明 bucket 中存储的是 key 的指针,因此需要将 insertk 看成指针的指针,这样才能将 bucket 中的相应位置的值设置成指向真实 key 的地址值,也就是说 key 存放的是指针。

下面这张图展示了设置 key 的全部操作:

map assign

obj 是真实的 key 存放的地方。第 4 号图,obj 表示执行完 typedmemmove 函数后,被成功赋值。

Offsetof 获取成员偏移量

对于一个结构体,通过 offset 函数可以获取结构体成员的偏移量,进而获取成员的地址,读写该地址的内存,就可以达到改变成员值的目的。

这里有一个内存分配相关的事实:结构体会被分配一块连续的内存,结构体的地址也代表了第一个成员的地址。

我们来看一个例子:

package main

import (
    "fmt"
    "unsafe"
)

type Programmer struct {
    name string
    language string
}

func main() {
    p := Programmer{"stefno", "go"}
    fmt.Println(p)
    
    name := (*string)(unsafe.Pointer(&p))
    *name = "qcrao"

    lang := (*string)(unsafe.Pointer(uintptr(unsafe.Pointer(&p)) + unsafe.Offsetof(p.language)))
    *lang = "Golang"

    fmt.Println(p)
}

运行代码,输出:

{stefno go}
{qcrao Golang}

name 是结构体的第一个成员,因此可以直接将 &p 解析成 *string。这一点,在前面获取 map 的 count 成员时,用的是同样的原理。

对于结构体的私有成员,现在有办法可以通过 unsafe.Pointer 改变它的值了。

我把 Programmer 结构体升级,多加一个字段:

type Programmer struct {
    name string
    age int
    language string
}

并且放在其他包,这样在 main 函数中,它的三个字段都是私有成员变量,不能直接修改。但我通过 unsafe.Sizeof() 函数可以获取成员大小,进而计算出成员的地址,直接修改内存。

func main() {
    p := Programmer{"stefno", 18, "go"}
    fmt.Println(p)

    lang := (*string)(unsafe.Pointer(uintptr(unsafe.Pointer(&p)) + unsafe.Sizeof(int(0)) + unsafe.Sizeof(string(""))))
    *lang = "Golang"

    fmt.Println(p)
}

输出:

{stefno 18 go}
{stefno 18 Golang}

string 和 slice 的相互转换

这是一个非常精典的例子。实现字符串和 bytes 切片之间的转换,要求是 zero-copy。想一下,一般的做法,都需要遍历字符串或 bytes 切片,再挨个赋值。

完成这个任务,我们需要了解 slice 和 string 的底层数据结构:

type StringHeader struct {
    Data uintptr
    Len  int
}

type SliceHeader struct {
    Data uintptr
    Len  int
    Cap  int
}

上面是反射包下的结构体,路径:src/reflect/value.go。只需要共享底层 []byte 数组就可以实现 zero-copy

func string2bytes(s string) []byte {
    stringHeader := (*reflect.StringHeader)(unsafe.Pointer(&s))

    bh := reflect.SliceHeader{
        Data: stringHeader.Data,
        Len:  stringHeader.Len,
        Cap:  stringHeader.Len,
    }

    return *(*[]byte)(unsafe.Pointer(&bh))
}

func bytes2string(b []byte) string{
    sliceHeader := (*reflect.SliceHeader)(unsafe.Pointer(&b))

    sh := reflect.StringHeader{
        Data: sliceHeader.Data,
        Len:  sliceHeader.Len,
    }

    return *(*string)(unsafe.Pointer(&sh))
}

代码比较简单,不作详细解释。通过构造 slice header 和 string header,来完成 string 和 byte slice 之间的转换。

总结

unsafe 包绕过了 Go 的类型系统,达到直接操作内存的目的,使用它有一定的风险性。但是在某些场景下,使用 unsafe 包提供的函数会提升代码的效率,Go 源码中也是大量使用 unsafe 包。

unsafe 包定义了 Pointer 和三个函数:

type ArbitraryType int
type Pointer *ArbitraryType

func Sizeof(x ArbitraryType) uintptr
func Offsetof(x ArbitraryType) uintptr
func Alignof(x ArbitraryType) uintptr

通过三个函数可以获取变量的大小、偏移、对齐等信息。

uintptr 可以和 unsafe.Pointer 进行相互转换,uintptr 可以进行数学运算。这样,通过 uintptr 和 unsafe.Pointer 的结合就解决了 Go 指针不能进行数学运算的限制。

通过 unsafe 相关函数,可以获取结构体私有成员的地址,进而对其做进一步的读写操作,突破 Go 的类型安全限制。关于 unsafe 包,我们更多关注它的用法。

顺便说一句,unsafe 包用多了之后,也不觉得它的名字有多么地不“美观”了。相反,因为使用了官方并不提倡的东西,反而觉得有点酷炫。这就是叛逆的感觉吧。

最后,点击阅读原文,你将参与见证一个千星项目的成长,你值得拥有!

QR

参考资料

【飞雪无情的博客】https://www.flysnow.org/2017/07/06/go-in-action-unsafe-pointer.html

【译文 unsafe包详解】https://gocn.vip/question/371

【官方文档】https://golang.org/pkg/unsafe/

【例子】http://www.opscoder.info/golang_unsafe.html

【煎鱼大佬的博客】https://segmentfault.com/a/1190000017389782

【go语言圣经】https://www.kancloud.cn/wizardforcel/gopl-zh/106477

【pointer and system calls】https://blog.gopheracademy.com/advent-2017/unsafe-pointer-and-system-calls/

【pointer and uintptr】https://my.oschina.net/xinxingegeya/blog/729673

【unsafe.pointer】https://go101.org/article/unsafe.html

[Go pointer type] https://go101.org/article/pointer.html

[Go yard hole quickly learn the language unsafe] https://juejin.im/post/5c189dce5188256b2e71e79b

[Official document] https://golang.org/pkg/unsafe/

[Of] jasper nest http://www.opscoder.info/golang_unsafe.html

Guess you like

Origin www.cnblogs.com/qcrao-2018/p/10964692.html