github.com/tencent/goom@v1.0.1/internal/bytecode/memory/mwrite_amd64.go (about) 1 //go:build !windows 2 // +build !windows 3 4 package memory 5 6 import ( 7 "fmt" 8 "syscall" 9 10 "github.com/tencent/goom/internal/logger" 11 ) 12 13 // accessMemGuide access mem error solution guide 14 const accessMemGuide = "https://github.com/tencent/goom" 15 16 // WriteTo this function is super unsafe 17 // aww yeah 18 // It copies a slice to a raw memory location, disabling all memory protection before doing so. 19 func WriteTo(addr uintptr, data []byte) error { 20 memoryAccessLock.Lock() 21 defer memoryAccessLock.Unlock() 22 23 f := RawAccess(addr, len(data)) 24 if err := mProtectCrossPage(addr, len(data), syscall.PROT_READ|syscall.PROT_WRITE|syscall.PROT_EXEC); err != nil { 25 // mac 环境下使用 hack 方式绕过权限检查 26 if e := writeTo(addr, data); e == nil { 27 return nil 28 } 29 errorDetail(err) 30 } 31 copy(f, data[:]) 32 if err := mProtectCrossPage(addr, len(data), syscall.PROT_READ|syscall.PROT_EXEC); err != nil { 33 errorDetail(err) 34 } 35 return nil 36 } 37 38 func errorDetail(err error) { 39 logger.Consolef(logger.ErrorLevel, "access mem error:permission denied, see details at %s", accessMemGuide) 40 panic(fmt.Errorf("access mem error: %w", err)) 41 }