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/157799
Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
diff --git a/src/runtime/symtab.go b/src/runtime/symtab.go
index e7ce3de..17e342e 100644
--- a/src/runtime/symtab.go
+++ b/src/runtime/symtab.go
@@ -474,7 +474,11 @@
 		return 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)
 			name := funcnameFromNameoff(f, inltree[ix].func_)
 			file, line := funcline(f, pc)
@@ -756,12 +760,22 @@
 	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 {
 	if table < 0 || table >= f.npcdata {
 		return -1
 	}
-	off := *(*int32)(add(unsafe.Pointer(&f.nfuncdata), unsafe.Sizeof(f.nfuncdata)+uintptr(table)*4))
-	return pcvalue(f, off, targetpc, cache, true)
+	return pcvalue(f, pcdatastart(f, table), 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 {
diff --git a/test/fixedbugs/issue29735.go b/test/fixedbugs/issue29735.go
new file mode 100644
index 0000000..7a0381d
--- /dev/null
+++ b/test/fixedbugs/issue29735.go
@@ -0,0 +1,33 @@
+// 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