aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/signal_unix.go
AgeCommit message (Collapse)Author
2022-05-06[release-branch.go1.17] runtime: don't block preemption signal in new M's or ↵Ian Lance Taylor
ensureSigM No test because we already have a test in the syscall package. The issue reports 1 failure per 100,000 iterations, which is rare enough that our builders won't catch the problem. For #52226 Fixes #52374 Change-Id: I17633ff6cf676b6d575356186dce42cdacad0746 Reviewed-on: https://go-review.googlesource.com/c/go/+/400315 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> (cherry picked from commit e3982660a73b04a87c08215cb5aaa16d816ea573) Reviewed-on: https://go-review.googlesource.com/c/go/+/400317 Reviewed-by: Austin Clements <austin@google.com>
2021-06-21runtime: fix crash during VDSO calls on PowerPCDerek Parker
This patch reinstates a fix for PowerPC with regard to making VDSO calls while receiving a signal, and subsequently crashing. The crash happens because certain VDSO calls can modify the r30 register, which is where g is stored. This change was reverted for PowerPC because r30 is supposed to be a non-volatile register. This is true, but that only makes a guarantee across function calls, but not "within" a function call. This patch was seemingly fine before because the Linux kernel still had hand rolled assembly VDSO function calls, however with a recent change to C function calls it seems the compiler used can generate instructions which temporarily clobber r30. This means that when we receive a signal during one of these calls the value of r30 will not be the g as the runtime expects, causing a segfault. You can see from this assembly dump how the register is clobbered during the call: (the following is from a 5.13rc2 kernel) ``` Dump of assembler code for function __cvdso_clock_gettime_data: 0x00007ffff7ff0700 <+0>: cmplwi r4,15 0x00007ffff7ff0704 <+4>: bgt 0x7ffff7ff07f0 <__cvdso_clock_gettime_data+240> 0x00007ffff7ff0708 <+8>: li r9,1 0x00007ffff7ff070c <+12>: slw r9,r9,r4 0x00007ffff7ff0710 <+16>: andi. r10,r9,2179 0x00007ffff7ff0714 <+20>: beq 0x7ffff7ff0810 <__cvdso_clock_gettime_data+272> 0x00007ffff7ff0718 <+24>: rldicr r10,r4,4,59 0x00007ffff7ff071c <+28>: lis r9,32767 0x00007ffff7ff0720 <+32>: std r30,-16(r1) 0x00007ffff7ff0724 <+36>: std r31,-8(r1) 0x00007ffff7ff0728 <+40>: add r6,r3,r10 0x00007ffff7ff072c <+44>: ori r4,r9,65535 0x00007ffff7ff0730 <+48>: lwz r8,0(r3) 0x00007ffff7ff0734 <+52>: andi. r9,r8,1 0x00007ffff7ff0738 <+56>: bne 0x7ffff7ff07d0 <__cvdso_clock_gettime_data+208> 0x00007ffff7ff073c <+60>: lwsync 0x00007ffff7ff0740 <+64>: mftb r30 <---- RIGHT HERE => 0x00007ffff7ff0744 <+68>: ld r12,40(r6) ``` What I believe is happening is that the kernel changed the PowerPC VDSO calls to use standard C calls instead of using hand rolled assembly. The hand rolled assembly calls never touched r30, so this change was safe to roll back. That does not seem to be the case anymore as on the 5.13rc2 kernel the compiler *is* generating assembly which modifies r30, making this change again unsafe and causing a crash when the program receives a signal during these calls (which will happen often due to async preempt). This change happened here: https://lwn.net/ml/linux-kernel/235e5571959cfa89ced081d7e838ed5ff38447d2.1601365870.git.christophe.leroy@csgroup.eu/. I realize this was reverted due to unexplained hangs in PowerPC builders, but I think we should reinstate this change and investigate those issues separately: https://github.com/golang/go/commit/f4ca3c1e0a2066ca4f7bd6203866d282ed34acf2 Fixes #46803 Change-Id: Ib18d7bbfc80a1a9cb558f0098878d41081324b52 GitHub-Last-Rev: c3002bcfca3ef58b27485e31328e6297b7a9dfe7 GitHub-Pull-Request: golang/go#46767 Reviewed-on: https://go-review.googlesource.com/c/go/+/328110 Run-TryBot: Lynn Boger <laboger@linux.vnet.ibm.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Cherry Mui <cherryyz@google.com> Trust: Lynn Boger <laboger@linux.vnet.ibm.com>
2021-06-18runtime: disable CPU profiling before removing the SIGPROF handlerIan Lance Taylor
Otherwise, in c-archive or c-shared mode, there is the chance of getting a SIGPROF just after the signal handler is removed but before profiling is disabled, in which case the program will die. Fixes #46498 Change-Id: I5492beef45fec9fb9a7f58724356d6aedaf799ac Reviewed-on: https://go-review.googlesource.com/c/go/+/329290 Trust: Ian Lance Taylor <iant@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com>
2021-02-20all: go fmt std cmd (but revert vendor)Russ Cox
Make all our package sources use Go 1.17 gofmt format (adding //go:build lines). Part of //go:build change (#41184). See https://golang.org/design/draft-gobuild Change-Id: Ia0534360e4957e58cd9a18429c39d0e32a6addb4 Reviewed-on: https://go-review.googlesource.com/c/go/+/294430 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Jason A. Donenfeld <Jason@zx2c4.com> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2021-01-22runtime: check for g0 stack last in signal handlerCherry Zhang
In the signal handler, we adjust gsingal's stack to the stack where the signal is delivered. TSAN may deliver signals to the g0 stack, so we have a special case for the g0 stack. However, we don't have very good accuracy in determining the g0 stack's bounds, as it is system allocated and we don't know where it is exactly. If g0.stack.lo is too low, the condition may be triggered incorrectly, where we thought the signal is delivered to the g0 stack but it is actually not. In this case, as the stack bounds is actually wrong, when the stack grows, it may go below the (inaccurate) lower bound, causing "morestack on gsignal" crash. Check for g0 stack last to avoid this situation. There could still be false positives, but for those cases we'll crash either way. (If we could in some way determine the g0 stack bounds accurately, this would not matter (but probably doesn't hurt).) Fixes #43853. Change-Id: I759717c5aa2b0deb83ffb23e57b7625a6b249ee8 Reviewed-on: https://go-review.googlesource.com/c/go/+/285772 Trust: Cherry Zhang <cherryyz@google.com> Reviewed-by: Michael Pratt <mpratt@google.com>
2020-12-23runtime: linux iscgo support for not blocking nptl signalsAndrew G. Morgan
Under linux+cgo, OS threads are launched via pthread_create(). This abstraction, under linux, requires we avoid blocking signals 32,33 and 34 indefinitely because they are needed to reliably execute POSIX-semantics threading in glibc and/or musl. When blocking signals the go runtime generally re-enables them quickly. However, when a thread exits (under cgo, this is via a return from mstart()), we avoid a deadlock in C-code by not blocking these three signals. Fixes #42494 Change-Id: I02dfb2480a1f97d11679e0c4b132b51bddbe4c14 Reviewed-on: https://go-review.googlesource.com/c/go/+/269799 Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Austin Clements <austin@google.com> Trust: Tobias Klauser <tobias.klauser@gmail.com>
2020-12-04runtime: avoid receiving preemotion signal while exec'ingCherry Zhang
The iOS kernel has the same problem as the macOS kernel. Extend the workaround of #41702 (CL 262438 and CL 262817) to iOS. Updates #35851. Change-Id: I7ccec00dc96643c08c5be8b385394856d0fa0f64 Reviewed-on: https://go-review.googlesource.com/c/go/+/275293 Trust: Cherry Zhang <cherryyz@google.com> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-10-28runtime: always enable async preemption on darwin/arm64Cherry Zhang
Now that we have the G register saved, we can enable asynchronous preemption for pure Go programs on darwin/arm64. Updates #38485, #36365. Change-Id: Ic654fa4dce369efe289b38d59cf1a184b358fe9e Reviewed-on: https://go-review.googlesource.com/c/go/+/265120 Trust: Cherry Zhang <cherryyz@google.com> Reviewed-by: Austin Clements <austin@google.com> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-10-28runtime: block signals in needm before allocating MIan Lance Taylor
Otherwise, if a signal occurs just after we allocated the M, we can deadlock if the signal handler needs to allocate an M itself. Fixes #42207 Change-Id: I76f44547f419e8b1c14cbf49bf602c6e645d8c14 Reviewed-on: https://go-review.googlesource.com/c/go/+/265759 Trust: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com>
2020-10-26runtime: tidy cgocallbackAustin Clements
On amd64 and 386, we have a very roundabout way of remembering that we need to dropm on return that currently involves saving a zero to needm's argument slot and later bringing it back. Just store the zero. This also makes amd64 and 386 more consistent with cgocallback on all other platforms: rather than saving the old M to the G stack, they now save it to a named slot on the G0 stack. The needm function no longer needs a dummy argument to get the SP, so we drop that. Change-Id: I7e84bb4a5ff9552de70dcf41d8accf02310535e7 Reviewed-on: https://go-review.googlesource.com/c/go/+/263268 Trust: Austin Clements <austin@google.com> Run-TryBot: Austin Clements <austin@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Cherry Zhang <cherryyz@google.com>
2020-10-16runtime: wait for preemption signals before syscall.ExecIan Lance Taylor
Fixes #41702 Fixes #42023 Change-Id: If07f40b1d73b8f276ee28ffb8b7214175e56c24d Reviewed-on: https://go-review.googlesource.com/c/go/+/262817 Trust: Ian Lance Taylor <iant@golang.org> Trust: Bryan C. Mills <bcmills@google.com> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Cherry Zhang <cherryyz@google.com>
2020-10-15runtime: stop preemption during syscall.Exec on DarwinIan Lance Taylor
On current macOS versions a program that receives a signal during an execve can fail with a SIGILL signal. This appears to be a macOS kernel bug. It has been reported to Apple. This CL partially works around the problem by using execLock to not send preemption signals during execve. Of course some other stray signal could occur, but at least we can avoid exacerbating the problem. We can't simply disable signals, as that would mean that the exec'ed process would start with all signals blocked, which it likely does not expect. Fixes #41702 Change-Id: I91b0add967b315671ddcf73269c4d30136e579b4 Reviewed-on: https://go-review.googlesource.com/c/go/+/262438 Trust: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Cherry Zhang <cherryyz@google.com>
2020-09-23all: add GOOS=iosCherry Zhang
Introduce GOOS=ios for iOS systems. GOOS=ios matches "darwin" build tag, like GOOS=android matches "linux" and GOOS=illumos matches "solaris". Only ios/arm64 is supported (ios/amd64 is not). GOOS=ios and GOOS=darwin remain essentially the same at this point. They will diverge at later time, to differentiate macOS and iOS. Uses of GOOS=="darwin" are changed to (GOOS=="darwin" || GOOS=="ios"), except if it clearly means macOS (e.g. GOOS=="darwin" && GOARCH=="amd64"), it remains GOOS=="darwin". Updates #38485. Change-Id: I4faacdc1008f42434599efb3c3ad90763a83b67c Reviewed-on: https://go-review.googlesource.com/c/go/+/254740 Trust: Cherry Zhang <cherryyz@google.com> Run-TryBot: Cherry Zhang <cherryyz@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Austin Clements <austin@google.com>
2020-09-18runtime/debug: provide Addr method for errors from SetPanicOnFaultKeith Randall
When we're building a panic that's triggered by a memory fault when SetPanicOnFault has been called, include an Addr method. This method reports the address at which the fault occurred. Fixes #37023 RELNOTE=yes Change-Id: Idff144587d6b75070fdc861a36efec76f4ec7384 Reviewed-on: https://go-review.googlesource.com/c/go/+/249677 Run-TryBot: Keith Randall <khr@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Trust: Keith Randall <khr@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
2020-08-19runtime: don't call setitimer for each threadIan Lance Taylor
Previously, on Unix systems, when the profiler was enabled or disabled, we called setitimer once per thread. With this change we instead call it once per process. Change-Id: I90f0189b562e11232816390dc7d55ed154bd836d Reviewed-on: https://go-review.googlesource.com/c/go/+/240003 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Keith Randall <khr@golang.org>
2020-08-17runtime: print faulting instruction on a SIGFPEKeith Randall
Just like SIGILL, it might be useful to see what the instruction that generated the SIGFPE is. Update #39816 Change-Id: I8b2ff692998f0b770289339537dceab96b09d1ee Reviewed-on: https://go-review.googlesource.com/c/go/+/239999 Run-TryBot: Keith Randall <khr@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-06-26runtime: don't preempt if preemption is disabledCherry Zhang
If asyncpreemptoff is set, don't preempt upon receiving a SIGURG. Fixes #38531. Change-Id: I6d9a828b45c199d3e479cbdfe17a896a40d540f9 Reviewed-on: https://go-review.googlesource.com/c/go/+/240121 Run-TryBot: Cherry Zhang <cherryyz@google.com> Reviewed-by: Keith Randall <khr@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2020-06-15runtime: set g to gsignal before adjustSignalStackCherry Zhang
When a signal is received, the runtime probes whether an alternate signal stack is set, if so, adjust gsignal's stack to point to the alternate signal stack. This is done in adjustSignalStack, which calls sigaltstack "syscall", which is a libc call on darwin through asmcgocall. asmcgocall decides whether to do stack switch based on whether we're running on g0 stack, gsignal stack, or regular g stack. If g is not set to gsignal, asmcgocall may make wrong decision. Set g first. adjustSignalStack is recursively nosplit, so it is okay that temporarily gsignal.stack doesn't match the stack we're running on. May fix #39079. Change-Id: I59b2c5dc08c3c951f1098fff038bf2e06d7ca055 Reviewed-on: https://go-review.googlesource.com/c/go/+/238020 Run-TryBot: Cherry Zhang <cherryyz@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-05-06cmd/internal/obj, runtime: preempt & restart some instruction sequencesCherry Zhang
On some architectures, for async preemption the injected call needs to clobber a register (usually REGTMP) in order to return to the preempted function. As a consequence, the PC ranges where REGTMP is live are not preemptible. The uses of REGTMP are usually generated by the assembler, where it needs to load or materialize a large constant or offset that doesn't fit into the instruction. In those cases, REGTMP is not live at the start of the instruction sequence. Instead of giving up preemption in those cases, we could preempt it and restart the sequence when resuming the execution. Basically, this is like reissuing an interrupted instruction, except that here the "instruction" is a Prog that consists of multiple machine instructions. For this to work, we need to generate PC data to mark the start of the Prog. Currently this is only done for ARM64. TODO: the split-stack function prologue is currently not async preemptible. We could use this mechanism, preempt it and restart at the function entry. Change-Id: I37cb282f8e606e7ab6f67b3edfdc6063097b4bd1 Reviewed-on: https://go-review.googlesource.com/c/go/+/208126 Run-TryBot: Cherry Zhang <cherryyz@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Austin Clements <austin@google.com>
2020-04-22runtime: crash on SI_USER SigPanic signalIan Lance Taylor
Clean up the code a little bit to make it clearer: Don't check throwsplit for a SI_USER signal. If throwsplit is set for a SigPanic signal, always throw; discard any other flags. Fixes #36420 Change-Id: Ic9dcd1108603d241f71c040504dfdc6e528f9767 Reviewed-on: https://go-review.googlesource.com/c/go/+/228900 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Austin Clements <austin@google.com>
2020-04-16runtime: clean up now unused pushCallSupportedJoel Sing
All platforms now support pushCall, hence remove the now unnecessary pushCallSupported flag/guard. Change-Id: I99e4be73839da68a742f3c239bae9ce2f8764624 Reviewed-on: https://go-review.googlesource.com/c/go/+/228497 Run-TryBot: Joel Sing <joel@sing.id.au> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Cherry Zhang <cherryyz@google.com>
2020-04-08all: remove scattered remnants of darwin/armAustin Clements
This removes all conditions and conditional code (that I could find) that depended on darwin/arm. Fixes #35439 (since that only happened on darwin/arm) Fixes #37611. Change-Id: Ia4c32a5a4368ed75231075832b0b5bfb1ad11986 Reviewed-on: https://go-review.googlesource.com/c/go/+/227198 Run-TryBot: Austin Clements <austin@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Cherry Zhang <cherryyz@google.com>
2020-03-18runtime: don't send preemption signal if there is a signal pendingCherry Zhang
If multiple threads call preemptone to preempt the same M, it may send many signals to the same M such that it hardly make progress, causing live-lock problem. Only send a signal if there isn't already one pending. Fixes #37741. Change-Id: Id94adb0b95acbd18b23abe637a8dcd81ab41b452 Reviewed-on: https://go-review.googlesource.com/c/go/+/223737 Run-TryBot: Cherry Zhang <cherryyz@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Keith Randall <khr@golang.org>
2020-03-02runtime: print instruction bytes when reporting a SIGILLKeith Randall
Print the bytes of the instruction that generated a SIGILL. This should help us respond to bug reports without having to go back-and-forth with the reporter to get the instruction involved. Might also help with SIGILL problems that are difficult to reproduce. Update #37513 Change-Id: I33059b1dbfc97bce16142a843f32a88a6547e280 Reviewed-on: https://go-review.googlesource.com/c/go/+/221431 Run-TryBot: Keith Randall <khr@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-02-05runtime: don't treat SIGURG as a bad signalIan Lance Taylor
It's possible for the scheduler to try to preempt a goroutine running on a thread created by C code just as the goroutine returns from Go code to C code. If that happens, the goroutine will have a nil g, which would normally cause us to enter the badsignal code. The badsignal code will allocate an M, reset the signal handler, and raise the signal. This is all wasted work for SIGURG, as the default behavior is for the kernel to ignore the signal. It also means that there is a period of time when preemption requests are ignored, because the signal handler is reset to the default. And, finally, it triggers a bug on 386 OpenBSD 6.2. So stop doing it. No test because there is no real change in behavior (other than on OpenBSD), the new code is just more efficient Fixes #36996 Change-Id: I8c1cb9bc09f5ef890cab567924417e2423fc71f6 Reviewed-on: https://go-review.googlesource.com/c/go/+/217617 Reviewed-by: Austin Clements <austin@google.com>
2019-11-26runtime: disable async preemption on darwin/arm(64) if no cgoCherry Zhang
On darwin, we use libc calls, and cgo is required on ARM and ARM64 so we have TLS set up to save/restore G during C calls. If cgo is absent, we cannot save/restore G in TLS, and if a signal is received during C execution we cannot get the G. Therefore don't send signals (and hope that we won't receive any signal during C execution). This can only happen in the go_bootstrap program (otherwise cgo is required). Fixes #35800. Change-Id: I6c02a9378af02c19d32749a42db45165b578188d Reviewed-on: https://go-review.googlesource.com/c/go/+/208818 Run-TryBot: Cherry Zhang <cherryyz@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-11-18runtime: always use Go signal stack in non-cgo programCherry Zhang
When initializing an M, we set up its signal stack to the gsignal stack if an alternate signal stack is not already set. On Android, an alternate signal stack is always set, even cgo is not used. This breaks the logic of saving/fetching G on the signal stack during VDSO, which assumes the signal stack is allocated by Go if cgo is not used (if cgo is used, we use TLS for saving G). When cgo is not used, we can always use the Go signal stack, even if an alternate signal stack is already set. Since cgo is not used, no one other than the Go runtime will care. Fixes #35554. Change-Id: Ia9d84cd55cb35097f3df46f37996589c86f10e0f Reviewed-on: https://go-review.googlesource.com/c/go/+/207445 Run-TryBot: Cherry Zhang <cherryyz@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-11-15runtime: crash if a signal is received with bad G and no extra MCherry Zhang
When we receive a signal, if G is nil we call badsignal, which calls needm. When cgo is not used, there is no extra M, so needm will just hang. In this situation, even GOTRACEBACK=crash cannot get a stack trace, as we're in the signal handler and cannot receive another signal (SIGQUIT). Instead, just crash. For #35554. Updates #34391. Change-Id: I061ac43fc0ac480435c050083096d126b149d21f Reviewed-on: https://go-review.googlesource.com/c/go/+/206959 Run-TryBot: Cherry Zhang <cherryyz@google.com> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-11-07runtime: add async preemption support on MIPS and MIPS64Cherry Zhang
This CL adds support of call injection and async preemption on MIPS and MIPS64. Like ARM64, we need to clobber one register (REGTMP) for returning from the injected call. Previous CLs have marked code sequences that use REGTMP async-nonpreemtible. It seems on MIPS/MIPS64, a CALL instruction is not "atomic" (!). If a signal is delivered right at the CALL instruction, we may see an updated LR with a not-yet-updated PC. In some cases this may lead to failed stack unwinding. Don't preempt in this case. Change-Id: I99437b2d05869ded5c0c8cb55265dbfc933aedab Reviewed-on: https://go-review.googlesource.com/c/go/+/203720 Reviewed-by: Keith Randall <khr@golang.org>
2019-11-05runtime: don't fetch G from signal stack when using cgoCherry Zhang
When using cgo, we save G to TLS, and when a signal happens, we load G from TLS in sigtramp. This should give us a valid G. Don't try to fetch from the signal stack. In particular, C code may change the signal stack or call our signal handler directly (e.g. TSAN), so we are not necessarily running on the original gsignal stack where we saved G. Also skip saving G on the signal stack when using cgo. Updates #35249. Change-Id: I40749ce6682709bd4ebfdfd9f23bd0f317fc197d Reviewed-on: https://go-review.googlesource.com/c/go/+/204519 Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-11-05runtime: setg after sigFetchGCherry Zhang
In the normal case, sigFetchG just returns the G register. But in the case that sigFetchG fetches the G from somewhere else, the G register still holding an invalid value. Setg here to make sure they match. This is particularly useful because setGsignalStack, called by adjustSignalStack from sigtrampgo before setg to gsignal, accesses the G register. Should fix #35249. Change-Id: I64c85143cb05cdb2ecca7f9936dbd8bfec186c2d Reviewed-on: https://go-review.googlesource.com/c/go/+/204441 Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-11-02runtime: use signals to preempt Gs for suspendGAustin Clements
This adds support for pausing a running G by sending a signal to its M. The main complication is that we want to target a G, but can only send a signal to an M. Hence, the protocol we use is to simply mark the G for preemption (which we already do) and send the M a "wake up and look around" signal. The signal checks if it's running a G with a preemption request and stops it if so in the same way that stack check preemptions stop Gs. Since the preemption may fail (the G could be moved or the signal could arrive at an unsafe point), we keep a count of the number of received preemption signals. This lets stopG detect if its request failed and should be retried without an explicit channel back to suspendG. For #10958, #24543. Change-Id: I3e1538d5ea5200aeb434374abb5d5fdc56107e53 Reviewed-on: https://go-review.googlesource.com/c/go/+/201760 Run-TryBot: Austin Clements <austin@google.com> Reviewed-by: Cherry Zhang <cherryyz@google.com>
2019-10-31runtime: atomically set span state and use as publication barrierAustin Clements
When everything is working correctly, any pointer the garbage collector encounters can only point into a fully initialized heap span, since the span must have been initialized before that pointer could escape the heap allocator and become visible to the GC. However, in various cases, we try to be defensive against bad pointers. In findObject, this is just a sanity check: we never expect to find a bad pointer, but programming errors can lead to them. In spanOfHeap, we don't necessarily trust the pointer and we're trying to check if it really does point to the heap, though it should always point to something. Conservative scanning takes this to a new level, since it can only guess that a word may be a pointer and verify this. In all of these cases, we have a problem that the span lookup and check can race with span initialization, since the span becomes visible to lookups before it's fully initialized. Furthermore, we're about to start initializing the span without the heap lock held, which is going to introduce races where accesses were previously protected by the heap lock. To address this, this CL makes accesses to mspan.state atomic, and ensures that the span is fully initialized before setting the state to mSpanInUse. All loads are now atomic, and in any case where we don't trust the pointer, it first atomically loads the span state and checks that it's mSpanInUse, after which it will have synchronized with span initialization and can safely check the other span fields. For #10958, #24543, but a good fix in general. Change-Id: I518b7c63555b02064b98aa5f802c92b758fef853 Reviewed-on: https://go-review.googlesource.com/c/go/+/203286 Run-TryBot: Austin Clements <austin@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com>
2019-10-26runtime: add test for signalMAustin Clements
For #10958, #24543. Change-Id: Ib009a83fe02bc623894f4908fe8f6b266382ba95 Reviewed-on: https://go-review.googlesource.com/c/go/+/201404 Run-TryBot: Austin Clements <austin@google.com> Reviewed-by: Cherry Zhang <cherryyz@google.com>
2019-10-23runtime: save/fetch g register during VDSO on ARM and ARM64Cherry Zhang
On ARM and ARM64, during a VDSO call, the g register may be temporarily clobbered by the VDSO code. If a signal is received during the execution of VDSO code, we may not find a valid g reading the g register. In CL 192937, we conservatively assume g is nil. But this approach has a problem: we cannot handle the signal in this case. Further, if the signal is not a profiling signal, we'll call badsignal, which calls needm, which wants to get an extra m, but we don't have one in a non-cgo binary, which cuases the program to hang. This is even more of a problem with async preemption, where we will receive more signals than before. I ran into this problem while working on async preemption support on ARM64. In this CL, before making a VDSO call, we save the g on the gsignal stack. When we receive a signal, we will be running on the gsignal stack, so we can fetch the g from there and move on. We probably want to do the same for PPC64. Currently we rely on that the VDSO code doesn't actually clobber the g register, but this is not guaranteed and we don't have control with. Idea from discussion with Dan Cross and Austin. Should fix #34391. Change-Id: Idbefc5e4c2f4373192c2be797be0140ae08b26e3 Reviewed-on: https://go-review.googlesource.com/c/go/+/202759 Run-TryBot: Cherry Zhang <cherryyz@google.com> Reviewed-by: Austin Clements <austin@google.com>
2019-10-22runtime: factor signal stack code out of sigtrampgoIan Lance Taylor
This reduces the required nosplit stack size, which permits building on Solaris with -gcflags=all=-N -l. Fixes #35046 Change-Id: Icb3a421bb791c73e2f670ecfadbe32daea79789f Reviewed-on: https://go-review.googlesource.com/c/go/+/202446 Reviewed-by: Bryan C. Mills <bcmills@google.com>
2019-10-11runtime: when disabling SIGPROF handler, ignore SIGPROFIan Lance Taylor
If the runtime disables the SIGPROF handler, because this is Go code that is linked into a non-Go program, then don't go back to the default handling of SIGPROF; just start ignoring SIGPROF. Otherwise the program can get killed by a stray SIGPROF that is delivered, presumably to a different thread, after profiling is disabled. Fixes #19320 Change-Id: Ifebae477d726699c8c82c867604b73110c1cf262 Reviewed-on: https://go-review.googlesource.com/c/go/+/200740 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com>
2019-10-11runtime: move sighandler into signal_unix.goIan Lance Taylor
We couldn't do this before because sighandler was compiled for nacl. Updates #30439 Change-Id: Ieec9938b6a1796c48d251cd8b1db1a42c25f3943 Reviewed-on: https://go-review.googlesource.com/c/go/+/200739 Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-09-23runtime: fix regression on ppc64x from CL 192937Lynn Boger
This fixes a regression introduced with CL 192937. That change was intended to fix a problem in arm and arm64 but also added code to change the behavior in ppc64 and ppc64le even though the error never occurred there. The change to function sigFetchG assumes that the register holding 'g' could be clobbered by vdso code when in fact 'g' is in R30 and that is nonvolatile in the 64-bit PowerPC ELF ABI so would not be clobbered in vdso code. So if this happens somehow the path it takes is incorrect, falling through to a call to badsignal which doesn't seem right. This regression caused intermittent hangs on the builder dashboard for ppc64, and can be reproduced consistently when running os/signal TestStress on some ppc64 systems. I mentioned this problem is issue #34391 because I thought it was related to another problem described there. Change-Id: I2ee3606de302bafe509d300077ce3b44b88571a1 Reviewed-on: https://go-review.googlesource.com/c/go/+/196658 Reviewed-by: Cherry Zhang <cherryyz@google.com> Run-TryBot: Cherry Zhang <cherryyz@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-09-11runtime: fix crash during VDSO calls on armYuichi Nishiwaki
As discussed in #32912, a crash occurs when go runtime calls a VDSO function (say __vdso_clock_gettime) and a signal arrives to that thread. Since VDSO functions temporarily destroy the G register (R10), Go functions asynchronously executed in that thread (i.e. Go's signal handler) can try to load data from the destroyed G, which causes segmentation fault. To fix the issue a guard is inserted in front of sigtrampgo, so that the control escapes from signal handlers without touching G in case the signal occurred in the VDSO context. The test case included in the patch is take from discussion in a relevant thread on github: https://github.com/golang/go/issues/32912#issuecomment-517874531. This patch not only fixes the issue on AArch64 but also that on 32bit ARM. Fixes #32912 Change-Id: I657472e54b7aa3c617fabc5019ce63aa4105624a GitHub-Last-Rev: 28ce42c4a02a060f08c1b0dd1c9a392123fd2ee9 GitHub-Pull-Request: golang/go#34030 Reviewed-on: https://go-review.googlesource.com/c/go/+/192937 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-08-31runtime: don't forward SIGPIPE on macOSElias Naur
macOS and iOS deliver SIGPIPE signals to the main thread and not the thread that raised it by writing to a closed socket or pipe. SIGPIPE signals can be suppressed for sockets with the SO_NOSIGPIPE option, but there is no similar option for pipes. We have no other choice but to never forward SIGPIPE on macOS. This is a fixup of reverted CL 188297. Fixes #33384 Change-Id: I09b258b078857ad3b22025bc2902d1b12d2afd92 Reviewed-on: https://go-review.googlesource.com/c/go/+/191785 Run-TryBot: Elias Naur <mail@eliasnaur.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-08-29Revert "runtime: don't forward SIGPIPE on macOS"Daniel Martí
This reverts CL 188297. Reason for revert: broke multiple of the darwin builders. Fixes #33943. Change-Id: Iacff98d1450edc70402dc7a220d16fcd73337c9e Reviewed-on: https://go-review.googlesource.com/c/go/+/191784 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> Reviewed-by: Bryan C. Mills <bcmills@google.com>
2019-08-29runtime: don't forward SIGPIPE on macOSElias Naur
macOS and iOS deliver SIGPIPE signals to the main thread and not the thread that raised it by writing to a closed socket or pipe. SIGPIPE signals can be suppressed for sockets with the SO_NOSIGPIPE option, but there is no similar option for pipes. We have no other choice but to never forward SIGPIPE on macOS. Fixes #33384 Change-Id: Ice3de75b121f00006ee11c26d560e619536460be Reviewed-on: https://go-review.googlesource.com/c/go/+/188297 Run-TryBot: Elias Naur <mail@eliasnaur.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-06-06runtime: mark all Go symbols called from assembly in other packagesAustin Clements
This marks all Go symbols called from assembly in other packages with "go:linkname" directives to ensure they get ABI wrappers. Now that we have this go:linkname convention, this also removes the abi0Syms definition in the runtime, which was used to give morestackc an ABI0 wrapper. Instead, we now just mark morestackc with a go:linkname directive. This was tested with buildall.bash in the default configuration, with -race, and with -gcflags=all=-d=ssa/intrinsics/off. Since I couldn't test cgo on non-Linux configurations, I manually grepped for runtime symbols in runtime/cgo. Updates #31230. Change-Id: I6c8aa56be2ca6802dfa2bf159e49c411b9071bf1 Reviewed-on: https://go-review.googlesource.com/c/go/+/179862 Run-TryBot: Austin Clements <austin@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com>
2019-06-03runtime: fix sigpipe do not check SIGPIPE was ignoredLE Manh Cuong
Fixes #32386 Change-Id: I29ad4113b02264336eebe7865895f07ef386f450 Reviewed-on: https://go-review.googlesource.com/c/go/+/180177 Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-04-02runtime, cmd/dist, misc/cgo: enable c-archive for aix/ppc64Clément Chigot
Change-Id: Ib9a40d5596f5735a00483e2d2db965402f05671b Reviewed-on: https://go-review.googlesource.com/c/go/+/169120 Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-03-05all: join a few chained ifsDaniel Martí
I had been finding these over a year or so, but none were big enough changes to warrant CLs. They're a handful now, so clean them all up in a single commit. The smaller bodies get a bit simpler, but most importantly, the larger bodies get unindented. Change-Id: I5707a6fee27d4c9ff9efd3d363af575d7a4bf2aa Reviewed-on: https://go-review.googlesource.com/c/go/+/165340 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-12-02all: use "reports whether" consistently instead of "returns whether"Tobias Klauser
Follow-up for CL 147037 and after Brad noticed the "returns whether" pattern during the review of CL 150621. Go documentation style for boolean funcs is to say: // Foo reports whether ... func Foo() bool (rather than "returns whether") Created with: $ perl -i -npe 's/returns whether/reports whether/' $(git grep -l "returns whether" | grep -v vendor) Change-Id: I15fe9ff99180ad97750cd05a10eceafdb12dc0b4 Reviewed-on: https://go-review.googlesource.com/c/150918 Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-10-03runtime: add AIX operating systemClément Chigot
This commit adds AIX operating system to runtime package for ppc64 architecture. Only new files and minor changes are in this commit. Others modifications in files like asm_ppc64.s will be in separate commits. Updates: #25893 Change-Id: I9c5e073f5f3debb43b004ad1167694a5afd31cfd Reviewed-on: https://go-review.googlesource.com/c/138716 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2018-05-21runtime: remove darwin workaround for process-wide raiseElias Naur
Now that raise on darwin targets the current thread, we can remove the workaround in dieFromSignal. Change-Id: I1e468dc05e49403ee0bbe0a3a85e764c81fec4f2 Reviewed-on: https://go-review.googlesource.com/110476 Run-TryBot: Elias Naur <elias.naur@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Keith Randall <khr@golang.org>