]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: keep FuncForPC from crashing for PCs between functions
authorKeith Randall <khr@google.com>
Mon, 14 Jan 2019 21:47:14 +0000 (13:47 -0800)
committerKeith Randall <khr@golang.org>
Mon, 14 Jan 2019 23:37:39 +0000 (23:37 +0000)
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>

src/runtime/symtab.go
test/fixedbugs/issue29735.go [new file with mode: 0644]

index e7ce3de497473ec37247603933254b09e1f3f1c1..17e342ef699a017c2849d6b8b50a1b36222ff591 100644 (file)
@@ -474,7 +474,11 @@ func FuncForPC(pc uintptr) *Func {
                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 @@ func funcspdelta(f funcInfo, targetpc uintptr, cache *pcvalueCache) int32 {
        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 (file)
index 0000000..7a0381d
--- /dev/null
@@ -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