github.com/gbl08ma/monkey@v1.1.0/replace.go (about) 1 package monkey 2 3 import ( 4 "reflect" 5 "syscall" 6 "unsafe" 7 ) 8 9 func rawMemoryAccess(p uintptr, length int) []byte { 10 return *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{ 11 Data: p, 12 Len: length, 13 Cap: length, 14 })) 15 } 16 17 func pageStart(ptr uintptr) uintptr { 18 return ptr & ^(uintptr(syscall.Getpagesize() - 1)) 19 } 20 21 // from is a pointer to the actual function 22 // to is a pointer to a go funcvalue 23 func replaceFunction(from, to uintptr) (original []byte) { 24 jumpData := jmpToFunctionValue(to) 25 f := rawMemoryAccess(from, len(jumpData)) 26 original = make([]byte, len(f)) 27 copy(original, f) 28 29 copyToLocation(from, jumpData) 30 return 31 }