Skip to content

Commit

Permalink
optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
cch123 committed Sep 4, 2020
1 parent 62a38fd commit 2fd1b78
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion examples/patch_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ func main() {

//go:noinline
func heyHey() {
fmt.Println("fuck")
fmt.Println("fake")
}
2 changes: 1 addition & 1 deletion examples/patch_func_symbol.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ func main() {

//go:noinline
func heyHey() {
fmt.Println("fuck")
fmt.Println("fake")
}
14 changes: 6 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# SuperMonkey

This lib is not production ready, please don't use

## Introduction

Patch all functions including which are unexported
Patch all functions without limits, including which are unexported

**Warning** : please add -l to your gcflags, or it will panic.
**Warning** : please add -l to your gcflags or add `//go:noinline` to func which you want to patch.

### patch private function

Expand All @@ -23,7 +21,7 @@ import (

func main() {
fmt.Println("original function output:")
heyHey() // fuck
heyHey() // fake
fmt.Println()

sm.Patch("main", "", "heyHey", func() {
Expand All @@ -35,11 +33,11 @@ func main() {

sm.UnpatchAll()
fmt.Println("unpatch all, then output:")
heyHey() // fuck
heyHey() // fake
}

func heyHey() {
fmt.Println("fuck")
fmt.Println("fake")
}
```

Expand Down Expand Up @@ -75,7 +73,7 @@ func main() {

//go:noinline
func heyHey() {
fmt.Println("fuck")
fmt.Println("fake")
}

```
Expand Down
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions supermonkey.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package supermonkey

import (
"fmt"
"os"
"reflect"
"strconv"
Expand All @@ -25,6 +26,12 @@ func Patch(pkgName, typeName, methodName string, patchFunc interface{}) {
// PatchByFullSymbolName needs user to provide the full symbol path
func PatchByFullSymbolName(symbolName string, patchFunc interface{}) {
addr := symbolTable[symbolName]
if addr == 0 {
fmt.Printf("The symbol is %v, and the patch target addr is 0, there may be 2 possible reasons\n", symbolName)
fmt.Println(" 1. the function is inlined, please add //go:noinline to function comment or add -l to gcflags")
fmt.Println(" 2. your input for symbolName or pkg/obj/method is wrong, check by using go tool nm {your_bin_file}")
panic("")
}
originalBytes := replaceFunction(addr, (uintptr)(getPtr(reflect.ValueOf(patchFunc))))
patchRecord[addr] = originalBytes
}
Expand Down

0 comments on commit 2fd1b78

Please sign in to comment.