catfile: Ensure structs are properly aligned in memory for 32-bit CPUs
In GitLab 14.5, the use of atomic.LoadInt64
caused a panic: unaligned 64-bit atomic operation
on a Raspberry Pi2 platform. While we don't
officially support 32-bit processors, we have been supporting armfh
builds for years now.
From https://pkg.go.dev/sync/atomic#pkg-note-BUG: 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. The first word in a variable or in an allocated struct, array, or slice can be relied upon to be 64-bit aligned.
We can fix this problem by listing the int64 and int32 atomic fields in order to ensure they are properly aligned. This does not change the overall size of the structure.
For example, with Object:
- Before: https://go.dev/play/p/MCk9TnxhkCY (80 bytes)
- After: https://go.dev/play/p/83jdBOP-wpA (80 bytes)
Relates to #3938 (closed)
Testing
You can test this on a 32-bit platform by running Docker and QEMU: https://www.ecliptik.com/Cross-Building-and-Running-Multi-Arch-Docker-Images/
- Be sure to use the
bullseye-backports
APT sources:
deb http://deb.debian.org/debian bullseye-backports main contrib non-free
deb-src http://deb.debian.org/debian bullseye-backports main contrib non-free
- Then run
apt update && apt install golang-1.16 git ruby ruby-dev libssl-dev lib1g-dev libpcre2-dev libcurl4-openssl-dev cmake make
- Set your
PATH
:export PATH=/usr/lib/go-1.16/bin:$PATH
- Then check out Gitaly and build. To speed things up, you can run the catfile test:
root@e4fcb0bd2e8d:/tmp/gitaly# uname -a
Linux e4fcb0bd2e8d 5.10.47-linuxkit #1 SMP Sat Jul 3 21:51:47 UTC 2021 armv7l GNU/Linux
root@e4fcb0bd2e8d:/tmp/gitaly# go test -run TestObjectReader_reader gitlab.com/gitlab-org/gitaly/v14/internal/git/catfile
ok gitlab.com/gitlab-org/gitaly/v14/internal/git/catfile 1.882s
Relates to #3938 (closed)