Skip to content

Commit 3e7259c

Browse files
committed
unix: don't use 32-bit aligned access for cmsgAlignOf on dragonfly after ABI change
Use 32-bit alignment for versions before the September 2019 ABI changes http://lists.dragonflybsd.org/pipermail/users/2019-September/358280.html Follows CL 201977 which did the same for package syscall. Updates golang/go#34958 Change-Id: I0e13fccf6563e4d34dd4aa7410be044881f220aa Reviewed-on: https://go-review.googlesource.com/c/sys/+/202179 Run-TryBot: Tobias Klauser <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 9984515 commit 3e7259c

File tree

4 files changed

+73
-29
lines changed

4 files changed

+73
-29
lines changed

unix/sockcmsg_dragonfly.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2019 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package unix
6+
7+
// Round the length of a raw sockaddr up to align it properly.
8+
func cmsgAlignOf(salen int) int {
9+
salign := SizeofPtr
10+
if SizeofPtr == 8 && !supportsABI(_dragonflyABIChangeVersion) {
11+
// 64-bit Dragonfly before the September 2019 ABI changes still requires
12+
// 32-bit aligned access to network subsystem.
13+
salign = 4
14+
}
15+
return (salen + salign - 1) & ^(salign - 1)
16+
}

unix/sockcmsg_unix.go

-26
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,9 @@
99
package unix
1010

1111
import (
12-
"runtime"
1312
"unsafe"
1413
)
1514

16-
// Round the length of a raw sockaddr up to align it properly.
17-
func cmsgAlignOf(salen int) int {
18-
salign := SizeofPtr
19-
20-
switch runtime.GOOS {
21-
case "aix":
22-
// There is no alignment on AIX.
23-
salign = 1
24-
case "darwin", "dragonfly", "solaris", "illumos":
25-
// NOTE: It seems like 64-bit Darwin, DragonFly BSD,
26-
// illumos, and Solaris kernels still require 32-bit
27-
// aligned access to network subsystem.
28-
if SizeofPtr == 8 {
29-
salign = 4
30-
}
31-
case "netbsd", "openbsd":
32-
// NetBSD and OpenBSD armv7 require 64-bit alignment.
33-
if runtime.GOARCH == "arm" {
34-
salign = 8
35-
}
36-
}
37-
38-
return (salen + salign - 1) & ^(salign - 1)
39-
}
40-
4115
// CmsgLen returns the value to store in the Len field of the Cmsghdr
4216
// structure, taking into account any necessary alignment.
4317
func CmsgLen(datalen int) int {

unix/sockcmsg_unix_other.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2019 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build aix darwin freebsd linux netbsd openbsd solaris
6+
7+
package unix
8+
9+
import (
10+
"runtime"
11+
)
12+
13+
// Round the length of a raw sockaddr up to align it properly.
14+
func cmsgAlignOf(salen int) int {
15+
salign := SizeofPtr
16+
17+
// dragonfly needs to check ABI version at runtime, see cmsgAlignOf in
18+
// sockcmsg_dragonfly.go
19+
switch runtime.GOOS {
20+
case "aix":
21+
// There is no alignment on AIX.
22+
salign = 1
23+
case "darwin", "illumos", "solaris":
24+
// NOTE: It seems like 64-bit Darwin, Illumos and Solaris
25+
// kernels still require 32-bit aligned access to network
26+
// subsystem.
27+
if SizeofPtr == 8 {
28+
salign = 4
29+
}
30+
case "netbsd", "openbsd":
31+
// NetBSD and OpenBSD armv7 require 64-bit alignment.
32+
if runtime.GOARCH == "arm" {
33+
salign = 8
34+
}
35+
}
36+
37+
return (salen + salign - 1) & ^(salign - 1)
38+
}

unix/syscall_dragonfly.go

+19-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,25 @@
1212

1313
package unix
1414

15-
import "unsafe"
16-
17-
//sys sysctl(mib []_C_int, old *byte, oldlen *uintptr, new *byte, newlen uintptr) (err error) = SYS___SYSCTL
15+
import (
16+
"sync"
17+
"unsafe"
18+
)
19+
20+
// See version list in https://github.com/DragonFlyBSD/DragonFlyBSD/blob/master/sys/sys/param.h
21+
var (
22+
osreldateOnce sync.Once
23+
osreldate uint32
24+
)
25+
26+
// First __DragonFly_version after September 2019 ABI changes
27+
// http://lists.dragonflybsd.org/pipermail/users/2019-September/358280.html
28+
const _dragonflyABIChangeVersion = 500705
29+
30+
func supportsABI(ver uint32) bool {
31+
osreldateOnce.Do(func() { osreldate, _ = SysctlUint32("kern.osreldate") })
32+
return osreldate >= ver
33+
}
1834

1935
// SockaddrDatalink implements the Sockaddr interface for AF_LINK type sockets.
2036
type SockaddrDatalink struct {

0 commit comments

Comments
 (0)