replace.go 627 B

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