fako1024/slimcap

Panic in atomic counter on ARM devices (mock socket)

Closed this issue · 0 comments

When running the tests on a Raspberry Pi (2B) a crash is encountered when accessing the mock socket packet counter:

--- FAIL: TestBasicInteraction (0.00s)
panic: unaligned 64-bit atomic operation [recovered]
	panic: unaligned 64-bit atomic operation

goroutine 6 [running]:
testing.tRunner.func1.2({0x1c36f8, 0x22cc18})
	/usr/local/go/src/testing/testing.go:1545 +0x27c
testing.tRunner.func1()
	/usr/local/go/src/testing/testing.go:1548 +0x42c
panic({0x1c36f8, 0x22cc18})
	/usr/local/go/src/runtime/panic.go:914 +0x244
runtime/internal/atomic.panicUnaligned()
	/usr/local/go/src/runtime/internal/atomic/unaligned.go:8 +0x24
runtime/internal/atomic.Xadd64(0x2000d04, 0x1)
	/usr/local/go/src/runtime/internal/atomic/atomic_arm.s:258 +0x14
github.com/fako1024/slimcap/capture/afpacket/socket.(*MockFileDescriptor).IncrementPacketCount(...)
	/root/Develop/slimcap/capture/afpacket/socket/socket_mock.go:48
github.com/fako1024/slimcap/capture/afpacket/socket.TestBasicInteraction(0x21320f0)
	/root/Develop/slimcap/capture/afpacket/socket/socket_test.go:21 +0x14c
testing.tRunner(0x21320f0, 0x1fc90c)
	/usr/local/go/src/testing/testing.go:1595 +0x124
created by testing.(*T).Run in goroutine 1
	/usr/local/go/src/testing/testing.go:1648 +0x3f0

When taking a close look at the documentation at the bottom of https://pkg.go.dev/sync/atomic it is clear why:

On ARM, 386, and 32-bit MIPS, it is the caller's responsibility to arrange for 64-bit alignment of 64-bit words accessed atomically via the primitive atomic functions (types Int64 and Uint64 are automatically aligned).

The current implementation used the "old" style of incrementing a basic uint64 via atomic.AddUInt64() etc., which are not guaranteed to be memory-aligned. Switching to atomic.UInt64 (type) referenced in the documentation should solve the issue.

DoD

  • Switch to specific variables provided by the atomic package
  • Check if an optimized assembler version of the PPOLL call can be added for ARM / 386