crossorigin="anonymous">

修复M1使用gomonkey提示permission defined错误

2023年4月20日 · 20 字 · 1 分钟

问题

Go单元测试在M1上使用github.com/agiledragon/gomonkey/v2 v2.9.0包提示permission defined。 网上查阅消息得知是由于内存安全导致,不能同时对内存进行写和执行

解决方法

下面分享一种比较简单的方法,需要修改本地的go源码。

修改go/pkg/mod/github.com/agiledragon/gomonkey/[email protected]/modify_binary_darwin.gomodifyBinary方法。 将

	err := mprotectCrossPage(target, len(bytes), syscall.PROT_READ|syscall.PROT_WRITE|syscall.PROT_EXEC)

修改为

	err := mprotectCrossPage(target, len(bytes), syscall.PROT_READ|syscall.PROT_WRITE)