使用golang-unsafe包的注意事項(xiàng)及說(shuō)明
基于golang 15.5
總結(jié)(詳細(xì)的內(nèi)容可以往下看)
1.不能使用unsafe包里的ArbitraryType類型
2.Pointer類型可以表示任意類型的指針,所以可以用Pointer類型作為中介進(jìn)行兩種不同類型指針的轉(zhuǎn)換,為保證作為中介的Pointer類型數(shù)據(jù)有效,必須保證所有轉(zhuǎn)換在同一個(gè)表達(dá)式中,如:
func Float64bits(f float64) uint64 {
return *(*uint64)(unsafe.Pointer(&f))
}3.Pointer類型可以安全有效的轉(zhuǎn)換成uintptr,不能以任何形式(包含變量)保存轉(zhuǎn)換后的uintptr值,轉(zhuǎn)換后的uintptr值僅在轉(zhuǎn)換所在的表達(dá)式中有效的。
4.因?yàn)閡intptr只是內(nèi)存地址值,并沒(méi)有指針語(yǔ)義,所以u(píng)intptr轉(zhuǎn)換成Pointer通常不會(huì)是有效的
下面列舉有效的轉(zhuǎn)換方式:
1:在同一個(gè)表達(dá)式內(nèi),對(duì)Pointer轉(zhuǎn)換成的uintptr值進(jìn)行算術(shù)運(yùn)算(包括加減偏移量等,和c不同,指向初始分配內(nèi)存的end邊界點(diǎn)是無(wú)效的),然后在轉(zhuǎn)換回Pointer
2:使用syscall.Syscall. syscall包的Syscall函數(shù)直接將uintptr傳遞給操作系統(tǒng),根據(jù)調(diào)用的細(xì)節(jié),會(huì)將他們中的一些重新轉(zhuǎn)換為指針,系統(tǒng)調(diào)用將會(huì)隱式轉(zhuǎn)換uintptr成pointer,如果一個(gè)指針作為函數(shù)的實(shí)參,而對(duì)應(yīng)的形參是uintptr,那這個(gè)轉(zhuǎn)換必須寫(xiě)在調(diào)用函數(shù)的表達(dá)式上,如
syscall.Syscall(SYS_READ, uintptr(fd), uintptr(unsafe.Pointer(p)), uintptr(n))
3:反射包的Value類型的方法Pointer()和UnsafeAddr()返回uintptr而不是unsafe.Pointer,以防止在沒(méi)有導(dǎo)入unsafe包情況下,調(diào)用者將結(jié)果更改為任意類型。但是這意味著結(jié)果是不穩(wěn)定的,必須在調(diào)用后立即在同一表達(dá)式中將其轉(zhuǎn)換為Pointer,如:
p := (*int)(unsafe.Pointer(reflect.ValueOf(new(int)).Pointer()))
4:反射包的結(jié)構(gòu)體SliceHeader和StringHeader將字段Data聲明為uintptr,以防止調(diào)用者在不沒(méi)有導(dǎo)入unsafe包的情況下將結(jié)果更改為任意類型。
但是,這意味著SliceHeader和StringHeader僅在解釋實(shí)際切片或字符串值的內(nèi)容時(shí)才有效。如:
var s string hdr := (*reflect.StringHeader)(unsafe.Pointer(&s)) // case 1 hdr.Data = uintptr(unsafe.Pointer(p)) // case 6 (this case) hdr.Len = n
詳細(xì)內(nèi)容
1.ArbitraryType類型在本文檔里表示任意一種類型,但并非一個(gè)實(shí)際存在于unsafe包的類型。
2.Pointer類型用于表示任意類型的指針。有4個(gè)特殊的只能用于Pointer類型的操作:
- 任意類型的指針可以轉(zhuǎn)換為一個(gè)Pointer類型值
- 一個(gè)Pointer類型值可以轉(zhuǎn)換為任意類型的指針
- 一個(gè)uintptr類型值可以轉(zhuǎn)換為一個(gè)Pointer類型值
- 一個(gè)Pointer類型值可以轉(zhuǎn)換為一個(gè)uintptr類型值
因此,Pointer類型允許程序繞過(guò)類型系統(tǒng)讀寫(xiě)任意內(nèi)存。使用它時(shí)必須謹(jǐn)慎。
3.以下使用Pointer類型的示范操作都能確保所使用的Pointer是有效的,不遵循這些示范的操作在后續(xù)的golang版本的迭代中不保證操作包含的Pointer是有效的
Conversion of a *T1 to Pointer to *T2
Sizeof(T2)必須小于等于Sizeof(T1),下面是例子
func Float64bits(f float64) uint64 {
return *(*uint64)(unsafe.Pointer(&f))
}
Conversion of a Pointer to a uintptr (but not back to Pointer)
將Pointer轉(zhuǎn)換成uintptr會(huì)用Pointer產(chǎn)生一個(gè)不帶指針語(yǔ)義的整數(shù)值并賦值給uintptr,這個(gè)整數(shù)值是一個(gè)內(nèi)存地址值,盡管是內(nèi)存地址值,但是當(dāng)這個(gè)內(nèi)存地址對(duì)應(yīng)的對(duì)象被移動(dòng)至其他內(nèi)存區(qū)域或者對(duì)應(yīng)的對(duì)象被回收時(shí),gc并不會(huì)更新這個(gè)值,所以將一個(gè)uintptr轉(zhuǎn)換成Pointer通常不會(huì)是有效的,下面列舉了將uintptr轉(zhuǎn)換成Pointer有效的幾種方式
Conversion of a Pointer to a uintptr and back, with arithmetic.
這種方式通常用于獲取結(jié)構(gòu)體字段值或者數(shù)組元素值,下面是例子
p = unsafe.Pointer(uintptr(p) + offset) // equivalent to f := unsafe.Pointer(&s.f) f := unsafe.Pointer(uintptr(unsafe.Pointer(&s)) + unsafe.Offsetof(s.f)) // equivalent to e := unsafe.Pointer(&x[i]) e := unsafe.Pointer(uintptr(unsafe.Pointer(&x[0])) + i*unsafe.Sizeof(x[0]))
使用這種方式對(duì)指針進(jìn)行加減偏移量(即±Offsetof)也是有效的,對(duì)指針進(jìn)行&^操作也是有效的,通常用于對(duì)齊。
所有的方式,結(jié)果都必須指向最初被分配的對(duì)象。
和c不同,指向初始分配內(nèi)存的end邊界點(diǎn)是無(wú)效的,下面是例子
// INVALID: end points outside allocated space. var s thing end = unsafe.Pointer(uintptr(unsafe.Pointer(&s)) + unsafe.Sizeof(s)) // INVALID: end points outside allocated space. b := make([]byte, n) end = unsafe.Pointer(uintptr(unsafe.Pointer(&b[0])) + uintptr(n))
所有的轉(zhuǎn)換都必須在相同的表達(dá)式內(nèi),他們之間也只能有算術(shù)操作,下面是無(wú)效的例子
// INVALID: uintptr cannot be stored in variable // before conversion back to Pointer. u := uintptr(p) p = unsafe.Pointer(u + offset)
指針必須指向一個(gè)已分配的對(duì)象,這樣才不會(huì)接觸到nil指針,下面是無(wú)效的例子
// INVALID: conversion of nil pointer u := unsafe.Pointer(nil) p := unsafe.Pointer(uintptr(u) + offset)
Conversion of a Pointer to a uintptr when calling syscall.Syscall.
syscall包的Syscall函數(shù)直接將uintptr傳遞給操作系統(tǒng),根據(jù)調(diào)用的細(xì)節(jié),會(huì)將他們中的一些重新轉(zhuǎn)換為指針,系統(tǒng)調(diào)用將會(huì)隱式轉(zhuǎn)換uintptr成pointer,如果一個(gè)指針作為函數(shù)的實(shí)參,而對(duì)應(yīng)的形參是uintptr,那這個(gè)轉(zhuǎn)換必須寫(xiě)在調(diào)用函數(shù)的表達(dá)式上,下面是例子
syscall.Syscall(SYS_READ, uintptr(fd), uintptr(unsafe.Pointer(p)), uintptr(n))
編譯器通過(guò)安排,保留所引用的分配對(duì)象(如果有的話),并且在調(diào)用完成之前不會(huì)移動(dòng)分配的對(duì)象,在匯編中實(shí)現(xiàn)的函數(shù)調(diào)用的參數(shù)列表中將Pointer轉(zhuǎn)換為uintptr,即使從類型本身來(lái)看,在調(diào)用過(guò)程中不再需要該對(duì)象。為了編譯器能識(shí)別這種轉(zhuǎn)換,轉(zhuǎn)換表達(dá)式必須顯式出現(xiàn)在實(shí)參列表中。下面是錯(cuò)誤的例子:
// INVALID: uintptr cannot be stored in variable // before implicit conversion back to Pointer during system call. u := uintptr(unsafe.Pointer(p)) syscall.Syscall(SYS_READ, uintptr(fd), u, uintptr(n)
Conversion of the result of reflect.Value.Pointer or reflect.Value.UnsafeAddr from uintptr to Pointer.
反射包的Value類型的方法Pointer()和UnsafeAddr()返回uintptr而不是unsafe.Pointer,以防止在沒(méi)有導(dǎo)入unsafe包情況下,調(diào)用者將結(jié)果更改為任意類型。
但是這意味著結(jié)果是不穩(wěn)定的,必須在調(diào)用后立即在同一表達(dá)式中將其轉(zhuǎn)換為Pointer,下面是正確的例子:
p := (*int)(unsafe.Pointer(reflect.ValueOf(new(int)).Pointer()))
與上面的例子相反,在轉(zhuǎn)換前用變量保存結(jié)果(反射包的Value類型的方法Pointer()和UnsafeAddr()的返回值)是無(wú)效的,下面是例子:
// INVALID: uintptr cannot be stored in variable // before conversion back to Pointer. u := reflect.ValueOf(new(int)).Pointer() p := (*int)(unsafe.Pointer(u))
Conversion of a reflect.SliceHeader or reflect.StringHeader Data field to or from Pointer.
與前面的情況一樣,反射包的結(jié)構(gòu)體SliceHeader和StringHeader將字段Data聲明為uintptr,以防止調(diào)用者在不沒(méi)有導(dǎo)入unsafe包的情況下將結(jié)果更改為任意類型。
但是,這意味著SliceHeader和StringHeader僅在解釋實(shí)際切片或字符串值的內(nèi)容時(shí)才有效。下面是例子:
var s string hdr := (*reflect.StringHeader)(unsafe.Pointer(&s)) // case 1 hdr.Data = uintptr(unsafe.Pointer(p)) // case 6 (this case) hdr.Len = n
在這種用法中,hdr.Data實(shí)際上是引用字符串基礎(chǔ)指針的替代方法,而不是uintptr變量本身。
通常,reflect.SliceHeader和reflect.StringHeader只能用作*reflect.SliceHeader和*reflect.StringHeader指向?qū)嶋H的切片或字符串的情況,而不能用作普通結(jié)構(gòu)體。
程序不應(yīng)聲明或分配這些結(jié)構(gòu)體類型的變量。下面是無(wú)效的例子:
// INVALID: a directly-declared header will not hold Data as a reference. var hdr reflect.StringHeader hdr.Data = uintptr(unsafe.Pointer(p)) hdr.Len = n s := *(*string)(unsafe.Pointer(&hdr)) // p possibly already lost
最后是unsafe包提供的幾種方法的解釋:
// Sizeof 返回類型v本身數(shù)據(jù)所占用的字節(jié)數(shù)。 // 返回值是“頂層”的數(shù)據(jù)占有的字節(jié)數(shù)。 // 例如,若v是一個(gè)切片,它會(huì)返回該切片描述符的大小, // 而非該切片底層引用的內(nèi)存的大小。 func Sizeof(x ArbitraryType) uintptr // Offsetof 返回類型v所代表的結(jié)構(gòu)體字段在結(jié)構(gòu)體中的偏移量, // 它必須為結(jié)構(gòu)體類型的字段的形式。 // 換句話說(shuō),它返回該結(jié)構(gòu)起始處與該字段起始處之間的字節(jié)數(shù)。 func Offsetof(x ArbitraryType) uintptr // Alignof 返回類型v的對(duì)齊方式(即類型v在內(nèi)存中占用的字節(jié)數(shù)); // 若是結(jié)構(gòu)體類型的字段的形式,它會(huì)返回字段f在該結(jié)構(gòu)體中的對(duì)齊方式。 func Alignof(x ArbitraryType) uintptr
以下是unsafe.go的原內(nèi)容
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
/*
Package unsafe contains operations that step around the type safety of Go programs.
Packages that import unsafe may be non-portable and are not protected by the
Go 1 compatibility guidelines.
*/
package unsafe
// ArbitraryType is here for the purposes of documentation only and is not actually
// part of the unsafe package. It represents the type of an arbitrary Go expression.
type ArbitraryType int
// Pointer represents a pointer to an arbitrary type. There are four special operations
// available for type Pointer that are not available for other types:
// - A pointer value of any type can be converted to a Pointer.
// - A Pointer can be converted to a pointer value of any type.
// - A uintptr can be converted to a Pointer.
// - A Pointer can be converted to a uintptr.
// Pointer therefore allows a program to defeat the type system and read and write
// arbitrary memory. It should be used with extreme care.
//
// The following patterns involving Pointer are valid.
// Code not using these patterns is likely to be invalid today
// or to become invalid in the future.
// Even the valid patterns below come with important caveats.
//
// Running "go vet" can help find uses of Pointer that do not conform to these patterns,
// but silence from "go vet" is not a guarantee that the code is valid.
//
// (1) Conversion of a *T1 to Pointer to *T2.
//
// Provided that T2 is no larger than T1 and that the two share an equivalent
// memory layout, this conversion allows reinterpreting data of one type as
// data of another type. An example is the implementation of
// math.Float64bits:
//
// func Float64bits(f float64) uint64 {
// return *(*uint64)(unsafe.Pointer(&f))
// }
//
// (2) Conversion of a Pointer to a uintptr (but not back to Pointer).
//
// Converting a Pointer to a uintptr produces the memory address of the value
// pointed at, as an integer. The usual use for such a uintptr is to print it.
//
// Conversion of a uintptr back to Pointer is not valid in general.
//
// A uintptr is an integer, not a reference.
// Converting a Pointer to a uintptr creates an integer value
// with no pointer semantics.
// Even if a uintptr holds the address of some object,
// the garbage collector will not update that uintptr's value
// if the object moves, nor will that uintptr keep the object
// from being reclaimed.
//
// The remaining patterns enumerate the only valid conversions
// from uintptr to Pointer.
//
// (3) Conversion of a Pointer to a uintptr and back, with arithmetic.
//
// If p points into an allocated object, it can be advanced through the object
// by conversion to uintptr, addition of an offset, and conversion back to Pointer.
//
// p = unsafe.Pointer(uintptr(p) + offset)
//
// The most common use of this pattern is to access fields in a struct
// or elements of an array:
//
// // equivalent to f := unsafe.Pointer(&s.f)
// f := unsafe.Pointer(uintptr(unsafe.Pointer(&s)) + unsafe.Offsetof(s.f))
//
// // equivalent to e := unsafe.Pointer(&x[i])
// e := unsafe.Pointer(uintptr(unsafe.Pointer(&x[0])) + i*unsafe.Sizeof(x[0]))
//
// It is valid both to add and to subtract offsets from a pointer in this way.
// It is also valid to use &^ to round pointers, usually for alignment.
// In all cases, the result must continue to point into the original allocated object.
//
// Unlike in C, it is not valid to advance a pointer just beyond the end of
// its original allocation:
//
// // INVALID: end points outside allocated space.
// var s thing
// end = unsafe.Pointer(uintptr(unsafe.Pointer(&s)) + unsafe.Sizeof(s))
//
// // INVALID: end points outside allocated space.
// b := make([]byte, n)
// end = unsafe.Pointer(uintptr(unsafe.Pointer(&b[0])) + uintptr(n))
//
// Note that both conversions must appear in the same expression, with only
// the intervening arithmetic between them:
//
// // INVALID: uintptr cannot be stored in variable
// // before conversion back to Pointer.
// u := uintptr(p)
// p = unsafe.Pointer(u + offset)
//
// Note that the pointer must point into an allocated object, so it may not be nil.
//
// // INVALID: conversion of nil pointer
// u := unsafe.Pointer(nil)
// p := unsafe.Pointer(uintptr(u) + offset)
//
// (4) Conversion of a Pointer to a uintptr when calling syscall.Syscall.
//
// The Syscall functions in package syscall pass their uintptr arguments directly
// to the operating system, which then may, depending on the details of the call,
// reinterpret some of them as pointers.
// That is, the system call implementation is implicitly converting certain arguments
// back from uintptr to pointer.
//
// If a pointer argument must be converted to uintptr for use as an argument,
// that conversion must appear in the call expression itself:
//
// syscall.Syscall(SYS_READ, uintptr(fd), uintptr(unsafe.Pointer(p)), uintptr(n))
//
// The compiler handles a Pointer converted to a uintptr in the argument list of
// a call to a function implemented in assembly by arranging that the referenced
// allocated object, if any, is retained and not moved until the call completes,
// even though from the types alone it would appear that the object is no longer
// needed during the call.
//
// For the compiler to recognize this pattern,
// the conversion must appear in the argument list:
//
// // INVALID: uintptr cannot be stored in variable
// // before implicit conversion back to Pointer during system call.
// u := uintptr(unsafe.Pointer(p))
// syscall.Syscall(SYS_READ, uintptr(fd), u, uintptr(n))
//
// (5) Conversion of the result of reflect.Value.Pointer or reflect.Value.UnsafeAddr
// from uintptr to Pointer.
//
// Package reflect's Value methods named Pointer and UnsafeAddr return type uintptr
// instead of unsafe.Pointer to keep callers from changing the result to an arbitrary
// type without first importing "unsafe". However, this means that the result is
// fragile and must be converted to Pointer immediately after making the call,
// in the same expression:
//
// p := (*int)(unsafe.Pointer(reflect.ValueOf(new(int)).Pointer()))
//
// As in the cases above, it is invalid to store the result before the conversion:
//
// // INVALID: uintptr cannot be stored in variable
// // before conversion back to Pointer.
// u := reflect.ValueOf(new(int)).Pointer()
// p := (*int)(unsafe.Pointer(u))
//
// (6) Conversion of a reflect.SliceHeader or reflect.StringHeader Data field to or from Pointer.
//
// As in the previous case, the reflect data structures SliceHeader and StringHeader
// declare the field Data as a uintptr to keep callers from changing the result to
// an arbitrary type without first importing "unsafe". However, this means that
// SliceHeader and StringHeader are only valid when interpreting the content
// of an actual slice or string value.
//
// var s string
// hdr := (*reflect.StringHeader)(unsafe.Pointer(&s)) // case 1
// hdr.Data = uintptr(unsafe.Pointer(p)) // case 6 (this case)
// hdr.Len = n
//
// In this usage hdr.Data is really an alternate way to refer to the underlying
// pointer in the string header, not a uintptr variable itself.
//
// In general, reflect.SliceHeader and reflect.StringHeader should be used
// only as *reflect.SliceHeader and *reflect.StringHeader pointing at actual
// slices or strings, never as plain structs.
// A program should not declare or allocate variables of these struct types.
//
// // INVALID: a directly-declared header will not hold Data as a reference.
// var hdr reflect.StringHeader
// hdr.Data = uintptr(unsafe.Pointer(p))
// hdr.Len = n
// s := *(*string)(unsafe.Pointer(&hdr)) // p possibly already lost
//
type Pointer *ArbitraryType
// Sizeof takes an expression x of any type and returns the size in bytes
// of a hypothetical variable v as if v was declared via var v = x.
// The size does not include any memory possibly referenced by x.
// For instance, if x is a slice, Sizeof returns the size of the slice
// descriptor, not the size of the memory referenced by the slice.
// The return value of Sizeof is a Go constant.
func Sizeof(x ArbitraryType) uintptr
// Offsetof returns the offset within the struct of the field represented by x,
// which must be of the form structValue.field. In other words, it returns the
// number of bytes between the start of the struct and the start of the field.
// The return value of Offsetof is a Go constant.
func Offsetof(x ArbitraryType) uintptr
// Alignof takes an expression x of any type and returns the required alignment
// of a hypothetical variable v as if v was declared via var v = x.
// It is the largest value m such that the address of v is always zero mod m.
// It is the same as the value returned by reflect.TypeOf(x).Align().
// As a special case, if a variable s is of struct type and f is a field
// within that struct, then Alignof(s.f) will return the required alignment
// of a field of that type within a struct. This case is the same as the
// value returned by reflect.TypeOf(s.f).FieldAlign().
// The return value of Alignof is a Go constant.
func Alignof(x ArbitraryType) uintptr
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Golang?實(shí)現(xiàn)Redis?協(xié)議解析器的解決方案
這篇文章主要介紹了Golang???實(shí)現(xiàn)?Redis?協(xié)議解析器,本文將分別介紹Redis 通信協(xié)議 以及 協(xié)議解析器 的實(shí)現(xiàn),若您對(duì)協(xié)議有所了解可以直接閱讀協(xié)議解析器部分,需要的朋友可以參考下2022-10-10
Go調(diào)用opencv實(shí)現(xiàn)圖片矯正的代碼示例
這篇文章主要為大家詳細(xì)介紹了Go調(diào)用opencv實(shí)現(xiàn)圖片矯正的代碼示例,文中的示例代碼簡(jiǎn)潔易懂,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-09-09
go-cqhttp權(quán)限管理系統(tǒng)的實(shí)現(xiàn)代碼
這篇文章主要介紹了go-cqhttp權(quán)限管理,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09
Golang異常處理之defer,panic,recover的使用詳解
這篇文章主要為大家介紹了Go語(yǔ)言異常處理機(jī)制中defer、panic和recover三者的使用方法,文中示例代碼講解詳細(xì),需要的朋友可以參考下2022-05-05
Go項(xiàng)目與Docker結(jié)合實(shí)現(xiàn)高效部署深入探究
在現(xiàn)代軟件開(kāi)發(fā)中,使用Docker部署應(yīng)用程序已經(jīng)成為一種標(biāo)準(zhǔn)實(shí)踐,本文將深入探討如何將Go項(xiàng)目與Docker結(jié)合,實(shí)現(xiàn)高效、可靠的部署過(guò)程,通過(guò)詳細(xì)的步驟和豐富的示例,你將能夠迅速掌握這一流程2023-12-12
Go語(yǔ)言MySQLCURD數(shù)據(jù)庫(kù)操作示例詳解
這篇文章主要為大家介紹了Go語(yǔ)言MySQLCURD數(shù)據(jù)庫(kù)操作示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
windows下使用GoLand生成proto文件的方法步驟
本文主要介紹了windows下使用GoLand生成proto文件的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
Windows下CMD執(zhí)行Go出現(xiàn)中文亂碼的解決方法
本文主要介紹了Windows下CMD執(zhí)行Go出現(xiàn)中文亂碼的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02

