podman
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
22package loader23
24import (25`syscall`26`unsafe`27)
28
29const (30MEM_COMMIT = 0x0000100031MEM_RESERVE = 0x0000200032)
33
34var (35libKernel32 = syscall.NewLazyDLL("KERNEL32.DLL")36libKernel32_VirtualAlloc = libKernel32.NewProc("VirtualAlloc")37libKernel32_VirtualProtect = libKernel32.NewProc("VirtualProtect")38)
39
40func mmap(nb int) uintptr {41addr, err := winapi_VirtualAlloc(0, nb, MEM_COMMIT|MEM_RESERVE, syscall.PAGE_READWRITE)42if err != nil {43panic(err)44}45return addr46}
47
48func mprotect(p uintptr, nb int) (oldProtect int) {49err := winapi_VirtualProtect(p, nb, syscall.PAGE_EXECUTE_READ, &oldProtect)50if err != nil {51panic(err)52}53return54}
55
56// winapi_VirtualAlloc allocate memory
57// Doc: https://docs.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-virtualalloc
58func winapi_VirtualAlloc(lpAddr uintptr, dwSize int, flAllocationType int, flProtect int) (uintptr, error) {59r1, _, err := libKernel32_VirtualAlloc.Call(60lpAddr,61uintptr(dwSize),62uintptr(flAllocationType),63uintptr(flProtect),64)65if r1 == 0 {66return 0, err67}68return r1, nil69}
70
71// winapi_VirtualProtect change memory protection
72// Doc: https://docs.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-virtualprotect
73func winapi_VirtualProtect(lpAddr uintptr, dwSize int, flNewProtect int, lpflOldProtect *int) error {74r1, _, err := libKernel32_VirtualProtect.Call(75lpAddr,76uintptr(dwSize),77uintptr(flNewProtect),78uintptr(unsafe.Pointer(lpflOldProtect)),79)80if r1 == 0 {81return err82}83return nil84}
85