diff options
| author | Randolph Sapp <rs@ti.com> | 2025-10-01 19:28:24 -0500 |
|---|---|---|
| committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2025-10-06 23:15:59 +0100 |
| commit | cbf72b3696497c0c794e4497c4a5dcdcf13d8219 (patch) | |
| tree | 6867758e3f2b2079bdb68a5b688fe7f78cefa0e1 /meta | |
| parent | bb8aa3740f6cb318f63b708e84ee67dbd1334ce1 (diff) | |
| download | poky-cbf72b3696497c0c794e4497c4a5dcdcf13d8219.tar.gz | |
go: fix sigaction usage on i386 platforms
In upstream the following commit [1] was submitted to resolve issues
with sigaction being used in linked libraries with cgo applications.
runtime: when using cgo on 386, call C sigaction function
This resolves potential segfaults with cgo applications that link to libraries
that intend to switch out signal handlers temporarily with sigaction.
[1] https://github.com/golang/go/commit/c5737dc21bbac9fbefc35ac9313e66291d66b382
(From OE-Core rev: 1bc7a1731b218bb5c8a08c9823c777a40e17555e)
Signed-off-by: Randolph Sapp <rs@ti.com>
Signed-off-by: Mathieu Dubois-Briand <mathieu.dubois-briand@bootlin.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'meta')
| -rw-r--r-- | meta/recipes-devtools/go/go-1.25.0.inc | 1 | ||||
| -rw-r--r-- | meta/recipes-devtools/go/go/0001-runtime-when-using-cgo-on-386-call-C-sigaction-funct.patch | 248 |
2 files changed, 249 insertions, 0 deletions
diff --git a/meta/recipes-devtools/go/go-1.25.0.inc b/meta/recipes-devtools/go/go-1.25.0.inc index f562fbb34b..1558b4623d 100644 --- a/meta/recipes-devtools/go/go-1.25.0.inc +++ b/meta/recipes-devtools/go/go-1.25.0.inc | |||
| @@ -16,5 +16,6 @@ SRC_URI += "\ | |||
| 16 | file://0009-go-Filter-build-paths-on-staticly-linked-arches.patch \ | 16 | file://0009-go-Filter-build-paths-on-staticly-linked-arches.patch \ |
| 17 | file://0010-cmd-go-clear-GOROOT-for-func-ldShared-when-trimpath-.patch \ | 17 | file://0010-cmd-go-clear-GOROOT-for-func-ldShared-when-trimpath-.patch \ |
| 18 | file://0011-cmd-link-stop-forcing-binutils-gold-dependency-on-aa.patch \ | 18 | file://0011-cmd-link-stop-forcing-binutils-gold-dependency-on-aa.patch \ |
| 19 | file://0001-runtime-when-using-cgo-on-386-call-C-sigaction-funct.patch \ | ||
| 19 | " | 20 | " |
| 20 | SRC_URI[main.sha256sum] = "4bd01e91297207bfa450ea40d4d5a93b1b531a5e438473b2a06e18e077227225" | 21 | SRC_URI[main.sha256sum] = "4bd01e91297207bfa450ea40d4d5a93b1b531a5e438473b2a06e18e077227225" |
diff --git a/meta/recipes-devtools/go/go/0001-runtime-when-using-cgo-on-386-call-C-sigaction-funct.patch b/meta/recipes-devtools/go/go/0001-runtime-when-using-cgo-on-386-call-C-sigaction-funct.patch new file mode 100644 index 0000000000..33e3b033a1 --- /dev/null +++ b/meta/recipes-devtools/go/go/0001-runtime-when-using-cgo-on-386-call-C-sigaction-funct.patch | |||
| @@ -0,0 +1,248 @@ | |||
| 1 | From c5737dc21bbac9fbefc35ac9313e66291d66b382 Mon Sep 17 00:00:00 2001 | ||
| 2 | From: Ian Lance Taylor <iant@golang.org> | ||
| 3 | Date: Fri, 5 Sep 2025 22:24:37 -0700 | ||
| 4 | Subject: [PATCH] runtime: when using cgo on 386, call C sigaction function | ||
| 5 | |||
| 6 | On 386 the C sigaction function assumes that the caller does not set | ||
| 7 | the SA_RESTORER flag. It does not copy the C sa_restorer field to | ||
| 8 | the kernel sa_restorer field. The effect is that the kernel sees | ||
| 9 | the SA_RESTORER flag but a NULL sa_restorer field, and the program | ||
| 10 | crashes when returning from a signal handler. | ||
| 11 | |||
| 12 | On the other hand, the C sigaction function will return the SA_RESTORER | ||
| 13 | flag and the sa_restorer field stored in the kernel. | ||
| 14 | |||
| 15 | This means that if the Go runtime installs a signal handler, | ||
| 16 | with SA_RESTORER as is required when calling the kernel, | ||
| 17 | and the Go program calls C code that calls the C sigaction function | ||
| 18 | to query the current signal handler, that C code will get a result | ||
| 19 | that it can't pass back to sigaction. | ||
| 20 | |||
| 21 | This CL fixes the problem by using the C sigaction function | ||
| 22 | for 386 programs that use cgo. This reuses the functionality | ||
| 23 | used on amd64 and other GOARCHs to support the race detector. | ||
| 24 | |||
| 25 | See #75253, or runtime/testdata/testprogcgo/eintr.go, for sample | ||
| 26 | code that used to fail on 386. No new test case is required, | ||
| 27 | we just remove the skip we used to have for eintr.go. | ||
| 28 | |||
| 29 | Fixes #75253 | ||
| 30 | |||
| 31 | Change-Id: I803059b1fb9e09e9fbb43f68eccb6a59a92c2991 | ||
| 32 | Reviewed-on: https://go-review.googlesource.com/c/go/+/701375 | ||
| 33 | LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> | ||
| 34 | Reviewed-by: Cherry Mui <cherryyz@google.com> | ||
| 35 | Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> | ||
| 36 | Auto-Submit: Ian Lance Taylor <iant@golang.org> | ||
| 37 | Upstream-Status: Backport [https://github.com/golang/go/commit/c5737dc21bbac9fbefc35ac9313e66291d66b382] | ||
| 38 | Signed-off-by: Randolph Sapp <rs@ti.com> | ||
| 39 | --- | ||
| 40 | src/runtime/cgo/gcc_sigaction.c | 8 ++++---- | ||
| 41 | src/runtime/cgo/sigaction.go | 2 +- | ||
| 42 | src/runtime/cgo_sigaction.go | 6 +++++- | ||
| 43 | src/runtime/crash_cgo_test.go | 11 ----------- | ||
| 44 | src/runtime/os_freebsd.go | 6 ++++++ | ||
| 45 | src/runtime/os_linux.go | 18 +++++++++++++++++- | ||
| 46 | src/runtime/sigaction.go | 2 +- | ||
| 47 | src/runtime/sys_linux_386.s | 19 +++++++++++++++++++ | ||
| 48 | 8 files changed, 53 insertions(+), 19 deletions(-) | ||
| 49 | |||
| 50 | diff --git a/src/runtime/cgo/gcc_sigaction.c b/src/runtime/cgo/gcc_sigaction.c | ||
| 51 | index 7cbef7db11..ad48a88dc1 100644 | ||
| 52 | --- a/src/runtime/cgo/gcc_sigaction.c | ||
| 53 | +++ b/src/runtime/cgo/gcc_sigaction.c | ||
| 54 | @@ -2,7 +2,7 @@ | ||
| 55 | // Use of this source code is governed by a BSD-style | ||
| 56 | // license that can be found in the LICENSE file. | ||
| 57 | |||
| 58 | -//go:build linux && (amd64 || arm64 || loong64 || ppc64le) | ||
| 59 | +//go:build linux && (386 || amd64 || arm64 || loong64 || ppc64le) | ||
| 60 | |||
| 61 | #include <errno.h> | ||
| 62 | #include <stddef.h> | ||
| 63 | @@ -17,7 +17,7 @@ | ||
| 64 | // to and from struct sigaction — are specific to ${goos}/${goarch}. | ||
| 65 | typedef struct { | ||
| 66 | uintptr_t handler; | ||
| 67 | - uint64_t flags; | ||
| 68 | + unsigned long flags; | ||
| 69 | #ifdef __loongarch__ | ||
| 70 | uint64_t mask; | ||
| 71 | uintptr_t restorer; | ||
| 72 | @@ -57,7 +57,7 @@ x_cgo_sigaction(intptr_t signum, const go_sigaction_t *goact, go_sigaction_t *ol | ||
| 73 | sigaddset(&act.sa_mask, (int)(i+1)); | ||
| 74 | } | ||
| 75 | } | ||
| 76 | - act.sa_flags = (int)(goact->flags & ~(uint64_t)SA_RESTORER); | ||
| 77 | + act.sa_flags = (int)(goact->flags & ~(unsigned long)SA_RESTORER); | ||
| 78 | } | ||
| 79 | |||
| 80 | ret = sigaction((int)signum, goact ? &act : NULL, oldgoact ? &oldact : NULL); | ||
| 81 | @@ -79,7 +79,7 @@ x_cgo_sigaction(intptr_t signum, const go_sigaction_t *goact, go_sigaction_t *ol | ||
| 82 | oldgoact->mask |= (uint64_t)(1)<<i; | ||
| 83 | } | ||
| 84 | } | ||
| 85 | - oldgoact->flags = (uint64_t)oldact.sa_flags; | ||
| 86 | + oldgoact->flags = (unsigned long)oldact.sa_flags; | ||
| 87 | } | ||
| 88 | |||
| 89 | _cgo_tsan_release(); | ||
| 90 | diff --git a/src/runtime/cgo/sigaction.go b/src/runtime/cgo/sigaction.go | ||
| 91 | index dc3f5fd255..90034bad32 100644 | ||
| 92 | --- a/src/runtime/cgo/sigaction.go | ||
| 93 | +++ b/src/runtime/cgo/sigaction.go | ||
| 94 | @@ -2,7 +2,7 @@ | ||
| 95 | // Use of this source code is governed by a BSD-style | ||
| 96 | // license that can be found in the LICENSE file. | ||
| 97 | |||
| 98 | -//go:build (linux && (amd64 || arm64 || loong64 || ppc64le)) || (freebsd && amd64) | ||
| 99 | +//go:build (linux && (386 || amd64 || arm64 || loong64 || ppc64le)) || (freebsd && amd64) | ||
| 100 | |||
| 101 | package cgo | ||
| 102 | |||
| 103 | diff --git a/src/runtime/cgo_sigaction.go b/src/runtime/cgo_sigaction.go | ||
| 104 | index 5c644587f0..f725dbef4d 100644 | ||
| 105 | --- a/src/runtime/cgo_sigaction.go | ||
| 106 | +++ b/src/runtime/cgo_sigaction.go | ||
| 107 | @@ -3,8 +3,10 @@ | ||
| 108 | // license that can be found in the LICENSE file. | ||
| 109 | |||
| 110 | // Support for sanitizers. See runtime/cgo/sigaction.go. | ||
| 111 | +// Also used on linux/386 to clear the SA_RESTORER flag | ||
| 112 | +// when using cgo; see issue #75253. | ||
| 113 | |||
| 114 | -//go:build (linux && (amd64 || arm64 || loong64 || ppc64le)) || (freebsd && amd64) | ||
| 115 | +//go:build (linux && (386 || amd64 || arm64 || loong64 || ppc64le)) || (freebsd && amd64) | ||
| 116 | |||
| 117 | package runtime | ||
| 118 | |||
| 119 | @@ -42,6 +44,8 @@ func sigaction(sig uint32, new, old *sigactiont) { | ||
| 120 | |||
| 121 | var ret int32 | ||
| 122 | |||
| 123 | + fixSigactionForCgo(new) | ||
| 124 | + | ||
| 125 | var g *g | ||
| 126 | if mainStarted { | ||
| 127 | g = getg() | ||
| 128 | diff --git a/src/runtime/crash_cgo_test.go b/src/runtime/crash_cgo_test.go | ||
| 129 | index c5d7303261..b77ff8dafd 100644 | ||
| 130 | --- a/src/runtime/crash_cgo_test.go | ||
| 131 | +++ b/src/runtime/crash_cgo_test.go | ||
| 132 | @@ -842,17 +842,6 @@ func TestEINTR(t *testing.T) { | ||
| 133 | switch runtime.GOOS { | ||
| 134 | case "plan9", "windows": | ||
| 135 | t.Skipf("no EINTR on %s", runtime.GOOS) | ||
| 136 | - case "linux": | ||
| 137 | - if runtime.GOARCH == "386" { | ||
| 138 | - // On linux-386 the Go signal handler sets | ||
| 139 | - // a restorer function that is not preserved | ||
| 140 | - // by the C sigaction call in the test, | ||
| 141 | - // causing the signal handler to crash when | ||
| 142 | - // returning the normal code. The test is not | ||
| 143 | - // architecture-specific, so just skip on 386 | ||
| 144 | - // rather than doing a complicated workaround. | ||
| 145 | - t.Skip("skipping on linux-386; C sigaction does not preserve Go restorer") | ||
| 146 | - } | ||
| 147 | } | ||
| 148 | if runtime.GOOS == "freebsd" && race.Enabled { | ||
| 149 | t.Skipf("race + cgo freebsd not supported. See https://go.dev/issue/73788.") | ||
| 150 | diff --git a/src/runtime/os_freebsd.go b/src/runtime/os_freebsd.go | ||
| 151 | index ab859cfb47..68d895b95d 100644 | ||
| 152 | --- a/src/runtime/os_freebsd.go | ||
| 153 | +++ b/src/runtime/os_freebsd.go | ||
| 154 | @@ -457,6 +457,12 @@ func sysSigaction(sig uint32, new, old *sigactiont) { | ||
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | +// fixSigactionForCgo is needed for Linux. | ||
| 159 | +// | ||
| 160 | +//go:nosplit | ||
| 161 | +func fixSigactionForCgo(new *sigactiont) { | ||
| 162 | +} | ||
| 163 | + | ||
| 164 | // asmSigaction is implemented in assembly. | ||
| 165 | // | ||
| 166 | //go:noescape | ||
| 167 | diff --git a/src/runtime/os_linux.go b/src/runtime/os_linux.go | ||
| 168 | index c9d25a5be8..f9fe1b5f33 100644 | ||
| 169 | --- a/src/runtime/os_linux.go | ||
| 170 | +++ b/src/runtime/os_linux.go | ||
| 171 | @@ -486,7 +486,8 @@ func setsig(i uint32, fn uintptr) { | ||
| 172 | sigfillset(&sa.sa_mask) | ||
| 173 | // Although Linux manpage says "sa_restorer element is obsolete and | ||
| 174 | // should not be used". x86_64 kernel requires it. Only use it on | ||
| 175 | - // x86. | ||
| 176 | + // x86. Note that on 386 this is cleared when using the C sigaction | ||
| 177 | + // function via cgo; see fixSigactionForCgo. | ||
| 178 | if GOARCH == "386" || GOARCH == "amd64" { | ||
| 179 | sa.sa_restorer = abi.FuncPCABI0(sigreturn__sigaction) | ||
| 180 | } | ||
| 181 | @@ -562,6 +563,21 @@ func sysSigaction(sig uint32, new, old *sigactiont) { | ||
| 182 | //go:noescape | ||
| 183 | func rt_sigaction(sig uintptr, new, old *sigactiont, size uintptr) int32 | ||
| 184 | |||
| 185 | +// fixSigactionForCgo is called when we are using cgo to call the | ||
| 186 | +// C sigaction function. On 386 the C function does not expect the | ||
| 187 | +// SA_RESTORER flag to be set, and in some cases will fail if it is set: | ||
| 188 | +// it will pass the SA_RESTORER flag to the kernel without passing | ||
| 189 | +// the sa_restorer field. Since the C function will handle SA_RESTORER | ||
| 190 | +// for us, we need not pass it. See issue #75253. | ||
| 191 | +// | ||
| 192 | +//go:nosplit | ||
| 193 | +func fixSigactionForCgo(new *sigactiont) { | ||
| 194 | + if GOARCH == "386" && new != nil { | ||
| 195 | + new.sa_flags &^= _SA_RESTORER | ||
| 196 | + new.sa_restorer = 0 | ||
| 197 | + } | ||
| 198 | +} | ||
| 199 | + | ||
| 200 | func getpid() int | ||
| 201 | func tgkill(tgid, tid, sig int) | ||
| 202 | |||
| 203 | diff --git a/src/runtime/sigaction.go b/src/runtime/sigaction.go | ||
| 204 | index 2027ae80bf..1a99f7f3ec 100644 | ||
| 205 | --- a/src/runtime/sigaction.go | ||
| 206 | +++ b/src/runtime/sigaction.go | ||
| 207 | @@ -2,7 +2,7 @@ | ||
| 208 | // Use of this source code is governed by a BSD-style | ||
| 209 | // license that can be found in the LICENSE file. | ||
| 210 | |||
| 211 | -//go:build (linux && !amd64 && !arm64 && !loong64 && !ppc64le) || (freebsd && !amd64) | ||
| 212 | +//go:build (linux && !386 && !amd64 && !arm64 && !loong64 && !ppc64le) || (freebsd && !amd64) | ||
| 213 | |||
| 214 | package runtime | ||
| 215 | |||
| 216 | diff --git a/src/runtime/sys_linux_386.s b/src/runtime/sys_linux_386.s | ||
| 217 | index d53be243fe..8e832687e0 100644 | ||
| 218 | --- a/src/runtime/sys_linux_386.s | ||
| 219 | +++ b/src/runtime/sys_linux_386.s | ||
| 220 | @@ -410,6 +410,25 @@ TEXT runtime·rt_sigaction(SB),NOSPLIT,$0 | ||
| 221 | MOVL AX, ret+16(FP) | ||
| 222 | RET | ||
| 223 | |||
| 224 | +// Call the function stored in _cgo_sigaction using the GCC calling convention. | ||
| 225 | +TEXT runtime·callCgoSigaction(SB),NOSPLIT,$0-16 | ||
| 226 | + MOVL _cgo_sigaction(SB), AX | ||
| 227 | + MOVL sig+0(FP), BX | ||
| 228 | + MOVL new+4(FP), CX | ||
| 229 | + MOVL old+8(FP), DX | ||
| 230 | + MOVL SP, SI // align stack to call C function | ||
| 231 | + SUBL $32, SP | ||
| 232 | + ANDL $~15, SP | ||
| 233 | + MOVL BX, 0(SP) | ||
| 234 | + MOVL CX, 4(SP) | ||
| 235 | + MOVL DX, 8(SP) | ||
| 236 | + MOVL SI, 12(SP) | ||
| 237 | + CALL AX | ||
| 238 | + MOVL 12(SP), BX | ||
| 239 | + MOVL BX, SP | ||
| 240 | + MOVL AX, ret+12(FP) | ||
| 241 | + RET | ||
| 242 | + | ||
| 243 | TEXT runtime·sigfwd(SB),NOSPLIT,$12-16 | ||
| 244 | MOVL fn+0(FP), AX | ||
| 245 | MOVL sig+4(FP), BX | ||
| 246 | -- | ||
| 247 | 2.51.0 | ||
| 248 | |||
