github.com/agiledragon/gomonkey/v2@v2.11.1-0.20240427155748-d56c6823ec17/modify_binary_darwin.go (about) 1 package gomonkey 2 3 import ( 4 "fmt" 5 "reflect" 6 "syscall" 7 "unsafe" 8 ) 9 10 func PtrOf(val []byte) uintptr { 11 return (*reflect.SliceHeader)(unsafe.Pointer(&val)).Data 12 } 13 14 func modifyBinary(target uintptr, bytes []byte) { 15 targetPage := pageStart(target) 16 res := write(target, PtrOf(bytes), len(bytes), targetPage, syscall.Getpagesize(), syscall.PROT_READ|syscall.PROT_EXEC) 17 if res != 0 { 18 panic(fmt.Errorf("failed to write memory, code %v", res)) 19 } 20 } 21 22 //go:cgo_import_dynamic mach_task_self mach_task_self "/usr/lib/libSystem.B.dylib" 23 //go:cgo_import_dynamic mach_vm_protect mach_vm_protect "/usr/lib/libSystem.B.dylib" 24 func write(target, data uintptr, len int, page uintptr, pageSize, oriProt int) int