Skip to content

Commit d9dab4f

Browse files
[release-branch.go1.14] runtime: stop preemption during syscall.Exec on Darwin
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. For #41702 Fixes #41703 Change-Id: I91b0add967b315671ddcf73269c4d30136e579b4 Reviewed-on: https://go-review.googlesource.com/c/go/+/262438 Trust: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Cherry Zhang <[email protected]> (cherry picked from commit 64fb6ae) Reviewed-on: https://go-review.googlesource.com/c/go/+/262737
1 parent fa44af7 commit d9dab4f

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

src/runtime/signal_unix.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,13 @@ func preemptM(mp *m) {
360360
// required).
361361
return
362362
}
363+
364+
// On Darwin, don't try to preempt threads during exec.
365+
// Issue #41702.
366+
if GOOS == "darwin" {
367+
execLock.rlock()
368+
}
369+
363370
if atomic.Cas(&mp.signalPending, 0, 1) {
364371
// If multiple threads are preempting the same M, it may send many
365372
// signals to the same M such that it hardly make progress, causing
@@ -368,6 +375,10 @@ func preemptM(mp *m) {
368375
// Only send a signal if there isn't already one pending.
369376
signalM(mp, sigPreempt)
370377
}
378+
379+
if GOOS == "darwin" {
380+
execLock.runlock()
381+
}
371382
}
372383

373384
// sigFetchG fetches the value of G safely when running in a signal handler.

src/syscall/exec_unix_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ package syscall_test
99
import (
1010
"internal/testenv"
1111
"io"
12+
"math/rand"
1213
"os"
1314
"os/exec"
1415
"os/signal"
16+
"runtime"
1517
"syscall"
1618
"testing"
19+
"time"
1720
"unsafe"
1821
)
1922

@@ -213,3 +216,45 @@ func TestForeground(t *testing.T) {
213216

214217
signal.Reset()
215218
}
219+
220+
// TestExec is for issue #41702.
221+
func TestExec(t *testing.T) {
222+
cmd := exec.Command(os.Args[0], "-test.run=TestExecHelper")
223+
cmd.Env = append(os.Environ(), "GO_WANT_HELPER_PROCESS=2")
224+
o, err := cmd.CombinedOutput()
225+
if err != nil {
226+
t.Errorf("%s\n%v", o, err)
227+
}
228+
}
229+
230+
// TestExecHelper is used by TestExec. It does nothing by itself.
231+
// In testing on macOS 10.14, this used to fail with
232+
// "signal: illegal instruction" more than half the time.
233+
func TestExecHelper(t *testing.T) {
234+
if os.Getenv("GO_WANT_HELPER_PROCESS") != "2" {
235+
return
236+
}
237+
238+
// We don't have to worry about restoring these values.
239+
// We are in a child process that only runs this test,
240+
// and we are going to call syscall.Exec anyhow.
241+
runtime.GOMAXPROCS(50)
242+
os.Setenv("GO_WANT_HELPER_PROCESS", "3")
243+
244+
stop := time.Now().Add(time.Second)
245+
for i := 0; i < 100; i++ {
246+
go func(i int) {
247+
r := rand.New(rand.NewSource(int64(i)))
248+
for time.Now().Before(stop) {
249+
r.Uint64()
250+
}
251+
}(i)
252+
}
253+
254+
time.Sleep(10 * time.Millisecond)
255+
256+
argv := []string{os.Args[0], "-test.run=TestExecHelper"}
257+
syscall.Exec(os.Args[0], argv, os.Environ())
258+
259+
t.Error("syscall.Exec returned")
260+
}

0 commit comments

Comments
 (0)