github.com/wasilibs/wazerox@v0.0.0-20240124024944-4923be63ab5f/internal/platform/mmap_windows.go (about)

     1  package platform
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"syscall"
     7  	"unsafe"
     8  )
     9  
    10  var (
    11  	kernel32           = syscall.NewLazyDLL("kernel32.dll")
    12  	procVirtualAlloc   = kernel32.NewProc("VirtualAlloc")
    13  	procVirtualProtect = kernel32.NewProc("VirtualProtect")
    14  	procVirtualFree    = kernel32.NewProc("VirtualFree")
    15  )
    16  
    17  const (
    18  	windows_MEM_COMMIT             uintptr = 0x00001000
    19  	windows_MEM_RELEASE            uintptr = 0x00008000
    20  	windows_PAGE_READWRITE         uintptr = 0x00000004
    21  	windows_PAGE_EXECUTE_READ      uintptr = 0x00000020
    22  	windows_PAGE_EXECUTE_READWRITE uintptr = 0x00000040
    23  )
    24  
    25  func munmapCodeSegment(code []byte) error {
    26  	return freeMemory(code)
    27  }
    28  
    29  // allocateMemory commits the memory region via the "VirtualAlloc" function.
    30  // See https://docs.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-virtualalloc
    31  func allocateMemory(size uintptr, protect uintptr) (uintptr, error) {
    32  	address := uintptr(0) // system determines where to allocate the region.
    33  	alloctype := windows_MEM_COMMIT
    34  	if r, _, err := procVirtualAlloc.Call(address, size, alloctype, protect); r == 0 {
    35  		return 0, fmt.Errorf("compiler: VirtualAlloc error: %w", ensureErr(err))
    36  	} else {
    37  		return r, nil
    38  	}
    39  }
    40  
    41  // freeMemory releases the memory region via the "VirtualFree" function.
    42  // See https://docs.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-virtualfree
    43  func freeMemory(code []byte) error {
    44  	address := unsafe.Pointer(&code[0])
    45  	size := uintptr(0) // size must be 0 because we're using MEM_RELEASE.
    46  	freetype := windows_MEM_RELEASE
    47  	if r, _, err := procVirtualFree.Call(uintptr(address), size, freetype); r == 0 {
    48  		return fmt.Errorf("compiler: VirtualFree error: %w", ensureErr(err))
    49  	}
    50  	return nil
    51  }
    52  
    53  func virtualProtect(address, size, newprotect uintptr, oldprotect *uint32) error {
    54  	if r, _, err := procVirtualProtect.Call(address, size, newprotect, uintptr(unsafe.Pointer(oldprotect))); r == 0 {
    55  		return fmt.Errorf("compiler: VirtualProtect error: %w", ensureErr(err))
    56  	}
    57  	return nil
    58  }
    59  
    60  func mmapMemory(size int) ([]byte, error) {
    61  	p, err := allocateMemory(uintptr(size), windows_PAGE_READWRITE)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  
    66  	var mem []byte
    67  	sh := (*reflect.SliceHeader)(unsafe.Pointer(&mem))
    68  	sh.Data = p
    69  	sh.Len = size
    70  	sh.Cap = size
    71  	return mem, err
    72  }
    73  
    74  func mmapCodeSegmentAMD64(size int) ([]byte, error) {
    75  	p, err := allocateMemory(uintptr(size), windows_PAGE_EXECUTE_READWRITE)
    76  	if err != nil {
    77  		return nil, err
    78  	}
    79  
    80  	var mem []byte
    81  	sh := (*reflect.SliceHeader)(unsafe.Pointer(&mem))
    82  	sh.Data = p
    83  	sh.Len = size
    84  	sh.Cap = size
    85  	return mem, err
    86  }
    87  
    88  func mmapCodeSegmentARM64(size int) ([]byte, error) {
    89  	p, err := allocateMemory(uintptr(size), windows_PAGE_READWRITE)
    90  	if err != nil {
    91  		return nil, err
    92  	}
    93  
    94  	var mem []byte
    95  	sh := (*reflect.SliceHeader)(unsafe.Pointer(&mem))
    96  	sh.Data = p
    97  	sh.Len = size
    98  	sh.Cap = size
    99  	return mem, nil
   100  }
   101  
   102  var old = uint32(windows_PAGE_READWRITE)
   103  
   104  func MprotectRX(b []byte) (err error) {
   105  	err = virtualProtect(uintptr(unsafe.Pointer(&b[0])), uintptr(len(b)), windows_PAGE_EXECUTE_READ, &old)
   106  	return
   107  }
   108  
   109  // ensureErr returns syscall.EINVAL when the input error is nil.
   110  //
   111  // We are supposed to use "GetLastError" which is more precise, but it is not safe to execute in goroutines. While
   112  // "GetLastError" is thread-local, goroutines are not pinned to threads.
   113  //
   114  // See https://docs.microsoft.com/en-us/windows/win32/api/errhandlingapi/nf-errhandlingapi-getlasterror
   115  func ensureErr(err error) error {
   116  	if err != nil {
   117  		return err
   118  	}
   119  	return syscall.EINVAL
   120  }