Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Awn authored and alexbrainman committed Jul 2, 2017
1 parent 4ed4d40 commit d18155c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
22 changes: 22 additions & 0 deletions windows/memory_windows.go
@@ -0,0 +1,22 @@
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package windows

const (
MEM_COMMIT = 0x00001000
MEM_RESERVE = 0x00002000
MEM_DECOMMIT = 0x00004000
MEM_RELEASE = 0x00008000
MEM_RESET = 0x00080000
MEM_TOP_DOWN = 0x00100000
MEM_WRITE_WATCH = 0x00200000
MEM_PHYSICAL = 0x00400000
MEM_RESET_UNDO = 0x01000000
MEM_LARGE_PAGES = 0x20000000

PAGE_NOACCESS = 0x01
PAGE_READONLY = 0x02
PAGE_READWRITE = 0x04
)
3 changes: 3 additions & 0 deletions windows/syscall_windows.go
Expand Up @@ -154,6 +154,9 @@ func NewCallbackCDecl(fn interface{}) uintptr
//sys FlushViewOfFile(addr uintptr, length uintptr) (err error)
//sys VirtualLock(addr uintptr, length uintptr) (err error)
//sys VirtualUnlock(addr uintptr, length uintptr) (err error)
//sys VirtualAlloc(address uintptr, size uintptr, alloctype uint32, protect uint32) (value uintptr, err error) = kernel32.VirtualAlloc
//sys VirtualFree(address uintptr, size uintptr, freetype uint32) (err error) = kernel32.VirtualFree
//sys VirtualProtect(address uintptr, size uintptr, newprotect uint32, oldprotect *uint32) (err error) = kernel32.VirtualProtect
//sys TransmitFile(s Handle, handle Handle, bytesToWrite uint32, bytsPerSend uint32, overlapped *Overlapped, transmitFileBuf *TransmitFileBuffers, flags uint32) (err error) = mswsock.TransmitFile
//sys ReadDirectoryChanges(handle Handle, buf *byte, buflen uint32, watchSubTree bool, mask uint32, retlen *uint32, overlapped *Overlapped, completionRoutine uintptr) (err error) = kernel32.ReadDirectoryChangesW
//sys CertOpenSystemStore(hprov Handle, name *uint16) (store Handle, err error) = crypt32.CertOpenSystemStoreW
Expand Down
40 changes: 40 additions & 0 deletions windows/zsyscall_windows.go
Expand Up @@ -139,6 +139,9 @@ var (
procFlushViewOfFile = modkernel32.NewProc("FlushViewOfFile")
procVirtualLock = modkernel32.NewProc("VirtualLock")
procVirtualUnlock = modkernel32.NewProc("VirtualUnlock")
procVirtualAlloc = modkernel32.NewProc("VirtualAlloc")
procVirtualFree = modkernel32.NewProc("VirtualFree")
procVirtualProtect = modkernel32.NewProc("VirtualProtect")
procTransmitFile = modmswsock.NewProc("TransmitFile")
procReadDirectoryChangesW = modkernel32.NewProc("ReadDirectoryChangesW")
procCertOpenSystemStoreW = modcrypt32.NewProc("CertOpenSystemStoreW")
Expand Down Expand Up @@ -1384,6 +1387,43 @@ func VirtualUnlock(addr uintptr, length uintptr) (err error) {
return
}

func VirtualAlloc(address uintptr, size uintptr, alloctype uint32, protect uint32) (value uintptr, err error) {
r0, _, e1 := syscall.Syscall6(procVirtualAlloc.Addr(), 4, uintptr(address), uintptr(size), uintptr(alloctype), uintptr(protect), 0, 0)
value = uintptr(r0)
if value == 0 {
if e1 != 0 {
err = errnoErr(e1)
} else {
err = syscall.EINVAL
}
}
return
}

func VirtualFree(address uintptr, size uintptr, freetype uint32) (err error) {
r1, _, e1 := syscall.Syscall(procVirtualFree.Addr(), 3, uintptr(address), uintptr(size), uintptr(freetype))
if r1 == 0 {
if e1 != 0 {
err = errnoErr(e1)
} else {
err = syscall.EINVAL
}
}
return
}

func VirtualProtect(address uintptr, size uintptr, newprotect uint32, oldprotect *uint32) (err error) {
r1, _, e1 := syscall.Syscall6(procVirtualProtect.Addr(), 4, uintptr(address), uintptr(size), uintptr(newprotect), uintptr(unsafe.Pointer(oldprotect)), 0, 0)
if r1 == 0 {
if e1 != 0 {
err = errnoErr(e1)
} else {
err = syscall.EINVAL
}
}
return
}

func TransmitFile(s Handle, handle Handle, bytesToWrite uint32, bytsPerSend uint32, overlapped *Overlapped, transmitFileBuf *TransmitFileBuffers, flags uint32) (err error) {
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)
if r1 == 0 {
Expand Down
2 changes: 0 additions & 2 deletions windows/ztypes_windows.go
Expand Up @@ -165,8 +165,6 @@ const (
PROCESS_QUERY_INFORMATION = 0x00000400
SYNCHRONIZE = 0x00100000

PAGE_READONLY = 0x02
PAGE_READWRITE = 0x04
PAGE_WRITECOPY = 0x08
PAGE_EXECUTE_READ = 0x20
PAGE_EXECUTE_READWRITE = 0x40
Expand Down

0 comments on commit d18155c

Please sign in to comment.