[sys] windows: add EnumProcessModules, EnumProcessModulesEx, GetModuleInfor…

251 views
Skip to first unread message

Gerrit Bot (Gerrit)

unread,
Sep 19, 2021, 3:20:06 AM9/19/21
to goph...@pubsubhelper.golang.org, Weilu Jia, golang-co...@googlegroups.com

Gerrit Bot has uploaded this change for review.

View Change

windows: add EnumProcessModules, EnumProcessModulesEx, GetModuleInfor…

…mation, GetModuleFileNameEx and GetModuleBaseName

Add process module related syscalls.

https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-enumprocessmodules
https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-enumprocessmodulesex
https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-getmoduleinformation
https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-getmodulefilenameexw
https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-getmodulebasenamew

Change-Id: I75b062daec7a2cc9685e803dfa851825acee32b6
GitHub-Last-Rev: f6f5f146cd76bf0b93036af99f63dc6ef0a3505a
GitHub-Pull-Request: golang/sys#116
---
M windows/syscall_windows.go
M windows/types_windows.go
M windows/zsyscall_windows.go
3 files changed, 64 insertions(+), 0 deletions(-)

diff --git a/windows/syscall_windows.go b/windows/syscall_windows.go
index 1215b2a..ff2d45d 100644
--- a/windows/syscall_windows.go
+++ b/windows/syscall_windows.go
@@ -398,6 +398,11 @@

// Process Status API (PSAPI)
//sys EnumProcesses(processIds []uint32, bytesReturned *uint32) (err error) = psapi.EnumProcesses
+//sys EnumProcessModules(process Handle, module *Handle, cb uint32, cbNeeded *uint32) (err error) = psapi.EnumProcessModules
+//sys EnumProcessModulesEx(process Handle, module *Handle, cb uint32, cbNeeded *uint32, filterFlag uint32) (err error) = psapi.EnumProcessModulesEx
+//sys GetModuleInformation(process Handle, module Handle, modinfo *ModuleInfo, cb uint32) (err error) = psapi.GetModuleInformation
+//sys GetModuleFileNameEx(process Handle, module Handle, filename *uint16, size uint32) (err error) = psapi.GetModuleFileNameExW
+//sys GetModuleBaseName(process Handle, module Handle, baseName *uint16, size uint32) (err error) = psapi.GetModuleBaseNameW

// NT Native APIs
//sys rtlNtStatusToDosErrorNoTeb(ntstatus NTStatus) (ret syscall.Errno) = ntdll.RtlNtStatusToDosErrorNoTeb
diff --git a/windows/types_windows.go b/windows/types_windows.go
index 17f0331..88e0ce5 100644
--- a/windows/types_windows.go
+++ b/windows/types_windows.go
@@ -243,6 +243,14 @@
)

const (
+ // flags for EnumProcessModulesEx
+ LIST_MODULES_32BIT = 0x01
+ LIST_MODULES_64BIT = 0x02
+ LIST_MODULES_ALL = 0x03
+ LIST_MODULES_DEFAULT = 0x00
+)
+
+const (
// filters for ReadDirectoryChangesW and FindFirstChangeNotificationW
FILE_NOTIFY_CHANGE_FILE_NAME = 0x001
FILE_NOTIFY_CHANGE_DIR_NAME = 0x002
@@ -2773,3 +2781,9 @@

// Flag for QueryFullProcessImageName.
const PROCESS_NAME_NATIVE = 1
+
+type ModuleInfo struct {
+ BaseOfDll uintptr
+ SizeOfImage uint32
+ EntryPoint uintptr
+}
diff --git a/windows/zsyscall_windows.go b/windows/zsyscall_windows.go
index 2083ec3..e282e24 100644
--- a/windows/zsyscall_windows.go
+++ b/windows/zsyscall_windows.go
@@ -377,7 +377,12 @@
procCoTaskMemFree = modole32.NewProc("CoTaskMemFree")
procCoUninitialize = modole32.NewProc("CoUninitialize")
procStringFromGUID2 = modole32.NewProc("StringFromGUID2")
+ procEnumProcessModules = modpsapi.NewProc("EnumProcessModules")
+ procEnumProcessModulesEx = modpsapi.NewProc("EnumProcessModulesEx")
procEnumProcesses = modpsapi.NewProc("EnumProcesses")
+ procGetModuleBaseNameW = modpsapi.NewProc("GetModuleBaseNameW")
+ procGetModuleFileNameExW = modpsapi.NewProc("GetModuleFileNameExW")
+ procGetModuleInformation = modpsapi.NewProc("GetModuleInformation")
procSubscribeServiceChangeNotifications = modsechost.NewProc("SubscribeServiceChangeNotifications")
procUnsubscribeServiceChangeNotifications = modsechost.NewProc("UnsubscribeServiceChangeNotifications")
procGetUserNameExW = modsecur32.NewProc("GetUserNameExW")
@@ -3225,6 +3230,22 @@
return
}

