From f2ce62c21a0ddb543d412be19f3168b09d76d1b8 Mon Sep 17 00:00:00 2001 From: database64128 Date: Tue, 4 Mar 2025 05:31:52 +0000 Subject: [PATCH 01/13] windows: add constants for PMTUD socket options Related documentation: - https://learn.microsoft.com/en-us/windows/win32/winsock/ipproto-ip-socket-options - https://learn.microsoft.com/en-us/windows/win32/winsock/ipproto-ipv6-socket-options Change-Id: I21b23ca815d1d8135ce5724115b9ca23819ea10a GitHub-Last-Rev: 9054c5c79068da8d2583b23dca9464293c36901a GitHub-Pull-Request: golang/sys#245 Reviewed-on: https://go-review.googlesource.com/c/sys/+/654495 Reviewed-by: Quim Muntal Reviewed-by: Ian Lance Taylor LUCI-TryBot-Result: Go LUCI Reviewed-by: Junyang Shao Auto-Submit: Ian Lance Taylor --- windows/types_windows.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/windows/types_windows.go b/windows/types_windows.go index 9d138de5fe..fac0309d08 100644 --- a/windows/types_windows.go +++ b/windows/types_windows.go @@ -1074,6 +1074,7 @@ const ( IP_ADD_MEMBERSHIP = 0xc IP_DROP_MEMBERSHIP = 0xd IP_PKTINFO = 0x13 + IP_MTU_DISCOVER = 0x47 IPV6_V6ONLY = 0x1b IPV6_UNICAST_HOPS = 0x4 @@ -1083,6 +1084,7 @@ const ( IPV6_JOIN_GROUP = 0xc IPV6_LEAVE_GROUP = 0xd IPV6_PKTINFO = 0x13 + IPV6_MTU_DISCOVER = 0x47 MSG_OOB = 0x1 MSG_PEEK = 0x2 @@ -1132,6 +1134,15 @@ const ( WSASYS_STATUS_LEN = 128 ) +// enum PMTUD_STATE from ws2ipdef.h +const ( + IP_PMTUDISC_NOT_SET = 0 + IP_PMTUDISC_DO = 1 + IP_PMTUDISC_DONT = 2 + IP_PMTUDISC_PROBE = 3 + IP_PMTUDISC_MAX = 4 +) + type WSABuf struct { Len uint32 Buf *byte From b8f7da6c5a244c4015da1e739f7f40b21f4c40c8 Mon Sep 17 00:00:00 2001 From: Guoqi Chen Date: Thu, 6 Mar 2025 19:41:06 +0800 Subject: [PATCH 02/13] cpu: add support for detecting cpu features on loong64 Except for lasx, all other features have been implemented in the Go mainline. Change-Id: I61a09396ed23d17991b641a1265e952585cb5636 Reviewed-on: https://go-review.googlesource.com/c/sys/+/655355 Reviewed-by: David Chase Reviewed-by: sophie zhao LUCI-TryBot-Result: Go LUCI Reviewed-by: Junyang Shao Reviewed-by: Meidan Li --- cpu/cpu.go | 12 ++++++++++++ cpu/cpu_linux_loong64.go | 22 ++++++++++++++++++++++ cpu/cpu_linux_noinit.go | 2 +- cpu/cpu_loong64.go | 38 ++++++++++++++++++++++++++++++++++++++ cpu/cpu_loong64.s | 12 ++++++++++++ cpu/cpu_test.go | 8 ++++++++ 6 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 cpu/cpu_linux_loong64.go create mode 100644 cpu/cpu_loong64.s diff --git a/cpu/cpu.go b/cpu/cpu.go index 9c105f23af..2e73ee1975 100644 --- a/cpu/cpu.go +++ b/cpu/cpu.go @@ -149,6 +149,18 @@ var ARM struct { _ CacheLinePad } +// The booleans in Loong64 contain the correspondingly named cpu feature bit. +// The struct is padded to avoid false sharing. +var Loong64 struct { + _ CacheLinePad + HasLSX bool // support 128-bit vector extension + HasLASX bool // support 256-bit vector extension + HasCRC32 bool // support CRC instruction + HasLAM_BH bool // support AM{SWAP/ADD}[_DB].{B/H} instruction + HasLAMCAS bool // support AMCAS[_DB].{B/H/W/D} instruction + _ CacheLinePad +} + // MIPS64X contains the supported CPU features of the current mips64/mips64le // platforms. If the current platform is not mips64/mips64le or the current // operating system is not Linux then all feature flags are false. diff --git a/cpu/cpu_linux_loong64.go b/cpu/cpu_linux_loong64.go new file mode 100644 index 0000000000..4f34114329 --- /dev/null +++ b/cpu/cpu_linux_loong64.go @@ -0,0 +1,22 @@ +// Copyright 2025 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 cpu + +// HWCAP bits. These are exposed by the Linux kernel. +const ( + hwcap_LOONGARCH_LSX = 1 << 4 + hwcap_LOONGARCH_LASX = 1 << 5 +) + +func doinit() { + // TODO: Features that require kernel support like LSX and LASX can + // be detected here once needed in std library or by the compiler. + Loong64.HasLSX = hwcIsSet(hwCap, hwcap_LOONGARCH_LSX) + Loong64.HasLASX = hwcIsSet(hwCap, hwcap_LOONGARCH_LASX) +} + +func hwcIsSet(hwc uint, val uint) bool { + return hwc&val != 0 +} diff --git a/cpu/cpu_linux_noinit.go b/cpu/cpu_linux_noinit.go index 7d902b6847..a428dec9cd 100644 --- a/cpu/cpu_linux_noinit.go +++ b/cpu/cpu_linux_noinit.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -//go:build linux && !arm && !arm64 && !mips64 && !mips64le && !ppc64 && !ppc64le && !s390x && !riscv64 +//go:build linux && !arm && !arm64 && !loong64 && !mips64 && !mips64le && !ppc64 && !ppc64le && !s390x && !riscv64 package cpu diff --git a/cpu/cpu_loong64.go b/cpu/cpu_loong64.go index 558635850c..45ecb29ae7 100644 --- a/cpu/cpu_loong64.go +++ b/cpu/cpu_loong64.go @@ -8,5 +8,43 @@ package cpu const cacheLineSize = 64 +// Bit fields for CPUCFG registers, Related reference documents: +// https://loongson.github.io/LoongArch-Documentation/LoongArch-Vol1-EN.html#_cpucfg +const ( + // CPUCFG1 bits + cpucfg1_CRC32 = 1 << 25 + + // CPUCFG2 bits + cpucfg2_LAM_BH = 1 << 27 + cpucfg2_LAMCAS = 1 << 28 +) + func initOptions() { + options = []option{ + {Name: "lsx", Feature: &Loong64.HasLSX}, + {Name: "lasx", Feature: &Loong64.HasLASX}, + {Name: "crc32", Feature: &Loong64.HasCRC32}, + {Name: "lam_bh", Feature: &Loong64.HasLAM_BH}, + {Name: "lamcas", Feature: &Loong64.HasLAMCAS}, + } + + // The CPUCFG data on Loong64 only reflects the hardware capabilities, + // not the kernel support status, so features such as LSX and LASX that + // require kernel support cannot be obtained from the CPUCFG data. + // + // These features only require hardware capability support and do not + // require kernel specific support, so they can be obtained directly + // through CPUCFG + cfg1 := get_cpucfg(1) + cfg2 := get_cpucfg(2) + + Loong64.HasCRC32 = cfgIsSet(cfg1, cpucfg1_CRC32) + Loong64.HasLAMCAS = cfgIsSet(cfg2, cpucfg2_LAMCAS) + Loong64.HasLAM_BH = cfgIsSet(cfg2, cpucfg2_LAM_BH) +} + +func get_cpucfg(reg uint32) uint32 + +func cfgIsSet(cfg uint32, val uint32) bool { + return cfg&val != 0 } diff --git a/cpu/cpu_loong64.s b/cpu/cpu_loong64.s new file mode 100644 index 0000000000..30b4c0d10b --- /dev/null +++ b/cpu/cpu_loong64.s @@ -0,0 +1,12 @@ +// Copyright 2025 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. + +#include "textflag.h" + +// func get_cpucfg(reg uint32) uint32 +TEXT ·get_cpucfg(SB), NOSPLIT|NOFRAME, $0 + MOVW reg+0(FP), R5 + CPUCFG R5, R4 + MOVW R4, ret+8(FP) + RET diff --git a/cpu/cpu_test.go b/cpu/cpu_test.go index dd493ece8c..6906786436 100644 --- a/cpu/cpu_test.go +++ b/cpu/cpu_test.go @@ -87,6 +87,14 @@ func TestARM64minimalFeatures(t *testing.T) { } } +func TestLOONG64Initialized(t *testing.T) { + if runtime.GOARCH == "loong64" { + if !cpu.Initialized { + t.Fatal("Initialized expected true, got false") + } + } +} + func TestMIPS64Initialized(t *testing.T) { if runtime.GOARCH == "mips64" || runtime.GOARCH == "mips64le" { if !cpu.Initialized { From 7401cce31392295f531e1d0fcc2f01d782353300 Mon Sep 17 00:00:00 2001 From: Guoqi Chen Date: Wed, 12 Mar 2025 10:18:00 +0800 Subject: [PATCH 03/13] cpu: replace specific instructions with WORD in the function get_cpucfg on loong64 The CPUCFG instruction on loong64 was introduced in Go1.24, which caused compilation errors when using this feature in Go1.23 and earlier versions. Change-Id: I68891bbfc527194ecdafebac3398e700bfb53c2f Reviewed-on: https://go-review.googlesource.com/c/sys/+/656915 Reviewed-by: Meidan Li LUCI-TryBot-Result: Go LUCI Reviewed-by: David Chase Reviewed-by: Junyang Shao --- cpu/cpu_loong64.s | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cpu/cpu_loong64.s b/cpu/cpu_loong64.s index 30b4c0d10b..71cbaf1ce2 100644 --- a/cpu/cpu_loong64.s +++ b/cpu/cpu_loong64.s @@ -7,6 +7,7 @@ // func get_cpucfg(reg uint32) uint32 TEXT ·get_cpucfg(SB), NOSPLIT|NOFRAME, $0 MOVW reg+0(FP), R5 - CPUCFG R5, R4 + // CPUCFG R5, R4 = 0x00006ca4 + WORD $0x00006ca4 MOVW R4, ret+8(FP) RET From 3330b5e756f9a4ffd9510bc98b4c8b5b6d05b9c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20Sch=C3=B6nlaub?= Date: Wed, 19 Mar 2025 21:08:22 -0600 Subject: [PATCH 04/13] unix: support Readv, Preadv, Writev and Pwritev for darwin Darwin, starting with Big Sur, supports vectorized IO. Fixes golang/go#64710 Change-Id: Ic3a3c51009eab24f70665d8d3a145b328a7713c6 Reviewed-on: https://go-review.googlesource.com/c/sys/+/548795 LUCI-TryBot-Result: Go LUCI Auto-Submit: Ian Lance Taylor Reviewed-by: Ian Lance Taylor Reviewed-by: Cherry Mui --- unix/darwin_amd64_test.go | 4 + unix/darwin_arm64_test.go | 3 + unix/syscall_darwin.go | 149 +++++++++++++++++++++++++++++++++- unix/zsyscall_darwin_amd64.go | 84 +++++++++++++++++++ unix/zsyscall_darwin_amd64.s | 20 +++++ unix/zsyscall_darwin_arm64.go | 84 +++++++++++++++++++ unix/zsyscall_darwin_arm64.s | 20 +++++ 7 files changed, 363 insertions(+), 1 deletion(-) diff --git a/unix/darwin_amd64_test.go b/unix/darwin_amd64_test.go index f89b50e717..a35f64e770 100644 --- a/unix/darwin_amd64_test.go +++ b/unix/darwin_amd64_test.go @@ -102,14 +102,17 @@ var darwinTests = [...]darwinTest{ {"pipe", libc_pipe_trampoline_addr}, {"poll", libc_poll_trampoline_addr}, {"pread", libc_pread_trampoline_addr}, + {"preadv", libc_preadv_trampoline_addr}, {"pthread_chdir_np", libc_pthread_chdir_np_trampoline_addr}, {"pthread_fchdir_np", libc_pthread_fchdir_np_trampoline_addr}, {"ptrace", libc_ptrace_trampoline_addr}, {"pwrite", libc_pwrite_trampoline_addr}, + {"pwritev", libc_pwritev_trampoline_addr}, {"read", libc_read_trampoline_addr}, {"readdir_r", libc_readdir_r_trampoline_addr}, {"readlink", libc_readlink_trampoline_addr}, {"readlinkat", libc_readlinkat_trampoline_addr}, + {"readv", libc_readv_trampoline_addr}, {"recvfrom", libc_recvfrom_trampoline_addr}, {"recvmsg", libc_recvmsg_trampoline_addr}, {"removexattr", libc_removexattr_trampoline_addr}, @@ -162,4 +165,5 @@ var darwinTests = [...]darwinTest{ {"utimes", libc_utimes_trampoline_addr}, {"wait4", libc_wait4_trampoline_addr}, {"write", libc_write_trampoline_addr}, + {"writev", libc_writev_trampoline_addr}, } diff --git a/unix/darwin_arm64_test.go b/unix/darwin_arm64_test.go index 5fc4feb44b..740d6f7d1e 100644 --- a/unix/darwin_arm64_test.go +++ b/unix/darwin_arm64_test.go @@ -102,6 +102,7 @@ var darwinTests = [...]darwinTest{ {"pipe", libc_pipe_trampoline_addr}, {"poll", libc_poll_trampoline_addr}, {"pread", libc_pread_trampoline_addr}, + {"preadv", libc_preadv_trampoline_addr}, {"pthread_chdir_np", libc_pthread_chdir_np_trampoline_addr}, {"pthread_fchdir_np", libc_pthread_fchdir_np_trampoline_addr}, {"ptrace", libc_ptrace_trampoline_addr}, @@ -110,6 +111,7 @@ var darwinTests = [...]darwinTest{ {"readdir_r", libc_readdir_r_trampoline_addr}, {"readlink", libc_readlink_trampoline_addr}, {"readlinkat", libc_readlinkat_trampoline_addr}, + {"readv", libc_readv_trampoline_addr}, {"recvfrom", libc_recvfrom_trampoline_addr}, {"recvmsg", libc_recvmsg_trampoline_addr}, {"removexattr", libc_removexattr_trampoline_addr}, @@ -162,4 +164,5 @@ var darwinTests = [...]darwinTest{ {"utimes", libc_utimes_trampoline_addr}, {"wait4", libc_wait4_trampoline_addr}, {"write", libc_write_trampoline_addr}, + {"writev", libc_writev_trampoline_addr}, } diff --git a/unix/syscall_darwin.go b/unix/syscall_darwin.go index 099867deed..798f61ad3b 100644 --- a/unix/syscall_darwin.go +++ b/unix/syscall_darwin.go @@ -602,7 +602,150 @@ func Connectx(fd int, srcIf uint32, srcAddr, dstAddr Sockaddr, associd SaeAssocI return } -//sys connectx(fd int, endpoints *SaEndpoints, associd SaeAssocID, flags uint32, iov []Iovec, n *uintptr, connid *SaeConnID) (err error) +// sys connectx(fd int, endpoints *SaEndpoints, associd SaeAssocID, flags uint32, iov []Iovec, n *uintptr, connid *SaeConnID) (err error) +const minIovec = 8 + +func Readv(fd int, iovs [][]byte) (n int, err error) { + if !darwinKernelVersionMin(11, 0, 0) { + return 0, ENOSYS + } + + iovecs := make([]Iovec, 0, minIovec) + iovecs = appendBytes(iovecs, iovs) + n, err = readv(fd, iovecs) + readvRacedetect(iovecs, n, err) + return n, err +} + +func Preadv(fd int, iovs [][]byte, offset int64) (n int, err error) { + if !darwinKernelVersionMin(11, 0, 0) { + return 0, ENOSYS + } + iovecs := make([]Iovec, 0, minIovec) + iovecs = appendBytes(iovecs, iovs) + n, err = preadv(fd, iovecs, offset) + readvRacedetect(iovecs, n, err) + return n, err +} + +func Writev(fd int, iovs [][]byte) (n int, err error) { + if !darwinKernelVersionMin(11, 0, 0) { + return 0, ENOSYS + } + + iovecs := make([]Iovec, 0, minIovec) + iovecs = appendBytes(iovecs, iovs) + if raceenabled { + raceReleaseMerge(unsafe.Pointer(&ioSync)) + } + n, err = writev(fd, iovecs) + writevRacedetect(iovecs, n) + return n, err +} + +func Pwritev(fd int, iovs [][]byte, offset int64) (n int, err error) { + if !darwinKernelVersionMin(11, 0, 0) { + return 0, ENOSYS + } + + iovecs := make([]Iovec, 0, minIovec) + iovecs = appendBytes(iovecs, iovs) + if raceenabled { + raceReleaseMerge(unsafe.Pointer(&ioSync)) + } + n, err = pwritev(fd, iovecs, offset) + writevRacedetect(iovecs, n) + return n, err +} + +func appendBytes(vecs []Iovec, bs [][]byte) []Iovec { + for _, b := range bs { + var v Iovec + v.SetLen(len(b)) + if len(b) > 0 { + v.Base = &b[0] + } else { + v.Base = (*byte)(unsafe.Pointer(&_zero)) + } + vecs = append(vecs, v) + } + return vecs +} + +func writevRacedetect(iovecs []Iovec, n int) { + if !raceenabled { + return + } + for i := 0; n > 0 && i < len(iovecs); i++ { + m := int(iovecs[i].Len) + if m > n { + m = n + } + n -= m + if m > 0 { + raceReadRange(unsafe.Pointer(iovecs[i].Base), m) + } + } +} + +func readvRacedetect(iovecs []Iovec, n int, err error) { + if !raceenabled { + return + } + for i := 0; n > 0 && i < len(iovecs); i++ { + m := int(iovecs[i].Len) + if m > n { + m = n + } + n -= m + if m > 0 { + raceWriteRange(unsafe.Pointer(iovecs[i].Base), m) + } + } + if err == nil { + raceAcquire(unsafe.Pointer(&ioSync)) + } +} + +func darwinMajorMinPatch() (maj, min, patch int, err error) { + var un Utsname + err = Uname(&un) + if err != nil { + return + } + + var mmp [3]int + c := 0 +Loop: + for _, b := range un.Release[:] { + switch { + case b >= '0' && b <= '9': + mmp[c] = 10*mmp[c] + int(b-'0') + case b == '.': + c++ + if c > 2 { + return 0, 0, 0, ENOTSUP + } + case b == 0: + break Loop + default: + return 0, 0, 0, ENOTSUP + } + } + if c != 2 { + return 0, 0, 0, ENOTSUP + } + return mmp[0], mmp[1], mmp[2], nil +} + +func darwinKernelVersionMin(maj, min, patch int) bool { + actualMaj, actualMin, actualPatch, err := darwinMajorMinPatch() + if err != nil { + return false + } + return actualMaj > maj || actualMaj == maj && (actualMin > min || actualMin == min && actualPatch >= patch) +} + //sys sendfile(infd int, outfd int, offset int64, len *int64, hdtr unsafe.Pointer, flags int) (err error) //sys shmat(id int, addr uintptr, flag int) (ret uintptr, err error) @@ -705,3 +848,7 @@ func Connectx(fd int, srcIf uint32, srcAddr, dstAddr Sockaddr, associd SaeAssocI //sys write(fd int, p []byte) (n int, err error) //sys mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error) //sys munmap(addr uintptr, length uintptr) (err error) +//sys readv(fd int, iovecs []Iovec) (n int, err error) +//sys preadv(fd int, iovecs []Iovec, offset int64) (n int, err error) +//sys writev(fd int, iovecs []Iovec) (n int, err error) +//sys pwritev(fd int, iovecs []Iovec, offset int64) (n int, err error) diff --git a/unix/zsyscall_darwin_amd64.go b/unix/zsyscall_darwin_amd64.go index 24b346e1a3..813c05b664 100644 --- a/unix/zsyscall_darwin_amd64.go +++ b/unix/zsyscall_darwin_amd64.go @@ -2512,6 +2512,90 @@ var libc_munmap_trampoline_addr uintptr // THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT +func readv(fd int, iovecs []Iovec) (n int, err error) { + var _p0 unsafe.Pointer + if len(iovecs) > 0 { + _p0 = unsafe.Pointer(&iovecs[0]) + } else { + _p0 = unsafe.Pointer(&_zero) + } + r0, _, e1 := syscall_syscall(libc_readv_trampoline_addr, uintptr(fd), uintptr(_p0), uintptr(len(iovecs))) + n = int(r0) + if e1 != 0 { + err = errnoErr(e1) + } + return +} + +var libc_readv_trampoline_addr uintptr + +//go:cgo_import_dynamic libc_readv readv "/usr/lib/libSystem.B.dylib" + +// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT + +func preadv(fd int, iovecs []Iovec, offset int64) (n int, err error) { + var _p0 unsafe.Pointer + if len(iovecs) > 0 { + _p0 = unsafe.Pointer(&iovecs[0]) + } else { + _p0 = unsafe.Pointer(&_zero) + } + r0, _, e1 := syscall_syscall6(libc_preadv_trampoline_addr, uintptr(fd), uintptr(_p0), uintptr(len(iovecs)), uintptr(offset), 0, 0) + n = int(r0) + if e1 != 0 { + err = errnoErr(e1) + } + return +} + +var libc_preadv_trampoline_addr uintptr + +//go:cgo_import_dynamic libc_preadv preadv "/usr/lib/libSystem.B.dylib" + +// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT + +func writev(fd int, iovecs []Iovec) (n int, err error) { + var _p0 unsafe.Pointer + if len(iovecs) > 0 { + _p0 = unsafe.Pointer(&iovecs[0]) + } else { + _p0 = unsafe.Pointer(&_zero) + } + r0, _, e1 := syscall_syscall(libc_writev_trampoline_addr, uintptr(fd), uintptr(_p0), uintptr(len(iovecs))) + n = int(r0) + if e1 != 0 { + err = errnoErr(e1) + } + return +} + +var libc_writev_trampoline_addr uintptr + +//go:cgo_import_dynamic libc_writev writev "/usr/lib/libSystem.B.dylib" + +// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT + +func pwritev(fd int, iovecs []Iovec, offset int64) (n int, err error) { + var _p0 unsafe.Pointer + if len(iovecs) > 0 { + _p0 = unsafe.Pointer(&iovecs[0]) + } else { + _p0 = unsafe.Pointer(&_zero) + } + r0, _, e1 := syscall_syscall6(libc_pwritev_trampoline_addr, uintptr(fd), uintptr(_p0), uintptr(len(iovecs)), uintptr(offset), 0, 0) + n = int(r0) + if e1 != 0 { + err = errnoErr(e1) + } + return +} + +var libc_pwritev_trampoline_addr uintptr + +//go:cgo_import_dynamic libc_pwritev pwritev "/usr/lib/libSystem.B.dylib" + +// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT + func Fstat(fd int, stat *Stat_t) (err error) { _, _, e1 := syscall_syscall(libc_fstat64_trampoline_addr, uintptr(fd), uintptr(unsafe.Pointer(stat)), 0) if e1 != 0 { diff --git a/unix/zsyscall_darwin_amd64.s b/unix/zsyscall_darwin_amd64.s index ebd213100b..fda328582b 100644 --- a/unix/zsyscall_darwin_amd64.s +++ b/unix/zsyscall_darwin_amd64.s @@ -738,6 +738,26 @@ TEXT libc_munmap_trampoline<>(SB),NOSPLIT,$0-0 GLOBL ·libc_munmap_trampoline_addr(SB), RODATA, $8 DATA ·libc_munmap_trampoline_addr(SB)/8, $libc_munmap_trampoline<>(SB) +TEXT libc_readv_trampoline<>(SB),NOSPLIT,$0-0 + JMP libc_readv(SB) +GLOBL ·libc_readv_trampoline_addr(SB), RODATA, $8 +DATA ·libc_readv_trampoline_addr(SB)/8, $libc_readv_trampoline<>(SB) + +TEXT libc_preadv_trampoline<>(SB),NOSPLIT,$0-0 + JMP libc_preadv(SB) +GLOBL ·libc_preadv_trampoline_addr(SB), RODATA, $8 +DATA ·libc_preadv_trampoline_addr(SB)/8, $libc_preadv_trampoline<>(SB) + +TEXT libc_writev_trampoline<>(SB),NOSPLIT,$0-0 + JMP libc_writev(SB) +GLOBL ·libc_writev_trampoline_addr(SB), RODATA, $8 +DATA ·libc_writev_trampoline_addr(SB)/8, $libc_writev_trampoline<>(SB) + +TEXT libc_pwritev_trampoline<>(SB),NOSPLIT,$0-0 + JMP libc_pwritev(SB) +GLOBL ·libc_pwritev_trampoline_addr(SB), RODATA, $8 +DATA ·libc_pwritev_trampoline_addr(SB)/8, $libc_pwritev_trampoline<>(SB) + TEXT libc_fstat64_trampoline<>(SB),NOSPLIT,$0-0 JMP libc_fstat64(SB) GLOBL ·libc_fstat64_trampoline_addr(SB), RODATA, $8 diff --git a/unix/zsyscall_darwin_arm64.go b/unix/zsyscall_darwin_arm64.go index 824b9c2d5e..e6f58f3c6f 100644 --- a/unix/zsyscall_darwin_arm64.go +++ b/unix/zsyscall_darwin_arm64.go @@ -2512,6 +2512,90 @@ var libc_munmap_trampoline_addr uintptr // THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT +func readv(fd int, iovecs []Iovec) (n int, err error) { + var _p0 unsafe.Pointer + if len(iovecs) > 0 { + _p0 = unsafe.Pointer(&iovecs[0]) + } else { + _p0 = unsafe.Pointer(&_zero) + } + r0, _, e1 := syscall_syscall(libc_readv_trampoline_addr, uintptr(fd), uintptr(_p0), uintptr(len(iovecs))) + n = int(r0) + if e1 != 0 { + err = errnoErr(e1) + } + return +} + +var libc_readv_trampoline_addr uintptr + +//go:cgo_import_dynamic libc_readv readv "/usr/lib/libSystem.B.dylib" + +// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT + +func preadv(fd int, iovecs []Iovec, offset int64) (n int, err error) { + var _p0 unsafe.Pointer + if len(iovecs) > 0 { + _p0 = unsafe.Pointer(&iovecs[0]) + } else { + _p0 = unsafe.Pointer(&_zero) + } + r0, _, e1 := syscall_syscall6(libc_preadv_trampoline_addr, uintptr(fd), uintptr(_p0), uintptr(len(iovecs)), uintptr(offset), 0, 0) + n = int(r0) + if e1 != 0 { + err = errnoErr(e1) + } + return +} + +var libc_preadv_trampoline_addr uintptr + +//go:cgo_import_dynamic libc_preadv preadv "/usr/lib/libSystem.B.dylib" + +// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT + +func writev(fd int, iovecs []Iovec) (n int, err error) { + var _p0 unsafe.Pointer + if len(iovecs) > 0 { + _p0 = unsafe.Pointer(&iovecs[0]) + } else { + _p0 = unsafe.Pointer(&_zero) + } + r0, _, e1 := syscall_syscall(libc_writev_trampoline_addr, uintptr(fd), uintptr(_p0), uintptr(len(iovecs))) + n = int(r0) + if e1 != 0 { + err = errnoErr(e1) + } + return +} + +var libc_writev_trampoline_addr uintptr + +//go:cgo_import_dynamic libc_writev writev "/usr/lib/libSystem.B.dylib" + +// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT + +func pwritev(fd int, iovecs []Iovec, offset int64) (n int, err error) { + var _p0 unsafe.Pointer + if len(iovecs) > 0 { + _p0 = unsafe.Pointer(&iovecs[0]) + } else { + _p0 = unsafe.Pointer(&_zero) + } + r0, _, e1 := syscall_syscall6(libc_pwritev_trampoline_addr, uintptr(fd), uintptr(_p0), uintptr(len(iovecs)), uintptr(offset), 0, 0) + n = int(r0) + if e1 != 0 { + err = errnoErr(e1) + } + return +} + +var libc_pwritev_trampoline_addr uintptr + +//go:cgo_import_dynamic libc_pwritev pwritev "/usr/lib/libSystem.B.dylib" + +// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT + func Fstat(fd int, stat *Stat_t) (err error) { _, _, e1 := syscall_syscall(libc_fstat_trampoline_addr, uintptr(fd), uintptr(unsafe.Pointer(stat)), 0) if e1 != 0 { diff --git a/unix/zsyscall_darwin_arm64.s b/unix/zsyscall_darwin_arm64.s index 4f178a2293..7f8998b905 100644 --- a/unix/zsyscall_darwin_arm64.s +++ b/unix/zsyscall_darwin_arm64.s @@ -738,6 +738,26 @@ TEXT libc_munmap_trampoline<>(SB),NOSPLIT,$0-0 GLOBL ·libc_munmap_trampoline_addr(SB), RODATA, $8 DATA ·libc_munmap_trampoline_addr(SB)/8, $libc_munmap_trampoline<>(SB) +TEXT libc_readv_trampoline<>(SB),NOSPLIT,$0-0 + JMP libc_readv(SB) +GLOBL ·libc_readv_trampoline_addr(SB), RODATA, $8 +DATA ·libc_readv_trampoline_addr(SB)/8, $libc_readv_trampoline<>(SB) + +TEXT libc_preadv_trampoline<>(SB),NOSPLIT,$0-0 + JMP libc_preadv(SB) +GLOBL ·libc_preadv_trampoline_addr(SB), RODATA, $8 +DATA ·libc_preadv_trampoline_addr(SB)/8, $libc_preadv_trampoline<>(SB) + +TEXT libc_writev_trampoline<>(SB),NOSPLIT,$0-0 + JMP libc_writev(SB) +GLOBL ·libc_writev_trampoline_addr(SB), RODATA, $8 +DATA ·libc_writev_trampoline_addr(SB)/8, $libc_writev_trampoline<>(SB) + +TEXT libc_pwritev_trampoline<>(SB),NOSPLIT,$0-0 + JMP libc_pwritev(SB) +GLOBL ·libc_pwritev_trampoline_addr(SB), RODATA, $8 +DATA ·libc_pwritev_trampoline_addr(SB)/8, $libc_pwritev_trampoline<>(SB) + TEXT libc_fstat_trampoline<>(SB),NOSPLIT,$0-0 JMP libc_fstat(SB) GLOBL ·libc_fstat_trampoline_addr(SB), RODATA, $8 From c175b6ba6781614eadf22a0727389d97e25f72ee Mon Sep 17 00:00:00 2001 From: database64128 Date: Mon, 17 Mar 2025 08:42:26 +0000 Subject: [PATCH 05/13] windows: add cmsghdr and pktinfo structures - CMSGHDR from ws2def.h, corresponds to Cmsghdr in unix - IN_PKTINFO from ws2ipdef.h, corresponds to InetPktinfo in unix - IN6_PKTINFO from ws2ipdef.h, corresponds to Inet6Pktinfo in unix Change-Id: I74f6812588859c3a6080e6675df28998fc435965 GitHub-Last-Rev: 7377c793c687a74012e0ff3ce458eb2d8717ea72 GitHub-Pull-Request: golang/sys#246 Reviewed-on: https://go-review.googlesource.com/c/sys/+/658175 Reviewed-by: Cherry Mui LUCI-TryBot-Result: Go LUCI Reviewed-by: Dmitri Shuralyov Reviewed-by: Alex Brainman --- windows/types_windows.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/windows/types_windows.go b/windows/types_windows.go index fac0309d08..ad67df2fdb 100644 --- a/windows/types_windows.go +++ b/windows/types_windows.go @@ -1157,6 +1157,22 @@ type WSAMsg struct { Flags uint32 } +type WSACMSGHDR struct { + Len uintptr + Level int32 + Type int32 +} + +type IN_PKTINFO struct { + Addr [4]byte + Ifindex uint32 +} + +type IN6_PKTINFO struct { + Addr [16]byte + Ifindex uint32 +} + // Flags for WSASocket const ( WSA_FLAG_OVERLAPPED = 0x01 From 1c3b72f1c1fa7f3a3a355c7a963cd1c958135d01 Mon Sep 17 00:00:00 2001 From: Mauri de Souza Meneguzzo Date: Mon, 24 Mar 2025 17:02:11 +0000 Subject: [PATCH 06/13] unix: update Linux kernel to 6.14 No new syscalls or constants in this release, just bumping the version number. Change-Id: I97f7e1092bb78d99342552ba18e1ea3cd40681f4 GitHub-Last-Rev: df5cca1a90e8d69769e164bcdaf165188bf04929 GitHub-Pull-Request: golang/sys#247 Reviewed-on: https://go-review.googlesource.com/c/sys/+/660375 Reviewed-by: Tobias Klauser Reviewed-by: Dmitri Shuralyov Reviewed-by: Cherry Mui LUCI-TryBot-Result: Go LUCI --- unix/linux/Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/unix/linux/Dockerfile b/unix/linux/Dockerfile index 2bbb21a37a..68fb9c3a5e 100644 --- a/unix/linux/Dockerfile +++ b/unix/linux/Dockerfile @@ -15,8 +15,8 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ # Get the git sources. If not cached, this takes O(5 minutes). WORKDIR /git RUN git config --global advice.detachedHead false -# Linux Kernel: Released 20 Jan 2025 -RUN git clone --branch v6.13 --depth 1 https://kernel.googlesource.com/pub/scm/linux/kernel/git/torvalds/linux +# Linux Kernel: Released 24 Mar 2025 +RUN git clone --branch v6.14 --depth 1 https://kernel.googlesource.com/pub/scm/linux/kernel/git/torvalds/linux # GNU C library: Released 29 Jan 2025 RUN git clone --branch release/2.41/master --depth 1 https://sourceware.org/git/glibc.git From 1b2bd6bb49091efddfc5ca87435bcea9e2090720 Mon Sep 17 00:00:00 2001 From: Douglas Danger Manley Date: Sat, 22 Mar 2025 17:40:25 -0400 Subject: [PATCH 07/13] windows: replace all StringToUTF16 calls with UTF16FromString `StringToUTF16` is deprecated and will panic if given an "invalid" string (in particular, one that has a null byte in it). The replacement function is `UTF16FromString`, and it returns an error if there was a problem. This change replaces all uses of `StringToUTF16` with `UTF16FromString`. The `service` struct now no longer stores a `string` name but rather a `*uint16` pointer to the name. It should not be possible to panic due to UTF16 string conversion at this point. Fixes golang/go#73006 Change-Id: Idce9cdbb4651fef8481f0cad19b5df0314fd4277 Reviewed-on: https://go-review.googlesource.com/c/sys/+/659936 Reviewed-by: Carlos Amedee Auto-Submit: Carlos Amedee LUCI-TryBot-Result: Go LUCI Reviewed-by: Alex Brainman TryBot-Result: Gopher Robot Reviewed-by: Dmitri Shuralyov --- windows/registry/key.go | 13 ++++++++-- windows/registry/value.go | 6 ++++- windows/svc/eventlog/log.go | 20 ++++++++++++--- windows/svc/mgr/config.go | 34 +++++++++++++++++++++---- windows/svc/mgr/mgr.go | 49 +++++++++++++++++++++++++++++++------ windows/svc/mgr/recovery.go | 14 +++++++++-- windows/svc/mgr/service.go | 6 ++++- windows/svc/service.go | 20 +++++++++------ 8 files changed, 132 insertions(+), 30 deletions(-) diff --git a/windows/registry/key.go b/windows/registry/key.go index fd8632444e..39aeeb644f 100644 --- a/windows/registry/key.go +++ b/windows/registry/key.go @@ -164,7 +164,12 @@ loopItems: func CreateKey(k Key, path string, access uint32) (newk Key, openedExisting bool, err error) { var h syscall.Handle var d uint32 - err = regCreateKeyEx(syscall.Handle(k), syscall.StringToUTF16Ptr(path), + var pathPointer *uint16 + pathPointer, err = syscall.UTF16PtrFromString(path) + if err != nil { + return 0, false, err + } + err = regCreateKeyEx(syscall.Handle(k), pathPointer, 0, nil, _REG_OPTION_NON_VOLATILE, access, nil, &h, &d) if err != nil { return 0, false, err @@ -174,7 +179,11 @@ func CreateKey(k Key, path string, access uint32) (newk Key, openedExisting bool // DeleteKey deletes the subkey path of key k and its values. func DeleteKey(k Key, path string) error { - return regDeleteKey(syscall.Handle(k), syscall.StringToUTF16Ptr(path)) + pathPointer, err := syscall.UTF16PtrFromString(path) + if err != nil { + return err + } + return regDeleteKey(syscall.Handle(k), pathPointer) } // A KeyInfo describes the statistics of a key. It is returned by Stat. diff --git a/windows/registry/value.go b/windows/registry/value.go index 74db26b94d..a1bcbb2362 100644 --- a/windows/registry/value.go +++ b/windows/registry/value.go @@ -340,7 +340,11 @@ func (k Key) SetBinaryValue(name string, value []byte) error { // DeleteValue removes a named value from the key k. func (k Key) DeleteValue(name string) error { - return regDeleteValue(syscall.Handle(k), syscall.StringToUTF16Ptr(name)) + namePointer, err := syscall.UTF16PtrFromString(name) + if err != nil { + return err + } + return regDeleteValue(syscall.Handle(k), namePointer) } // ReadValueNames returns the value names of key k. diff --git a/windows/svc/eventlog/log.go b/windows/svc/eventlog/log.go index f279444d98..ad40c2f48b 100644 --- a/windows/svc/eventlog/log.go +++ b/windows/svc/eventlog/log.go @@ -29,11 +29,19 @@ func OpenRemote(host, source string) (*Log, error) { if source == "" { return nil, errors.New("Specify event log source") } - var s *uint16 + var hostPointer *uint16 if host != "" { - s = syscall.StringToUTF16Ptr(host) + var err error + hostPointer, err = syscall.UTF16PtrFromString(host) + if err != nil { + return nil, err + } } - h, err := windows.RegisterEventSource(s, syscall.StringToUTF16Ptr(source)) + sourcePointer, err := syscall.UTF16PtrFromString(source) + if err != nil { + return nil, err + } + h, err := windows.RegisterEventSource(hostPointer, sourcePointer) if err != nil { return nil, err } @@ -46,7 +54,11 @@ func (l *Log) Close() error { } func (l *Log) report(etype uint16, eid uint32, msg string) error { - ss := []*uint16{syscall.StringToUTF16Ptr(msg)} + msgPointer, err := syscall.UTF16PtrFromString(msg) + if err != nil { + return err + } + ss := []*uint16{msgPointer} return windows.ReportEvent(l.Handle, etype, 0, eid, 0, 1, 0, &ss[0], nil) } diff --git a/windows/svc/mgr/config.go b/windows/svc/mgr/config.go index 3c7ba08f58..ec01f96eba 100644 --- a/windows/svc/mgr/config.go +++ b/windows/svc/mgr/config.go @@ -121,7 +121,11 @@ func (s *Service) Config() (Config, error) { } func updateDescription(handle windows.Handle, desc string) error { - d := windows.SERVICE_DESCRIPTION{Description: toPtr(desc)} + descPointer, err := toPtr(desc) + if err != nil { + return err + } + d := windows.SERVICE_DESCRIPTION{Description: descPointer} return windows.ChangeServiceConfig2(handle, windows.SERVICE_CONFIG_DESCRIPTION, (*byte)(unsafe.Pointer(&d))) } @@ -141,10 +145,30 @@ func updateStartUp(handle windows.Handle, isDelayed bool) error { // UpdateConfig updates service s configuration parameters. func (s *Service) UpdateConfig(c Config) error { - err := windows.ChangeServiceConfig(s.Handle, c.ServiceType, c.StartType, - c.ErrorControl, toPtr(c.BinaryPathName), toPtr(c.LoadOrderGroup), - nil, toStringBlock(c.Dependencies), toPtr(c.ServiceStartName), - toPtr(c.Password), toPtr(c.DisplayName)) + binaryPathNamePointer, err := toPtr(c.BinaryPathName) + if err != nil { + return err + } + loadOrderGroupPointer, err := toPtr(c.LoadOrderGroup) + if err != nil { + return err + } + serviceStartNamePointer, err := toPtr(c.ServiceStartName) + if err != nil { + return err + } + passwordPointer, err := toPtr(c.Password) + if err != nil { + return err + } + displayNamePointer, err := toPtr(c.DisplayName) + if err != nil { + return err + } + err = windows.ChangeServiceConfig(s.Handle, c.ServiceType, c.StartType, + c.ErrorControl, binaryPathNamePointer, loadOrderGroupPointer, + nil, toStringBlock(c.Dependencies), serviceStartNamePointer, + passwordPointer, displayNamePointer) if err != nil { return err } diff --git a/windows/svc/mgr/mgr.go b/windows/svc/mgr/mgr.go index dbfd729fec..74755940a1 100644 --- a/windows/svc/mgr/mgr.go +++ b/windows/svc/mgr/mgr.go @@ -34,7 +34,11 @@ func Connect() (*Mgr, error) { func ConnectRemote(host string) (*Mgr, error) { var s *uint16 if host != "" { - s = syscall.StringToUTF16Ptr(host) + var err error + s, err = syscall.UTF16PtrFromString(host) + if err != nil { + return nil, err + } } h, err := windows.OpenSCManager(s, nil, windows.SC_MANAGER_ALL_ACCESS) if err != nil { @@ -78,11 +82,11 @@ func (m *Mgr) LockStatus() (*LockStatus, error) { } } -func toPtr(s string) *uint16 { +func toPtr(s string) (*uint16, error) { if len(s) == 0 { - return nil + return nil, nil } - return syscall.StringToUTF16Ptr(s) + return syscall.UTF16PtrFromString(s) } // toStringBlock terminates strings in ss with 0, and then @@ -122,10 +126,34 @@ func (m *Mgr) CreateService(name, exepath string, c Config, args ...string) (*Se for _, v := range args { s += " " + syscall.EscapeArg(v) } - h, err := windows.CreateService(m.Handle, toPtr(name), toPtr(c.DisplayName), + namePointer, err := toPtr(name) + if err != nil { + return nil, err + } + displayNamePointer, err := toPtr(c.DisplayName) + if err != nil { + return nil, err + } + sPointer, err := toPtr(s) + if err != nil { + return nil, err + } + loadOrderGroupPointer, err := toPtr(c.LoadOrderGroup) + if err != nil { + return nil, err + } + serviceStartNamePointer, err := toPtr(c.ServiceStartName) + if err != nil { + return nil, err + } + passwordPointer, err := toPtr(c.Password) + if err != nil { + return nil, err + } + h, err := windows.CreateService(m.Handle, namePointer, displayNamePointer, windows.SERVICE_ALL_ACCESS, c.ServiceType, - c.StartType, c.ErrorControl, toPtr(s), toPtr(c.LoadOrderGroup), - nil, toStringBlock(c.Dependencies), toPtr(c.ServiceStartName), toPtr(c.Password)) + c.StartType, c.ErrorControl, sPointer, loadOrderGroupPointer, + nil, toStringBlock(c.Dependencies), serviceStartNamePointer, passwordPointer) if err != nil { return nil, err } @@ -159,7 +187,12 @@ func (m *Mgr) CreateService(name, exepath string, c Config, args ...string) (*Se // OpenService retrieves access to service name, so it can // be interrogated and controlled. func (m *Mgr) OpenService(name string) (*Service, error) { - h, err := windows.OpenService(m.Handle, syscall.StringToUTF16Ptr(name), windows.SERVICE_ALL_ACCESS) + namePointer, err := syscall.UTF16PtrFromString(name) + if err != nil { + return nil, err + } + + h, err := windows.OpenService(m.Handle, namePointer, windows.SERVICE_ALL_ACCESS) if err != nil { return nil, err } diff --git a/windows/svc/mgr/recovery.go b/windows/svc/mgr/recovery.go index ef2a687840..1a07374838 100644 --- a/windows/svc/mgr/recovery.go +++ b/windows/svc/mgr/recovery.go @@ -99,8 +99,13 @@ func (s *Service) ResetPeriod() (uint32, error) { // SetRebootMessage sets service s reboot message. // If msg is "", the reboot message is deleted and no message is broadcast. func (s *Service) SetRebootMessage(msg string) error { + msgPointer, err := syscall.UTF16PtrFromString(msg) + if err != nil { + return err + } + rActions := windows.SERVICE_FAILURE_ACTIONS{ - RebootMsg: syscall.StringToUTF16Ptr(msg), + RebootMsg: msgPointer, } return windows.ChangeServiceConfig2(s.Handle, windows.SERVICE_CONFIG_FAILURE_ACTIONS, (*byte)(unsafe.Pointer(&rActions))) } @@ -118,8 +123,13 @@ func (s *Service) RebootMessage() (string, error) { // SetRecoveryCommand sets the command line of the process to execute in response to the RunCommand service controller action. // If cmd is "", the command is deleted and no program is run when the service fails. func (s *Service) SetRecoveryCommand(cmd string) error { + cmdPointer, err := syscall.UTF16PtrFromString(cmd) + if err != nil { + return err + } + rActions := windows.SERVICE_FAILURE_ACTIONS{ - Command: syscall.StringToUTF16Ptr(cmd), + Command: cmdPointer, } return windows.ChangeServiceConfig2(s.Handle, windows.SERVICE_CONFIG_FAILURE_ACTIONS, (*byte)(unsafe.Pointer(&rActions))) } diff --git a/windows/svc/mgr/service.go b/windows/svc/mgr/service.go index c9740ef0ce..9f59eab754 100644 --- a/windows/svc/mgr/service.go +++ b/windows/svc/mgr/service.go @@ -37,7 +37,11 @@ func (s *Service) Start(args ...string) error { if len(args) > 0 { vs := make([]*uint16, len(args)) for i := range vs { - vs[i] = syscall.StringToUTF16Ptr(args[i]) + argPointer, err := syscall.UTF16PtrFromString(args[i]) + if err != nil { + return err + } + vs[i] = argPointer } p = &vs[0] } diff --git a/windows/svc/service.go b/windows/svc/service.go index c4f74924dd..a9b1c192d3 100644 --- a/windows/svc/service.go +++ b/windows/svc/service.go @@ -132,10 +132,10 @@ type ctlEvent struct { // service provides access to windows service api. type service struct { - name string - h windows.Handle - c chan ctlEvent - handler Handler + namePointer *uint16 + h windows.Handle + c chan ctlEvent + handler Handler } type exitCode struct { @@ -209,7 +209,7 @@ var theService service // This is, unfortunately, a global, which means only one // serviceMain is the entry point called by the service manager, registered earlier by // the call to StartServiceCtrlDispatcher. func serviceMain(argc uint32, argv **uint16) uintptr { - handle, err := windows.RegisterServiceCtrlHandlerEx(windows.StringToUTF16Ptr(theService.name), ctlHandlerCallback, 0) + handle, err := windows.RegisterServiceCtrlHandlerEx(theService.namePointer, ctlHandlerCallback, 0) if sysErr, ok := err.(windows.Errno); ok { return uintptr(sysErr) } else if err != nil { @@ -280,15 +280,21 @@ loop: // Run executes service name by calling appropriate handler function. func Run(name string, handler Handler) error { + // Check to make sure that the service name is valid. + namePointer, err := windows.UTF16PtrFromString(name) + if err != nil { + return err + } + initCallbacks.Do(func() { ctlHandlerCallback = windows.NewCallback(ctlHandler) serviceMainCallback = windows.NewCallback(serviceMain) }) - theService.name = name + theService.namePointer = namePointer theService.handler = handler theService.c = make(chan ctlEvent) t := []windows.SERVICE_TABLE_ENTRY{ - {ServiceName: windows.StringToUTF16Ptr(theService.name), ServiceProc: serviceMainCallback}, + {ServiceName: namePointer, ServiceProc: serviceMainCallback}, {ServiceName: nil, ServiceProc: 0}, } return windows.StartServiceCtrlDispatcher(&t[0]) From 01aaa8342f9d6e36356d05d0baff28e64ee6367e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Tue, 1 Apr 2025 11:05:11 +0200 Subject: [PATCH 08/13] all: simplify code by using modern Go constructs Generated using modernize by running: go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./... Change-Id: Ifc7d61cf6735cc53f2bdf890a338961f55075af5 Reviewed-on: https://go-review.googlesource.com/c/sys/+/661975 Auto-Submit: Tobias Klauser Reviewed-by: Carlos Amedee LUCI-TryBot-Result: Go LUCI Reviewed-by: Dmitri Shuralyov --- cpu/parse.go | 4 +-- unix/dirent_test.go | 2 +- unix/fdset_test.go | 10 +++---- unix/internal/mkmerge/mkmerge.go | 4 +-- unix/internal/mkmerge/mkmerge_test.go | 2 +- unix/syscall_linux.go | 42 ++++++++++----------------- unix/syscall_linux_test.go | 6 ++-- unix/xattr_test.go | 17 ++--------- 8 files changed, 33 insertions(+), 54 deletions(-) diff --git a/cpu/parse.go b/cpu/parse.go index 762b63d688..56a7e1a176 100644 --- a/cpu/parse.go +++ b/cpu/parse.go @@ -13,7 +13,7 @@ import "strconv" // https://golang.org/cl/209597. func parseRelease(rel string) (major, minor, patch int, ok bool) { // Strip anything after a dash or plus. - for i := 0; i < len(rel); i++ { + for i := range len(rel) { if rel[i] == '-' || rel[i] == '+' { rel = rel[:i] break @@ -21,7 +21,7 @@ func parseRelease(rel string) (major, minor, patch int, ok bool) { } next := func() (int, bool) { - for i := 0; i < len(rel); i++ { + for i := range len(rel) { if rel[i] == '.' { ver, err := strconv.Atoi(rel[:i]) rel = rel[i+1:] diff --git a/unix/dirent_test.go b/unix/dirent_test.go index e47d091d7f..f209fa1fc7 100644 --- a/unix/dirent_test.go +++ b/unix/dirent_test.go @@ -107,7 +107,7 @@ func TestDirentRepeat(t *testing.T) { d := t.TempDir() var files []string - for i := 0; i < N; i++ { + for i := range N { files = append(files, fmt.Sprintf("file%d", i)) } for _, file := range files { diff --git a/unix/fdset_test.go b/unix/fdset_test.go index 26d05c7a52..7d08f691dc 100644 --- a/unix/fdset_test.go +++ b/unix/fdset_test.go @@ -15,21 +15,21 @@ import ( func TestFdSet(t *testing.T) { var fdSet unix.FdSet fdSet.Zero() - for fd := 0; fd < unix.FD_SETSIZE; fd++ { + for fd := range unix.FD_SETSIZE { if fdSet.IsSet(fd) { t.Fatalf("Zero did not clear fd %d", fd) } fdSet.Set(fd) } - for fd := 0; fd < unix.FD_SETSIZE; fd++ { + for fd := range unix.FD_SETSIZE { if !fdSet.IsSet(fd) { t.Fatalf("IsSet(%d): expected true, got false", fd) } } fdSet.Zero() - for fd := 0; fd < unix.FD_SETSIZE; fd++ { + for fd := range unix.FD_SETSIZE { if fdSet.IsSet(fd) { t.Fatalf("Zero did not clear fd %d", fd) } @@ -39,7 +39,7 @@ func TestFdSet(t *testing.T) { fdSet.Set(fd) } - for fd := 0; fd < unix.FD_SETSIZE; fd++ { + for fd := range unix.FD_SETSIZE { if fd&0x1 == 0x1 { if !fdSet.IsSet(fd) { t.Fatalf("IsSet(%d): expected true, got false", fd) @@ -55,7 +55,7 @@ func TestFdSet(t *testing.T) { fdSet.Clear(fd) } - for fd := 0; fd < unix.FD_SETSIZE; fd++ { + for fd := range unix.FD_SETSIZE { if fdSet.IsSet(fd) { t.Fatalf("Clear(%d) did not clear fd", fd) } diff --git a/unix/internal/mkmerge/mkmerge.go b/unix/internal/mkmerge/mkmerge.go index 52f1d12bb2..06a6900647 100644 --- a/unix/internal/mkmerge/mkmerge.go +++ b/unix/internal/mkmerge/mkmerge.go @@ -137,7 +137,7 @@ type filterFn func(codeElem) bool // filter parses and filters Go source code from src, removing top // level declarations using keep as predicate. // For src parameter, please see docs for parser.ParseFile. -func filter(src interface{}, keep filterFn) ([]byte, error) { +func filter(src any, keep filterFn) ([]byte, error) { // Parse the src into an ast fset := token.NewFileSet() f, err := parser.ParseFile(fset, "", src, parser.ParseComments) @@ -243,7 +243,7 @@ func getCommonSet(files []srcFile) (*codeSet, error) { // getCodeSet returns the set of all top-level consts, types, and funcs from src. // src must be string, []byte, or io.Reader (see go/parser.ParseFile docs) -func getCodeSet(src interface{}) (*codeSet, error) { +func getCodeSet(src any) (*codeSet, error) { set := newCodeSet() fset := token.NewFileSet() diff --git a/unix/internal/mkmerge/mkmerge_test.go b/unix/internal/mkmerge/mkmerge_test.go index 2566dad9e0..29b584dd21 100644 --- a/unix/internal/mkmerge/mkmerge_test.go +++ b/unix/internal/mkmerge/mkmerge_test.go @@ -498,7 +498,7 @@ func diffLines(t *testing.T, got, expected []byte) { func addLineNr(src []byte) []byte { lines := bytes.Split(src, []byte("\n")) for i, line := range lines { - lines[i] = []byte(fmt.Sprintf("%d: %s", i+1, line)) + lines[i] = fmt.Appendf(nil, "%d: %s", i+1, line) } return bytes.Join(lines, []byte("\n")) } diff --git a/unix/syscall_linux.go b/unix/syscall_linux.go index 230a94549a..4958a65708 100644 --- a/unix/syscall_linux.go +++ b/unix/syscall_linux.go @@ -13,6 +13,7 @@ package unix import ( "encoding/binary" + "slices" "strconv" "syscall" "time" @@ -417,7 +418,7 @@ func (sa *SockaddrUnix) sockaddr() (unsafe.Pointer, _Socklen, error) { return nil, 0, EINVAL } sa.raw.Family = AF_UNIX - for i := 0; i < n; i++ { + for i := range n { sa.raw.Path[i] = int8(name[i]) } // length is family (uint16), name, NUL. @@ -507,7 +508,7 @@ func (sa *SockaddrL2) sockaddr() (unsafe.Pointer, _Socklen, error) { psm := (*[2]byte)(unsafe.Pointer(&sa.raw.Psm)) psm[0] = byte(sa.PSM) psm[1] = byte(sa.PSM >> 8) - for i := 0; i < len(sa.Addr); i++ { + for i := range len(sa.Addr) { sa.raw.Bdaddr[i] = sa.Addr[len(sa.Addr)-1-i] } cid := (*[2]byte)(unsafe.Pointer(&sa.raw.Cid)) @@ -589,11 +590,11 @@ func (sa *SockaddrCAN) sockaddr() (unsafe.Pointer, _Socklen, error) { sa.raw.Family = AF_CAN sa.raw.Ifindex = int32(sa.Ifindex) rx := (*[4]byte)(unsafe.Pointer(&sa.RxID)) - for i := 0; i < 4; i++ { + for i := range 4 { sa.raw.Addr[i] = rx[i] } tx := (*[4]byte)(unsafe.Pointer(&sa.TxID)) - for i := 0; i < 4; i++ { + for i := range 4 { sa.raw.Addr[i+4] = tx[i] } return unsafe.Pointer(&sa.raw), SizeofSockaddrCAN, nil @@ -618,11 +619,11 @@ func (sa *SockaddrCANJ1939) sockaddr() (unsafe.Pointer, _Socklen, error) { sa.raw.Family = AF_CAN sa.raw.Ifindex = int32(sa.Ifindex) n := (*[8]byte)(unsafe.Pointer(&sa.Name)) - for i := 0; i < 8; i++ { + for i := range 8 { sa.raw.Addr[i] = n[i] } p := (*[4]byte)(unsafe.Pointer(&sa.PGN)) - for i := 0; i < 4; i++ { + for i := range 4 { sa.raw.Addr[i+8] = p[i] } sa.raw.Addr[12] = sa.Addr @@ -911,7 +912,7 @@ func (sa *SockaddrIUCV) sockaddr() (unsafe.Pointer, _Socklen, error) { // These are EBCDIC encoded by the kernel, but we still need to pad them // with blanks. Initializing with blanks allows the caller to feed in either // a padded or an unpadded string. - for i := 0; i < 8; i++ { + for i := range 8 { sa.raw.Nodeid[i] = ' ' sa.raw.User_id[i] = ' ' sa.raw.Name[i] = ' ' @@ -1148,7 +1149,7 @@ func anyToSockaddr(fd int, rsa *RawSockaddrAny) (Sockaddr, error) { var user [8]byte var name [8]byte - for i := 0; i < 8; i++ { + for i := range 8 { user[i] = byte(pp.User_id[i]) name[i] = byte(pp.Name[i]) } @@ -1173,11 +1174,11 @@ func anyToSockaddr(fd int, rsa *RawSockaddrAny) (Sockaddr, error) { Ifindex: int(pp.Ifindex), } name := (*[8]byte)(unsafe.Pointer(&sa.Name)) - for i := 0; i < 8; i++ { + for i := range 8 { name[i] = pp.Addr[i] } pgn := (*[4]byte)(unsafe.Pointer(&sa.PGN)) - for i := 0; i < 4; i++ { + for i := range 4 { pgn[i] = pp.Addr[i+8] } addr := (*[1]byte)(unsafe.Pointer(&sa.Addr)) @@ -1188,11 +1189,11 @@ func anyToSockaddr(fd int, rsa *RawSockaddrAny) (Sockaddr, error) { Ifindex: int(pp.Ifindex), } rx := (*[4]byte)(unsafe.Pointer(&sa.RxID)) - for i := 0; i < 4; i++ { + for i := range 4 { rx[i] = pp.Addr[i] } tx := (*[4]byte)(unsafe.Pointer(&sa.TxID)) - for i := 0; i < 4; i++ { + for i := range 4 { tx[i] = pp.Addr[i+4] } return sa, nil @@ -2216,10 +2217,7 @@ func readvRacedetect(iovecs []Iovec, n int, err error) { return } for i := 0; n > 0 && i < len(iovecs); i++ { - m := int(iovecs[i].Len) - if m > n { - m = n - } + m := min(int(iovecs[i].Len), n) n -= m if m > 0 { raceWriteRange(unsafe.Pointer(iovecs[i].Base), m) @@ -2270,10 +2268,7 @@ func writevRacedetect(iovecs []Iovec, n int) { return } for i := 0; n > 0 && i < len(iovecs); i++ { - m := int(iovecs[i].Len) - if m > n { - m = n - } + m := min(int(iovecs[i].Len), n) n -= m if m > 0 { raceReadRange(unsafe.Pointer(iovecs[i].Base), m) @@ -2320,12 +2315,7 @@ func isGroupMember(gid int) bool { return false } - for _, g := range groups { - if g == gid { - return true - } - } - return false + return slices.Contains(groups, gid) } func isCapDacOverrideSet() bool { diff --git a/unix/syscall_linux_test.go b/unix/syscall_linux_test.go index d4ed88726d..d3075ca74c 100644 --- a/unix/syscall_linux_test.go +++ b/unix/syscall_linux_test.go @@ -367,7 +367,7 @@ func TestTime(t *testing.T) { var now time.Time - for i := 0; i < 10; i++ { + for range 10 { ut, err = unix.Time(nil) if err != nil { t.Fatalf("Time: %v", err) @@ -555,7 +555,7 @@ func TestSchedSetaffinity(t *testing.T) { cpu = 1 if !oldMask.IsSet(cpu) { newMask.Zero() - for i := 0; i < len(oldMask); i++ { + for i := range len(oldMask) { if oldMask.IsSet(i) { newMask.Set(i) break @@ -878,7 +878,7 @@ func openMountByID(mountID int) (f *os.File, err error) { } defer mi.Close() bs := bufio.NewScanner(mi) - wantPrefix := []byte(fmt.Sprintf("%v ", mountID)) + wantPrefix := fmt.Appendf(nil, "%v ", mountID) for bs.Scan() { if !bytes.HasPrefix(bs.Bytes(), wantPrefix) { continue diff --git a/unix/xattr_test.go b/unix/xattr_test.go index dfa208f160..3b5111497c 100644 --- a/unix/xattr_test.go +++ b/unix/xattr_test.go @@ -10,6 +10,7 @@ import ( "os" "path/filepath" "runtime" + "slices" "strings" "testing" @@ -62,13 +63,7 @@ func TestXattr(t *testing.T) { // name and Listxattr doesn't return the namespace prefix. xattrWant = strings.TrimPrefix(xattrWant, "user.") } - found := false - for _, name := range xattrs { - if name == xattrWant { - found = true - break - } - } + found := slices.Contains(xattrs, xattrWant) if !found { t.Errorf("Listxattr did not return previously set attribute %q in attributes %v", xattrName, xattrs) @@ -170,13 +165,7 @@ func TestFdXattr(t *testing.T) { // name and Listxattr doesn't return the namespace prefix. xattrWant = strings.TrimPrefix(xattrWant, "user.") } - found := false - for _, name := range xattrs { - if name == xattrWant { - found = true - break - } - } + found := slices.Contains(xattrs, xattrWant) if !found { t.Errorf("Flistxattr did not return previously set attribute %q in attributes %v", xattrName, xattrs) From 6a85559a3f1bd2a5418354bf4bd7e306213a81bc Mon Sep 17 00:00:00 2001 From: thepudds Date: Sun, 6 Apr 2025 21:29:03 -0400 Subject: [PATCH 09/13] windows: fix dangling pointers in (*SECURITY_DESCRIPTOR).ToAbsolute Prior to this CL, a byte slice was allocated via make to use as the absoluteSD argument passed to the Windows API MakeAbsoluteSD. MakeAbsoluteSD then sets pointers outside the view of the GC, including pointers within absoluteSD that point to other chunks of memory we pass into MakeAbsoluteSD. CL 653856 recently allowed more make results to be stack allocated, which worsened the problems here and made it easier for those pointers in absoluteSD to become dangling pointers, though the core problems here existed before. This CL instead allocates absoluteSD as a proper SECURITY_DESCRIPTOR struct so that the GC can be aware of its pointers. We also verify the pointers are as we expect, and then set them explicitly in view of the GC. Updates golang/go#73199 Change-Id: Id8038d38a887bb8ff3ffc6eae603589b97e92cdc Reviewed-on: https://go-review.googlesource.com/c/sys/+/663355 LUCI-TryBot-Result: Go LUCI Reviewed-by: Keith Randall Reviewed-by: Dmitri Shuralyov Auto-Submit: Dmitri Shuralyov Reviewed-by: Keith Randall --- windows/security_windows.go | 49 +++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/windows/security_windows.go b/windows/security_windows.go index b6e1ab76f8..a8b0364c7c 100644 --- a/windows/security_windows.go +++ b/windows/security_windows.go @@ -1303,7 +1303,10 @@ func (selfRelativeSD *SECURITY_DESCRIPTOR) ToAbsolute() (absoluteSD *SECURITY_DE return nil, err } if absoluteSDSize > 0 { - absoluteSD = (*SECURITY_DESCRIPTOR)(unsafe.Pointer(&make([]byte, absoluteSDSize)[0])) + absoluteSD = new(SECURITY_DESCRIPTOR) + if unsafe.Sizeof(*absoluteSD) < uintptr(absoluteSDSize) { + panic("sizeof(SECURITY_DESCRIPTOR) too small") + } } var ( dacl *ACL @@ -1312,19 +1315,55 @@ func (selfRelativeSD *SECURITY_DESCRIPTOR) ToAbsolute() (absoluteSD *SECURITY_DE group *SID ) if daclSize > 0 { - dacl = (*ACL)(unsafe.Pointer(&make([]byte, daclSize)[0])) + dacl = (*ACL)(unsafe.Pointer(unsafe.SliceData(make([]byte, daclSize)))) } if saclSize > 0 { - sacl = (*ACL)(unsafe.Pointer(&make([]byte, saclSize)[0])) + sacl = (*ACL)(unsafe.Pointer(unsafe.SliceData(make([]byte, saclSize)))) } if ownerSize > 0 { - owner = (*SID)(unsafe.Pointer(&make([]byte, ownerSize)[0])) + owner = (*SID)(unsafe.Pointer(unsafe.SliceData(make([]byte, ownerSize)))) } if groupSize > 0 { - group = (*SID)(unsafe.Pointer(&make([]byte, groupSize)[0])) + group = (*SID)(unsafe.Pointer(unsafe.SliceData(make([]byte, groupSize)))) } + // We call into Windows via makeAbsoluteSD, which sets up + // pointers within absoluteSD that point to other chunks of memory + // we pass into makeAbsoluteSD, and that happens outside the view of the GC. + // We therefore take some care here to then verify the pointers are as we expect + // and set them explicitly in view of the GC. See https://go.dev/issue/73199. + // TODO: consider weak pointers once Go 1.24 is appropriate. See suggestion in https://go.dev/cl/663575. err = makeAbsoluteSD(selfRelativeSD, absoluteSD, &absoluteSDSize, dacl, &daclSize, sacl, &saclSize, owner, &ownerSize, group, &groupSize) + if err != nil { + // Don't return absoluteSD, which might be partially initialized. + return nil, err + } + // Before using any fields, verify absoluteSD is in the format we expect according to Windows. + // See https://learn.microsoft.com/en-us/windows/win32/secauthz/absolute-and-self-relative-security-descriptors + absControl, _, err := absoluteSD.Control() + if err != nil { + panic("absoluteSD: " + err.Error()) + } + if absControl&SE_SELF_RELATIVE != 0 { + panic("absoluteSD not in absolute format") + } + if absoluteSD.dacl != dacl { + panic("dacl pointer mismatch") + } + if absoluteSD.sacl != sacl { + panic("sacl pointer mismatch") + } + if absoluteSD.owner != owner { + panic("owner pointer mismatch") + } + if absoluteSD.group != group { + panic("group pointer mismatch") + } + absoluteSD.dacl = dacl + absoluteSD.sacl = sacl + absoluteSD.owner = owner + absoluteSD.group = group + return } From 7138967c196ab71ebc2d3b2cae146abeab672de0 Mon Sep 17 00:00:00 2001 From: Keith Randall Date: Mon, 21 Apr 2025 18:28:27 -0700 Subject: [PATCH 10/13] windows: fix slicing of NTUnicodeString values We were slicing using a count of bytes, not a count of uint16s. Fixes golang/go#73460 Change-Id: If0fd19e795078c01fda5b976e3c34af115b25dcc Reviewed-on: https://go-review.googlesource.com/c/sys/+/667235 LUCI-TryBot-Result: Go LUCI Reviewed-by: Junyang Shao Reviewed-by: Quim Muntal Auto-Submit: Keith Randall Reviewed-by: Keith Randall --- windows/syscall_windows.go | 5 +++-- windows/syscall_windows_test.go | 20 ++++++++++++++++++++ windows/types_windows.go | 2 ++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/windows/syscall_windows.go b/windows/syscall_windows.go index 4a32543868..044fac4da3 100644 --- a/windows/syscall_windows.go +++ b/windows/syscall_windows.go @@ -1698,8 +1698,9 @@ func NewNTUnicodeString(s string) (*NTUnicodeString, error) { // Slice returns a uint16 slice that aliases the data in the NTUnicodeString. func (s *NTUnicodeString) Slice() []uint16 { - slice := unsafe.Slice(s.Buffer, s.MaximumLength) - return slice[:s.Length] + // Note: this rounds the length down, if it happens + // to (incorrectly) be odd. Probably safer than rounding up. + return unsafe.Slice(s.Buffer, s.MaximumLength/2)[:s.Length/2] } func (s *NTUnicodeString) String() string { diff --git a/windows/syscall_windows_test.go b/windows/syscall_windows_test.go index 1e686a4fd3..f81fea08a3 100644 --- a/windows/syscall_windows_test.go +++ b/windows/syscall_windows_test.go @@ -1473,3 +1473,23 @@ func TestToUnicodeEx(t *testing.T) { t.Errorf("UnloadKeyboardLayout failed: %v", err) } } + +func TestRoundtripNTUnicodeString(t *testing.T) { + for _, s := range []string{ + "", + "hello", + "Ƀ", + strings.Repeat("*", 32000), // NTUnicodeString works up to 2^16 byte lengths == 32768 uint16s. + // TODO: various encoding errors? + } { + ntus, err := windows.NewNTUnicodeString(s) + if err != nil { + t.Errorf("encoding %q failed: %v", s, err) + continue + } + s2 := ntus.String() + if s != s2 { + t.Errorf("round trip of %q = %q, wanted original", s, s2) + } + } +} diff --git a/windows/types_windows.go b/windows/types_windows.go index ad67df2fdb..ce741c9c7f 100644 --- a/windows/types_windows.go +++ b/windows/types_windows.go @@ -2700,6 +2700,8 @@ type CommTimeouts struct { // NTUnicodeString is a UTF-16 string for NT native APIs, corresponding to UNICODE_STRING. type NTUnicodeString struct { + // Note: Length and MaximumLength are in *bytes*, not uint16s. + // They should always be even. Length uint16 MaximumLength uint16 Buffer *uint16 From 8e9e04625d1e3542f462ec617fd078b2d2961e5a Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Sun, 20 Apr 2025 09:46:18 +0000 Subject: [PATCH 11/13] windows: add virtual key codes and console input consts This adds console input related key codes and consts Change-Id: I8ebd76995a2c9ddd150cf04890a9e5053841fcab GitHub-Last-Rev: 8e534f4589e57f1d681e72877917578a0ed67622 GitHub-Pull-Request: golang/sys#226 Reviewed-on: https://go-review.googlesource.com/c/sys/+/621495 Reviewed-by: Junyang Shao Auto-Submit: Michael Pratt Reviewed-by: Michael Pratt Reviewed-by: Alex Brainman LUCI-TryBot-Result: Go LUCI --- windows/types_windows.go | 210 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 210 insertions(+) diff --git a/windows/types_windows.go b/windows/types_windows.go index ce741c9c7f..958bcf47a3 100644 --- a/windows/types_windows.go +++ b/windows/types_windows.go @@ -3630,3 +3630,213 @@ const ( KLF_NOTELLSHELL = 0x00000080 KLF_SETFORPROCESS = 0x00000100 ) + +// Virtual Key codes +// https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes +const ( + VK_LBUTTON = 0x01 + VK_RBUTTON = 0x02 + VK_CANCEL = 0x03 + VK_MBUTTON = 0x04 + VK_XBUTTON1 = 0x05 + VK_XBUTTON2 = 0x06 + VK_BACK = 0x08 + VK_TAB = 0x09 + VK_CLEAR = 0x0C + VK_RETURN = 0x0D + VK_SHIFT = 0x10 + VK_CONTROL = 0x11 + VK_MENU = 0x12 + VK_PAUSE = 0x13 + VK_CAPITAL = 0x14 + VK_KANA = 0x15 + VK_HANGEUL = 0x15 + VK_HANGUL = 0x15 + VK_IME_ON = 0x16 + VK_JUNJA = 0x17 + VK_FINAL = 0x18 + VK_HANJA = 0x19 + VK_KANJI = 0x19 + VK_IME_OFF = 0x1A + VK_ESCAPE = 0x1B + VK_CONVERT = 0x1C + VK_NONCONVERT = 0x1D + VK_ACCEPT = 0x1E + VK_MODECHANGE = 0x1F + VK_SPACE = 0x20 + VK_PRIOR = 0x21 + VK_NEXT = 0x22 + VK_END = 0x23 + VK_HOME = 0x24 + VK_LEFT = 0x25 + VK_UP = 0x26 + VK_RIGHT = 0x27 + VK_DOWN = 0x28 + VK_SELECT = 0x29 + VK_PRINT = 0x2A + VK_EXECUTE = 0x2B + VK_SNAPSHOT = 0x2C + VK_INSERT = 0x2D + VK_DELETE = 0x2E + VK_HELP = 0x2F + VK_LWIN = 0x5B + VK_RWIN = 0x5C + VK_APPS = 0x5D + VK_SLEEP = 0x5F + VK_NUMPAD0 = 0x60 + VK_NUMPAD1 = 0x61 + VK_NUMPAD2 = 0x62 + VK_NUMPAD3 = 0x63 + VK_NUMPAD4 = 0x64 + VK_NUMPAD5 = 0x65 + VK_NUMPAD6 = 0x66 + VK_NUMPAD7 = 0x67 + VK_NUMPAD8 = 0x68 + VK_NUMPAD9 = 0x69 + VK_MULTIPLY = 0x6A + VK_ADD = 0x6B + VK_SEPARATOR = 0x6C + VK_SUBTRACT = 0x6D + VK_DECIMAL = 0x6E + VK_DIVIDE = 0x6F + VK_F1 = 0x70 + VK_F2 = 0x71 + VK_F3 = 0x72 + VK_F4 = 0x73 + VK_F5 = 0x74 + VK_F6 = 0x75 + VK_F7 = 0x76 + VK_F8 = 0x77 + VK_F9 = 0x78 + VK_F10 = 0x79 + VK_F11 = 0x7A + VK_F12 = 0x7B + VK_F13 = 0x7C + VK_F14 = 0x7D + VK_F15 = 0x7E + VK_F16 = 0x7F + VK_F17 = 0x80 + VK_F18 = 0x81 + VK_F19 = 0x82 + VK_F20 = 0x83 + VK_F21 = 0x84 + VK_F22 = 0x85 + VK_F23 = 0x86 + VK_F24 = 0x87 + VK_NUMLOCK = 0x90 + VK_SCROLL = 0x91 + VK_OEM_NEC_EQUAL = 0x92 + VK_OEM_FJ_JISHO = 0x92 + VK_OEM_FJ_MASSHOU = 0x93 + VK_OEM_FJ_TOUROKU = 0x94 + VK_OEM_FJ_LOYA = 0x95 + VK_OEM_FJ_ROYA = 0x96 + VK_LSHIFT = 0xA0 + VK_RSHIFT = 0xA1 + VK_LCONTROL = 0xA2 + VK_RCONTROL = 0xA3 + VK_LMENU = 0xA4 + VK_RMENU = 0xA5 + VK_BROWSER_BACK = 0xA6 + VK_BROWSER_FORWARD = 0xA7 + VK_BROWSER_REFRESH = 0xA8 + VK_BROWSER_STOP = 0xA9 + VK_BROWSER_SEARCH = 0xAA + VK_BROWSER_FAVORITES = 0xAB + VK_BROWSER_HOME = 0xAC + VK_VOLUME_MUTE = 0xAD + VK_VOLUME_DOWN = 0xAE + VK_VOLUME_UP = 0xAF + VK_MEDIA_NEXT_TRACK = 0xB0 + VK_MEDIA_PREV_TRACK = 0xB1 + VK_MEDIA_STOP = 0xB2 + VK_MEDIA_PLAY_PAUSE = 0xB3 + VK_LAUNCH_MAIL = 0xB4 + VK_LAUNCH_MEDIA_SELECT = 0xB5 + VK_LAUNCH_APP1 = 0xB6 + VK_LAUNCH_APP2 = 0xB7 + VK_OEM_1 = 0xBA + VK_OEM_PLUS = 0xBB + VK_OEM_COMMA = 0xBC + VK_OEM_MINUS = 0xBD + VK_OEM_PERIOD = 0xBE + VK_OEM_2 = 0xBF + VK_OEM_3 = 0xC0 + VK_OEM_4 = 0xDB + VK_OEM_5 = 0xDC + VK_OEM_6 = 0xDD + VK_OEM_7 = 0xDE + VK_OEM_8 = 0xDF + VK_OEM_AX = 0xE1 + VK_OEM_102 = 0xE2 + VK_ICO_HELP = 0xE3 + VK_ICO_00 = 0xE4 + VK_PROCESSKEY = 0xE5 + VK_ICO_CLEAR = 0xE6 + VK_OEM_RESET = 0xE9 + VK_OEM_JUMP = 0xEA + VK_OEM_PA1 = 0xEB + VK_OEM_PA2 = 0xEC + VK_OEM_PA3 = 0xED + VK_OEM_WSCTRL = 0xEE + VK_OEM_CUSEL = 0xEF + VK_OEM_ATTN = 0xF0 + VK_OEM_FINISH = 0xF1 + VK_OEM_COPY = 0xF2 + VK_OEM_AUTO = 0xF3 + VK_OEM_ENLW = 0xF4 + VK_OEM_BACKTAB = 0xF5 + VK_ATTN = 0xF6 + VK_CRSEL = 0xF7 + VK_EXSEL = 0xF8 + VK_EREOF = 0xF9 + VK_PLAY = 0xFA + VK_ZOOM = 0xFB + VK_NONAME = 0xFC + VK_PA1 = 0xFD + VK_OEM_CLEAR = 0xFE +) + +// Mouse button constants. +// https://docs.microsoft.com/en-us/windows/console/mouse-event-record-str +const ( + FROM_LEFT_1ST_BUTTON_PRESSED = 0x0001 + RIGHTMOST_BUTTON_PRESSED = 0x0002 + FROM_LEFT_2ND_BUTTON_PRESSED = 0x0004 + FROM_LEFT_3RD_BUTTON_PRESSED = 0x0008 + FROM_LEFT_4TH_BUTTON_PRESSED = 0x0010 +) + +// Control key state constaints. +// https://docs.microsoft.com/en-us/windows/console/key-event-record-str +// https://docs.microsoft.com/en-us/windows/console/mouse-event-record-str +const ( + CAPSLOCK_ON = 0x0080 + ENHANCED_KEY = 0x0100 + LEFT_ALT_PRESSED = 0x0002 + LEFT_CTRL_PRESSED = 0x0008 + NUMLOCK_ON = 0x0020 + RIGHT_ALT_PRESSED = 0x0001 + RIGHT_CTRL_PRESSED = 0x0004 + SCROLLLOCK_ON = 0x0040 + SHIFT_PRESSED = 0x0010 +) + +// Mouse event record event flags. +// https://docs.microsoft.com/en-us/windows/console/mouse-event-record-str +const ( + MOUSE_MOVED = 0x0001 + DOUBLE_CLICK = 0x0002 + MOUSE_WHEELED = 0x0004 + MOUSE_HWHEELED = 0x0008 +) + +// Input Record Event Types +// https://learn.microsoft.com/en-us/windows/console/input-record-str +const ( + FOCUS_EVENT = 0x0010 + KEY_EVENT = 0x0001 + MENU_EVENT = 0x0008 + MOUSE_EVENT = 0x0002 + WINDOW_BUFFER_SIZE_EVENT = 0x0004 +) From c0a955987704d2beda71a48afc43cec03e8bcb96 Mon Sep 17 00:00:00 2001 From: Meng Zhuo Date: Thu, 10 Apr 2025 12:24:41 +0800 Subject: [PATCH 12/13] cpu: add crypto extensions detection for riscv64 This CL adds RISC-V cryptography extensions detection. Direct detection of the extensions zvkned, zvknhb, zvksed and zvksh is not supported, since the crypto spec requires these extensions implemented with data independent timing (zkt). However, their presence may be inferred by checking for the shorthand extensions: zvkn, zvknc, zvkng, zvks, zvksc, zvksg. Change-Id: Ic00038cebf1b9f77426876b06b08f206473ad6fb Reviewed-on: https://go-review.googlesource.com/c/sys/+/664375 LUCI-TryBot-Result: Go LUCI Reviewed-by: Junyang Shao Reviewed-by: Mark Ryan Reviewed-by: Carlos Amedee Reviewed-by: Pengcheng Wang Reviewed-by: Dmitri Shuralyov --- cpu/cpu.go | 11 +++++++++++ cpu/cpu_linux_riscv64.go | 23 +++++++++++++++++++++++ cpu/cpu_riscv64.go | 12 ++++++++++++ 3 files changed, 46 insertions(+) diff --git a/cpu/cpu.go b/cpu/cpu.go index 2e73ee1975..63541994ef 100644 --- a/cpu/cpu.go +++ b/cpu/cpu.go @@ -232,6 +232,17 @@ var RISCV64 struct { HasZba bool // Address generation instructions extension HasZbb bool // Basic bit-manipulation extension HasZbs bool // Single-bit instructions extension + HasZvbb bool // Vector Basic Bit-manipulation + HasZvbc bool // Vector Carryless Multiplication + HasZvkb bool // Vector Cryptography Bit-manipulation + HasZvkt bool // Vector Data-Independent Execution Latency + HasZvkg bool // Vector GCM/GMAC + HasZvkn bool // NIST Algorithm Suite (AES/SHA256/SHA512) + HasZvknc bool // NIST Algorithm Suite with carryless multiply + HasZvkng bool // NIST Algorithm Suite with GCM + HasZvks bool // ShangMi Algorithm Suite + HasZvksc bool // ShangMi Algorithm Suite with carryless multiplication + HasZvksg bool // ShangMi Algorithm Suite with GCM _ CacheLinePad } diff --git a/cpu/cpu_linux_riscv64.go b/cpu/cpu_linux_riscv64.go index cb4a0c5728..ad741536f3 100644 --- a/cpu/cpu_linux_riscv64.go +++ b/cpu/cpu_linux_riscv64.go @@ -58,6 +58,15 @@ const ( riscv_HWPROBE_EXT_ZBA = 0x8 riscv_HWPROBE_EXT_ZBB = 0x10 riscv_HWPROBE_EXT_ZBS = 0x20 + riscv_HWPROBE_EXT_ZVBB = 0x20000 + riscv_HWPROBE_EXT_ZVBC = 0x40000 + riscv_HWPROBE_EXT_ZVKB = 0x80000 + riscv_HWPROBE_EXT_ZVKG = 0x100000 + riscv_HWPROBE_EXT_ZVKNED = 0x200000 + riscv_HWPROBE_EXT_ZVKNHB = 0x800000 + riscv_HWPROBE_EXT_ZVKSED = 0x1000000 + riscv_HWPROBE_EXT_ZVKSH = 0x2000000 + riscv_HWPROBE_EXT_ZVKT = 0x4000000 riscv_HWPROBE_KEY_CPUPERF_0 = 0x5 riscv_HWPROBE_MISALIGNED_FAST = 0x3 riscv_HWPROBE_MISALIGNED_MASK = 0x7 @@ -99,6 +108,20 @@ func doinit() { RISCV64.HasZba = isSet(v, riscv_HWPROBE_EXT_ZBA) RISCV64.HasZbb = isSet(v, riscv_HWPROBE_EXT_ZBB) RISCV64.HasZbs = isSet(v, riscv_HWPROBE_EXT_ZBS) + RISCV64.HasZvbb = isSet(v, riscv_HWPROBE_EXT_ZVBB) + RISCV64.HasZvbc = isSet(v, riscv_HWPROBE_EXT_ZVBC) + RISCV64.HasZvkb = isSet(v, riscv_HWPROBE_EXT_ZVKB) + RISCV64.HasZvkg = isSet(v, riscv_HWPROBE_EXT_ZVKG) + RISCV64.HasZvkt = isSet(v, riscv_HWPROBE_EXT_ZVKT) + // Cryptography shorthand extensions + RISCV64.HasZvkn = isSet(v, riscv_HWPROBE_EXT_ZVKNED) && + isSet(v, riscv_HWPROBE_EXT_ZVKNHB) && RISCV64.HasZvkb && RISCV64.HasZvkt + RISCV64.HasZvknc = RISCV64.HasZvkn && RISCV64.HasZvbc + RISCV64.HasZvkng = RISCV64.HasZvkn && RISCV64.HasZvkg + RISCV64.HasZvks = isSet(v, riscv_HWPROBE_EXT_ZVKSED) && + isSet(v, riscv_HWPROBE_EXT_ZVKSH) && RISCV64.HasZvkb && RISCV64.HasZvkt + RISCV64.HasZvksc = RISCV64.HasZvks && RISCV64.HasZvbc + RISCV64.HasZvksg = RISCV64.HasZvks && RISCV64.HasZvkg } if pairs[1].key != -1 { v := pairs[1].value & riscv_HWPROBE_MISALIGNED_MASK diff --git a/cpu/cpu_riscv64.go b/cpu/cpu_riscv64.go index aca3199c91..0f617aef54 100644 --- a/cpu/cpu_riscv64.go +++ b/cpu/cpu_riscv64.go @@ -16,5 +16,17 @@ func initOptions() { {Name: "zba", Feature: &RISCV64.HasZba}, {Name: "zbb", Feature: &RISCV64.HasZbb}, {Name: "zbs", Feature: &RISCV64.HasZbs}, + // RISC-V Cryptography Extensions + {Name: "zvbb", Feature: &RISCV64.HasZvbb}, + {Name: "zvbc", Feature: &RISCV64.HasZvbc}, + {Name: "zvkb", Feature: &RISCV64.HasZvkb}, + {Name: "zvkg", Feature: &RISCV64.HasZvkg}, + {Name: "zvkt", Feature: &RISCV64.HasZvkt}, + {Name: "zvkn", Feature: &RISCV64.HasZvkn}, + {Name: "zvknc", Feature: &RISCV64.HasZvknc}, + {Name: "zvkng", Feature: &RISCV64.HasZvkng}, + {Name: "zvks", Feature: &RISCV64.HasZvks}, + {Name: "zvksc", Feature: &RISCV64.HasZvksc}, + {Name: "zvksg", Feature: &RISCV64.HasZvksg}, } } From 3d9a6b80792a3911da1fa665c959a5ede3abf476 Mon Sep 17 00:00:00 2001 From: qmuntal Date: Fri, 25 Apr 2025 14:35:01 +0200 Subject: [PATCH 13/13] windows: add WSADuplicateSocket WSADuplicateSocket is useful to pass sockets between processes on Windows. For golang/go#10350. Change-Id: I6563184fe4d4477d402a6af81b254bd8aa992d2e Reviewed-on: https://go-review.googlesource.com/c/sys/+/668215 Reviewed-by: Damien Neil Reviewed-by: Carlos Amedee LUCI-TryBot-Result: Go LUCI Auto-Submit: Carlos Amedee --- windows/syscall_windows.go | 1 + windows/zsyscall_windows.go | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/windows/syscall_windows.go b/windows/syscall_windows.go index 044fac4da3..640f6b153f 100644 --- a/windows/syscall_windows.go +++ b/windows/syscall_windows.go @@ -870,6 +870,7 @@ const socket_error = uintptr(^uint32(0)) //sys WSARecvFrom(s Handle, bufs *WSABuf, bufcnt uint32, recvd *uint32, flags *uint32, from *RawSockaddrAny, fromlen *int32, overlapped *Overlapped, croutine *byte) (err error) [failretval==socket_error] = ws2_32.WSARecvFrom //sys WSASendTo(s Handle, bufs *WSABuf, bufcnt uint32, sent *uint32, flags uint32, to *RawSockaddrAny, tolen int32, overlapped *Overlapped, croutine *byte) (err error) [failretval==socket_error] = ws2_32.WSASendTo //sys WSASocket(af int32, typ int32, protocol int32, protoInfo *WSAProtocolInfo, group uint32, flags uint32) (handle Handle, err error) [failretval==InvalidHandle] = ws2_32.WSASocketW +//sys WSADuplicateSocket(s Handle, processID uint32, info *WSAProtocolInfo) (err error) [failretval!=0] = ws2_32.WSADuplicateSocketW //sys GetHostByName(name string) (h *Hostent, err error) [failretval==nil] = ws2_32.gethostbyname //sys GetServByName(name string, proto string) (s *Servent, err error) [failretval==nil] = ws2_32.getservbyname //sys Ntohs(netshort uint16) (u uint16) = ws2_32.ntohs diff --git a/windows/zsyscall_windows.go b/windows/zsyscall_windows.go index 01c0716c2c..a58bc48b8e 100644 --- a/windows/zsyscall_windows.go +++ b/windows/zsyscall_windows.go @@ -511,6 +511,7 @@ var ( procFreeAddrInfoW = modws2_32.NewProc("FreeAddrInfoW") procGetAddrInfoW = modws2_32.NewProc("GetAddrInfoW") procWSACleanup = modws2_32.NewProc("WSACleanup") + procWSADuplicateSocketW = modws2_32.NewProc("WSADuplicateSocketW") procWSAEnumProtocolsW = modws2_32.NewProc("WSAEnumProtocolsW") procWSAGetOverlappedResult = modws2_32.NewProc("WSAGetOverlappedResult") procWSAIoctl = modws2_32.NewProc("WSAIoctl") @@ -4391,6 +4392,14 @@ func WSACleanup() (err error) { return } +func WSADuplicateSocket(s Handle, processID uint32, info *WSAProtocolInfo) (err error) { + r1, _, e1 := syscall.Syscall(procWSADuplicateSocketW.Addr(), 3, uintptr(s), uintptr(processID), uintptr(unsafe.Pointer(info))) + if r1 != 0 { + err = errnoErr(e1) + } + return +} + func WSAEnumProtocols(protocols *int32, protocolBuffer *WSAProtocolInfo, bufferLength *uint32) (n int32, err error) { r0, _, e1 := syscall.Syscall(procWSAEnumProtocolsW.Addr(), 3, uintptr(unsafe.Pointer(protocols)), uintptr(unsafe.Pointer(protocolBuffer)), uintptr(unsafe.Pointer(bufferLength))) n = int32(r0)