runtime: handle nil gp in cpuprof

This can happen on Windows when recording profile samples for system threads.

This CL is part of a stack adding windows/arm64
support (#36439), intended to land in the Go 1.17 cycle.
This CL is, however, not windows/arm64-specific.
It is cleanup meant to make the port (and future ports) easier.

Change-Id: I5a7ba32b1900a69f3b7acada9cb6cf8396d8a03f
Reviewed-on: https://go-review.googlesource.com/c/go/+/288797
Trust: Russ Cox <rsc@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Russ Cox 2021-01-27 11:00:21 -05:00
parent a78879ac67
commit c80da0a33a

View file

@ -103,7 +103,16 @@ func (p *cpuProfile) add(gp *g, stk []uintptr) {
// because otherwise its write barrier behavior may not
// be correct. See the long comment there before
// changing the argument here.
cpuprof.log.write(&gp.labels, nanotime(), hdr[:], stk)
//
// Note: it can happen on Windows, where we are calling
// p.add with a gp that is not the current g, that gp is nil,
// meaning we interrupted a system thread with no g.
// Avoid faulting in that case.
var tagPtr *unsafe.Pointer
if gp != nil {
tagPtr = &gp.labels
}
cpuprof.log.write(tagPtr, nanotime(), hdr[:], stk)
}
atomic.Store(&prof.signalLock, 0)