使用golang-unsafe包的注意事项及说明
偷偷努力,悄无声息地变强,然后惊艳所有人!哈哈,小伙伴们又来学习啦~今天我将给大家介绍《使用golang-unsafe包的注意事项及说明》,这篇文章主要会讲到unsafe等等知识点,不知道大家对其都有多少了解,下面我们就一起来看一吧!当然,非常希望大家能多多评论,给出合理的建议,我们一起学习,一起进步!
基于golang 15.5
总结(详细的内容可以往下看)
1.不能使用unsafe包里的ArbitraryType类型
2.Pointer类型可以表示任意类型的指针,所以可以用Pointer类型作为中介进行两种不同类型指针的转换,为保证作为中介的Pointer类型数据有效,必须保证所有转换在同一个表达式中,如:
func Float64bits(f float64) uint64 {
return *(*uint64)(unsafe.Pointer(&f))
}3.Pointer类型可以安全有效的转换成uintptr,不能以任何形式(包含变量)保存转换后的uintptr值,转换后的uintptr值仅在转换所在的表达式中有效的。
4.因为uintptr只是内存地址值,并没有指针语义,所以uintptr转换成Pointer通常不会是有效的
下面列举有效的转换方式:
1:在同一个表达式内,对Pointer转换成的uintptr值进行算术运算(包括加减偏移量等,和c不同,指向初始分配内存的end边界点是无效的),然后在转换回Pointer
2:使用syscall.Syscall. syscall包的Syscall函数直接将uintptr传递给操作系统,根据调用的细节,会将他们中的一些重新转换为指针,系统调用将会隐式转换uintptr成pointer,如果一个指针作为函数的实参,而对应的形参是uintptr,那这个转换必须写在调用函数的表达式上,如
syscall.Syscall(SYS_READ, uintptr(fd), uintptr(unsafe.Pointer(p)), uintptr(n))
3:反射包的Value类型的方法Pointer()和UnsafeAddr()返回uintptr而不是unsafe.Pointer,以防止在没有导入unsafe包情况下,调用者将结果更改为任意类型。但是这意味着结果是不稳定的,必须在调用后立即在同一表达式中将其转换为Pointer,如:
p := (*int)(unsafe.Pointer(reflect.ValueOf(new(int)).Pointer()))
4:反射包的结构体SliceHeader和StringHeader将字段Data声明为uintptr,以防止调用者在不没有导入unsafe包的情况下将结果更改为任意类型。
但是,这意味着SliceHeader和StringHeader仅在解释实际切片或字符串值的内容时才有效。如:
var s string hdr := (*reflect.StringHeader)(unsafe.Pointer(&s)) // case 1 hdr.Data = uintptr(unsafe.Pointer(p)) // case 6 (this case) hdr.Len = n
详细内容
1.ArbitraryType类型在本文档里表示任意一种类型,但并非一个实际存在于unsafe包的类型。
2.Pointer类型用于表示任意类型的指针。有4个特殊的只能用于Pointer类型的操作:
- 任意类型的指针可以转换为一个Pointer类型值
- 一个Pointer类型值可以转换为任意类型的指针
- 一个uintptr类型值可以转换为一个Pointer类型值
- 一个Pointer类型值可以转换为一个uintptr类型值
因此,Pointer类型允许程序绕过类型系统读写任意内存。使用它时必须谨慎。
3.以下使用Pointer类型的示范操作都能确保所使用的Pointer是有效的,不遵循这些示范的操作在后续的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转换成uintptr会用Pointer产生一个不带指针语义的整数值并赋值给uintptr,这个整数值是一个内存地址值,尽管是内存地址值,但是当这个内存地址对应的对象被移动至其他内存区域或者对应的对象被回收时,gc并不会更新这个值,所以将一个uintptr转换成Pointer通常不会是有效的,下面列举了将uintptr转换成Pointer有效的几种方式
Conversion of a Pointer to a uintptr and back, with arithmetic.
这种方式通常用于获取结构体字段值或者数组元素值,下面是例子
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]))
使用这种方式对指针进行加减偏移量(即±Offsetof)也是有效的,对指针进行&^操作也是有效的,通常用于对齐。
所有的方式,结果都必须指向最初被分配的对象。
和c不同,指向初始分配内存的end边界点是无效的,下面是例子
// 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))
所有的转换都必须在相同的表达式内,他们之间也只能有算术操作,下面是无效的例子
// INVALID: uintptr cannot be stored in variable // before conversion back to Pointer. u := uintptr(p) p = unsafe.Pointer(u + offset)
指针必须指向一个已分配的对象,这样才不会接触到nil指针,下面是无效的例子
// 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函数直接将uintptr传递给操作系统,根据调用的细节,会将他们中的一些重新转换为指针,系统调用将会隐式转换uintptr成pointer,如果一个指针作为函数的实参,而对应的形参是uintptr,那这个转换必须写在调用函数的表达式上,下面是例子
syscall.Syscall(SYS_READ, uintptr(fd), uintptr(unsafe.Pointer(p)), uintptr(n))
编译器通过安排,保留所引用的分配对象(如果有的话),并且在调用完成之前不会移动分配的对象,在汇编中实现的函数调用的参数列表中将Pointer转换为uintptr,即使从类型本身来看,在调用过程中不再需要该对象。为了编译器能识别这种转换,转换表达式必须显式出现在实参列表中。下面是错误的例子:
// 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,以防止在没有导入unsafe包情况下,调用者将结果更改为任意类型。
但是这意味着结果是不稳定的,必须在调用后立即在同一表达式中将其转换为Pointer,下面是正确的例子:
p := (*int)(unsafe.Pointer(reflect.ValueOf(new(int)).Pointer()))
与上面的例子相反,在转换前用变量保存结果(反射包的Value类型的方法Pointer()和UnsafeAddr()的返回值)是无效的,下面是例子:
// 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.
与前面的情况一样,反射包的结构体SliceHeader和StringHeader将字段Data声明为uintptr,以防止调用者在不没有导入unsafe包的情况下将结果更改为任意类型。
但是,这意味着SliceHeader和StringHeader仅在解释实际切片或字符串值的内容时才有效。下面是例子:
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实际上是引用字符串基础指针的替代方法,而不是uintptr变量本身。
通常,reflect.SliceHeader和reflect.StringHeader只能用作*reflect.SliceHeader和*reflect.StringHeader指向实际的切片或字符串的情况,而不能用作普通结构体。
程序不应声明或分配这些结构体类型的变量。下面是无效的例子:
// 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本身数据所占用的字节数。 // 返回值是“顶层”的数据占有的字节数。 // 例如,若v是一个切片,它会返回该切片描述符的大小, // 而非该切片底层引用的内存的大小。 func Sizeof(x ArbitraryType) uintptr // Offsetof 返回类型v所代表的结构体字段在结构体中的偏移量, // 它必须为结构体类型的字段的形式。 // 换句话说,它返回该结构起始处与该字段起始处之间的字节数。 func Offsetof(x ArbitraryType) uintptr // Alignof 返回类型v的对齐方式(即类型v在内存中占用的字节数); // 若是结构体类型的字段的形式,它会返回字段f在该结构体中的对齐方式。 func Alignof(x ArbitraryType) uintptr
以下是unsafe.go的原内容
// 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
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持golang学习网。
理论要掌握,实操不能落!以上关于《使用golang-unsafe包的注意事项及说明》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!
解读SQL语句中要不要加单引号的问题
- 上一篇
- 解读SQL语句中要不要加单引号的问题
- 下一篇
- 解读unsafe.Pointer和uintptr的区别
-
- Golang · Go教程 | 7小时前 | 格式化输出 printf fmt库 格式化动词 Stringer接口
- Golangfmt库用法与格式化技巧解析
- 140浏览 收藏
-
- Golang · Go教程 | 7小时前 |
- Golang配置Protobuf安装教程
- 147浏览 收藏
-
- Golang · Go教程 | 8小时前 |
- Golang中介者模式实现与通信解耦技巧
- 378浏览 收藏
-
- Golang · Go教程 | 8小时前 |
- Golang多协程通信技巧分享
- 255浏览 收藏
-
- Golang · Go教程 | 8小时前 |
- Golang如何判断变量类型?
- 393浏览 收藏
-
- Golang · Go教程 | 8小时前 |
- Golang云原生微服务实战教程
- 310浏览 收藏
-
- Golang · Go教程 | 9小时前 |
- Golang迭代器与懒加载结合应用
- 110浏览 收藏
-
- Golang · Go教程 | 9小时前 | 性能优化 并发安全 Golangslicemap 预设容量 指针拷贝
- Golangslicemap优化技巧分享
- 412浏览 收藏
-
- Golang · Go教程 | 9小时前 |
- Golang代理模式与访问控制实现解析
- 423浏览 收藏
-
- Golang · Go教程 | 9小时前 |
- Golang事件管理模块实现教程
- 274浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
-
- ChatExcel酷表
- ChatExcel酷表是由北京大学团队打造的Excel聊天机器人,用自然语言操控表格,简化数据处理,告别繁琐操作,提升工作效率!适用于学生、上班族及政府人员。
- 3167次使用
-
- Any绘本
- 探索Any绘本(anypicturebook.com/zh),一款开源免费的AI绘本创作工具,基于Google Gemini与Flux AI模型,让您轻松创作个性化绘本。适用于家庭、教育、创作等多种场景,零门槛,高自由度,技术透明,本地可控。
- 3380次使用
-
- 可赞AI
- 可赞AI,AI驱动的办公可视化智能工具,助您轻松实现文本与可视化元素高效转化。无论是智能文档生成、多格式文本解析,还是一键生成专业图表、脑图、知识卡片,可赞AI都能让信息处理更清晰高效。覆盖数据汇报、会议纪要、内容营销等全场景,大幅提升办公效率,降低专业门槛,是您提升工作效率的得力助手。
- 3409次使用
-
- 星月写作
- 星月写作是国内首款聚焦中文网络小说创作的AI辅助工具,解决网文作者从构思到变现的全流程痛点。AI扫榜、专属模板、全链路适配,助力新人快速上手,资深作者效率倍增。
- 4513次使用
-
- MagicLight
- MagicLight.ai是全球首款叙事驱动型AI动画视频创作平台,专注于解决从故事想法到完整动画的全流程痛点。它通过自研AI模型,保障角色、风格、场景高度一致性,让零动画经验者也能高效产出专业级叙事内容。广泛适用于独立创作者、动画工作室、教育机构及企业营销,助您轻松实现创意落地与商业化。
- 3789次使用
-
- Golang标准库unsafe源码解读
- 2022-12-29 464浏览
-
- golang利用unsafe操作未导出变量-Pointer使用详解
- 2022-12-24 151浏览
-
- 使用golang-unsafe包的注意事项及说明
- 2023-02-25 237浏览
-
- Go语言中unsafe包怎么使用
- 2023-05-03 453浏览

