Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When GOARCH is set to mipsle, sync/atomic.Loadint64 will prompt "invalid memory address or nil pointer dereference" when sync.WaitGroup exists in the structure #41970

Closed
ssoor opened this issue Oct 14, 2020 · 2 comments

Comments

@ssoor
Copy link

ssoor commented Oct 14, 2020

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

$ go version
go version go1.15 linux/amd64

yum package(Repository: epel):

golang-src-1.15.2-1.el7.noarch
golang-bin-1.15.2-1.el7.x86_64
golang-1.15.2-1.el7.x86_64
golang-misc-1.15.2-1.el7.noarch

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/core/.cache/go-build"
GOENV="/home/core/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/mnt/source_code/golang/pkg/mod"
GOOS="linux"
GOPATH="/mnt/source_code/golang/"
GOPROXY="https://goproxy.cn,direct"
GOROOT="/home/core/.go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/core/.go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/mnt/source_code/golang/src/github.com/ssoor/test/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build294050133=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Build(x86):

$GOOS=linux GOARCH=mipsle GOMIPS=softfloat go build-o testRun
$scp testRun root@xxxxxx:~/gogs/testRun

Run(mips):

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

goroutine 1 [running]:
runtime/internal/atomic.lockAndCheck(...)
        /home/core/.go/src/runtime/internal/atomic/atomic_mipsx.go:40
runtime/internal/atomic.Load64(0x81404c, 0x814040, 0x15060)
        /home/core/.go/src/runtime/internal/atomic/atomic_mipsx.go:93 +0x20
main.main()
        /mnt/source_code/golang/src/github.com/ssoor/test/main.go:23 +0x54

Source:

package main

import (
	"fmt"
	"sync"
	"sync/atomic"
)

type TestStruct struct {
	wgClose        sync.WaitGroup // When it exists, it will report an error, comment out it is normal
	requestTimeout int64
}

func main() {
	testV := TestStruct{}
	fmt.Println(atomic.LoadInt64(&testV.requestTimeout))
}

What did you expect to see?

0

What did you see instead?

panic: runtime error: invalid memory address or nil pointer dereference
@davecheney
Copy link
Contributor

Switch the order of the fields in the struct. See #599

@rsc
Copy link
Contributor

rsc commented Oct 14, 2020

https://golang.org/pkg/sync/atomic/#pkg-note-BUG explains:

On ARM, x86-32, and 32-bit MIPS, it is the caller's responsibility to arrange for 64-bit alignment of 64-bit words accessed atomically. The first word in a variable or in an allocated struct, array, or slice can be relied upon to be 64-bit aligned.

We are working to fix this (#36606) but for now the solution is to put atomically accessed 64-bit fields at the start of the struct to guarantee 64-bit alignment.

@rsc rsc closed this as completed Oct 14, 2020
@golang golang locked and limited conversation to collaborators Oct 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants