From c80da0a33a240469892a0b0713f09607efb28752 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Wed, 27 Jan 2021 11:00:21 -0500 Subject: [PATCH] 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 Reviewed-by: Cherry Zhang Reviewed-by: Ian Lance Taylor --- src/runtime/cpuprof.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/runtime/cpuprof.go b/src/runtime/cpuprof.go index 9bfdfe7c74..e5d0193b9c 100644 --- a/src/runtime/cpuprof.go +++ b/src/runtime/cpuprof.go @@ -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) -- 2.50.0