+func EnumProcessModules(process Handle, module *Handle, cb uint32, cbNeeded *uint32) (err error) {
+ r1, _, e1 := syscall.Syscall6(procEnumProcessModules.Addr(), 4, uintptr(process), uintptr(unsafe.Pointer(module)), uintptr(cb), uintptr(unsafe.Pointer(cbNeeded)), 0, 0)
+ if r1 == 0 {
+ err = errnoErr(e1)
+ }
+ return
+}
+
+func EnumProcessModulesEx(process Handle, module *Handle, cb uint32, cbNeeded *uint32, filterFlag uint32) (err error) {
+ r1, _, e1 := syscall.Syscall6(procEnumProcessModulesEx.Addr(), 5, uintptr(process), uintptr(unsafe.Pointer(module)), uintptr(cb), uintptr(unsafe.Pointer(cbNeeded)), uintptr(filterFlag), 0)
+ if r1 == 0 {
+ err = errnoErr(e1)
+ }
+ return
+}
+
func EnumProcesses(processIds []uint32, bytesReturned *uint32) (err error) {
var _p0 *uint32
if len(processIds) > 0 {
@@ -3237,6 +3258,30 @@
return
}

+func GetModuleBaseName(process Handle, module Handle, baseName *uint16, size uint32) (err error) {
+ r1, _, e1 := syscall.Syscall6(procGetModuleBaseNameW.Addr(), 4, uintptr(process), uintptr(module), uintptr(unsafe.Pointer(baseName)), uintptr(size), 0, 0)
+ if r1 == 0 {
+ err = errnoErr(e1)
+ }
+ return
+}
+
+func GetModuleFileNameEx(process Handle, module Handle, filename *uint16, size uint32) (err error) {
+ r1, _, e1 := syscall.Syscall6(procGetModuleFileNameExW.Addr(), 4, uintptr(process), uintptr(module), uintptr(unsafe.Pointer(filename)), uintptr(size), 0, 0)
+ if r1 == 0 {
+ err = errnoErr(e1)
+ }
+ return
+}
+
+func GetModuleInformation(process Handle, module Handle, modinfo *ModuleInfo, cb uint32) (err error) {
+ r1, _, e1 := syscall.Syscall6(procGetModuleInformation.Addr(), 4, uintptr(process), uintptr(module), uintptr(unsafe.Pointer(modinfo)), uintptr(cb), 0, 0)
+ if r1 == 0 {
+ err = errnoErr(e1)
+ }
+ return
+}
+
func SubscribeServiceChangeNotifications(service Handle, eventType uint32, callback uintptr, callbackCtx uintptr, subscription *uintptr) (ret error) {
ret = procSubscribeServiceChangeNotifications.Find()
if ret != nil {

To view, visit change 350870. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: sys
Gerrit-Branch: master
Gerrit-Change-Id: I75b062daec7a2cc9685e803dfa851825acee32b6
Gerrit-Change-Number: 350870
Gerrit-PatchSet: 1
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-CC: Weilu Jia <opti...@gmail.com>
Gerrit-MessageType: newchange

Alex Brainman (Gerrit)

unread,
Sep 19, 2021, 5:29:27 AM9/19/21
to Gerrit Bot, Weilu Jia, goph...@pubsubhelper.golang.org, Jason A. Donenfeld, Brad Fitzpatrick, Go Bot, golang-co...@googlegroups.com

Attention is currently required from: Brad Fitzpatrick.

Patch set 1:Run-TryBot +1Trust +1

View Change

2 comments:

    • add EnumProcessModules, EnumProcessModulesEx, GetModuleInfor…

      …mation, GetModuleFileNameEx and GetModuleBaseName

    • s/add EnumProcessModules, EnumProcessModulesEx, GetModuleInformation, GetModuleFileNameEx and GetModuleBaseName/add process module related syscalls/

      Otherwise commit message first line is too long.

      Then you could remove lines 11 and 12.

  • Patchset:

    • Patch Set #1:

      Thank you for submitting this change.

      Looks reasonable. But please see 1 comment below.

      Also is it possible to add test that uses these functions, so we can actually verify that this CL is correct?

      Thank you.

      Alex

To view, visit change 350870. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: sys
Gerrit-Branch: master
Gerrit-Change-Id: I75b062daec7a2cc9685e803dfa851825acee32b6
Gerrit-Change-Number: 350870
Gerrit-PatchSet: 1
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-Reviewer: Alex Brainman <alex.b...@gmail.com>
Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
Gerrit-CC: Go Bot <go...@golang.org>
Gerrit-CC: Jason A. Donenfeld <Ja...@zx2c4.com>
Gerrit-CC: Weilu Jia <opti...@gmail.com>
Gerrit-Attention: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Comment-Date: Sun, 19 Sep 2021 09:29:19 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment

Weilu Jia (Gerrit)

unread,
Sep 19, 2021, 1:43:04 PM9/19/21
to Gerrit Bot, goph...@pubsubhelper.golang.org, Alex Brainman, Brad Fitzpatrick, Go Bot, golang-co...@googlegroups.com

Attention is currently required from: Alex Brainman, Brad Fitzpatrick.

View Change

1 comment:

  • Patchset:

    • Patch Set #1:

      Could alternatively do `GetModuleFileNameEx(process Handle, module Handle, filename []uint16)`, but `GetModuleFileName()` uses `(filename *uint16, size uint32)`.

      I actually prefer the slice, but I split them for consistency with `GetModuleFileName()`.

To view, visit change 350870. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: sys
Gerrit-Branch: master
Gerrit-Change-Id: I75b062daec7a2cc9685e803dfa851825acee32b6
Gerrit-Change-Number: 350870
Gerrit-PatchSet: 1
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-Reviewer: Alex Brainman <alex.b...@gmail.com>
Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
Gerrit-CC: Go Bot <go...@golang.org>
Gerrit-CC: Weilu Jia <opti...@gmail.com>
Gerrit-Attention: Alex Brainman <alex.b...@gmail.com>
Gerrit-Attention: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Comment-Date: Sun, 19 Sep 2021 07:30:58 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Gerrit-MessageType: comment

Gerrit Bot (Gerrit)

unread,
Sep 19, 2021, 10:14:17 PM9/19/21
to Weilu Jia, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Attention is currently required from: Brad Fitzpatrick.

Gerrit Bot uploaded patch set #2 to this change.

View Change

windows: add EnumProcessModules, EnumProcessModulesEx, GetModuleInfor…

…mation, GetModuleFileNameEx and GetModuleBaseName
GitHub-Last-Rev: cf298734aee4a2bb335b09b21c0ff9a3de67406a

GitHub-Pull-Request: golang/sys#116
---
M windows/syscall_windows.go
M windows/syscall_windows_test.go
M windows/types_windows.go
M windows/zsyscall_windows.go
4 files changed, 148 insertions(+), 0 deletions(-)

To view, visit change 350870. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: sys
Gerrit-Branch: master
Gerrit-Change-Id: I75b062daec7a2cc9685e803dfa851825acee32b6
Gerrit-Change-Number: 350870
Gerrit-PatchSet: 2
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-Reviewer: Alex Brainman <alex.b...@gmail.com>
Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Reviewer: Go Bot <go...@golang.org>
Gerrit-CC: Jason A. Donenfeld <Ja...@zx2c4.com>
Gerrit-CC: Weilu Jia <opti...@gmail.com>
Gerrit-Attention: Brad Fitzpatrick <brad...@golang.org>
Gerrit-MessageType: newpatchset

Gerrit Bot (Gerrit)

unread,
Sep 19, 2021, 10:17:00 PM9/19/21
to Weilu Jia, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Attention is currently required from: Brad Fitzpatrick.

Gerrit Bot uploaded patch set #3 to this change.

View Change

windows: add process module related syscalls

Add EnumProcessModules, EnumProcessModulesEx, GetModuleInformation, GetModuleFileNameEx and GetModuleBaseName.


https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-enumprocessmodules
https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-enumprocessmodulesex
https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-getmoduleinformation
https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-getmodulefilenameexw
https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-getmodulebasenamew

Change-Id: I75b062daec7a2cc9685e803dfa851825acee32b6
GitHub-Last-Rev: cf298734aee4a2bb335b09b21c0ff9a3de67406a
GitHub-Pull-Request: golang/sys#116
---
M windows/syscall_windows.go
M windows/syscall_windows_test.go
M windows/types_windows.go
M windows/zsyscall_windows.go
4 files changed, 148 insertions(+), 0 deletions(-)

To view, visit change 350870. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: sys
Gerrit-Branch: master
Gerrit-Change-Id: I75b062daec7a2cc9685e803dfa851825acee32b6
Gerrit-Change-Number: 350870
Gerrit-PatchSet: 3

Weilu Jia (Gerrit)

unread,
Sep 19, 2021, 10:21:02 PM9/19/21
to Gerrit Bot, goph...@pubsubhelper.golang.org, Go Bot, Alex Brainman, Jason A. Donenfeld, Brad Fitzpatrick, golang-co...@googlegroups.com

Attention is currently required from: Brad Fitzpatrick.

View Change

1 comment:

  • Patchset:

    • Patch Set #3:

      Commit message fixed and tests added.

      For my future knowledge, can someone let me know what signature is preferred? ([]uint16) or (*uint16, size uint32)

To view, visit change 350870. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: sys
Gerrit-Branch: master
Gerrit-Change-Id: I75b062daec7a2cc9685e803dfa851825acee32b6
Gerrit-Change-Number: 350870
Gerrit-PatchSet: 3
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-Reviewer: Alex Brainman <alex.b...@gmail.com>
Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Reviewer: Go Bot <go...@golang.org>
Gerrit-CC: Jason A. Donenfeld <Ja...@zx2c4.com>
Gerrit-CC: Weilu Jia <opti...@gmail.com>
Gerrit-Attention: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Comment-Date: Mon, 20 Sep 2021 02:20:58 +0000

Weilu Jia (Gerrit)

unread,
Sep 19, 2021, 10:42:46 PM9/19/21
to Gerrit Bot, goph...@pubsubhelper.golang.org, Go Bot, Alex Brainman, Jason A. Donenfeld, Brad Fitzpatrick, golang-co...@googlegroups.com

Attention is currently required from: Alex Brainman, Brad Fitzpatrick.

View Change

1 comment:

  • Commit Message:

    • add EnumProcessModules, EnumProcessModulesEx, GetModuleInfor…

      …mation, GetModuleFileNameEx and GetModuleBaseName

      s/add EnumProcessModules, EnumProcessModulesEx, GetModuleInformation, GetModuleFileNameEx and GetMod […]

      Done

To view, visit change 350870. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: sys
Gerrit-Branch: master
Gerrit-Change-Id: I75b062daec7a2cc9685e803dfa851825acee32b6
Gerrit-Change-Number: 350870
Gerrit-PatchSet: 3
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-Reviewer: Alex Brainman <alex.b...@gmail.com>
Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Reviewer: Go Bot <go...@golang.org>
Gerrit-CC: Jason A. Donenfeld <Ja...@zx2c4.com>
Gerrit-CC: Weilu Jia <opti...@gmail.com>
Gerrit-Attention: Alex Brainman <alex.b...@gmail.com>
Gerrit-Attention: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Comment-Date: Mon, 20 Sep 2021 02:42:41 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: Alex Brainman <alex.b...@gmail.com>
Gerrit-MessageType: comment

Gerrit Bot (Gerrit)

unread,
Sep 19, 2021, 10:45:08 PM9/19/21
to Weilu Jia, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Attention is currently required from: Alex Brainman, Brad Fitzpatrick.

Gerrit Bot uploaded patch set #4 to this change.

View Change

GitHub-Last-Rev: 657191f0c1a123df337602b85ad2628d2b62747e

GitHub-Pull-Request: golang/sys#116
---
M windows/syscall_windows.go
M windows/syscall_windows_test.go
M windows/types_windows.go
M windows/zsyscall_windows.go
4 files changed, 150 insertions(+), 0 deletions(-)

To view, visit change 350870. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: sys
Gerrit-Branch: master
Gerrit-Change-Id: I75b062daec7a2cc9685e803dfa851825acee32b6
Gerrit-Change-Number: 350870
Gerrit-PatchSet: 4
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-Reviewer: Alex Brainman <alex.b...@gmail.com>
Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Reviewer: Go Bot <go...@golang.org>
Gerrit-CC: Jason A. Donenfeld <Ja...@zx2c4.com>
Gerrit-CC: Weilu Jia <opti...@gmail.com>
Gerrit-Attention: Alex Brainman <alex.b...@gmail.com>
Gerrit-Attention: Brad Fitzpatrick <brad...@golang.org>
Gerrit-MessageType: newpatchset

Gerrit Bot (Gerrit)

unread,
Sep 19, 2021, 10:51:21 PM9/19/21
to Weilu Jia, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Attention is currently required from: Alex Brainman, Brad Fitzpatrick.

Gerrit Bot uploaded patch set #5 to this change.

View Change

GitHub-Last-Rev: 42c4fc748961b8487e925e685794b1eb7699942e

GitHub-Pull-Request: golang/sys#116
---
M windows/syscall_windows.go
M windows/syscall_windows_test.go
M windows/types_windows.go
M windows/zsyscall_windows.go
4 files changed, 150 insertions(+), 0 deletions(-)

To view, visit change 350870. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: sys
Gerrit-Branch: master
Gerrit-Change-Id: I75b062daec7a2cc9685e803dfa851825acee32b6
Gerrit-Change-Number: 350870
Gerrit-PatchSet: 5

Alex Brainman (Gerrit)

unread,
Sep 20, 2021, 4:38:14 AM9/20/21
to Gerrit Bot, Weilu Jia, goph...@pubsubhelper.golang.org, Go Bot, Jason A. Donenfeld, Brad Fitzpatrick, golang-co...@googlegroups.com

Attention is currently required from: Brad Fitzpatrick, Weilu Jia.

Patch set 5:Trust +1

View Change

19 comments:

  • Patchset:

    • Patch Set #3:

      Commit message fixed and tests added. […]

      I prefer to use

      str *uint16, size uint32

      because that is what real Windows API does. Sometimes you need control both of str and size when calling API.

  • Patchset:

    • Patch Set #5:

      Weilu Jia,

      Thank you for adding test.

      I did not expect you to test every new API. But that is fine. We will see how it goes.

      Alex

  • File windows/syscall_windows_test.go:

To view, visit change 350870. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: sys
Gerrit-Branch: master
Gerrit-Change-Id: I75b062daec7a2cc9685e803dfa851825acee32b6
Gerrit-Change-Number: 350870
Gerrit-PatchSet: 5
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-Reviewer: Alex Brainman <alex.b...@gmail.com>
Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Reviewer: Go Bot <go...@golang.org>
Gerrit-CC: Jason A. Donenfeld <Ja...@zx2c4.com>
Gerrit-CC: Weilu Jia <opti...@gmail.com>
Gerrit-Attention: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Attention: Weilu Jia <opti...@gmail.com>
Gerrit-Comment-Date: Mon, 20 Sep 2021 08:38:07 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
Comment-In-Reply-To: Weilu Jia <opti...@gmail.com>
Gerrit-MessageType: comment

Weilu Jia (Gerrit)

unread,
Sep 20, 2021, 6:55:49 PM9/20/21
to Gerrit Bot, goph...@pubsubhelper.golang.org, Go Bot, Alex Brainman, Jason A. Donenfeld, Brad Fitzpatrick, golang-co...@googlegroups.com

Attention is currently required from: Alex Brainman, Brad Fitzpatrick.

View Change

17 comments:

  • Patchset:

  • File windows/syscall_windows_test.go:

    • s/Errorf/Fatalf/ […]

      The general idea here was to catch as many failures as possible at once, since some (but not all) of the tests are independent from each other (ie GetModuleFileName and GetModuleInformation). Though I can see how that could make the resultant errors confusing.

    • s/unable to call EnumProcessModules/EnumProcessModules failed/ […]

      Done

    • Done

    • Done

    • Done

    • Maybe […]

      Done

    • Done

    • Done

    • Done

    • Done

    • Done

    • Patch Set #5, Line 762: peFile, err := pe.Open(exePath)

      I am surprised you were able to open executable file. We will see what builders say about that.

    • It arguably _has_ to be openable since it's the currently running test executable and iirc Windows effectively mmaps executables.

    • Done

    • Done

    • You don't need arch variable here. You can just do […]

      The more you know. Thanks.

    • Done

To view, visit change 350870. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: sys
Gerrit-Branch: master
Gerrit-Change-Id: I75b062daec7a2cc9685e803dfa851825acee32b6
Gerrit-Change-Number: 350870
Gerrit-PatchSet: 5
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-Reviewer: Alex Brainman <alex.b...@gmail.com>
Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Reviewer: Go Bot <go...@golang.org>
Gerrit-CC: Jason A. Donenfeld <Ja...@zx2c4.com>
Gerrit-CC: Weilu Jia <opti...@gmail.com>
Gerrit-Attention: Alex Brainman <alex.b...@gmail.com>
Gerrit-Attention: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Comment-Date: Mon, 20 Sep 2021 22:55:44 +0000

Gerrit Bot (Gerrit)

unread,
Sep 20, 2021, 6:56:10 PM9/20/21
to Weilu Jia, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Attention is currently required from: Alex Brainman, Brad Fitzpatrick.

Gerrit Bot uploaded patch set #6 to this change.

View Change

GitHub-Last-Rev: 7c208d6d3eedfbcd536470c6dbde4f4aecdae61e

GitHub-Pull-Request: golang/sys#116
---
M windows/syscall_windows.go
M windows/syscall_windows_test.go
M windows/types_windows.go
M windows/zsyscall_windows.go
4 files changed, 140 insertions(+), 0 deletions(-)

To view, visit change 350870. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: sys
Gerrit-Branch: master
Gerrit-Change-Id: I75b062daec7a2cc9685e803dfa851825acee32b6
Gerrit-Change-Number: 350870
Gerrit-PatchSet: 6
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-Reviewer: Alex Brainman <alex.b...@gmail.com>
Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Reviewer: Go Bot <go...@golang.org>
Gerrit-CC: Jason A. Donenfeld <Ja...@zx2c4.com>
Gerrit-CC: Weilu Jia <opti...@gmail.com>
Gerrit-Attention: Alex Brainman <alex.b...@gmail.com>
Gerrit-Attention: Brad Fitzpatrick <brad...@golang.org>
Gerrit-MessageType: newpatchset

Weilu Jia (Gerrit)

unread,
Sep 20, 2021, 7:00:18 PM9/20/21
to Gerrit Bot, goph...@pubsubhelper.golang.org, Go Bot, Alex Brainman, Jason A. Donenfeld, Brad Fitzpatrick, golang-co...@googlegroups.com

Attention is currently required from: Alex Brainman, Brad Fitzpatrick.

View Change

1 comment:

  • Patchset:

    • Patch Set #3:

      I prefer to use […]

      In practice, doesn't a slice maintain the same control over both str and size? The only case where a slice doesn't behave the same is if you set size > len(str) which sounds exceptionally bad to allow.

       var str = make([]uint16, 8)

      // func(str uint16, size uint32)
      func(&str[0], uint32(len(str))) // use all of str
      func(&str[4], 4) // use part of str
      // This is allowed
      func(&str[7], 65535) // try to go out of bounds
       // func(str []uint16)
      func(str) // use all of str
      func(str[4:8]) // use part of str
      // This will throw an out of bounds error
      func(str[7:65535]) // try to go out of bounds

To view, visit change 350870. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: sys
Gerrit-Branch: master
Gerrit-Change-Id: I75b062daec7a2cc9685e803dfa851825acee32b6
Gerrit-Change-Number: 350870
Gerrit-PatchSet: 5
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-Reviewer: Alex Brainman <alex.b...@gmail.com>
Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Reviewer: Go Bot <go...@golang.org>
Gerrit-CC: Jason A. Donenfeld <Ja...@zx2c4.com>
Gerrit-CC: Weilu Jia <opti...@gmail.com>
Gerrit-Attention: Alex Brainman <alex.b...@gmail.com>
Gerrit-Attention: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Comment-Date: Mon, 20 Sep 2021 23:00:15 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: Alex Brainman <alex.b...@gmail.com>

Weilu Jia (Gerrit)

unread,
Sep 20, 2021, 7:00:30 PM9/20/21
to Gerrit Bot, goph...@pubsubhelper.golang.org, Go Bot, Alex Brainman, Jason A. Donenfeld, Brad Fitzpatrick, golang-co...@googlegroups.com

Attention is currently required from: Alex Brainman, Brad Fitzpatrick.

View Change

1 comment:

To view, visit change 350870. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: sys
Gerrit-Branch: master
Gerrit-Change-Id: I75b062daec7a2cc9685e803dfa851825acee32b6
Gerrit-Change-Number: 350870
Gerrit-PatchSet: 6
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-Reviewer: Alex Brainman <alex.b...@gmail.com>
Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Reviewer: Go Bot <go...@golang.org>
Gerrit-CC: Jason A. Donenfeld <Ja...@zx2c4.com>
Gerrit-CC: Weilu Jia <opti...@gmail.com>
Gerrit-Attention: Alex Brainman <alex.b...@gmail.com>
Gerrit-Attention: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Comment-Date: Mon, 20 Sep 2021 23:00:26 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: Alex Brainman <alex.b...@gmail.com>
Gerrit-MessageType: comment

Alex Brainman (Gerrit)

unread,
Sep 24, 2021, 11:06:52 PM9/24/21
to Gerrit Bot, Weilu Jia, goph...@pubsubhelper.golang.org, Go Bot, Jason A. Donenfeld, Brad Fitzpatrick, golang-co...@googlegroups.com

Attention is currently required from: Brad Fitzpatrick, Weilu Jia.

Patch set 6:Run-TryBot +1Trust +1

View Change

2 comments:

  • Patchset:

    • Patch Set #3:

      In practice, doesn't a slice maintain the same control over both str and size? The only case where a […]

      I meant API like RegEnumKeyExW

      https://docs.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regenumkeyexw

      lpcchName parameter is used to pass data in both directions - program to OS and back. It would be impossible to receive lpcchName value from OS, if we would use Go slice here.

      And if we don't use slice for RegEnumKeyExW, we should do the same in other APIs. So API users don't need to remember with API uses slice and which are not.

      Another benefit of using standard Windows API is that you can just google your function signature.

  • File windows/syscall_windows_test.go:

    • Patch Set #5, Line 762: peFile, err := pe.Open(exePath)

      It arguably _has_ to be openable since it's the currently running test executable and iirc Windows e […]

      Yes, you are correct, and I was wrong. Successful build proves your point.

To view, visit change 350870. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: sys
Gerrit-Branch: master
Gerrit-Change-Id: I75b062daec7a2cc9685e803dfa851825acee32b6
Gerrit-Change-Number: 350870
Gerrit-PatchSet: 6
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-Reviewer: Alex Brainman <alex.b...@gmail.com>
Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Reviewer: Go Bot <go...@golang.org>
Gerrit-CC: Jason A. Donenfeld <Ja...@zx2c4.com>
Gerrit-CC: Weilu Jia <opti...@gmail.com>
Gerrit-Attention: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Attention: Weilu Jia <opti...@gmail.com>
Gerrit-Comment-Date: Sat, 25 Sep 2021 03:06:45 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes

Alex Brainman (Gerrit)

unread,
Sep 24, 2021, 11:18:37 PM9/24/21
to Gerrit Bot, Weilu Jia, goph...@pubsubhelper.golang.org, Go Bot, Jason A. Donenfeld, Brad Fitzpatrick, golang-co...@googlegroups.com

Attention is currently required from: Brad Fitzpatrick, Weilu Jia.

Patch set 6:Code-Review +2

View Change

1 comment:

  • Patchset:

    • Patch Set #6:

      LGTM.

      I would have to find another reviewer of this CL before it can be submitted.

      Thanks again.

      Alex

To view, visit change 350870. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: sys
Gerrit-Branch: master
Gerrit-Change-Id: I75b062daec7a2cc9685e803dfa851825acee32b6
Gerrit-Change-Number: 350870
Gerrit-PatchSet: 6
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-Reviewer: Alex Brainman <alex.b...@gmail.com>
Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Reviewer: Go Bot <go...@golang.org>
Gerrit-CC: Jason A. Donenfeld <Ja...@zx2c4.com>
Gerrit-CC: Weilu Jia <opti...@gmail.com>
Gerrit-Attention: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Attention: Weilu Jia <opti...@gmail.com>
Gerrit-Comment-Date: Sat, 25 Sep 2021 03:18:29 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment

Minux Ma (Gerrit)

unread,
Sep 24, 2021, 11:24:27 PM9/24/21
to Gerrit Bot, Weilu Jia, goph...@pubsubhelper.golang.org, Minux Ma, Alex Brainman, Go Bot, Jason A. Donenfeld, Brad Fitzpatrick, golang-co...@googlegroups.com

Attention is currently required from: Brad Fitzpatrick, Weilu Jia.

Patch set 6:Trust +1

View Change

    To view, visit change 350870. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: sys
    Gerrit-Branch: master
    Gerrit-Change-Id: I75b062daec7a2cc9685e803dfa851825acee32b6
    Gerrit-Change-Number: 350870
    Gerrit-PatchSet: 6
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Alex Brainman <alex.b...@gmail.com>
    Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
    Gerrit-Reviewer: Go Bot <go...@golang.org>
    Gerrit-Reviewer: Minux Ma <mi...@golang.org>
    Gerrit-CC: Jason A. Donenfeld <Ja...@zx2c4.com>
    Gerrit-CC: Weilu Jia <opti...@gmail.com>
    Gerrit-Attention: Brad Fitzpatrick <brad...@golang.org>
    Gerrit-Attention: Weilu Jia <opti...@gmail.com>
    Gerrit-Comment-Date: Sat, 25 Sep 2021 03:24:23 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes
    Gerrit-MessageType: comment

    Alex Brainman (Gerrit)

    unread,
    Sep 24, 2021, 11:26:00 PM9/24/21
    to Gerrit Bot, Weilu Jia, goph...@pubsubhelper.golang.org, Minux Ma, Go Bot, Jason A. Donenfeld, Brad Fitzpatrick, golang-co...@googlegroups.com

    Attention is currently required from: Brad Fitzpatrick, Weilu Jia.

    View Change

    1 comment:

    • Patchset:

      • Patch Set #6:

        Thank you Minux.

        Long time no speak. I hope you doing well.

        Alex

    To view, visit change 350870. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: sys
    Gerrit-Branch: master
    Gerrit-Change-Id: I75b062daec7a2cc9685e803dfa851825acee32b6
    Gerrit-Change-Number: 350870
    Gerrit-PatchSet: 6
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Alex Brainman <alex.b...@gmail.com>
    Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
    Gerrit-Reviewer: Go Bot <go...@golang.org>
    Gerrit-Reviewer: Minux Ma <mi...@golang.org>
    Gerrit-CC: Jason A. Donenfeld <Ja...@zx2c4.com>
    Gerrit-CC: Weilu Jia <opti...@gmail.com>
    Gerrit-Attention: Brad Fitzpatrick <brad...@golang.org>
    Gerrit-Attention: Weilu Jia <opti...@gmail.com>
    Gerrit-Comment-Date: Sat, 25 Sep 2021 03:25:53 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    Gerrit-MessageType: comment

    Alex Brainman (Gerrit)

    unread,
    Sep 24, 2021, 11:26:08 PM9/24/21
    to Gerrit Bot, Weilu Jia, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Minux Ma, Go Bot, Jason A. Donenfeld, Brad Fitzpatrick, golang-co...@googlegroups.com

    Alex Brainman submitted this change.

    View Change


    Approvals: Alex Brainman: Looks good to me, approved; Trusted; Run TryBots Minux Ma: Trusted Go Bot: TryBots succeeded
    windows: add process module related syscalls

    Add EnumProcessModules, EnumProcessModulesEx, GetModuleInformation, GetModuleFileNameEx and GetModuleBaseName.

    https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-enumprocessmodules
    https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-enumprocessmodulesex
    https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-getmoduleinformation
    https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-getmodulefilenameexw
    https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-getmodulebasenamew

    Change-Id: I75b062daec7a2cc9685e803dfa851825acee32b6
    GitHub-Last-Rev: 7c208d6d3eedfbcd536470c6dbde4f4aecdae61e
    GitHub-Pull-Request: golang/sys#116
    Reviewed-on: https://go-review.googlesource.com/c/sys/+/350870
    Trust: Alex Brainman <alex.b...@gmail.com>
    Trust: Minux Ma <mi...@golang.org>
    Run-TryBot: Alex Brainman <alex.b...@gmail.com>
    TryBot-Result: Go Bot <go...@golang.org>
    Reviewed-by: Alex Brainman <alex.b...@gmail.com>

    ---
    M windows/syscall_windows.go
    M windows/syscall_windows_test.go
    M windows/types_windows.go
    M windows/zsyscall_windows.go
    4 files changed, 140 insertions(+), 0 deletions(-)

    diff --git a/windows/syscall_windows.go b/windows/syscall_windows.go
    index 1215b2a..ff2d45d 100644
    --- a/windows/syscall_windows.go
    +++ b/windows/syscall_windows.go
    @@ -398,6 +398,11 @@

    // Process Status API (PSAPI)
    //sys EnumProcesses(processIds []uint32, bytesReturned *uint32) (err error) = psapi.EnumProcesses
    +//sys EnumProcessModules(process Handle, module *Handle, cb uint32, cbNeeded *uint32) (err error) = psapi.EnumProcessModules
    +//sys EnumProcessModulesEx(process Handle, module *Handle, cb uint32, cbNeeded *uint32, filterFlag uint32) (err error) = psapi.EnumProcessModulesEx
    +//sys GetModuleInformation(process Handle, module Handle, modinfo *ModuleInfo, cb uint32) (err error) = psapi.GetModuleInformation
    +//sys GetModuleFileNameEx(process Handle, module Handle, filename *uint16, size uint32) (err error) = psapi.GetModuleFileNameExW
    +//sys GetModuleBaseName(process Handle, module Handle, baseName *uint16, size uint32) (err error) = psapi.GetModuleBaseNameW

    // NT Native APIs
    //sys rtlNtStatusToDosErrorNoTeb(ntstatus NTStatus) (ret syscall.Errno) = ntdll.RtlNtStatusToDosErrorNoTeb
    diff --git a/windows/syscall_windows_test.go b/windows/syscall_windows_test.go
    index fd75c09..bc11750 100644
    --- a/windows/syscall_windows_test.go
    +++ b/windows/syscall_windows_test.go
    @@ -701,3 +701,79 @@
    }

    }
    +
    +func TestProcessModules(t *testing.T) {
    + process, err := windows.GetCurrentProcess()
    + if err != nil {
    + t.Fatalf("unable to get current process: %v", err)
    + }
    + // NB: Assume that we're always the first module. This technically isn't documented anywhere (that I could find), but seems to always hold.
    + var module windows.Handle
    + var cbNeeded uint32
    + err = windows.EnumProcessModules(process, &module, uint32(unsafe.Sizeof(module)), &cbNeeded)
    + if err != nil {
    + t.Fatalf("EnumProcessModules failed: %v", err)
    + }
    +
    + var moduleEx windows.Handle
    + err = windows.EnumProcessModulesEx(process, &moduleEx, uint32(unsafe.Sizeof(moduleEx)), &cbNeeded, windows.LIST_MODULES_DEFAULT)
    + if err != nil {
    + t.Fatalf("EnumProcessModulesEx failed: %v", err)
    + }
    + if module != moduleEx {
    + t.Fatalf("module from EnumProcessModules does not match EnumProcessModulesEx: %v != %v", module, moduleEx)
    + }
    +
    + exePath, err := os.Executable()
    + if err != nil {
    + t.Fatalf("unable to get current executable path: %v", err)
    + }
    +
    + modulePathUTF16 := make([]uint16, len(exePath)+1)
    + err = windows.GetModuleFileNameEx(process, module, &modulePathUTF16[0], uint32(len(modulePathUTF16)))
    + if err != nil {
    + t.Fatalf("GetModuleFileNameEx failed: %v", err)
    + }
    +
    + modulePath := windows.UTF16ToString(modulePathUTF16)
    + if modulePath != exePath {
    + t.Fatalf("module does not match executable for GetModuleFileNameEx: %s != %s", modulePath, exePath)
    + }
    +
    + err = windows.GetModuleBaseName(process, module, &modulePathUTF16[0], uint32(len(modulePathUTF16)))
    + if err != nil {
    + t.Fatalf("GetModuleBaseName failed: %v", err)
    + }
    +
    + modulePath = windows.UTF16ToString(modulePathUTF16)
    + baseExePath := filepath.Base(exePath)
    + if modulePath != baseExePath {
    + t.Fatalf("module does not match executable for GetModuleBaseName: %s != %s", modulePath, baseExePath)
    + }
    +
    + var moduleInfo windows.ModuleInfo
    + err = windows.GetModuleInformation(process, module, &moduleInfo, uint32(unsafe.Sizeof(moduleInfo)))
    + if err != nil {
    + t.Fatalf("GetModuleInformation failed: %v", err)
    + }
    +
    + peFile, err := pe.Open(exePath)
    + if err != nil {
    + t.Fatalf("unable to open current executable: %v", err)
    + }
    + defer peFile.Close()
    +
    + var peSizeOfImage uint32
    + switch runtime.GOARCH {
    + case "amd64", "arm64":
    + peSizeOfImage = peFile.OptionalHeader.(*pe.OptionalHeader64).SizeOfImage
    + case "386", "arm":
    + peSizeOfImage = peFile.OptionalHeader.(*pe.OptionalHeader32).SizeOfImage
    + default:
    + t.Fatalf("unable to test GetModuleInformation on arch %v", runtime.GOARCH)
    + }
    +
    + if moduleInfo.SizeOfImage != peSizeOfImage {
    + t.Fatalf("module size does not match executable: %v != %v", moduleInfo.SizeOfImage, peSizeOfImage)
    + }
    +}

    To view, visit change 350870. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: sys
    Gerrit-Branch: master
    Gerrit-Change-Id: I75b062daec7a2cc9685e803dfa851825acee32b6
    Gerrit-Change-Number: 350870
    Gerrit-PatchSet: 7
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Alex Brainman <alex.b...@gmail.com>
    Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
    Gerrit-Reviewer: Go Bot <go...@golang.org>
    Gerrit-Reviewer: Minux Ma <mi...@golang.org>
    Gerrit-CC: Jason A. Donenfeld <Ja...@zx2c4.com>
    Gerrit-CC: Weilu Jia <opti...@gmail.com>
    Gerrit-MessageType: merged

    Weilu Jia (Gerrit)

    unread,
    Sep 24, 2021, 11:53:17 PM9/24/21
    to Alex Brainman, Gerrit Bot, goph...@pubsubhelper.golang.org, Minux Ma, Go Bot, Jason A. Donenfeld, Brad Fitzpatrick, golang-co...@googlegroups.com

    View Change

    1 comment:

    • Patchset:

      • Patch Set #7:

        Thanks for putting up with my questions and reviewing this!

    To view, visit change 350870. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: sys
    Gerrit-Branch: master
    Gerrit-Change-Id: I75b062daec7a2cc9685e803dfa851825acee32b6
    Gerrit-Change-Number: 350870
    Gerrit-PatchSet: 7
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Alex Brainman <alex.b...@gmail.com>
    Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
    Gerrit-Reviewer: Go Bot <go...@golang.org>
    Gerrit-Reviewer: Minux Ma <mi...@golang.org>
    Gerrit-CC: Jason A. Donenfeld <Ja...@zx2c4.com>
    Gerrit-CC: Weilu Jia <opti...@gmail.com>
    Gerrit-Comment-Date: Sat, 25 Sep 2021 03:53:11 +0000
    Reply all
    Reply to author
    Forward
    0 new messages