Skip to content

runtime: WIP: fix infinite loop in lockextra on linux/arm #34979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/runtime/crash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,18 @@ func buildTestProg(t *testing.T, binary string, flags ...string) (string, error)
return exe, nil
}

func TestVDSO(t *testing.T) {
func TestSIGPROFInVDSO(t *testing.T) {
t.Parallel()
output := runTestProg(t, "testprog", "SignalInVDSO")
output := runTestProg(t, "testprog", "SIGPROFInVDSO")
want := "success\n"
if output != want {
t.Fatalf("output:\n%s\n\nwanted:\n%s", output, want)
}
}

func TestSIGUSR1InVDSO(t *testing.T) {
t.Parallel()
output := runTestProg(t, "testprog", "SIGUSR1InVDSO")
want := "success\n"
if output != want {
t.Fatalf("output:\n%s\n\nwanted:\n%s", output, want)
Expand Down
78 changes: 65 additions & 13 deletions src/runtime/signal_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,19 +274,56 @@ func sigpipe() {
dieFromSignal(_SIGPIPE)
}

// sigFetchG fetches the value of G safely when running in a signal handler.
// On some architectures, the g value may be clobbered when running in a VDSO.
// See issue #32912.
//
//go:nosplit
func sigFetchG(c *sigctxt) *g {
func sigClobbered(c *sigctxt) bool {
switch GOARCH {
case "arm", "arm64":
if inVDSOPage(c.sigpc()) {
return nil
return inVDSOPage(c.sigpc());
}
return false
}

// sigpending stores signals during the Go signal handler when the g value is clobbered.
// See issue #34391.
var sigpending [(_NSIG + 31) / 32]uint32

func sigAddPending(s uint32) {
for {
p := sigpending[s/32]
q := p | (1 << (s & 31))
if atomic.Cas(&sigpending[s/32], p, q) {
return
}
}
return getg()
}

// sigClearPending is called from outside the signal handler context.
// It should be called just after the clobbered G value is restored.
//go:nosplit
//go:nowritebarrierrec
func sigClearPending() {
for s := 0; s < _NSIG; s++ {
// steal signal from pending queue
steal := false
for {
p := sigpending[s/32]
if p & (1 << (s & 31)) == 0 {
break
}
q := p &^ (1 << (s & 31))
if atomic.Cas(&sigpending[s/32], p, q) {
steal = true
break
}
}
if !steal {
continue
}
raise(uint32(s))
}
}

// sigtrampgo is called from the signal handler function, sigtramp,
Expand All @@ -305,7 +342,16 @@ func sigtrampgo(sig uint32, info *siginfo, ctx unsafe.Pointer) {
return
}
c := &sigctxt{info, ctx}
g := sigFetchG(c)
if sigClobbered(c) {
if sig == _SIGPROF {
sigprofNonGoPC(c.sigpc())
return
}
// at this point iscgo must be true
sigAddPending(sig)
return
}
g := getg()
if g == nil {
if sig == _SIGPROF {
sigprofNonGoPC(c.sigpc())
Expand Down Expand Up @@ -670,13 +716,19 @@ func sigfwdgo(sig uint32, info *siginfo, ctx unsafe.Pointer) bool {
if (c.sigcode() == _SI_USER || flags&_SigPanic == 0) && sig != _SIGPIPE {
return false
}
// Determine if the signal occurred inside Go code. We test that:
// (1) we weren't in VDSO page,
// (2) we were in a goroutine (i.e., m.curg != nil), and
// (3) we weren't in CGO.
g := sigFetchG(c)
if g != nil && g.m != nil && g.m.curg != nil && !g.m.incgo {
return false
if sigClobbered(c) {
// There is no handler to be forwarded to.
if !iscgo {
return false
}
} else {
// Determine if the signal occurred inside Go code. We test that:
// (1) we were in a goroutine (i.e., m.curg != nil), and
// (2) we weren't in CGO.
g := getg()
if g != nil && g.m != nil && g.m.curg != nil && !g.m.incgo {
return false
}
}

// Signal not handled by Go, forward it.
Expand Down
18 changes: 18 additions & 0 deletions src/runtime/sys_linux_arm.s
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,15 @@ noswitch:
B.EQ fallback

BL (R11)

SUB $24, R13
MOVW R4, 8(R13)
MOVW R5, 12(R13)
BL runtime·sigClearPending(SB)
MOVW 12(R13), R5
MOVW 8(R13), R4
ADD $24, R13

JMP finish

fallback:
Expand Down Expand Up @@ -298,6 +307,15 @@ noswitch:
B.EQ fallback

BL (R11)

SUB $24, R13
MOVW R4, 8(R13)
MOVW R5, 12(R13)
BL runtime·sigClearPending(SB)
MOVW 12(R13), R5
MOVW 8(R13), R4
ADD $24, R13

JMP finish

fallback:
Expand Down
14 changes: 14 additions & 0 deletions src/runtime/sys_linux_arm64.s
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,13 @@ noswitch:
MOVD runtime·vdsoClockgettimeSym(SB), R2
CBZ R2, fallback
BL (R2)

SUB $24, RSP
MOVD R20, 16(RSP)
BL runtime·sigClearPending(SB)
MOVD 16(RSP), R20
ADD $24, RSP

B finish

fallback:
Expand Down Expand Up @@ -251,6 +258,13 @@ noswitch:
MOVD runtime·vdsoClockgettimeSym(SB), R2
CBZ R2, fallback
BL (R2)

SUB $24, RSP
MOVD R20, 16(RSP)
BL runtime·sigClearPending(SB)
MOVD 16(RSP), R20
ADD $24, RSP

B finish

fallback:
Expand Down
34 changes: 31 additions & 3 deletions src/runtime/testdata/testprog/vdso.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Invoke signal hander in the VDSO context (see issue 32912).
// Invoke signal hander in the VDSO context.
// See issue 32912 and 34391.

package main

Expand All @@ -11,14 +12,16 @@ import (
"io/ioutil"
"os"
"runtime/pprof"
"syscall"
"time"
)

func init() {
register("SignalInVDSO", signalInVDSO)
register("SIGPROFInVDSO", signalSIGPROFInVDSO)
register("SIGUSR1InVDSO", signalSIGUSR1InVDSO)
}

func signalInVDSO() {
func signalSIGPROFInVDSO() {
f, err := ioutil.TempFile("", "timeprofnow")
if err != nil {
fmt.Fprintln(os.Stderr, err)
Expand Down Expand Up @@ -53,3 +56,28 @@ func signalInVDSO() {

fmt.Println("success")
}

func signalSIGUSR1InVDSO() {
done := make(chan struct {})

p, _ := os.FindProcess(os.Getpid())
go func() {
for {
p.Signal(syscall.SIGUSR1)
select {
case <- done:
return
default:
}
}
}()

t0 := time.Now()
t1 := t0
for t1.Sub(t0) < time.Second {
t1 = time.Now()
}
close(done)

fmt.Println("success")
}