环境:MacBook Pro M2芯片; go version go1.20.5 darwin/arm64
详细报错:
--- FAIL: TestErrorCode (2.38s)
panic: permission denied [recovered]
panic: permission denied
解决方法:
报错位置:vendor/github.com/agiledragon/gomonkey/v2/modify_binary_darwin.go:9
源文件:
package gomonkey
import "syscall"
func modifyBinary(target uintptr, bytes []byte) {
function := entryAddress(target, len(bytes))
err := mprotectCrossPage(target, len(bytes), syscall.PROT_READ|syscall.PROT_WRITE|syscall.PROT_EXEC)
if err != nil {
panic(err)
}
copy(function, bytes)
err = mprotectCrossPage(target, len(bytes), syscall.PROT_READ|syscall.PROT_EXEC)
if err != nil {
panic(err)
}
}
func mprotectCrossPage(addr uintptr, length int, prot int) error {
pageSize := syscall.Getpagesize()
for p := pageStart(addr); p < addr+uintptr(length); p += uintptr(pageSize) {
page := entryAddress(p, pageSize)
if err := syscall.Mprotect(page, prot); err != nil {
return err
}
}
return nil
}
修改为
package gomonkey
import (
"syscall"
)
func modifyBinary(target uintptr, bytes []byte) {
function := entryAddress(target, len(bytes))
page := entryAddress(pageStart(target), syscall.Getpagesize())
err := syscall.Mprotect(page, syscall.PROT_READ|syscall.PROT_WRITE)
if err != nil {
panic(err)
}
copy(function, bytes)
err = syscall.Mprotect(page, syscall.PROT_READ|syscall.PROT_EXEC)
if err != nil {
panic(err)
}
}
func mprotectCrossPage(addr uintptr, length int, prot int) error {
pageSize := syscall.Getpagesize()
for p := pageStart(addr); p < addr+uintptr(length); p += uintptr(pageSize) {
page := entryAddress(p, pageSize)
if err := syscall.Mprotect(page, prot); err != nil {
return err
}
}
return nil
}