]> git.ipfire.org Git - thirdparty/wireguard-go.git/commitdiff
tun: windows: check alignment in unit test
authorJason A. Donenfeld <Jason@zx2c4.com>
Fri, 7 May 2021 07:10:41 +0000 (09:10 +0200)
committerJason A. Donenfeld <Jason@zx2c4.com>
Fri, 7 May 2021 07:15:50 +0000 (09:15 +0200)
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
tun/alignment_windows_test.go [new file with mode: 0644]

diff --git a/tun/alignment_windows_test.go b/tun/alignment_windows_test.go
new file mode 100644 (file)
index 0000000..ddb3e3f
--- /dev/null
@@ -0,0 +1,67 @@
+/* SPDX-License-Identifier: MIT
+ *
+ * Copyright (C) 2017-2021 WireGuard LLC. All Rights Reserved.
+ */
+
+package tun
+
+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))
+       }
+}
+
+// TestRateJugglerAlignment 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 TestRateJugglerAlignment(t *testing.T) {
+       var r rateJuggler
+
+       typ := reflect.TypeOf(&r).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, "rateJuggler.current", unsafe.Offsetof(r.current))
+       checkAlignment(t, "rateJuggler.nextByteCount", unsafe.Offsetof(r.nextByteCount))
+       checkAlignment(t, "rateJuggler.nextStartTime", unsafe.Offsetof(r.nextStartTime))
+}
+
+// TestNativeTunAlignment 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 TestNativeTunAlignment(t *testing.T) {
+       var tun NativeTun
+
+       typ := reflect.TypeOf(&tun).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, "NativeTun.rate", unsafe.Offsetof(tun.rate))
+}
\ No newline at end of file