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

runtime: unexpected slice append behavior on ARM #47447

Closed
tmm1 opened this issue Jul 28, 2021 · 3 comments
Closed

runtime: unexpected slice append behavior on ARM #47447

tmm1 opened this issue Jul 28, 2021 · 3 comments

Comments

@tmm1
Copy link
Contributor

tmm1 commented Jul 28, 2021

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

$ go version
go version go1.16.5 linux/arm

Does this issue reproduce with the latest release?

Yes

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

I have observed this bug on android and linux-based embedded ARM systems, primarily with 32-bit operating systems.

What did you do?

I'm using a simple reflection loop to capture field indices into a global lookup map. On ARM systems, I noticed that the captured value is incorrect.

The code responsible looks like:

	finfo := &fieldInfo{
		index: append(prefixIdx, f.Index...),
	}

changing it to the following fixes the issue:

	idx := []int{}
	idx = append(idx, prefixIdx...)
	idx = append(idx, f.Index...)
	finfo := &fieldInfo{
 		index:  idx,
 	}

I have an executable test available in https://github.com/tmm1/gobug, along with a Dockerfile which runs it in an arm32 environment to replicate the failure:

$ git clone github.com/tmm1/gobug && cd gobug
$ make docker-build
#10 34.00 === RUN   TestTypeInfo
#10 34.01     types_test.go:27: 
#10 34.01               Error Trace:    types_test.go:27
#10 34.01               Error:          Not equal: 
#10 34.01                               expected: []int{1, 0}
#10 34.01                               actual  : []int{1, 1}
#10 34.01                               
#10 34.01                               Diff:
#10 34.01                               --- Expected
#10 34.01                               +++ Actual
#10 34.01                               @@ -2,3 +2,3 @@
#10 34.01                                 (int) 1,
#10 34.01                               - (int) 0
#10 34.01                               + (int) 1
#10 34.01                                }
#10 34.01               Test:           TestTypeInfo
#10 34.01 --- FAIL: TestTypeInfo (0.01s)
#10 34.01 FAIL
#10 34.01 FAIL  github.com/tmm1/gobug   0.105s
@seankhliao
Copy link
Member

There is nothing in your code forcing append to allocate a new backing array, so when you do append(prefixIdx, f.Index...), if prefixIdx has enough capacity it can use the same backing array and your 2 slices point to the same space.

@tmm1
Copy link
Contributor Author

tmm1 commented Jul 29, 2021

Could you help me understand why the behavior varies per platform?

Is it unreasonable to expect that my golang code would behave the same way on different computers?

@ianlancetaylor
Copy link
Contributor

We don't use the issue tracker for discussion. You will get better and faster answers on a forum; see https://golang.org/wiki/Questions. Thanks.

You may also want to read https://blog.golang.org/slices for more background.

@golang golang locked and limited conversation to collaborators Jul 29, 2022
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