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 <filename>  <*_test文件>

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

$  printf '\x07' | dd of=<filename> 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文件>

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值