]> Cypherpunks repositories - gostls13.git/commitdiff
net,internal/poll: load ws2_32.dll only when net is imported
authorqmuntal <quimmuntal@gmail.com>
Fri, 19 Jan 2024 15:27:22 +0000 (16:27 +0100)
committerQuim Muntal <quimmuntal@gmail.com>
Tue, 23 Jan 2024 18:57:32 +0000 (18:57 +0000)
On Windows, ws2_32.dll is loaded and WSA initialized even if websockets
are not used.

This CL delays loading of ws2_32.dll and starting WSA until net is
initialized.

Change-Id: I07ea8241d79709cd4e80d29ba0d792c7444bbfe9
Reviewed-on: https://go-review.googlesource.com/c/go/+/557015
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

src/cmd/go/testdata/script/ws2_32.txt [new file with mode: 0644]
src/internal/poll/fd_windows.go
src/internal/poll/fd_windows_test.go
src/net/fd_windows.go

diff --git a/src/cmd/go/testdata/script/ws2_32.txt b/src/cmd/go/testdata/script/ws2_32.txt
new file mode 100644 (file)
index 0000000..54f6a94
--- /dev/null
@@ -0,0 +1,48 @@
+[!GOOS:windows] skip
+
+go run .
+stdout 'ws2_32.dll: not found'
+
+go run -tags net .
+stdout 'ws2_32.dll: found'
+
+-- go.mod --
+module m
+
+go 1.21
+
+-- utils.go --
+package main
+
+import (
+       "fmt"
+       "syscall"
+       "unsafe"
+)
+
+func hasModuleHandle() {
+       const ws2_32 = "ws2_32.dll"
+       getModuleHandle := syscall.MustLoadDLL("kernel32.dll").MustFindProc("GetModuleHandleW")
+       mod, _, _ := getModuleHandle.Call(uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(ws2_32))))
+       if mod != 0 {
+               fmt.Println(ws2_32+":", "found")
+       } else {
+               fmt.Println(ws2_32+":", "not found")
+       }
+}
+-- net.go --
+//go:build net
+package main
+
+import _ "net"
+
+func main() {
+    hasModuleHandle()
+}
+-- nonet.go --
+//go:build !net
+package main
+
+func main() {
+    hasModuleHandle()
+}
\ No newline at end of file
index fe23d4b3d74e3fe321184d7adce6a3c498ea8479..b08ca615c6701654c39700caa7e03125bec952c4 100644 (file)
@@ -53,14 +53,17 @@ func checkSetFileCompletionNotificationModes() {
        useSetFileCompletionNotificationModes = true
 }
 
-func init() {
+// InitWSA initiates the use of the Winsock DLL by the current process.
+// It is called from the net package at init time to avoid
+// loading ws2_32.dll when net is not used.
+var InitWSA = sync.OnceFunc(func() {
        var d syscall.WSAData
        e := syscall.WSAStartup(uint32(0x202), &d)
        if e != nil {
                initErr = e
        }
        checkSetFileCompletionNotificationModes()
-}
+})
 
 // operation contains superset of data necessary to perform all async IO.
 type operation struct {
index f0697a0d7b80851fa04f9afbf9beaaa751f11810..1cee18dcba53fc8d0a4081f9e1071ac05226a24c 100644 (file)
@@ -41,6 +41,8 @@ func logFD(net string, fd *poll.FD, err error) {
 func init() {
        loggedFDs = make(map[syscall.Handle]*loggedFD)
        *poll.LogInitFD = logFD
+
+       poll.InitWSA()
 }
 
 func findLoggedFD(h syscall.Handle) (lfd *loggedFD, found bool) {
index 45a10cf1eb0121c8f483c110e3045924b943d1ab..f9a077b6311930f1e8966a731b82ef3faf205e16 100644 (file)
@@ -23,6 +23,10 @@ const (
        writeMsgSyscallName = "wsasendmsg"
 )
 
+func init() {
+       poll.InitWSA()
+}
+
 // canUseConnectEx reports whether we can use the ConnectEx Windows API call
 // for the given network type.
 func canUseConnectEx(net string) bool {