github.com/eh-steve/goloader@v0.0.0-20240111193454-90ff3cfdae39/mmap/zsyscall_windows.go (about)

     1  //go:build windows
     2  // +build windows
     3  
     4  package mmap
     5  
     6  import (
     7  	"syscall"
     8  	"unsafe"
     9  )
    10  
    11  var (
    12  	modkernel32         = syscall.NewLazyDLL("kernel32.dll")
    13  	procMapViewOfFileEx = modkernel32.NewProc("MapViewOfFileEx")
    14  	procVirtualQueryEx  = modkernel32.NewProc("VirtualQueryEx")
    15  	procGetSystemInfo   = modkernel32.NewProc("GetSystemInfo")
    16  )
    17  
    18  const (
    19  	errnoERROR_IO_PENDING = 997
    20  )
    21  
    22  var (
    23  	errERROR_IO_PENDING error = syscall.Errno(errnoERROR_IO_PENDING)
    24  	errERROR_EINVAL     error = syscall.EINVAL
    25  )
    26  
    27  // errnoErr returns common boxed Errno values, to prevent
    28  // allocations at runtime.
    29  func errnoErr(e syscall.Errno) error {
    30  	switch e {
    31  	case 0:
    32  		return errERROR_EINVAL
    33  	case errnoERROR_IO_PENDING:
    34  		return errERROR_IO_PENDING
    35  	}
    36  	return e
    37  }
    38  
    39  func MapViewOfFileEx(handle syscall.Handle, access uint32, offsetHigh uint32, offsetLow uint32, length uintptr, baseAddr uintptr) (addr uintptr, err error) {
    40  	r0, _, e1 := syscall.SyscallN(procMapViewOfFileEx.Addr(), uintptr(handle), uintptr(access), uintptr(offsetHigh), uintptr(offsetLow), length, baseAddr)
    41  	addr = r0
    42  	if addr == 0 {
    43  		err = errnoErr(e1)
    44  	}
    45  	return
    46  }
    47  
    48  type MemoryBasicInformation struct {
    49  	BaseAddress       uintptr
    50  	AllocationBase    uintptr
    51  	AllocationProtect uint32
    52  	PartitionId       uint16
    53  	RegionSize        uintptr
    54  	State             uint32
    55  	Protect           uint32
    56  	Type              uint32
    57  }
    58  
    59  func VirtualQueryEx(processHandle syscall.Handle, addr uintptr, m *MemoryBasicInformation) (infoSize uintptr, err error) {
    60  	r0, _, e1 := syscall.SyscallN(procVirtualQueryEx.Addr(), uintptr(processHandle), addr, uintptr(unsafe.Pointer(m)), unsafe.Sizeof(*m))
    61  	if r0 < 0 {
    62  		err = errnoErr(e1)
    63  	}
    64  	return r0, err
    65  }
    66  
    67  type SystemInfo struct {
    68  	OemId                     uint32
    69  	PageSize                  uint32
    70  	MinimumApplicationAddress uintptr
    71  	MaximumApplicationAddress uintptr
    72  	ActiveProcessorMask       uintptr
    73  	NumberOfProcessors        uint32
    74  	ProcessorType             uint32
    75  	AllocationGranularity     uint32
    76  	ProcessorLevel            uint16
    77  	ProcessorRevision         uint16
    78  }
    79  
    80  func GetSystemInfo(info *SystemInfo) {
    81  	syscall.SyscallN(procGetSystemInfo.Addr(), uintptr(unsafe.Pointer(info)))
    82  }
    83  
    84  func GetAllocationGranularity() uint32 {
    85  	info := SystemInfo{}
    86  	GetSystemInfo(&info)
    87  	return info.AllocationGranularity
    88  }