github.com/tencent/goom@v1.0.1/internal/bytecode/memory/memory.go (about)

     1  // Package memory 包负责内存读写控制
     2  package memory
     3  
     4  import (
     5  	"reflect"
     6  	"sync"
     7  	"syscall"
     8  	"unsafe"
     9  )
    10  
    11  // memoryAccessLock .text 区内存操作度协作
    12  var memoryAccessLock sync.RWMutex
    13  
    14  // PageStart page start of memory
    15  func PageStart(addr uintptr) uintptr {
    16  	return addr & ^(uintptr(syscall.Getpagesize() - 1))
    17  }
    18  
    19  // RawAccess 内存数据读取(非线程安全的)
    20  // nolint
    21  func RawAccess(addr uintptr, length int) []byte {
    22  	return *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
    23  		Data: addr,
    24  		Len:  length,
    25  		Cap:  length,
    26  	}))
    27  }
    28  
    29  // RawRead 内存数据读取(线程安全的)
    30  func RawRead(addr uintptr, length int) []byte {
    31  	memoryAccessLock.RLock()
    32  	defer memoryAccessLock.RUnlock()
    33  
    34  	data := RawAccess(addr, length)
    35  	duplicate := make([]byte, length)
    36  	copy(duplicate, data)
    37  	return duplicate
    38  }