gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

mmap_windows.go (2318B)


      1 //go:build windows
      2 // +build windows
      3 
      4 // build
      5 
      6 /*
      7  * Copyright 2021 ByteDance Inc.
      8  *
      9  * Licensed under the Apache License, Version 2.0 (the "License");
     10  * you may not use this file except in compliance with the License.
     11  * You may obtain a copy of the License at
     12  *
     13  *     http://www.apache.org/licenses/LICENSE-2.0
     14  *
     15  * Unless required by applicable law or agreed to in writing, software
     16  * distributed under the License is distributed on an "AS IS" BASIS,
     17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     18  * See the License for the specific language governing permissions and
     19  * limitations under the License.
     20  */
     21 
     22 package loader
     23 
     24 import (
     25     `syscall`
     26     `unsafe`
     27 )
     28 
     29 const (
     30     MEM_COMMIT  = 0x00001000
     31     MEM_RESERVE = 0x00002000
     32 )
     33 
     34 var (
     35     libKernel32                = syscall.NewLazyDLL("KERNEL32.DLL")
     36     libKernel32_VirtualAlloc   = libKernel32.NewProc("VirtualAlloc")
     37     libKernel32_VirtualProtect = libKernel32.NewProc("VirtualProtect")
     38 )
     39 
     40 func mmap(nb int) uintptr {
     41     addr, err := winapi_VirtualAlloc(0, nb, MEM_COMMIT|MEM_RESERVE, syscall.PAGE_READWRITE)
     42     if err != nil {
     43         panic(err)
     44     }
     45     return addr
     46 }
     47 
     48 func mprotect(p uintptr, nb int) (oldProtect int) {
     49     err := winapi_VirtualProtect(p, nb, syscall.PAGE_EXECUTE_READ, &oldProtect)
     50     if err != nil {
     51         panic(err)
     52     }
     53     return
     54 }
     55 
     56 // winapi_VirtualAlloc allocate memory
     57 // Doc: https://docs.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-virtualalloc
     58 func winapi_VirtualAlloc(lpAddr uintptr, dwSize int, flAllocationType int, flProtect int) (uintptr, error) {
     59     r1, _, err := libKernel32_VirtualAlloc.Call(
     60         lpAddr,
     61         uintptr(dwSize),
     62         uintptr(flAllocationType),
     63         uintptr(flProtect),
     64     )
     65     if r1 == 0 {
     66         return 0, err
     67     }
     68     return r1, nil
     69 }
     70 
     71 // winapi_VirtualProtect change memory protection
     72 // Doc: https://docs.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-virtualprotect
     73 func winapi_VirtualProtect(lpAddr uintptr, dwSize int, flNewProtect int, lpflOldProtect *int) error {
     74     r1, _, err := libKernel32_VirtualProtect.Call(
     75         lpAddr,
     76         uintptr(dwSize),
     77         uintptr(flNewProtect),
     78         uintptr(unsafe.Pointer(lpflOldProtect)),
     79     )
     80     if r1 == 0 {
     81         return err
     82     }
     83     return nil
     84 }