From b526118f5e1cb96a8864c03e977426aa6e0b33b5 Mon Sep 17 00:00:00 2001 From: Jouni Hartikainen Date: Thu, 5 Nov 2020 22:56:55 +0200 Subject: [PATCH] Move uint64 fields that are accessed using sync/atomic to the beginning of struct to ensure proper alignment in memory On 32-bit platforms (e.g. Raspberry Pi) it is caller's responsibility to arrange 64-bit alignment for sync/atomic operation parameters, see: https://golang.org/pkg/sync/atomic/#pkg-note-BUG. This commit fixes "SIGSEGV: segmentation violation" errors on Raspberry Pi 3. --- tcp/session.go | 4 +++- udp/client/clientconn.go | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/tcp/session.go b/tcp/session.go index 72eebca5..736002f2 100644 --- a/tcp/session.go +++ b/tcp/session.go @@ -18,6 +18,9 @@ import ( type EventFunc func() type Session struct { + // This field needs to be the first in the struct to ensure proper word alignment on 32-bit platforms. + // See: https://golang.org/pkg/sync/atomic/#pkg-note-BUG + sequence uint64 connection *coapNet.Conn maxMessageSize int @@ -29,7 +32,6 @@ type Session struct { errors ErrorFunc closeSocket bool - sequence uint64 tokenHandlerContainer *HandlerContainer midHandlerContainer *HandlerContainer handler HandlerFunc diff --git a/udp/client/clientconn.go b/udp/client/clientconn.go index 17c78f34..1bbf9d6c 100644 --- a/udp/client/clientconn.go +++ b/udp/client/clientconn.go @@ -36,6 +36,9 @@ type Session interface { // ClientConn represents a virtual connection to a conceptual endpoint, to perform COAPs commands. type ClientConn struct { + // This field needs to be the first in the struct to ensure proper word alignment on 32-bit platforms. + // See: https://golang.org/pkg/sync/atomic/#pkg-note-BUG + sequence uint64 session Session handler HandlerFunc observationTokenHandler *HandlerContainer @@ -51,7 +54,6 @@ type ClientConn struct { tokenHandlerContainer *HandlerContainer midHandlerContainer *HandlerContainer - sequence uint64 } // NewClientConn creates connection over session and observation.