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

     1  //go:build windows
     2  // +build windows
     3  
     4  package mprotect
     5  
     6  import (
     7  	"syscall"
     8  	"unsafe"
     9  )
    10  
    11  var (
    12  	modkernel32        = syscall.NewLazyDLL("kernel32.dll")
    13  	procVirtualProtect = modkernel32.NewProc("VirtualProtect")
    14  )
    15  
    16  const (
    17  	errnoERROR_IO_PENDING = 997
    18  )
    19  
    20  var (
    21  	errERROR_IO_PENDING error = syscall.Errno(errnoERROR_IO_PENDING)
    22  	errERROR_EINVAL     error = syscall.EINVAL
    23  )
    24  
    25  // errnoErr returns common boxed Errno values, to prevent
    26  // allocations at runtime.
    27  func errnoErr(e syscall.Errno) error {
    28  	switch e {
    29  	case 0:
    30  		return errERROR_EINVAL
    31  	case errnoERROR_IO_PENDING:
    32  		return errERROR_IO_PENDING
    33  	}
    34  	return e
    35  }
    36  
    37  func VirtualProtect(addr uintptr, length uintptr, flNewProtect uintptr) (err error) {
    38  	var out int
    39  	r1, _, e1 := syscall.SyscallN(procVirtualProtect.Addr(), addr, length, flNewProtect, uintptr(unsafe.Pointer(&out)))
    40  	if r1 == 0 {
    41  		err = errnoErr(e1)
    42  	}
    43  	return
    44  }