aboutsummaryrefslogtreecommitdiff
path: root/device/alignment_test.go
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2021-01-29 18:54:19 +0100
committerJason A. Donenfeld <Jason@zx2c4.com>2021-01-29 18:57:03 +0100
commitde51129e33a5fe4fad3da172539e9be640d39211 (patch)
treeb444289dfd35d941c4d11441f336401b9ce81a61 /device/alignment_test.go
parentbeb25cc4fd31da09590fed3200628baf4c701f8b (diff)
downloadwireguard-go-de51129e33a5fe4fad3da172539e9be640d39211.tar.gz
wireguard-go-de51129e33a5fe4fad3da172539e9be640d39211.zip
device: use int64 instead of atomic.Value for time stamp
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'device/alignment_test.go')
-rw-r--r--device/alignment_test.go67
1 files changed, 67 insertions, 0 deletions
diff --git a/device/alignment_test.go b/device/alignment_test.go
new file mode 100644
index 0000000..5587cbe
--- /dev/null
+++ b/device/alignment_test.go
@@ -0,0 +1,67 @@
+/* SPDX-License-Identifier: MIT
+ *
+ * Copyright (C) 2017-2021 WireGuard LLC. All Rights Reserved.
+ */
+
+package device
+
+import (
+ "reflect"
+ "testing"
+ "unsafe"
+)
+
+func checkAlignment(t *testing.T, name string, offset uintptr) {
+ t.Helper()
+ if offset%8 != 0 {
+ t.Errorf("offset of %q within struct is %d bytes, which does not align to 64-bit word boundaries (missing %d bytes). Atomic operations will crash on 32-bit systems.", name, offset, 8-(offset%8))
+ }
+}
+
+// TestPeerAlignment checks that atomically-accessed fields are
+// aligned to 64-bit boundaries, as required by the atomic package.
+//
+// Unfortunately, violating this rule on 32-bit platforms results in a
+// hard segfault at runtime.
+func TestPeerAlignment(t *testing.T) {
+ var p Peer
+
+ typ := reflect.TypeOf(&p).Elem()
+ t.Logf("Peer type size: %d, with fields:", typ.Size())
+ for i := 0; i < typ.NumField(); i++ {
+ field := typ.Field(i)
+ t.Logf("\t%30s\toffset=%3v\t(type size=%3d, align=%d)",
+ field.Name,
+ field.Offset,
+ field.Type.Size(),
+ field.Type.Align(),
+ )
+ }
+
+ checkAlignment(t, "Peer.stats", unsafe.Offsetof(p.stats))
+ checkAlignment(t, "Peer.isRunning", unsafe.Offsetof(p.isRunning))
+}
+
+
+// TestDeviceAlignment checks that atomically-accessed fields are
+// aligned to 64-bit boundaries, as required by the atomic package.
+//
+// Unfortunately, violating this rule on 32-bit platforms results in a
+// hard segfault at runtime.
+func TestDeviceAlignment(t *testing.T) {
+ var d Device
+
+ typ := reflect.TypeOf(&d).Elem()
+ t.Logf("Device type size: %d, with fields:", typ.Size())
+ for i := 0; i < typ.NumField(); i++ {
+ field := typ.Field(i)
+ t.Logf("\t%30s\toffset=%3v\t(type size=%3d, align=%d)",
+ field.Name,
+ field.Offset,
+ field.Type.Size(),
+ field.Type.Align(),
+ )
+ }
+
+ checkAlignment(t, "Device.rate.underLoadUntil", unsafe.Offsetof(d.rate.underLoadUntil))
+}