| 1 |
// Copyright 2009 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package runtime_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"runtime" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
"unsafe" |
| 12 |
) |
| 13 |
|
| 14 |
var _ = runtime.Caller |
| 15 |
var _ = strings.HasSuffix |
| 16 |
|
| 17 |
type _ testing.T |
| 18 |
|
| 19 |
func TestCaller(t *testing.T) { |
| 20 |
procs := runtime.GOMAXPROCS(-1) |
| 21 |
c := make(chan bool, procs) |
| 22 |
for p := 0; p < procs; p++ { |
| 23 |
go func() { |
| 24 |
for i := 0; i < 1000; i++ { |
| 25 |
testCallerFoo(t) |
| 26 |
} |
| 27 |
c <- true |
| 28 |
}() |
| 29 |
defer func() { |
| 30 |
<-c |
| 31 |
}() |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
// These are marked noinline so that we can use FuncForPC |
| 36 |
// in testCallerBar. |
| 37 |
//go:noinline |
| 38 |
func testCallerFoo(t *testing.T) { |
| 39 |
testCallerBar(t) |
| 40 |
} |
| 41 |
|
| 42 |
//go:noinline |
| 43 |
func testCallerBar(t *testing.T) { |
| 44 |
for i := 0; i < 2; i++ { |
| 45 |
pc, file, line, ok := runtime.Caller(i) |
| 46 |
f := runtime.FuncForPC(pc) |
| 47 |
if !ok || |
| 48 |
!strings.HasSuffix(file, "symtab_test.go") || |
| 49 |
// FuncForPC doesn't work gccgo, because of inlining. |
| 50 |
// (i == 0 && !strings.HasSuffix(f.Name(), "testCallerBar")) || |
| 51 |
// (i == 1 && !strings.HasSuffix(f.Name(), "testCallerFoo")) || |
| 52 |
line < 5 || line > 1000 || |
| 53 |
f.Entry() >= pc { |
| 54 |
t.Errorf("incorrect symbol info %d: %t %d %d %s %s %d", |
| 55 |
i, ok, f.Entry(), pc, f.Name(), file, line) |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
func lineNumber() int { |
| 61 |
_, _, line, _ := runtime.Caller(1) |
| 62 |
return line // return 0 for error |
| 63 |
} |
| 64 |
|
| 65 |
// Do not add/remove lines in this block without updating the line numbers. |
| 66 |
var firstLine = lineNumber() // 0 |
| 67 |
var ( // 1 |
| 68 |
lineVar1 = lineNumber() // 2 |
| 69 |
lineVar2a, lineVar2b = lineNumber(), lineNumber() // 3 |
| 70 |
) // 4 |
| 71 |
var compLit = []struct { // 5 |
| 72 |
lineA, lineB int // 6 |
| 73 |
}{ // 7 |
| 74 |
{ // 8 |
| 75 |
lineNumber(), lineNumber(), // 9 |
| 76 |
}, // 10 |
| 77 |
{ // 11 |
| 78 |
lineNumber(), // 12 |
| 79 |
lineNumber(), // 13 |
| 80 |
}, // 14 |
| 81 |
{ // 15 |
| 82 |
lineB: lineNumber(), // 16 |
| 83 |
lineA: lineNumber(), // 17 |
| 84 |
}, // 18 |
| 85 |
} // 19 |
| 86 |
var arrayLit = [...]int{lineNumber(), // 20 |
| 87 |
lineNumber(), lineNumber(), // 21 |
| 88 |
lineNumber(), // 22 |
| 89 |
} // 23 |
| 90 |
var sliceLit = []int{lineNumber(), // 24 |
| 91 |
lineNumber(), lineNumber(), // 25 |
| 92 |
lineNumber(), // 26 |
| 93 |
} // 27 |
| 94 |
var mapLit = map[int]int{ // 28 |
| 95 |
29: lineNumber(), // 29 |
| 96 |
30: lineNumber(), // 30 |
| 97 |
lineNumber(): 31, // 31 |
| 98 |
lineNumber(): 32, // 32 |
| 99 |
} // 33 |
| 100 |
var intLit = lineNumber() + // 34 |
| 101 |
lineNumber() + // 35 |
| 102 |
lineNumber() // 36 |
| 103 |
func trythis() { // 37 |
| 104 |
recordLines(lineNumber(), // 38 |
| 105 |
lineNumber(), // 39 |
| 106 |
lineNumber()) // 40 |
| 107 |
} |
| 108 |
|
| 109 |
// Modifications below this line are okay. |
| 110 |
|
| 111 |
var l38, l39, l40 int |
| 112 |
|
| 113 |
func recordLines(a, b, c int) { |
| 114 |
l38 = a |
| 115 |
l39 = b |
| 116 |
l40 = c |
| 117 |
} |
| 118 |
|
| 119 |
func TestLineNumber(t *testing.T) { |
| 120 |
trythis() |
| 121 |
for _, test := range []struct { |
| 122 |
name string |
| 123 |
val int |
| 124 |
want int |
| 125 |
}{ |
| 126 |
{"firstLine", firstLine, 0}, |
| 127 |
{"lineVar1", lineVar1, 2}, |
| 128 |
{"lineVar2a", lineVar2a, 3}, |
| 129 |
{"lineVar2b", lineVar2b, 3}, |
| 130 |
{"compLit[0].lineA", compLit[0].lineA, 9}, |
| 131 |
{"compLit[0].lineB", compLit[0].lineB, 9}, |
| 132 |
{"compLit[1].lineA", compLit[1].lineA, 12}, |
| 133 |
{"compLit[1].lineB", compLit[1].lineB, 13}, |
| 134 |
{"compLit[2].lineA", compLit[2].lineA, 17}, |
| 135 |
{"compLit[2].lineB", compLit[2].lineB, 16}, |
| 136 |
|
| 137 |
{"arrayLit[0]", arrayLit[0], 20}, |
| 138 |
{"arrayLit[1]", arrayLit[1], 21}, |
| 139 |
{"arrayLit[2]", arrayLit[2], 21}, |
| 140 |
{"arrayLit[3]", arrayLit[3], 22}, |
| 141 |
|
| 142 |
{"sliceLit[0]", sliceLit[0], 24}, |
| 143 |
{"sliceLit[1]", sliceLit[1], 25}, |
| 144 |
{"sliceLit[2]", sliceLit[2], 25}, |
| 145 |
{"sliceLit[3]", sliceLit[3], 26}, |
| 146 |
|
| 147 |
{"mapLit[29]", mapLit[29], 29}, |
| 148 |
{"mapLit[30]", mapLit[30], 30}, |
| 149 |
{"mapLit[31]", mapLit[31+firstLine] + firstLine, 31}, // nb it's the key not the value |
| 150 |
{"mapLit[32]", mapLit[32+firstLine] + firstLine, 32}, // nb it's the key not the value |
| 151 |
|
| 152 |
{"intLit", intLit - 2*firstLine, 34 + 35 + 36}, |
| 153 |
|
| 154 |
{"l38", l38, 38}, |
| 155 |
{"l39", l39, 39}, |
| 156 |
{"l40", l40, 40}, |
| 157 |
} { |
| 158 |
if got := test.val - firstLine; got != test.want { |
| 159 |
t.Errorf("%s on firstLine+%d want firstLine+%d (firstLine=%d, val=%d)", |
| 160 |
test.name, got, test.want, firstLine, test.val) |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
func TestNilName(t *testing.T) { |
| 166 |
defer func() { |
| 167 |
if ex := recover(); ex != nil { |
| 168 |
t.Fatalf("expected no nil panic, got=%v", ex) |
| 169 |
} |
| 170 |
}() |
| 171 |
if got := (*runtime.Func)(nil).Name(); got != "" { |
| 172 |
t.Errorf("Name() = %q, want %q", got, "") |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
var dummy int |
| 177 |
|
| 178 |
func inlined() { |
| 179 |
// Side effect to prevent elimination of this entire function. |
| 180 |
dummy = 42 |
| 181 |
} |
| 182 |
|
| 183 |
// A function with an InlTree. Returns a PC within the function body. |
| 184 |
// |
| 185 |
// No inline to ensure this complete function appears in output. |
| 186 |
// |
| 187 |
//go:noinline |
| 188 |
func tracebackFunc(t *testing.T) uintptr { |
| 189 |
// This body must be more complex than a single call to inlined to get |
| 190 |
// an inline tree. |
| 191 |
inlined() |
| 192 |
inlined() |
| 193 |
|
| 194 |
// Acquire a PC in this function. |
| 195 |
pc, _, _, ok := runtime.Caller(0) |
| 196 |
if !ok { |
| 197 |
t.Fatalf("Caller(0) got ok false, want true") |
| 198 |
} |
| 199 |
|
| 200 |
return pc |
| 201 |
} |
| 202 |
|
| 203 |
// Test that CallersFrames handles PCs in the alignment region between |
| 204 |
// functions (int 3 on amd64) without crashing. |
| 205 |
// |
| 206 |
// Go will never generate a stack trace containing such an address, as it is |
| 207 |
// not a valid call site. However, the cgo traceback function passed to |
| 208 |
// runtime.SetCgoTraceback may not be completely accurate and may incorrect |
| 209 |
// provide PCs in Go code or the alignement region between functions. |
| 210 |
// |
| 211 |
// Go obviously doesn't easily expose the problematic PCs to running programs, |
| 212 |
// so this test is a bit fragile. Some details: |
| 213 |
// |
| 214 |
// * tracebackFunc is our target function. We want to get a PC in the |
| 215 |
// alignment region following this function. This function also has other |
| 216 |
// functions inlined into it to ensure it has an InlTree (this was the source |
| 217 |
// of the bug in issue 44971). |
| 218 |
// |
| 219 |
// * We acquire a PC in tracebackFunc, walking forwards until FuncForPC says |
| 220 |
// we're in a new function. The last PC of the function according to FuncForPC |
| 221 |
// should be in the alignment region (assuming the function isn't already |
| 222 |
// perfectly aligned). |
| 223 |
// |
| 224 |
// This is a regression test for issue 44971. |
| 225 |
func TestFunctionAlignmentTraceback(t *testing.T) { |
| 226 |
pc := tracebackFunc(t) |
| 227 |
|
| 228 |
// Double-check we got the right PC. |
| 229 |
f := runtime.FuncForPC(pc) |
| 230 |
if !strings.HasSuffix(f.Name(), "tracebackFunc") { |
| 231 |
t.Fatalf("Caller(0) = %+v, want tracebackFunc", f) |
| 232 |
} |
| 233 |
|
| 234 |
// Iterate forward until we find a different function. Back up one |
| 235 |
// instruction is (hopefully) an alignment instruction. |
| 236 |
for runtime.FuncForPC(pc) == f { |
| 237 |
pc++ |
| 238 |
} |
| 239 |
pc-- |
| 240 |
|
| 241 |
// Is this an alignment region filler instruction? We only check this |
| 242 |
// on amd64 for simplicity. If this function has no filler, then we may |
| 243 |
// get a false negative, but will never get a false positive. |
| 244 |
if runtime.GOARCH == "amd64" && runtime.Compiler == "gc" { |
| 245 |
code := *(*uint8)(unsafe.Pointer(pc)) |
| 246 |
if code != 0xcc { // INT $3 |
| 247 |
t.Errorf("PC %v code got %#x want 0xcc", pc, code) |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
// Finally ensure that Frames.Next doesn't crash when processing this |
| 252 |
// PC. |
| 253 |
frames := runtime.CallersFrames([]uintptr{pc}) |
| 254 |
frame, _ := frames.Next() |
| 255 |
if *frame.Func != *f { |
| 256 |
t.Errorf("frames.Next() got %+v want %+v", frame.Func, f) |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
func BenchmarkFunc(b *testing.B) { |
| 261 |
pc, _, _, ok := runtime.Caller(0) |
| 262 |
if !ok { |
| 263 |
b.Fatal("failed to look up PC") |
| 264 |
} |
| 265 |
f := runtime.FuncForPC(pc) |
| 266 |
b.Run("Name", func(b *testing.B) { |
| 267 |
for i := 0; i < b.N; i++ { |
| 268 |
name := f.Name() |
| 269 |
if name != "runtime_test.BenchmarkFunc" { |
| 270 |
b.Fatalf("unexpected name %q", name) |
| 271 |
} |
| 272 |
} |
| 273 |
}) |
| 274 |
b.Run("Entry", func(b *testing.B) { |
| 275 |
for i := 0; i < b.N; i++ { |
| 276 |
pc := f.Entry() |
| 277 |
if pc == 0 { |
| 278 |
b.Fatal("zero PC") |
| 279 |
} |
| 280 |
} |
| 281 |
}) |
| 282 |
b.Run("FileLine", func(b *testing.B) { |
| 283 |
for i := 0; i < b.N; i++ { |
| 284 |
file, line := f.FileLine(pc) |
| 285 |
if !strings.HasSuffix(file, "symtab_test.go") || line == 0 { |
| 286 |
b.Fatalf("unexpected file/line %q:%d", file, line) |
| 287 |
} |
| 288 |
} |
| 289 |
}) |
| 290 |
} |