Skip to content
This repository was archived by the owner on Jun 17, 2020. It is now read-only.

Commit f95082e

Browse files
committed
Hold onto replacement references
This is to prevent the garbage collector from freeing the replacement. Doing so appears to cause a crash when the patched function is called: unexpected fault address 0xc82041c740 fatal error: fault [signal 0xb code=0x2 addr=0xc82041c740 pc=0xc82041c740] goroutine 4 [running]: runtime.throw(0xa1b1c8, 0x5) /home/aluce/.gvm/gos/go1.6.2/src/runtime/panic.go:547 +0x90 fp=0xc820037d08 sp=0xc820037cf0 runtime.sigpanic() /home/aluce/.gvm/gos/go1.6.2/src/runtime/sigpanic_unix.go:27 +0x2ab fp=0xc820037d58 sp=0xc820037d08 github.com/lyfe-mobile/receiver.StartMovers.func1(0xc8204090e0, 0xc820409140, 0x1312d00, 0xa33b30, 0xa) /home/aluce/.gvm/pkgsets/go1.6.2/global/src/github.com/lyfe-mobile/receiver/movers.go:37 +0x531 fp=0xc820037f88 sp=0xc20037d58
1 parent 56c0911 commit f95082e

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

monkey.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var (
1717
lock = sync.Mutex{}
1818

1919
patches = make(map[reflect.Value]patch)
20+
values = make(map[reflect.Value]*reflect.Value)
2021
)
2122

2223
type value struct {
@@ -85,6 +86,7 @@ func patchValue(target, replacement reflect.Value) {
8586

8687
bytes := replaceFunction(*(*uintptr)(getPtr(target)), uintptr(getPtr(replacement)))
8788
patches[target] = patch{bytes}
89+
values[target] = &replacement // To prevent GC from causing a crash
8890
}
8991

9092
// Unpatch removes any monkey patches on target

0 commit comments

Comments
 (0)