cch123 / supermonkey

Patch all Go functions for testing
MIT License
250 stars 17 forks source link

Is there any way to call original function by symbol name? #18

Open cutecutecat opened 2 years ago

cutecutecat commented 2 years ago
        var guard_down *sm.PatchGuard
    download_hook := "xxx.DownloadPiece"
    guard_down = sm.PatchByFullSymbolName(download_hook, func(ctx context.Context, pt peer.Task, request *peer.DownloadPieceRequest) (success bool) {
        // do something

                // temporary unpatch
        guard_down.Unpatch()
        defer guard_down.Restore()

                // use forceexport to fetch original function
        var function func(context.Context, peer.Task, *peer.DownloadPieceRequest) (success bool)
        err := forceexport.GetFunc(&function, download_hook)
        if err != nil {
            log.Panicf("connect target error: %s", err)
        }

                // call original function
        success = function(ctx, pt, request)
        return
    })

If the original function is unexported, and should be called inside the hooked method as above, is supermonkey provide some way to call it by symbol name rather than use forceexport?

cch123 commented 2 years ago

I have read the code of forceexport, we can use most of its code, and only change the implementation of:

func FindFuncWithName(name string) (uintptr, error) {
}

我看了一下 forceexport 的代码,如果在 supermonkey 里实现的话,应该简单改改 forceexport 的 FindFuncWithName 这部分就行了, 有兴趣贡献一发么~

cutecutecat commented 2 years ago

没问题,但是可能要等一两周,最近手头其他工作比较忙。