Skip to contentSkip to conversation

go.bbclass: change GOTMPDIR to improve reproducibility

PATCH New Peter Marko 1 participant 3 messages May 13

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 go.bbclass: change GOTMPDIR to improve reproducibility

From: Changqing Li <[email protected]>

When cgo is enabled, the Go toolchain writes temporary source files (*.c) under GOTMPDIR and compiles them there. when -trimpath is passed to go, Go passes options such as -ffile-prefix-map=$WORK/b387=/tmp/go-build internally to the GCC instance it invokes. The variable WORK is a temporary directory created under GOTMPDIR, refer the following log:

[snip of compile log]
WORK=/build/tmp/work/x86-64-v3-wrs-linux/buildah/1.41.5/build-tmp/go-build377321751
cd $WORK/b387
TERM='dumb' x86_64-wrs-linux-gcc -m64 -march=x86-64-v3 -fstack-protector-strong -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security --sysroot=/tmp/work/x86-64-v3-wrs-linux/buildah/1.41.5/recipe-sysroot -I /tmp/work/x86-64-v3-wrs-linux/buildah/1.41.5/sources/buildah-1.41.5/src/github.com/containers/buildah/vendor/github.com/proglottis/gpgme -fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=$WORK/b387=/tmp/go-build -gno-record-gcc-switches -v -D_FILE_OFFSET_BITS=64 -I $WORK/b387/ -O2 -g -ffile-prefix-map=/tmp/work/x86-64-v3-wrs-linux/buildah/1.41.5/sources/buildah-1.41.5=/usr/src/debug/buildah/1.41.5 -ffile-prefix-map=/tmp/work/x86-64-v3-wrs-linux/buildah/1.41.5/build=/usr/src/debug/buildah/1.41.5 -ffile-prefix-map=/tmp/work/x86-64-v3-wrs-linux/buildah/1.41.5/recipe-sysroot= -ffile-prefix-map=/tmp/work/x86-64-v3-wrs-linux/buildah/1.41.5/recipe-sysroot-native= -pipe -v -ffile-prefix-map=/tmp/work/x86-64-v3-wrs-linux/buildah/1.41.5/sources/buildah-1.41.5/src/github.com/containers/buildah/vendor=/_/vendor -frandom-seed=TZkSPVSBUvDMjg4wKjWS -o $WORK/b387/_x004.o -c unset_agent_info.cgo2.c [snip of compile log]

OE also passes its own DEBUG_PREFIX_MAP to GCC(finally by CGO_CFLAGS), including -ffile-prefix-map=${B}=${TARGET_DBGSRC_DIR}, where B is ${WORKDIR}/build. Because GOTMPDIR defaults to ${WORKDIR}/build-tmp, the Go temporary directory looks like ${WORKDIR}/build-tmp/go-buildXYZ. Its prefix therefore begins with ${WORKDIR}/build, so GCC matches the DEBUG_PREFIX_MAP entry for ${B} first.

As a result, a path such as ${WORKDIR}/build-tmp/go-buildXYZ is rewritten to ${TARGET_DBGSRC_DIR}-tmp/go-buildXYZ. This breaks the -ffile-prefix-map option that Go itself adds, because the original WORK path no longer matches the value Go expects. Since Go creates go-buildXYZ directories randomly and internally, this causes the build non-reproducible.

This patch changes GOTMPDIR from ${WORKDIR}/build-tmp to ${WORKDIR}/tmp-go-build so that the path no longer matches ${B}. This prevents unintended replacements by OE's DEBUG_PREFIX_MAP and restores reproducibility.

Note that pure go program like go-helloworld under OE will not have this issue since it doen't use cgo, it is reproducible without this fix

(From OE-Core rev: 0642d2323072f561a4d0eeb9266213387b2997fc)

---
 meta/classes-recipe/go.bbclass | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
meta/classes-recipe/go.bbclass +1 -1
index d32509aa6d1..61b08a082f7 100644
@@ -77,7 +77,7 @@ B = "${WORKDIR}/build"
 export GOPATH = "${B}"
 export GOENV = "off"
 export GOPROXY ??= "https://proxy.golang.org,direct"
-export GOTMPDIR ?= "${WORKDIR}/build-tmp"
+export GOTMPDIR ?= "${WORKDIR}/tmp-go-build"
 GOTMPDIR[vardepvalue] = ""
 
 python go_do_unpack() {
#1 lore raw

PATCH 2/3 go.bbclass: disable workspaces

From: Peter Bergin <[email protected]>

go has a feature of workspaces [1]. If there is a file called go.work in your working directory on any of its parent directories that will be read and used during build. For OE where the builds shall be sandboxed this is bad as a workspace file outside of the build environment can be picked up. This commit wil disable that feature according to the instruction in [1].

This was found and introduced build failures when a file go.work was in the parent directory outside of OE build directory.

[1] https://go.dev/ref/mod#workspaces

(From OE-Core rev: c52c5e88626968b08510818f09829f2e1c9f94ae)

---
 meta/classes-recipe/go.bbclass | 1 +
 1 file changed, 1 insertion(+)
meta/classes-recipe/go.bbclass +1 -0
index 61b08a082f7..36103e8eb0f 100644
@@ -22,6 +22,7 @@ export GOARCH = "${TARGET_GOARCH}"
 export GOOS = "${TARGET_GOOS}"
 export GOHOSTARCH="${BUILD_GOARCH}"
 export GOHOSTOS="${BUILD_GOOS}"
+export GOWORK = "off"
 
 GOARM[export] = "0"
 GOARM:arm:class-target = "${TARGET_GOARM}"
#2 lore raw

PATCH 3/3 oeqa: runtime: go: Increase test_go_compile/test_go_module timeout

In reply to Peter Marko

From: Mathieu Dubois-Briand <[email protected]>

These tests tend do take a bit of time, and this is probably why they have been seen failing a few times in the past months. Rising the timeout from 5 to 10 minutes appears to help.

Fixes [YOCTO #15999]

(From OE-Core rev: 998ebfc77db4c8d7567d82560595e0994a310ae0)

---
 meta/lib/oeqa/runtime/cases/go.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
meta/lib/oeqa/runtime/cases/go.py +2 -2
index d4b69438a56..0c9c4ff4cd3 100644
@@ -35,7 +35,7 @@ class GoCompileTest(OERuntimeTestCase):
             self.skipTest('go command not found, output: %s' % output)
 
         # Compile the simple Go program
-        status, output = self.target.run('go build -o /tmp/test /tmp/test.go')
+        status, output = self.target.run('go build -o /tmp/test /tmp/test.go', 600)
         msg = 'go compile failed, output: %s' % output
         self.assertEqual(status, 0, msg=msg)
 
@@ -65,7 +65,7 @@ class GoCompileTest(OERuntimeTestCase):
         self.assertEqual(status, 0, msg=msg)
 
         # Build the module
-        status, output = self.target.run('cd /tmp/hello-go && go build -o hello main.go')
+        status, output = self.target.run('cd /tmp/hello-go && go build -o hello main.go', 600)
         msg = 'go build failed, output: %s' % output
         self.assertEqual(status, 0, msg=msg)