github.com/pkujhd/goloader@v0.0.0-20240411034752-1a28096bd7bd/mmap/mmap_darwin_arm64.go (about) 1 //go:build darwin && arm64 2 // +build darwin,arm64 3 4 package mmap 5 6 /* 7 #cgo darwin LDFLAGS: -lpthread 8 9 #include <pthread.h> 10 #include <libkern/OSCacheControl.h> 11 12 void jit_write_protect(int enable) { 13 pthread_jit_write_protect_np(enable); 14 } 15 void cache_invalidate(void* start, size_t len) { 16 sys_icache_invalidate(start, len); 17 } 18 */ 19 import "C" 20 21 import ( 22 "os" 23 "syscall" 24 "unsafe" 25 ) 26 27 func MakeThreadJITCodeExecutable(ptr uintptr, len int) { 28 C.jit_write_protect(C.int(1)) 29 C.cache_invalidate(unsafe.Pointer(ptr), C.size_t(len)) 30 } 31 32 func Mmap(size int) ([]byte, error) { 33 data, err := syscall.Mmap( 34 0, 35 0, 36 size, 37 syscall.PROT_READ|syscall.PROT_WRITE|syscall.PROT_EXEC, 38 syscall.MAP_PRIVATE|syscall.MAP_ANON|syscall.MAP_JIT) 39 if err != nil { 40 err = os.NewSyscallError("syscall.Mmap", err) 41 } 42 C.jit_write_protect(C.int(0)) 43 return data, err 44 } 45 46 func MmapData(size int) ([]byte, error) { 47 data, err := syscall.Mmap( 48 0, 49 0, 50 size, 51 syscall.PROT_READ|syscall.PROT_WRITE, 52 syscall.MAP_PRIVATE|syscall.MAP_ANON) 53 if err != nil { 54 err = os.NewSyscallError("syscall.Mmap", err) 55 } 56 return data, err 57 } 58 59 func Munmap(b []byte) (err error) { 60 err = syscall.Munmap(b) 61 if err != nil { 62 err = os.NewSyscallError("syscall.Munmap", err) 63 } 64 return 65 }