]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: clean up canpanic
authorMichael Pratt <mpratt@google.com>
Wed, 20 Jul 2022 15:09:14 +0000 (11:09 -0400)
committerMichael Pratt <mpratt@google.com>
Tue, 2 Aug 2022 18:50:55 +0000 (18:50 +0000)
* The gp argument to canpanic is always equivalent to getg(), so no need
to pass it at all.
* gp must not be nil or _g_.m would have crashed, so no need to check
for nil.
* Use acquirem to better reason about preemption.

Change-Id: Ic7dc8dc1e56ab4c1644965f6aeba16807cdb2df4
Reviewed-on: https://go-review.googlesource.com/c/go/+/418575
Reviewed-by: Austin Clements <austin@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Michael Pratt <mpratt@google.com>

src/runtime/os_js.go
src/runtime/os_plan9.go
src/runtime/panic.go
src/runtime/signal_unix.go
src/runtime/signal_windows.go

index 34cc0271f050b70a77549016cad91ce770b87b3a..7fbeb5a832b2849590b7f2bfa34412b7c46bb455 100644 (file)
@@ -50,7 +50,7 @@ const _SIGSEGV = 0xb
 
 func sigpanic() {
        g := getg()
-       if !canpanic(g) {
+       if !canpanic() {
                throw("unexpected signal during runtime execution")
        }
 
index f0e7c6ae70f814e28562182af88604e20377641f..b86bd6b3a9f30b2c8edf0dad8a734233c50a1ee9 100644 (file)
@@ -76,7 +76,7 @@ func os_sigpipe() {
 
 func sigpanic() {
        g := getg()
-       if !canpanic(g) {
+       if !canpanic() {
                throw("unexpected signal during runtime execution")
        }
 
index 121f2022a4ef2587138a3f059fade138e806fb40..ab8d1f82b406da5f4149c55348aa496cd2b22992 100644 (file)
@@ -1290,29 +1290,32 @@ func dopanic_m(gp *g, pc, sp uintptr) bool {
 // panicking.
 //
 //go:nosplit
-func canpanic(gp *g) bool {
-       // Note that g is m->gsignal, different from gp.
-       // Note also that g->m can change at preemption, so m can go stale
-       // if this function ever makes a function call.
-       _g_ := getg()
-       mp := _g_.m
+func canpanic() bool {
+       gp := getg()
+       mp := acquirem()
 
        // Is it okay for gp to panic instead of crashing the program?
        // Yes, as long as it is running Go code, not runtime code,
        // and not stuck in a system call.
-       if gp == nil || gp != mp.curg {
+       if gp != mp.curg {
+               releasem(mp)
                return false
        }
-       if mp.locks != 0 || mp.mallocing != 0 || mp.throwing != throwTypeNone || mp.preemptoff != "" || mp.dying != 0 {
+       // N.B. mp.locks != 1 instead of 0 to account for acquirem.
+       if mp.locks != 1 || mp.mallocing != 0 || mp.throwing != throwTypeNone || mp.preemptoff != "" || mp.dying != 0 {
+               releasem(mp)
                return false
        }
        status := readgstatus(gp)
        if status&^_Gscan != _Grunning || gp.syscallsp != 0 {
+               releasem(mp)
                return false
        }
        if GOOS == "windows" && mp.libcallsp != 0 {
+               releasem(mp)
                return false
        }
+       releasem(mp)
        return true
 }
 
index 0be499b2e9a9a9c9946e87df13b19129f93a47d3..a220f8347e50dce2acd0718e5e1110ba003fbafe 100644 (file)
@@ -815,7 +815,7 @@ func sighandler(sig uint32, info *siginfo, ctxt unsafe.Pointer, gp *g) {
 //go:linkname sigpanic
 func sigpanic() {
        g := getg()
-       if !canpanic(g) {
+       if !canpanic() {
                throw("unexpected signal during runtime execution")
        }
 
index c5cf38c5c2b7db206ff45fe09a0d928eb87ff4a7..f732d1d5c0e08b4cbf5f146a41306efc2bdedcb8 100644 (file)
@@ -245,7 +245,7 @@ func winthrow(info *exceptionrecord, r *context, gp *g) {
 
 func sigpanic() {
        g := getg()
-       if !canpanic(g) {
+       if !canpanic() {
                throw("unexpected signal during runtime execution")
        }