]> Cypherpunks repositories - gostls13.git/commitdiff
os/user: test that Current does not depend on netapi32.dll
authorqmuntal <quimmuntal@gmail.com>
Fri, 9 Aug 2024 08:57:51 +0000 (10:57 +0200)
committerQuim Muntal <quimmuntal@gmail.com>
Tue, 27 Aug 2024 13:21:49 +0000 (13:21 +0000)
Updates #21867.

Change-Id: I1eb923ef66aa0f338bfa0d683159edc1d8ae2a6c
Reviewed-on: https://go-review.googlesource.com/c/go/+/604415
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
src/internal/syscall/windows/syscall_windows.go
src/internal/syscall/windows/zsyscall_windows.go
src/os/user/user_windows_test.go

index 944e4e24505bc2365d4cdbf330d57bd53f405e0c..b8168678c3c361101ed2e9a96893ae67b28cf087 100644 (file)
@@ -500,3 +500,5 @@ func QueryPerformanceCounter() int64 // Implemented in runtime package.
 //
 //go:linkname QueryPerformanceFrequency
 func QueryPerformanceFrequency() int64 // Implemented in runtime package.
+
+//sys   GetModuleHandle(modulename *uint16) (handle syscall.Handle, err error) = kernel32.GetModuleHandleW
index 7e4d91112b1188b90035cac187cefeb111c1017e..07627bb6caf905a5c221c325744af32333a2f3ed 100644 (file)
@@ -69,6 +69,7 @@ var (
        procGetFileInformationByHandleEx      = modkernel32.NewProc("GetFileInformationByHandleEx")
        procGetFinalPathNameByHandleW         = modkernel32.NewProc("GetFinalPathNameByHandleW")
        procGetModuleFileNameW                = modkernel32.NewProc("GetModuleFileNameW")
+       procGetModuleHandleW                  = modkernel32.NewProc("GetModuleHandleW")
        procGetTempPath2W                     = modkernel32.NewProc("GetTempPath2W")
        procGetVolumeInformationByHandleW     = modkernel32.NewProc("GetVolumeInformationByHandleW")
        procGetVolumeNameForVolumeMountPointW = modkernel32.NewProc("GetVolumeNameForVolumeMountPointW")
@@ -287,6 +288,15 @@ func GetModuleFileName(module syscall.Handle, fn *uint16, len uint32) (n uint32,
        return
 }
 
+func GetModuleHandle(modulename *uint16) (handle syscall.Handle, err error) {
+       r0, _, e1 := syscall.Syscall(procGetModuleHandleW.Addr(), 1, uintptr(unsafe.Pointer(modulename)), 0, 0)
+       handle = syscall.Handle(r0)
+       if handle == 0 {
+               err = errnoErr(e1)
+       }
+       return
+}
+
 func GetTempPath2(buflen uint32, buf *uint16) (n uint32, err error) {
        r0, _, e1 := syscall.Syscall(procGetTempPath2W.Addr(), 2, uintptr(buflen), uintptr(unsafe.Pointer(buf)), 0)
        n = uint32(r0)
index 3364d7c9eac565814f837350b1a351adf5b64135..deb3db6a87193610cc48652e23ab51205c674978 100644 (file)
@@ -8,7 +8,11 @@ import (
        "crypto/rand"
        "encoding/base64"
        "errors"
+       "fmt"
        "internal/syscall/windows"
+       "internal/testenv"
+       "os"
+       "os/exec"
        "runtime"
        "strconv"
        "syscall"
@@ -143,3 +147,37 @@ func TestImpersonated(t *testing.T) {
        }
        compare(t, want, got)
 }
+
+func TestCurrentNetapi32(t *testing.T) {
+       if os.Getenv("GO_WANT_HELPER_PROCESS") == "1" {
+               // Test that Current does not load netapi32.dll.
+               // First call Current.
+               Current()
+
+               // Then check if netapi32.dll is loaded.
+               netapi32, err := syscall.UTF16PtrFromString("netapi32.dll")
+               if err != nil {
+                       fmt.Fprintf(os.Stderr, "error: %s\n", err.Error())
+                       os.Exit(9)
+                       return
+               }
+               mod, _ := windows.GetModuleHandle(netapi32)
+               if mod != 0 {
+                       fmt.Fprintf(os.Stderr, "netapi32.dll is loaded\n")
+                       os.Exit(9)
+                       return
+               }
+               os.Exit(0)
+               return
+       }
+       exe, err := os.Executable()
+       if err != nil {
+               t.Fatal(err)
+       }
+       cmd := testenv.CleanCmdEnv(exec.Command(exe, "-test.run=^TestCurrentNetapi32$"))
+       cmd.Env = append(cmd.Env, "GO_WANT_HELPER_PROCESS=1")
+       out, err := cmd.CombinedOutput()
+       if err != nil {
+               t.Fatalf("%v\n%s", err, out)
+       }
+}