Commit 462e9025 authored by Keith Randall's avatar Keith Randall Committed by Keith Randall

runtime: keep FuncForPC from crashing for PCs between functions

Reuse the strict mechanism from FileLine for FuncForPC, so we don't
crash when asking the pcln table about bad pcs.

Fixes #29735

Change-Id: Iaffb32498b8586ecf4eae03823e8aecef841aa68
Reviewed-on: https://go-review.googlesource.com/c/157799Reviewed-by: 's avatarJosh Bleecher Snyder <josharian@gmail.com>
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent e1d20ce2
...@@ -474,7 +474,11 @@ func FuncForPC(pc uintptr) *Func { ...@@ -474,7 +474,11 @@ func FuncForPC(pc uintptr) *Func {
return nil return nil
} }
if inldata := funcdata(f, _FUNCDATA_InlTree); inldata != nil { if inldata := funcdata(f, _FUNCDATA_InlTree); inldata != nil {
if ix := pcdatavalue(f, _PCDATA_InlTreeIndex, pc, nil); ix >= 0 { // Note: strict=false so bad PCs (those between functions) don't crash the runtime.
// We just report the preceeding function in that situation. See issue 29735.
// TODO: Perhaps we should report no function at all in that case.
// The runtime currently doesn't have function end info, alas.
if ix := pcdatavalue1(f, _PCDATA_InlTreeIndex, pc, nil, false); ix >= 0 {
inltree := (*[1 << 20]inlinedCall)(inldata) inltree := (*[1 << 20]inlinedCall)(inldata)
name := funcnameFromNameoff(f, inltree[ix].func_) name := funcnameFromNameoff(f, inltree[ix].func_)
file, line := funcline(f, pc) file, line := funcline(f, pc)
...@@ -756,12 +760,22 @@ func funcspdelta(f funcInfo, targetpc uintptr, cache *pcvalueCache) int32 { ...@@ -756,12 +760,22 @@ func funcspdelta(f funcInfo, targetpc uintptr, cache *pcvalueCache) int32 {
return x return x
} }
func pcdatastart(f funcInfo, table int32) int32 {
return *(*int32)(add(unsafe.Pointer(&f.nfuncdata), unsafe.Sizeof(f.nfuncdata)+uintptr(table)*4))
}
func pcdatavalue(f funcInfo, table int32, targetpc uintptr, cache *pcvalueCache) int32 { func pcdatavalue(f funcInfo, table int32, targetpc uintptr, cache *pcvalueCache) int32 {
if table < 0 || table >= f.npcdata { if table < 0 || table >= f.npcdata {
return -1 return -1
} }
off := *(*int32)(add(unsafe.Pointer(&f.nfuncdata), unsafe.Sizeof(f.nfuncdata)+uintptr(table)*4)) return pcvalue(f, pcdatastart(f, table), targetpc, cache, true)
return pcvalue(f, off, targetpc, cache, true) }
func pcdatavalue1(f funcInfo, table int32, targetpc uintptr, cache *pcvalueCache, strict bool) int32 {
if table < 0 || table >= f.npcdata {
return -1
}
return pcvalue(f, pcdatastart(f, table), targetpc, cache, strict)
} }
func funcdata(f funcInfo, i uint8) unsafe.Pointer { func funcdata(f funcInfo, i uint8) unsafe.Pointer {
......
// run
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Make sure FuncForPC won't panic when given a pc which
// lies between two functions.
package main
import (
"runtime"
)
func main() {
var stack [1]uintptr
runtime.Callers(1, stack[:])
f() // inlined function, to give main some inlining info
for i := uintptr(0); true; i++ {
f := runtime.FuncForPC(stack[0] + i)
if f.Name() != "main.main" && f.Name() != "main.f" {
// Reached next function successfully.
break
}
}
}
func f() {
sink = 0 // one instruction which can't be removed
}
var sink int
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment