agiledragon / gomonkey

gomonkey is a library to make monkey patching in unit tests easy
MIT License
1.92k stars 178 forks source link

MacOS 上的 permission denied 问题,也可能和 cgo 有关 #142

Open myzhan opened 8 months ago

myzhan commented 8 months ago

环境

  1. macOS Sonoma
  2. AMD64
  3. go 1.20.7

问题 之前都是正常使用,通过 cgo 引入 sqlite 依赖后,运行出现 permission denied 的问题。引入 cgo 的模块,跟 monkey patch 的模块没有任何关联。只是发现引入 cgo 后,monkey patch 的函数地址变得很大,40 多亿的一个整数,没有引入 cgo 之前是 4 千万。

解法

  1. 通过 -ldflags=-extldflags=-Wl,-segprot,__TEXT,rwx,rx 参数运行 go test,可行。
  2. 换用 https://github.com/bytedance/mockey 库,可行。通过对比代码,发现 mockey 在通过 mprotect 修改失败后,还会通过 mach_vm_protect 再尝试修改。去掉 mach_vm_protect 部分后,mockey 也不能 patch。
agiledragon commented 8 months ago

please review https://github.com/agiledragon/gomonkey/pull/143

myzhan commented 7 months ago

Tried on both intel machine and M1 machine, no luck.

agiledragon commented 7 months ago

Can you propose a PR to port mockey's mach_vm_protect feature to gomonkey?

agiledragon commented 7 months ago

https://github.com/agiledragon/gomonkey/pull/144 has been merged, please test it on both intel machine and M1 machine.

myzhan commented 7 months ago

Yes, I have tested it on both intel and M1 machine, macOS sonoma.

myzhan commented 7 months ago

Can you push a release tag?

agiledragon commented 7 months ago

Yes, I have tested it on both intel and M1 machine, macOS sonoma.

Good job!

agiledragon commented 7 months ago

Can you push a release tag?

v2.11.0 has been released!