2020-03-01 08:39:24 +00:00
/ * SPDX - License - Identifier : MIT
*
2021-01-28 16:52:15 +00:00
* Copyright ( C ) 2017 - 2021 WireGuard LLC . All Rights Reserved .
2020-03-01 08:39:24 +00:00
* /
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
2020-12-05 00:05:51 +00:00
typ := reflect . TypeOf ( & p ) . Elem ( )
2020-03-01 08:39:24 +00:00
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 ) )
}
2021-01-29 17:54:19 +00:00
// 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 ) )
}