agiledragon / gomonkey

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

macOS permission denied #70

Open liangjingkanji opened 2 years ago

liangjingkanji commented 2 years ago

Line 19: - permission denied goroutine 4 [running]:

发生在

err := syscall.Mprotect(page, syscall.PROT_READ|syscall.PROT_WRITE|syscall.PROT_EXEC)
if err != nil {
    panic(err)
}

环境

描述:

jinshanwang commented 8 months ago

thanks

Original

From:"Zpl"< @.*** >;

Date:2023/10/14 23:56

To:"agiledragon/gomonkey"< @.*** >;

CC:"Joseph"< @. >;"Comment"< @. >;

Subject:Re: [agiledragon/gomonkey] macOS permission denied (Issue #70)

Test ok at Mac M1 13.2.1 Please follow step below to solve problem "permission denied" follow this issues steam solution and test at Mac M1.

Enjoy it and save your life, I donnot know why!

find this file modify_binary_darwin.go in your GoProjects/pkg

currently gomonkey version is @.***, please replace this file with code below directly package gomonkey import ( "syscall" "time" ) func modifyBinary(target uintptr, bytes []byte) { function := entryAddress(target, len(bytes)) err := mprotectCrossPage(target, len(bytes), syscall.PROT_READ|syscall.PROT_WRITE) if err != nil { panic(err) } copy(function, bytes) err = mprotectCrossPage(target, len(bytes), syscall.PROT_READ|syscall.PROT_EXEC) if err != nil { panic(err) } time.Sleep(time.Millisecond) } 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 }
这个测试可用,但是这里的表述有点问题,我补充一下: 1、只需要修改文件modify_binary_darwin.go第7行,删除参数:syscall.PROT_EXEC 最终:err := mprotectCrossPage(target, len(bytes), syscall.PROT_READ|syscall.PROT_WRITE) 2、使用go test -gcflags=all=-l 进行测试

work for me

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

LargeOrange commented 8 months ago

Test ok at Mac M1 13.2.1 Please follow step below to solve problem "permission denied" follow this issues steam solution and test at Mac M1.

  • Enjoy it and save your life, I donnot know why!
  1. find this file modify_binary_darwin.go in your GoProjects/pkg
  2. currently gomonkey version is v2@v2.9.0, please replace this file with code below directly
package gomonkey

import (
    "syscall"
    "time"
)

func modifyBinary(target uintptr, bytes []byte) {
    function := entryAddress(target, len(bytes))
    err := mprotectCrossPage(target, len(bytes), syscall.PROT_READ|syscall.PROT_WRITE)
    if err != nil {
        panic(err)
    }
    copy(function, bytes)
    err = mprotectCrossPage(target, len(bytes), syscall.PROT_READ|syscall.PROT_EXEC)
    if err != nil {
        panic(err)
    }
    time.Sleep(time.Millisecond)
}

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
}

这个测试可用,但是这里的表述有点问题,我补充一下: 1、只需要修改文件modify_binary_darwin.go第7行,删除参数:syscall.PROT_EXEC 最终:err := mprotectCrossPage(target, len(bytes), syscall.PROT_READ|syscall.PROT_WRITE) 2、使用go test -gcflags=all=-l 进行测试

worked for me.

agiledragon commented 8 months ago

v2.11.0 has been released!

Nomango commented 8 months ago

v2.11.0 has been released!

It works on my M2 Mac! 🎉

gaochuwuhan commented 8 months ago

The solution for mac with intel: add go tool args: -gcflags "all=-N -l" -ldflags="-extldflags="-Wl,-segprot,__TEXT,rwx,rx""

arfan commented 8 months ago

in mac M1 I get Process finished with the exit code 139 (interrupted by signal 11: SIGSEGV)

my code is


import (
    "fmt"
    "github.com/agiledragon/gomonkey/v2"
)

func add(a, b int) int {
    return a + b
}

func main() {
    fmt.Println(add(5, 2))
    gomonkey.ApplyFunc(add, func(a, b int) int {
        return a * b
    })
    fmt.Println(add(5, 2))
}

im using version 2.11.0, golang 1.20.5

HildaM commented 7 months ago

I update gomonkey from v2.9.0 to v2.11.0 and finally fix this problem!

Additional info:

  1. m1 pro
  2. go 1.12
  3. goconvey 1.7.2
719010539 commented 7 months ago

收到!

agiledragon commented 7 months ago

I update gomonkey from v2.9.0 to v2.11.0 and finally fix this problem!

Additional info:

  1. m1 pro
  2. go 1.12
  3. goconvey 1.7.2

@myzhan Good news!

sidgwick commented 3 months ago

执行到 mProtect 函数的时候还是在报错:

719010539 commented 3 months ago

收到!

xhd2015 commented 3 months ago

It seems that gomonkey is unstable. People are getting frequently trapped into this error. xgo has solved all the compatibility issues. If anyone want to try, check https://github.com/xhd2015/xgo for more details.