Skip to content

Commit

Permalink
Ensure MessageServer.ReqID is 8 byte aligned (#2346)
Browse files Browse the repository at this point in the history
Atomic operations such as atomic.AddUint64 must operate on 64-bit aligned memory when running on 32-bit systems, otherwise a runtime will panic occur. This is a known issue in go and might be addressed in a future release, see golang/go#36606.

## Motivation
Using atomic.AddUint64 on non 64 bit aligned address on 32 bit systems results in a runtime panic. The solution is to guarantee the data it operates on is 64 bit aligned. This can be done by defining such fields as the first item in a struct. See #2336.

Closes #2336 

## Changes
Moving ReqID to the top of MessageServer guarantees 8 byte alignment. See https://go101.org/article/memory-layout.html section _The Alignment Requirement for 64-bit Word Atomic Operations_ for details on why this works.

## Test Plan
This was tested on a Raspberry Pi 4B (armv7l) and on linux x86_64 resulting in no breaking changes. 

## TODO
- [ ] Test changes

## DevOps Notes
- [x] This PR does not require configuration changes (e.g., environment variables, GitHub secrets, VM resources)
- [x] This PR does not affect public APIs
- [x] This PR does not rely on a new version of external services (PoET, elasticsearch, etc.)
- [x] This PR does not make changes to log messages (which monitoring infrastructure may rely on)


Co-authored-by: Nikita Kryuchkov <nkryuchkov10@gmail.com>
  • Loading branch information
jacquesadit and nkryuchkov committed Jul 13, 2021
1 parent 81dd68b commit a068859
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions p2p/server/msgserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ type ResponseHandlers struct {
// MessageServer is a request-response multiplexer on top of the p2p layer. it provides a way to register
// message types on top of a protocol and declare request and response handlers. it matches incoming responses to requests.
type MessageServer struct {
log.Log
ReqID uint64 //request id
ReqID uint64 //request id (must be declared first to ensure 8 byte alignment on 32-bit systems, required by atomic operations)
name string //server name
network Service
pendMutex sync.RWMutex
Expand All @@ -74,6 +73,7 @@ type MessageServer struct {
workerCount sync.WaitGroup
workerLimiter chan struct{}
exit chan struct{}
log.Log
}

// Service is the subset of method used by MessageServer for p2p communications.
Expand Down

0 comments on commit a068859

Please sign in to comment.