Skip to content

sync/atomic: addUint64 panics on ARM with unaligned pointers #23345

@gernoteger

Description

@gernoteger

atomic.addUint64() panics if the pointer to its argument is not 64byte aligned.

it panics with:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x11438]

goroutine 1 [running]:
runtime/internal/atomic.Cas64(0x10410074, 0x1, 0x0, 0x2, 0x0, 0x7b728)
        /usr/local/go-1.9.2/src/runtime/internal/atomic/atomic_arm.go:110 +0x20
sync/atomic.addUint64(0x10410074, 0x1, 0x0, 0x3536c, 0x0)
        /usr/local/go-1.9.2/src/sync/atomic/64bit_arm.go:31 +0x4c

reproduce

package main

import "sync/atomic"

type X struct {
	foo int8
	bar uint64
}

func main() {
	x := X{bar: 1}
	atomic.AddUint64(&x.bar, 1)
	println("bar=", x.bar)
}

Hardware

I tested in on physical arm5/7 hardware. the arm5 code was executed on both targets, with equal results.

arm5:

uname -a
Linux tuxnet 2.6.31-626-g602af1c #131 PREEMPT Sat Jan 22 00:08:11 CET 2011 armv5tejl GNU/Linux

arm7:

uname -a
Linux rasp3 4.9.59-v7+ #1047 SMP Sun Oct 29 12:19:23 GMT 2017 armv7l GNU/Linux

compile options

This panics for:

  • GOOS=linux GOARCH=arm GOARM=5 with go 1.9.2
  • GOOS=linux GOARCH=arm GOARM=7 with go 1.9.2
  • GOOS=linux GOARCH=arm GOARM=7 with go 1.7.2

This works for:

  • GOOS=linux GOARCH=arm GOARM=7with go 1.71

so the behaviour is somewhat undefined, but became worse with 1.9.2.

Workaround

assure the atomic variabbles are aligned. E.g. putting bar first eliminates the issue

type X struct {
bar uint64
foo int8
}

This seems to be connected to #599.

What version of Go are you using (go version)?

1.7.1, 1.9.2

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

GOOS=linux GOARCH=arm GOARM=5
GOOS=linux GOARCH=arm GOARM=7

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions