Skip to content

Commit d18155c

Browse files
Awnalexbrainman
authored andcommitted
windows: add memory-related VirtualX calls and associated constants
Fixes golang/go#20086 Change-Id: Ifdc2561fe6cc125fa45a57bad9750f3f3f055e66 Reviewed-on: https://go-review.googlesource.com/47335 TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
1 parent 4ed4d40 commit d18155c

4 files changed

Lines changed: 65 additions & 2 deletions

File tree

windows/memory_windows.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package windows
6+
7+
const (
8+
MEM_COMMIT = 0x00001000
9+
MEM_RESERVE = 0x00002000
10+
MEM_DECOMMIT = 0x00004000
11+
MEM_RELEASE = 0x00008000
12+
MEM_RESET = 0x00080000
13+
MEM_TOP_DOWN = 0x00100000
14+
MEM_WRITE_WATCH = 0x00200000
15+
MEM_PHYSICAL = 0x00400000
16+
MEM_RESET_UNDO = 0x01000000
17+
MEM_LARGE_PAGES = 0x20000000
18+
19+
PAGE_NOACCESS = 0x01
20+
PAGE_READONLY = 0x02
21+
PAGE_READWRITE = 0x04
22+
)

windows/syscall_windows.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ func NewCallbackCDecl(fn interface{}) uintptr
154154
//sys FlushViewOfFile(addr uintptr, length uintptr) (err error)
155155
//sys VirtualLock(addr uintptr, length uintptr) (err error)
156156
//sys VirtualUnlock(addr uintptr, length uintptr) (err error)
157+
//sys VirtualAlloc(address uintptr, size uintptr, alloctype uint32, protect uint32) (value uintptr, err error) = kernel32.VirtualAlloc
158+
//sys VirtualFree(address uintptr, size uintptr, freetype uint32) (err error) = kernel32.VirtualFree
159+
//sys VirtualProtect(address uintptr, size uintptr, newprotect uint32, oldprotect *uint32) (err error) = kernel32.VirtualProtect
157160
//sys TransmitFile(s Handle, handle Handle, bytesToWrite uint32, bytsPerSend uint32, overlapped *Overlapped, transmitFileBuf *TransmitFileBuffers, flags uint32) (err error) = mswsock.TransmitFile
158161
//sys ReadDirectoryChanges(handle Handle, buf *byte, buflen uint32, watchSubTree bool, mask uint32, retlen *uint32, overlapped *Overlapped, completionRoutine uintptr) (err error) = kernel32.ReadDirectoryChangesW
159162
//sys CertOpenSystemStore(hprov Handle, name *uint16) (store Handle, err error) = crypt32.CertOpenSystemStoreW

windows/zsyscall_windows.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ var (
139139
procFlushViewOfFile = modkernel32.NewProc("FlushViewOfFile")
140140
procVirtualLock = modkernel32.NewProc("VirtualLock")
141141
procVirtualUnlock = modkernel32.NewProc("VirtualUnlock")
142+
procVirtualAlloc = modkernel32.NewProc("VirtualAlloc")
143+
procVirtualFree = modkernel32.NewProc("VirtualFree")
144+
procVirtualProtect = modkernel32.NewProc("VirtualProtect")
142145
procTransmitFile = modmswsock.NewProc("TransmitFile")
143146
procReadDirectoryChangesW = modkernel32.NewProc("ReadDirectoryChangesW")
144147
procCertOpenSystemStoreW = modcrypt32.NewProc("CertOpenSystemStoreW")
@@ -1384,6 +1387,43 @@ func VirtualUnlock(addr uintptr, length uintptr) (err error) {
13841387
return
13851388
}
13861389

1390+
func VirtualAlloc(address uintptr, size uintptr, alloctype uint32, protect uint32) (value uintptr, err error) {
1391+
r0, _, e1 := syscall.Syscall6(procVirtualAlloc.Addr(), 4, uintptr(address), uintptr(size), uintptr(alloctype), uintptr(protect), 0, 0)
1392+
value = uintptr(r0)
1393+
if value == 0 {
1394+
if e1 != 0 {
1395+
err = errnoErr(e1)
1396+
} else {
1397+
err = syscall.EINVAL
1398+
}
1399+
}
1400+
return
1401+
}
1402+
1403+
func VirtualFree(address uintptr, size uintptr, freetype uint32) (err error) {
1404+
r1, _, e1 := syscall.Syscall(procVirtualFree.Addr(), 3, uintptr(address), uintptr(size), uintptr(freetype))
1405+
if r1 == 0 {
1406+
if e1 != 0 {
1407+
err = errnoErr(e1)
1408+
} else {
1409+
err = syscall.EINVAL
1410+
}
1411+
}
1412+
return
1413+
}
1414+
1415+
func VirtualProtect(address uintptr, size uintptr, newprotect uint32, oldprotect *uint32) (err error) {
1416+
r1, _, e1 := syscall.Syscall6(procVirtualProtect.Addr(), 4, uintptr(address), uintptr(size), uintptr(newprotect), uintptr(unsafe.Pointer(oldprotect)), 0, 0)
1417+
if r1 == 0 {
1418+
if e1 != 0 {
1419+
err = errnoErr(e1)
1420+
} else {
1421+
err = syscall.EINVAL
1422+
}
1423+
}
1424+
return
1425+
}
1426+
13871427
func TransmitFile(s Handle, handle Handle, bytesToWrite uint32, bytsPerSend uint32, overlapped *Overlapped, transmitFileBuf *TransmitFileBuffers, flags uint32) (err error) {
13881428
r1, _, e1 := syscall.Syscall9(procTransmitFile.Addr(), 7, uintptr(s), uintptr(handle), uintptr(bytesToWrite), uintptr(bytsPerSend), uintptr(unsafe.Pointer(overlapped)), uintptr(unsafe.Pointer(transmitFileBuf)), uintptr(flags), 0, 0)
13891429
if r1 == 0 {

windows/ztypes_windows.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,6 @@ const (
165165
PROCESS_QUERY_INFORMATION = 0x00000400
166166
SYNCHRONIZE = 0x00100000
167167

168-
PAGE_READONLY = 0x02
169-
PAGE_READWRITE = 0x04
170168
PAGE_WRITECOPY = 0x08
171169
PAGE_EXECUTE_READ = 0x20
172170
PAGE_EXECUTE_READWRITE = 0x40

0 commit comments

Comments
 (0)