Skip to content

Commit cff7267

Browse files
committed
cmd/internal/osinfo,runtime,syscall: use RtlGetVersion instead of RtlGetNtVersionNumbers
The RtlGetNtVersionNumbers function is not documented by Microsoft. Use RtlGetVersion instead, which is documented and available on all supported versions of Windows. Cq-Include-Trybots: luci.golang.try:gotip-windows-amd64-longtest,gotip-windows-arm64 Change-Id: Ibaf0e2c28e673951476c5d863a829fd166705aea Reviewed-on: https://go-review.googlesource.com/c/go/+/571015 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: David Chase <[email protected]> Reviewed-by: Than McIntosh <[email protected]>
1 parent 68a508c commit cff7267

File tree

8 files changed

+44
-25
lines changed

8 files changed

+44
-25
lines changed

Diff for: src/cmd/internal/osinfo/os_windows.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ import (
1414

1515
// Version returns the OS version name/number.
1616
func Version() (string, error) {
17-
major, minor, patch := windows.RtlGetNtVersionNumbers()
18-
return fmt.Sprintf("%d.%d.%d", major, minor, patch), nil
17+
info := windows.RtlGetVersion()
18+
return fmt.Sprintf("%d.%d.%d", info.MajorVersion, info.MinorVersion, info.BuildNumber), nil
1919
}

Diff for: src/internal/syscall/windows/mksyscall.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66

77
package windows
88

9-
//go:generate go run ../../../syscall/mksyscall_windows.go -output zsyscall_windows.go syscall_windows.go security_windows.go psapi_windows.go symlink_windows.go
9+
//go:generate go run ../../../syscall/mksyscall_windows.go -output zsyscall_windows.go syscall_windows.go security_windows.go psapi_windows.go symlink_windows.go version_windows.go

Diff for: src/internal/syscall/windows/version_windows.go

+18-9
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,28 @@ import (
1111
"unsafe"
1212
)
1313

14+
// https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/ns-wdm-_osversioninfow
15+
type _OSVERSIONINFOW struct {
16+
osVersionInfoSize uint32
17+
majorVersion uint32
18+
minorVersion uint32
19+
buildNumber uint32
20+
platformId uint32
21+
csdVersion [128]uint16
22+
}
23+
24+
// According to documentation, RtlGetVersion function always succeeds.
25+
//sys rtlGetVersion(info *_OSVERSIONINFOW) = ntdll.RtlGetVersion
26+
1427
// version retrieves the major, minor, and build version numbers
15-
// of the current Windows OS from the RtlGetNtVersionNumbers API
16-
// and parse the results properly.
28+
// of the current Windows OS from the RtlGetVersion API.
1729
func version() (major, minor, build uint32) {
18-
rtlGetNtVersionNumbers(&major, &minor, &build)
19-
build &= 0x7fff
20-
return
30+
info := _OSVERSIONINFOW{}
31+
info.osVersionInfoSize = uint32(unsafe.Sizeof(info))
32+
rtlGetVersion(&info)
33+
return info.majorVersion, info.minorVersion, info.buildNumber
2134
}
2235

23-
//go:linkname rtlGetNtVersionNumbers syscall.rtlGetNtVersionNumbers
24-
//go:noescape
25-
func rtlGetNtVersionNumbers(majorVersion *uint32, minorVersion *uint32, buildNumber *uint32)
26-
2736
var (
2837
supportTCPKeepAliveIdle bool
2938
supportTCPKeepAliveInterval bool

Diff for: src/internal/syscall/windows/zsyscall_windows.go

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: src/runtime/defs_windows.go

+10
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,13 @@ type memoryBasicInformation struct {
8989
protect uint32
9090
type_ uint32
9191
}
92+
93+
// https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/ns-wdm-_osversioninfow
94+
type _OSVERSIONINFOW struct {
95+
osVersionInfoSize uint32
96+
majorVersion uint32
97+
minorVersion uint32
98+
buildNumber uint32
99+
platformId uint32
100+
csdVersion [128]uint16
101+
}

Diff for: src/runtime/os_windows.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ var (
135135
_NtAssociateWaitCompletionPacket stdFunction
136136
_NtCancelWaitCompletionPacket stdFunction
137137
_RtlGetCurrentPeb stdFunction
138-
_RtlGetNtVersionNumbers stdFunction
138+
_RtlGetVersion stdFunction
139139

140140
// These are from non-kernel32.dll, so we prefer to LoadLibraryEx them.
141141
_timeBeginPeriod,
@@ -268,7 +268,7 @@ func loadOptionalSyscalls() {
268268
}
269269
}
270270
_RtlGetCurrentPeb = windowsFindfunc(n32, []byte("RtlGetCurrentPeb\000"))
271-
_RtlGetNtVersionNumbers = windowsFindfunc(n32, []byte("RtlGetNtVersionNumbers\000"))
271+
_RtlGetVersion = windowsFindfunc(n32, []byte("RtlGetVersion\000"))
272272
}
273273

274274
func monitorSuspendResume() {
@@ -437,9 +437,10 @@ func initLongPathSupport() {
437437
)
438438

439439
// Check that we're ≥ 10.0.15063.
440-
var maj, min, build uint32
441-
stdcall3(_RtlGetNtVersionNumbers, uintptr(unsafe.Pointer(&maj)), uintptr(unsafe.Pointer(&min)), uintptr(unsafe.Pointer(&build)))
442-
if maj < 10 || (maj == 10 && min == 0 && build&0xffff < 15063) {
440+
info := _OSVERSIONINFOW{}
441+
info.osVersionInfoSize = uint32(unsafe.Sizeof(info))
442+
stdcall1(_RtlGetVersion, uintptr(unsafe.Pointer(&info)))
443+
if info.majorVersion < 10 || (info.majorVersion == 10 && info.minorVersion == 0 && info.buildNumber < 15063) {
443444
return
444445
}
445446

Diff for: src/syscall/syscall_windows.go

-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,6 @@ func NewCallbackCDecl(fn any) uintptr {
233233
//sys FreeLibrary(handle Handle) (err error)
234234
//sys GetProcAddress(module Handle, procname string) (proc uintptr, err error)
235235
//sys GetVersion() (ver uint32, err error)
236-
//sys rtlGetNtVersionNumbers(majorVersion *uint32, minorVersion *uint32, buildNumber *uint32) = ntdll.RtlGetNtVersionNumbers
237236
//sys formatMessage(flags uint32, msgsrc uintptr, msgid uint32, langid uint32, buf []uint16, args *byte) (n uint32, err error) = FormatMessageW
238237
//sys ExitProcess(exitcode uint32)
239238
//sys CreateFile(name *uint16, access uint32, mode uint32, sa *SecurityAttributes, createmode uint32, attrs uint32, templatefile int32) (handle Handle, err error) [failretval==InvalidHandle] = CreateFileW

Diff for: src/syscall/zsyscall_windows.go

-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)