go-common/vendor/github.com/bouk/monkey/replace.go
2019-04-22 18:49:16 +08:00

32 lines
627 B
Go

package monkey
import (
"reflect"
"syscall"
"unsafe"
)
func rawMemoryAccess(p uintptr, length int) []byte {
return *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
Data: p,
Len: length,
Cap: length,
}))
}
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))
original = make([]byte, len(f))
copy(original, f)
copyToLocation(from, jumpData)
return
}