栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Go语言

Mac 10.15之后版本系统Gomonkey错误解决

Go语言 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Mac 10.15之后版本系统Gomonkey错误解决

Mac 10.15之后版本系统Gomonkey错误解决

前景:在写单元测试的时候,利用gomonkey框架打桩方法函数的时候,出现了如下问题:

Line 81: - permission denied 
  goroutine 13 [running]:
  	/Users/wuhao/go/pkg/mod/github.com/smartystreets/goconvey@v1.6.4/convey/reporting/reports.go:148 +0x6d
  	/Users/wuhao/go/pkg/mod/github.com/smartystreets/goconvey@v1.6.4/convey/reporting/reports.go:121 +0x70
  	/Users/wuhao/go/pkg/mod/github.com/smartystreets/goconvey@v1.6.4/convey/context.go:235 +0x137

原因如下(属实没看懂):

Your problem is caused by changes of the linker (ld64) in macOS Catalina. The default value of the max_prot attribute of the __TEXT segment in the Mach-O header has been changed.
Previously max_prot default value was 0x7 (PROT_READ | PROT_WRITE | PROT_EXEC).
The default value has now been changed to 0x5 (PROT_READ | PROT_EXEC)
This means that mprotect cannot make any region that resides within __TEXT writable.
In theory, this should be resolved by providing the linker flag -segprot __TEXT rwx rx, but this is not the case. Since Catalina, the max_prot field is ignored. Instead, max_prot is set to the value of init_prot (see here).
To top it all off, init_prot cannot be set to rwx either due to macOS refusing to execute a file which has a writable __TEXT(init_prot) attribute.

解决方案1:
1.将测试功能编译为一个临时二进制文件

$ go test -c -o   <*_test文件>

2.通过dd命令将二进制文件修改为set __TEXT(max_prot)到0x7:

$  printf 'x07' | dd of= bs=1 seek=160 count=1 conv=notrunc

3.运行 二进制文件

缺点:每次改动都要重新编译,较为麻烦,且开发效率低下

解决方案2:
1.使用docker镜像,将自己的/Users/admin/go下的文件映射到docker中的/go

docker run -it --rm -v /Users/go:/go --privileged docker.io/golang  bash

2.运行go test <*_test文件>

缺点:找不出更好的方法了~

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/991377.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号