Skip to content

catfile: Ensure structs are properly aligned in memory for 32-bit CPUs

Stan Hu requested to merge sh-ensure-bytes-remaining-64-bit-aligned into master

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:

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/

  1. 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
  1. Then run apt update && apt install golang-1.16 git ruby ruby-dev libssl-dev lib1g-dev libpcre2-dev libcurl4-openssl-dev cmake make
  2. Set your PATH: export PATH=/usr/lib/go-1.16/bin:$PATH
  3. 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)

Edited by Stan Hu

Merge request reports