Skip to content

Commit

Permalink
Add note about unpatchall
Browse files Browse the repository at this point in the history
  • Loading branch information
bouk committed Mar 29, 2015
1 parent ed0b7cf commit 32cf4d1
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ func main() {

Note that patching the method for just one instance is currently not possible, `PatchInstanceMethod` will patch it for all instances. Don't bother trying `monkey.Patch(instance.Method, replacement)`, it won't work. `monkey.UnpatchInstanceMethod(<type>, <name>)` will undo `PatchInstanceMethod`.

If you want to remove all currently applied monkeypatches simply call `monkey.UnpatchAll`. This could be useful in a test teardown function.

If you want to call the original function from within the replacement you need to use a `monkey.PatchGuard`. A patchguard allows you to easily remove and restore the patch so you can call the original function. For example:

```go
Expand Down
2 changes: 2 additions & 0 deletions replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ func pageStart(ptr uintptr) uintptr {
return ptr & ^(uintptr(syscall.Getpagesize() - 1))
}

// from is a pointer to the actual function
// to is a pointer to a go funcvalue
func replaceFunction(from, to uintptr) (original []byte) {
jumpData := jmpToFunctionValue(to)
f := rawMemoryAccess(from, len(jumpData))
Expand Down
1 change: 1 addition & 0 deletions replace_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

// this function is super unsafe
// aww yeah
// It copies a slice to a raw memory location, disabling all memory protection before doing so.
func copyToLocation(location uintptr, data []byte) {
f := rawMemoryAccess(location, len(data))

Expand Down

0 comments on commit 32cf4d1

Please sign in to comment.