Debian Codegraph @

stack/source_test.go

raw
1 // Copyright 2015 Marc-Antoine Ruel. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file.
4
5 package stack
6
7 import (
8 "bytes"
9 "fmt"
10 "math/bits"
11 "os"
12 "os/exec"
13 "path"
14 "path/filepath"
15 "regexp"
16 "runtime"
17 "strings"
18 "testing"
19
20 "github.com/google/go-cmp/cmp"
21 "github.com/maruel/panicparse/v2/internal/internaltest"
22 )
23
24 // goarchList is a list of GOARCH values.
25 type goarchList []string
26
27 var allPlatforms goarchList = nil
28
29 func TestAugment(t *testing.T) {
30 t.Parallel()
31
32 gm := map[string]string{"/root": "main"}
33 newCallSrc := func(f string, a Args, s string, l int) Call {
34 c := newCall(f, a, s, l)
35 // Simulate findRoots().
36 if !c.updateLocations(goroot, goroot, gm, gopaths) {
37 t.Fatalf("c.updateLocations(%v, %v, %v, %v) failed on %s", goroot, goroot, gm, gopaths, s)
38 }
39 return c
40 }
41
42 // For test case "negative int32".
43 negInt := uint64(4294967173)
44 negPtr := false
45 if bits.UintSize == 64 {
46 negInt = 828928688005
47 negPtr = true
48 }
49
50 type testCase struct {
51 name string
52 input string
53 // Due to inlining, the stack trace may (it depends on tool chain version)
54 // not contain much information about the arguments and shows as elided.
55 // Non-pointer call may show an elided argument, while there was no
56 // argument listed before.
57 mayBeInlined bool
58 // archBlock lists the CPU architectures to skip this test case on.
59 //
60 // Many test are hard to parse in 32 bits. Eventually we should fix these
61 // but I don't have time for this.
62 //
63 // The list is based on https://github.com/maruel/panicparse/issues/80.
64 // I was able to locally reproduce amd64 and 386 but not the rest.
65 archBlock goarchList
66 want Stack
67 }
68 data := []testCase{
69 {
70 "local function doesn't interfere",
71 `func main() {
72 f("yo")
73 }
74 func f(s string) {
75 a := func(i int) int {
76 return 1 + i
77 }
78 _ = a(3)
79 panic("ooh")
80 }`,
81 // The function became inlinable in go 1.17.
82 true,
83 goarchList{"arm", "arm64", "ppc64le", "riscv64"},
84 Stack{
85 Calls: []Call{
86 newCallSrc(
87 "main.f",
88 Args{
89 Values: []Arg{{IsAggregate: true, Fields: Args{
90 Values: []Arg{{Value: pointer, IsPtr: true}, {Value: 2}},
91 }}},
92 Processed: []string{"string(0x2fffffff, len=2)"},
93 },
94 "/root/main.go",
95 10),
96 newCallSrc("main.main", Args{}, "/root/main.go", 3),
97 },
98 },
99 },
100 {
101 "func",
102 `func main() {
103 f(func() string { return "ooh" })
104 }
105 func f(a func() string) {
106 panic(a())
107 }`,
108 true,
109 goarchList{"arm"},
110 Stack{
111 Calls: []Call{
112 newCallSrc(
113 "main.f",
114 Args{
115 Values: []Arg{{Value: pointer, IsPtr: true}},
116 Processed: []string{"func(0x2fffffff)"},
117 },
118 "/root/main.go",
119 6),
120 newCallSrc("main.main", Args{}, "/root/main.go", 3),
121 },
122 },
123 },
124 {
125 "func ellipsis",
126 `func main() {
127 f(func() string { return "ooh" })
128 }
129 func f(a ...func() string) {
130 panic(a[0]())
131 }`,
132 true,
133 allPlatforms,
134 Stack{
135 Calls: []Call{
136 newCallSrc(
137 "main.f",
138 Args{
139 Values: []Arg{{IsAggregate: true, Fields: Args{
140 Values: []Arg{{Value: pointer, IsPtr: true}, {Value: 1}, {Value: 1}},
141 }}},
142 Processed: []string{"<unknown>{0x2fffffff, 0x1, 0x1}"},
143 },
144 "/root/main.go",
145 6),
146 newCallSrc("main.main", Args{}, "/root/main.go", 3),
147 },
148 },
149 },
150 {
151 "interface{}",
152 `func main() {
153 f(make([]interface{}, 5, 7))
154 }
155 func f(a []interface{}) {
156 panic("ooh")
157 }`,
158 true,
159 allPlatforms,
160 Stack{
161 Calls: []Call{
162 newCallSrc(
163 "main.f",
164 Args{
165 Values: []Arg{{IsAggregate: true, Fields: Args{
166 Values: []Arg{{Value: pointer, IsPtr: true}, {Value: 5}, {Value: 7}},
167 }}},
168 Processed: []string{"[]interface{}(0x2fffffff len=5 cap=7)"},
169 },
170 "/root/main.go",
171 6),
172 newCallSrc("main.main", Args{}, "/root/main.go", 3),
173 },
174 },
175 },
176 {
177 "[]int",
178 `func main() {
179 f(make([]int, 5, 7))
180 }
181 func f(a []int) {
182 panic("ooh")
183 }`,
184 true,
185 allPlatforms,
186 Stack{
187 Calls: []Call{
188 newCallSrc(
189 "main.f",
190 Args{
191 Values: []Arg{{IsAggregate: true, Fields: Args{
192 Values: []Arg{{Value: pointer, IsPtr: true}, {Value: 5}, {Value: 7}},
193 }}},
194 Processed: []string{"[]int(0x2fffffff len=5 cap=7)"},
195 },
196 "/root/main.go",
197 6),
198 newCallSrc("main.main", Args{}, "/root/main.go", 3),
199 },
200 },
201 },
202 {
203 "[]interface{}",
204 `func main() {
205 f([]interface{}{"ooh"})
206 }
207 func f(a []interface{}) {
208 panic(a[0].(string))
209 }`,
210 true,
211 allPlatforms,
212 Stack{
213 Calls: []Call{
214 newCallSrc(
215 "main.f",
216 Args{
217 Values: []Arg{{IsAggregate: true, Fields: Args{
218 Values: []Arg{{Value: pointer, IsPtr: true}, {Value: 1}, {Value: 1}},
219 }}},
220 Processed: []string{"[]interface{}(0x2fffffff len=1 cap=1)"},
221 },
222 "/root/main.go",
223 6),
224 newCallSrc("main.main", Args{}, "/root/main.go", 3),
225 },
226 },
227 },
228 {
229 "map[int]int",
230 `func main() {
231 f(map[int]int{1: 2})
232 }
233 func f(a map[int]int) {
234 panic("ooh")
235 }`,
236 true,
237 allPlatforms,
238 Stack{
239 Calls: []Call{
240 newCallSrc(
241 "main.f",
242 Args{
243 Values: []Arg{{Value: pointer, IsPtr: true}},
244 Processed: []string{"map[int]int(0x2fffffff)"},
245 },
246 "/root/main.go",
247 6),
248 newCallSrc("main.main", Args{}, "/root/main.go", 3),
249 },
250 },
251 },
252 {
253 "map[interface{}]interface{}",
254 `func main() {
255 f(make(map[interface{}]interface{}))
256 }
257 func f(a map[interface{}]interface{}) {
258 panic("ooh")
259 }`,
260 true,
261 allPlatforms,
262 Stack{
263 Calls: []Call{
264 newCallSrc(
265 "main.f",
266 Args{
267 Values: []Arg{{Value: pointer, IsPtr: true}},
268 Processed: []string{"map[interface{}]interface{}(0x2fffffff)"},
269 },
270 "/root/main.go",
271 6),
272 newCallSrc("main.main", Args{}, "/root/main.go", 3),
273 },
274 },
275 },
276 {
277 "chan int",
278 `func main() {
279 f(make(chan int))
280 }
281 func f(a chan int) {
282 panic("ooh")
283 }`,
284 true,
285 allPlatforms,
286 Stack{
287 Calls: []Call{
288 newCallSrc(
289 "main.f",
290 Args{
291 Values: []Arg{{Value: pointer, IsPtr: true}},
292 Processed: []string{"chan int(0x2fffffff)"},
293 },
294 "/root/main.go",
295 6),
296 newCallSrc("main.main", Args{}, "/root/main.go", 3),
297 },
298 },
299 },
300 {
301 "chan interface{}",
302 `func main() {
303 f(make(chan interface{}))
304 }
305 func f(a chan interface{}) {
306 panic("ooh")
307 }`,
308 true,
309 allPlatforms,
310 Stack{
311 Calls: []Call{
312 newCallSrc(
313 "main.f",
314 Args{
315 Values: []Arg{{Value: pointer, IsPtr: true}},
316 Processed: []string{"chan interface{}(0x2fffffff)"},
317 },
318 "/root/main.go",
319 6),
320 newCallSrc("main.main", Args{}, "/root/main.go", 3),
321 },
322 },
323 },
324 {
325 "non-pointer method",
326 `func main() {
327 var s S
328 s.f()
329 }
330 type S struct {}
331 func (s S) f() {
332 panic("ooh")
333 }`,
334 true,
335 allPlatforms,
336 Stack{
337 Calls: []Call{
338 newCallSrc(
339 "main.S.f",
340 Args{Values: []Arg{{IsAggregate: true, Fields: Args{}}}},
341 "/root/main.go",
342 8),
343 newCallSrc("main.main", Args{}, "/root/main.go", 4),
344 },
345 },
346 },
347 {
348 "pointer method",
349 `func main() {
350 var s S
351 s.f()
352 }
353 type S struct {}
354 func (s *S) f() {
355 panic("ooh")
356 }`,
357 true,
358 allPlatforms,
359 Stack{
360 Calls: []Call{
361 newCallSrc(
362 "main.(*S).f",
363 Args{
364 Values: []Arg{{Value: pointer, IsPtr: true}},
365 Processed: []string{"*S(0x2fffffff)"},
366 },
367 "/root/main.go",
368 8),
369 newCallSrc("main.main", Args{}, "/root/main.go", 4),
370 },
371 },
372 },
373 {
374 "string",
375 `func main() {
376 f("ooh")
377 }
378 func f(s string) {
379 panic(s)
380 }`,
381 true,
382 goarchList{"arm", "arm64", "ppc64le", "riscv64"},
383 Stack{
384 Calls: []Call{
385 newCallSrc(
386 "main.f",
387 Args{
388 Values: []Arg{{IsAggregate: true, Fields: Args{
389 Values: []Arg{{Value: pointer, IsPtr: true}, {Value: 3}},
390 }}},
391 Processed: []string{"string(0x2fffffff, len=3)"},
392 },
393 "/root/main.go",
394 6),
395 newCallSrc("main.main", Args{}, "/root/main.go", 3),
396 },
397 },
398 },
399 {
400 "string and int",
401 `func main() {
402 f("ooh", 42)
403 }
404 func f(s string, i int) {
405 panic(s)
406 }`,
407 true,
408 goarchList{"arm", "arm64", "ppc64le", "riscv64"},
409 Stack{
410 Calls: []Call{
411 newCallSrc(
412 "main.f",
413 Args{
414 Values: []Arg{
415 {IsAggregate: true, Fields: Args{
416 Values: []Arg{{Value: pointer, IsPtr: true}, {Value: 3}},
417 }},
418 {Value: 42},
419 },
420 Processed: []string{"string(0x2fffffff, len=3)", "42"},
421 },
422 "/root/main.go",
423 6),
424 newCallSrc("main.main", Args{}, "/root/main.go", 3),
425 },
426 },
427 },
428 {
429 "values are elided",
430 `func main() {
431 f(0, 0, 0, 0, 0, 0, 0, 0, 42, 43, 44, 45, nil)
432 }
433 func f(s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12 int, s13 interface{}) {
434 panic("ooh")
435 }`,
436 true,
437 allPlatforms,
438 Stack{
439 Calls: []Call{
440 newCallSrc(
441 "main.f",
442 Args{
443 Values: []Arg{{}, {}, {}, {}, {}, {}, {}, {}, {Value: 42}, {Value: 43}},
444 Processed: []string{"0", "0", "0", "0", "0", "0", "0", "0", "42", "43"},
445 Elided: true,
446 },
447 "/root/main.go",
448 6),
449 newCallSrc("main.main", Args{}, "/root/main.go", 3),
450 },
451 },
452 },
453 {
454 "error",
455 `import "errors"
456 func main() {
457 f(errors.New("ooh"))
458 }
459 func f(err error) {
460 panic(err.Error())
461 }`,
462 true,
463 allPlatforms,
464 Stack{
465 Calls: []Call{
466 newCallSrc(
467 "main.f",
468 Args{
469 Values: []Arg{{IsAggregate: true, Fields: Args{
470 Values: []Arg{{Value: pointer, IsPtr: true}, {Value: pointer, IsPtr: true}},
471 }}},
472 Processed: []string{"error{0x2fffffff, 0x2fffffff}"},
473 },
474 "/root/main.go",
475 7),
476 newCallSrc("main.main", Args{}, "/root/main.go", 4),
477 },
478 },
479 },
480 {
481 "error unnamed",
482 `import "errors"
483 func main() {
484 f(errors.New("ooh"))
485 }
486 func f(error) {
487 panic("ooh")
488 }`,
489 true,
490 goarchList{"386", "arm", "arm64", "mipsle", "mips64le", "ppc64le", "riscv64", "s390x"},
491 Stack{
492 Calls: []Call{
493 newCallSrc(
494 "main.f",
495 Args{
496 Values: []Arg{{IsAggregate: true, Fields: Args{
497 Values: []Arg{
498 {Value: pointer, IsPtr: true},
499 {Value: pointer, IsPtr: true},
500 },
501 }}},
502 Processed: []string{"error{0x2fffffff, 0x2fffffff}"},
503 },
504 "/root/main.go",
505 7),
506 newCallSrc("main.main", Args{}, "/root/main.go", 4),
507 },
508 },
509 },
510 {
511 "float32",
512 `func main() {
513 f(0.5)
514 }
515 func f(v float32) {
516 panic("ooh")
517 }`,
518 true,
519 allPlatforms,
520 Stack{
521 Calls: []Call{
522 newCallSrc(
523 "main.f",
524 // The value is NOT a pointer but floating point encoding is not
525 // deterministic.
526 Args{
527 Values: []Arg{{Value: pointer, IsPtr: true}},
528 Processed: []string{"0.5"},
529 },
530 "/root/main.go",
531 6),
532 newCallSrc("main.main", Args{}, "/root/main.go", 3),
533 },
534 },
535 },
536 {
537 "float64",
538 `func main() {
539 f(0.5)
540 }
541 func f(v float64) {
542 panic("ooh")
543 }`,
544 true,
545 goarchList{"386", "arm", "mipsle"},
546 Stack{
547 Calls: []Call{
548 newCallSrc(
549 "main.f",
550 // The value is NOT a pointer but floating point encoding is not
551 // deterministic.
552 Args{
553 Values: []Arg{{Value: pointer, IsPtr: true}},
554 Processed: []string{"0.5"},
555 },
556 "/root/main.go",
557 6),
558 newCallSrc("main.main", Args{}, "/root/main.go", 3),
559 },
560 },
561 },
562 {
563 "uint",
564 `func main() {
565 f(123)
566 }
567 func f(v uint) {
568 panic("ooh")
569 }`,
570 true,
571 allPlatforms,
572 Stack{
573 Calls: []Call{
574 newCallSrc(
575 "main.f",
576 Args{
577 Values: []Arg{{Value: 123}},
578 Processed: []string{"123"},
579 },
580 "/root/main.go",
581 6),
582 newCallSrc("main.main", Args{}, "/root/main.go", 3),
583 },
584 },
585 },
586 {
587 "negative int32",
588 `func main() {
589 f(-123)
590 }
591 func f(v int32) {
592 panic("ooh")
593 }`,
594 true,
595 allPlatforms,
596 Stack{
597 Calls: []Call{
598 newCallSrc(
599 "main.f",
600 Args{
601 Values: []Arg{{Value: negInt, IsPtr: negPtr}},
602 Processed: []string{"-123"},
603 },
604 "/root/main.go",
605 6),
606 newCallSrc("main.main", Args{}, "/root/main.go", 3),
607 },
608 },
609 },
610 {
611 "array",
612 `func main() {
613 f([3]byte{2, 3, 4})
614 }
615 func f(v2 [3]byte) {
616 panic("ooh")
617 }`,
618 true,
619 allPlatforms,
620 Stack{
621 Calls: []Call{
622 newCallSrc(
623 "main.f",
624 Args{
625 Values: []Arg{
626 {IsAggregate: true, Fields: Args{
627 Values: []Arg{{Value: 2}, {Value: 3}, {Value: 4}},
628 }},
629 },
630 Processed: []string{"[3]byte{0x2, 0x3, 0x4}"},
631 },
632 "/root/main.go",
633 6),
634 newCallSrc("main.main", Args{}, "/root/main.go", 3),
635 },
636 },
637 },
638 {
639 "deeply nested aggregate type",
640 `func main() {
641 f(a{b{c{d{13}}}})
642 }
643 func f(v a) {
644 panic("ooh")
645 }
646 type a struct{ b }
647 type b struct{ c }
648 type c struct{ d }
649 type d struct{ i int }`,
650 true,
651 allPlatforms,
652 Stack{
653 Calls: []Call{
654 newCallSrc(
655 "main.f",
656 Args{
657 Values: []Arg{{IsAggregate: true, Fields: Args{
658 Values: []Arg{{IsAggregate: true, Fields: Args{
659 Values: []Arg{{IsAggregate: true, Fields: Args{
660 Values: []Arg{{IsAggregate: true, Fields: Args{
661 Values: []Arg{{Value: 13}},
662 }}},
663 }}},
664 }}},
665 }}},
666 Processed: []string{"a{0xd}"},
667 },
668 "/root/main.go",
669 6),
670 newCallSrc("main.main", Args{}, "/root/main.go", 3),
671 },
672 },
673 },
674 {
675 "deeply nested aggregate type with elision",
676 `func main() {
677 f(a{b{c{d{e{13}}}}})
678 }
679 func f(v a) {
680 panic("ooh")
681 }
682 type a struct{ b }
683 type b struct{ c }
684 type c struct{ d }
685 type d struct{ e }
686 type e struct{ i int }`,
687 true,
688 allPlatforms,
689 Stack{
690 Calls: []Call{
691 newCallSrc(
692 "main.f",
693 Args{
694 Values: []Arg{{IsAggregate: true, Fields: Args{
695 Values: []Arg{{IsAggregate: true, Fields: Args{
696 Values: []Arg{{IsAggregate: true, Fields: Args{
697 Values: []Arg{{IsAggregate: true, Fields: Args{
698 Values: []Arg{{IsAggregate: true, Fields: Args{
699 Elided: true,
700 }}},
701 }}},
702 }}},
703 }}},
704 }}},
705 },
706 "/root/main.go",
707 6),
708 newCallSrc("main.main", Args{}, "/root/main.go", 3),
709 },
710 },
711 },
712 {
713 "argument offsets partially too large",
714 `func main() {
715 f(a{b{c{d{e{}}}}}, 13, []int{14, 15})
716 }
717 func f(v a, i int, s []int) {
718 panic("ooh")
719 }
720 type a struct{ b }
721 type b struct{ c }
722 type c struct{ d }
723 type d struct{ e }
724 type e struct{ i [27]int }`,
725 true,
726 goarchList{"386", "arm", "mipsle"},
727 Stack{
728 Calls: []Call{
729 newCallSrc(
730 "main.f",
731 Args{
732 Values: []Arg{
733 {IsAggregate: true, Fields: Args{
734 Values: []Arg{{IsAggregate: true, Fields: Args{
735 Values: []Arg{{IsAggregate: true, Fields: Args{
736 Values: []Arg{{IsAggregate: true, Fields: Args{
737 Values: []Arg{{IsAggregate: true, Fields: Args{
738 Elided: true,
739 }}},
740 }}},
741 }}},
742 }}},
743 }},
744 {Value: 13},
745 {IsAggregate: true, Fields: Args{
746 Values: []Arg{
747 {Value: pointer, IsPtr: true},
748 {Value: 2},
749 {IsOffsetTooLarge: true},
750 },
751 }},
752 },
753 Processed: []string{"a{}", "13", "[]int(0x2fffffff len=2 cap=_)"},
754 },
755 "/root/main.go",
756 6),
757 newCallSrc("main.main", Args{}, "/root/main.go", 3),
758 },
759 },
760 },
761 {
762 "argument offsets entirely too large",
763 `func main() {
764 f(a{b{c{d{e{}}}}}, 13, []int{14, 15})
765 }
766 func f(v a, i int, s []int) {
767 panic("ooh")
768 }
769 type a struct{ b }
770 type b struct{ c }
771 type c struct{ d }
772 type d struct{ e }
773 type e struct{ i [30]int }`,
774 true,
775 goarchList{"386", "arm", "mipsle"},
776 Stack{
777 Calls: []Call{
778 newCallSrc(
779 "main.f",
780 Args{
781 Values: []Arg{
782 {IsAggregate: true, Fields: Args{
783 Values: []Arg{{IsAggregate: true, Fields: Args{
784 Values: []Arg{{IsAggregate: true, Fields: Args{
785 Values: []Arg{{IsAggregate: true, Fields: Args{
786 Values: []Arg{{IsAggregate: true, Fields: Args{
787 Elided: true,
788 }}},
789 }}},
790 }}},
791 }}},
792 }},
793 {IsOffsetTooLarge: true},
794 {IsAggregate: true, Fields: Args{
795 Values: []Arg{
796 {IsOffsetTooLarge: true},
797 {IsOffsetTooLarge: true},
798 {IsOffsetTooLarge: true},
799 },
800 }},
801 },
802 Processed: []string{"a{}", "_", "[]int(_ len=_ cap=_)"},
803 },
804 "/root/main.go",
805 6),
806 newCallSrc("main.main", Args{}, "/root/main.go", 3),
807 },
808 },
809 },
810 // The following subtests are adapted from TestTracebackArgs in
811 // src/runtime/traceback_test.go in the Go runtime.
812 {
813 "testTracebackArgs1",
814 `func main() {
815 testTracebackArgs1(1, 2, 3, 4, 5)
816 }
817 func testTracebackArgs1(a, b, c, d, e int) int {
818 panic("ooh")
819 }`,
820 true,
821 allPlatforms,
822 Stack{
823 Calls: []Call{
824 newCallSrc(
825 "main.testTracebackArgs1",
826 Args{
827 Values: []Arg{
828 {Value: 1}, {Value: 2}, {Value: 3}, {Value: 4}, {Value: 5},
829 },
830 Processed: []string{"1", "2", "3", "4", "5"},
831 },
832 "/root/main.go",
833 6),
834 newCallSrc("main.main", Args{}, "/root/main.go", 3),
835 },
836 },
837 },
838 {
839 "testTracebackArgs2",
840 `func main() {
841 testTracebackArgs2(false, struct {
842 a, b, c int
843 x [2]int
844 }{1, 2, 3, [2]int{4, 5}}, [0]int{}, [3]byte{6, 7, 8})
845 }
846 func testTracebackArgs2(a bool, b struct {
847 a, b, c int
848 x [2]int
849 }, _ [0]int, d [3]byte) int {
850 panic("ooh")
851 }`,
852 true,
853 allPlatforms,
854 Stack{
855 Calls: []Call{
856 newCallSrc(
857 "main.testTracebackArgs2",
858 Args{
859 Values: []Arg{
860 {Value: 0},
861 {IsAggregate: true, Fields: Args{
862 Values: []Arg{
863 {Value: 1},
864 {Value: 2},
865 {Value: 3},
866 {IsAggregate: true, Fields: Args{
867 Values: []Arg{
868 {Value: 4},
869 {Value: 5},
870 },
871 }},
872 },
873 }},
874 {IsAggregate: true},
875 {IsAggregate: true, Fields: Args{
876 Values: []Arg{
877 {Value: 6},
878 {Value: 7},
879 {Value: 8},
880 },
881 }},
882 },
883 Processed: []string{"false", "<unknown>{0x1, 0x2, 0x3, 0x4, 0x5}", "[0]int{}", "[3]byte{0x6, 0x7, 0x8}"},
884 },
885 "/root/main.go",
886 12),
887 newCallSrc("main.main", Args{}, "/root/main.go", 3),
888 },
889 },
890 },
891 {
892 "testTracebackArgs3",
893 `func main() {
894 testTracebackArgs3([3]byte{1, 2, 3}, 4, 5, 6, [3]byte{7, 8, 9})
895 }
896 func testTracebackArgs3(x [3]byte, a, b, c int, y [3]byte) int {
897 panic("ooh")
898 }`,
899 true,
900 allPlatforms,
901 Stack{
902 Calls: []Call{
903 newCallSrc(
904 "main.testTracebackArgs3",
905 Args{
906 Values: []Arg{
907 {IsAggregate: true, Fields: Args{
908 Values: []Arg{
909 {Value: 1},
910 {Value: 2},
911 {Value: 3},
912 },
913 }},
914 {Value: 4},
915 {Value: 5},
916 {Value: 6},
917 {IsAggregate: true, Fields: Args{
918 Values: []Arg{
919 {Value: 7},
920 {Value: 8},
921 {Value: 9},
922 },
923 }},
924 },
925 Processed: []string{"[3]byte{0x1, 0x2, 0x3}", "4", "5", "6", "[3]byte{0x7, 0x8, 0x9}"},
926 },
927 "/root/main.go",
928 6),
929 newCallSrc("main.main", Args{}, "/root/main.go", 3),
930 },
931 },
932 },
933 {
934 "testTracebackArgs4",
935 `func main() {
936 testTracebackArgs4(true, [1][1][1][1][1][1][1][1][1][1]int{})
937 }
938 func testTracebackArgs4(a bool, x [1][1][1][1][1][1][1][1][1][1]int) int {
939 panic("ooh")
940 }`,
941 true,
942 allPlatforms,
943 Stack{
944 Calls: []Call{
945 newCallSrc(
946 "main.testTracebackArgs4",
947 Args{
948 Values: []Arg{
949 {Value: 1},
950 {IsAggregate: true, Fields: Args{
951 Values: []Arg{{IsAggregate: true, Fields: Args{
952 Values: []Arg{{IsAggregate: true, Fields: Args{
953 Values: []Arg{{IsAggregate: true, Fields: Args{
954 Values: []Arg{{IsAggregate: true, Fields: Args{
955 Elided: true,
956 }}},
957 }}},
958 }}},
959 }}},
960 }},
961 },
962 Processed: []string{"true"},
963 },
964 "/root/main.go",
965 6),
966 newCallSrc("main.main", Args{}, "/root/main.go", 3),
967 },
968 },
969 },
970 {
971 "testTracebackArgs5",
972 `func main() {
973 z := [0]int{}
974 testTracebackArgs5(false, struct {
975 x int
976 y [0]int
977 z [2][0]int
978 }{1, z, [2][0]int{}}, z, z, z, z, z, z, z, z, z, z, z, z)
979 }
980 func testTracebackArgs5(a bool, x struct {
981 x int
982 y [0]int
983 z [2][0]int
984 }, _, _, _, _, _, _, _, _, _, _, _, _ [0]int) {
985 panic("ooh")
986 }`,
987 true,
988 allPlatforms,
989 Stack{
990 Calls: []Call{
991 newCallSrc(
992 "main.testTracebackArgs5",
993 Args{
994 Values: []Arg{
995 {Value: 0},
996 {IsAggregate: true, Fields: Args{
997 Values: []Arg{
998 {Value: 1},
999 {IsAggregate: true},
1000 {IsAggregate: true, Fields: Args{
1001 Values: []Arg{
1002 {IsAggregate: true}, {IsAggregate: true},
1003 },
1004 }},
1005 },
1006 }},
1007 {IsAggregate: true}, {IsAggregate: true},
1008 {IsAggregate: true}, {IsAggregate: true},
1009 {IsAggregate: true},
1010 },
1011 Processed: []string{"false", "<unknown>{0x1}"},
1012 Elided: true,
1013 },
1014 "/root/main.go",
1015 15),
1016 newCallSrc("main.main", Args{}, "/root/main.go", 4),
1017 },
1018 },
1019 },
1020 {
1021 "testTracebackArgs6a",
1022 `func main() {
1023 testTracebackArgs6a(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
1024 }
1025 func testTracebackArgs6a(a, b, c, d, e, f, g, h, i, j int) int {
1026 panic("ooh")
1027 }`,
1028 true,
1029 allPlatforms,
1030 Stack{
1031 Calls: []Call{
1032 newCallSrc(
1033 "main.testTracebackArgs6a",
1034 Args{
1035 Values: []Arg{
1036 {Value: 1}, {Value: 2}, {Value: 3}, {Value: 4}, {Value: 5},
1037 {Value: 6}, {Value: 7}, {Value: 8}, {Value: 9}, {Value: 10},
1038 },
1039 Processed: []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"},
1040 },
1041 "/root/main.go",
1042 6),
1043 newCallSrc("main.main", Args{}, "/root/main.go", 3),
1044 },
1045 },
1046 },
1047 {
1048 "testTracebackArgs6b",
1049 `func main() {
1050 testTracebackArgs6b(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
1051 }
1052 func testTracebackArgs6b(a, b, c, d, e, f, g, h, i, j, k int) int {
1053 panic("ooh")
1054 }`,
1055 true,
1056 allPlatforms,
1057 Stack{
1058 Calls: []Call{
1059 newCallSrc(
1060 "main.testTracebackArgs6b",
1061 Args{
1062 Values: []Arg{
1063 {Value: 1}, {Value: 2}, {Value: 3}, {Value: 4}, {Value: 5},
1064 {Value: 6}, {Value: 7}, {Value: 8}, {Value: 9}, {Value: 10},
1065 },
1066 Processed: []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"},
1067 Elided: true,
1068 },
1069 "/root/main.go",
1070 6),
1071 newCallSrc("main.main", Args{}, "/root/main.go", 3),
1072 },
1073 },
1074 },
1075 {
1076 "testTracebackArgs7a",
1077 `func main() {
1078 testTracebackArgs7a([10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
1079 }
1080 func testTracebackArgs7a(a [10]int) int {
1081 panic("ooh")
1082 }`,
1083 true,
1084 allPlatforms,
1085 Stack{
1086 Calls: []Call{
1087 newCallSrc(
1088 "main.testTracebackArgs7a",
1089 Args{
1090 Values: []Arg{{IsAggregate: true, Fields: Args{
1091 Values: []Arg{
1092 {Value: 1}, {Value: 2}, {Value: 3}, {Value: 4}, {Value: 5},
1093 {Value: 6}, {Value: 7}, {Value: 8}, {Value: 9}, {Value: 10},
1094 },
1095 }}},
1096 Processed: []string{"[10]int{0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa}"},
1097 },
1098 "/root/main.go",
1099 6),
1100 newCallSrc("main.main", Args{}, "/root/main.go", 3),
1101 },
1102 },
1103 },
1104 {
1105 "testTracebackArgs7b",
1106 `func main() {
1107 testTracebackArgs7b([11]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11})
1108 }
1109 func testTracebackArgs7b(a [11]int) int {
1110 panic("ooh")
1111 }`,
1112 true,
1113 allPlatforms,
1114 Stack{
1115 Calls: []Call{
1116 newCallSrc(
1117 "main.testTracebackArgs7b",
1118 Args{
1119 Values: []Arg{{IsAggregate: true, Fields: Args{
1120 Values: []Arg{
1121 {Value: 1}, {Value: 2}, {Value: 3}, {Value: 4}, {Value: 5},
1122 {Value: 6}, {Value: 7}, {Value: 8}, {Value: 9}, {Value: 10},
1123 },
1124 Elided: true,
1125 }}},
1126 Processed: []string{"[11]int{0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, ...}"},
1127 },
1128 "/root/main.go",
1129 6),
1130 newCallSrc("main.main", Args{}, "/root/main.go", 3),
1131 },
1132 },
1133 },
1134 {
1135 "testTracebackArgs7c",
1136 `func main() {
1137 testTracebackArgs7c([10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 11)
1138 }
1139 func testTracebackArgs7c(a [10]int, b int) int {
1140 panic("ooh")
1141 }`,
1142 true,
1143 allPlatforms,
1144 Stack{
1145 Calls: []Call{
1146 newCallSrc(
1147 "main.testTracebackArgs7c",
1148 Args{
1149 Values: []Arg{{IsAggregate: true, Fields: Args{
1150 Values: []Arg{
1151 {Value: 1}, {Value: 2}, {Value: 3}, {Value: 4}, {Value: 5},
1152 {Value: 6}, {Value: 7}, {Value: 8}, {Value: 9}, {Value: 10},
1153 },
1154 }}},
1155 Processed: []string{"[10]int{0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa}"},
1156 Elided: true,
1157 },
1158 "/root/main.go",
1159 6),
1160 newCallSrc("main.main", Args{}, "/root/main.go", 3),
1161 },
1162 },
1163 },
1164 {
1165 "testTracebackArgs7d",
1166 `func main() {
1167 testTracebackArgs7d([11]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, 12)
1168 }
1169 func testTracebackArgs7d(a [11]int, b int) int {
1170 panic("ooh")
1171 }`,
1172 true,
1173 allPlatforms,
1174 Stack{
1175 Calls: []Call{
1176 newCallSrc(
1177 "main.testTracebackArgs7d",
1178 Args{
1179 Values: []Arg{{IsAggregate: true, Fields: Args{
1180 Values: []Arg{
1181 {Value: 1}, {Value: 2}, {Value: 3}, {Value: 4}, {Value: 5},
1182 {Value: 6}, {Value: 7}, {Value: 8}, {Value: 9}, {Value: 10},
1183 },
1184 Elided: true,
1185 }}},
1186 Processed: []string{"[11]int{0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, ...}"},
1187 Elided: true,
1188 },
1189 "/root/main.go",
1190 6),
1191 newCallSrc("main.main", Args{}, "/root/main.go", 3),
1192 },
1193 },
1194 },
1195 {
1196 "testTracebackArgs8a",
1197 `func main() {
1198 testTracebackArgs8a(testArgsType8a{1, 2, 3, 4, 5, 6, 7, 8, [2]int{9, 10}})
1199 }
1200 func testTracebackArgs8a(a testArgsType8a) int {
1201 panic("ooh")
1202 }
1203 type testArgsType8a struct {
1204 a, b, c, d, e, f, g, h int
1205 i [2]int
1206 }`,
1207 true,
1208 allPlatforms,
1209 Stack{
1210 Calls: []Call{
1211 newCallSrc(
1212 "main.testTracebackArgs8a",
1213 Args{
1214 Values: []Arg{{IsAggregate: true, Fields: Args{
1215 Values: []Arg{
1216 {Value: 1}, {Value: 2}, {Value: 3}, {Value: 4},
1217 {Value: 5}, {Value: 6}, {Value: 7}, {Value: 8},
1218 {IsAggregate: true, Fields: Args{
1219 Values: []Arg{{Value: 9}, {Value: 10}},
1220 }},
1221 },
1222 }}},
1223 Processed: []string{"testArgsType8a{0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa}"},
1224 },
1225 "/root/main.go",
1226 6),
1227 newCallSrc("main.main", Args{}, "/root/main.go", 3),
1228 },
1229 },
1230 },
1231 {
1232 "testTracebackArgs8b",
1233 `func main() {
1234 testTracebackArgs8b(testArgsType8b{1, 2, 3, 4, 5, 6, 7, 8, [3]int{9, 10, 11}})
1235 }
1236 func testTracebackArgs8b(a testArgsType8b) int {
1237 panic("ooh")
1238 }
1239 type testArgsType8b struct {
1240 a, b, c, d, e, f, g, h int
1241 i [3]int
1242 }`,
1243 true,
1244 allPlatforms,
1245 Stack{
1246 Calls: []Call{
1247 newCallSrc(
1248 "main.testTracebackArgs8b",
1249 Args{
1250 Values: []Arg{{IsAggregate: true, Fields: Args{
1251 Values: []Arg{
1252 {Value: 1}, {Value: 2}, {Value: 3}, {Value: 4},
1253 {Value: 5}, {Value: 6}, {Value: 7}, {Value: 8},
1254 {IsAggregate: true, Fields: Args{
1255 Values: []Arg{{Value: 9}, {Value: 10}},
1256 Elided: true,
1257 }},
1258 },
1259 }}},
1260 Processed: []string{"testArgsType8b{0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa}"},
1261 },
1262 "/root/main.go",
1263 6),
1264 newCallSrc("main.main", Args{}, "/root/main.go", 3),
1265 },
1266 },
1267 },
1268 {
1269 "testTracebackArgs8c",
1270 `func main() {
1271 testTracebackArgs8c(testArgsType8c{1, 2, 3, 4, 5, 6, 7, 8, [2]int{9, 10}, 11})
1272 }
1273 func testTracebackArgs8c(a testArgsType8c) int {
1274 panic("ooh")
1275 }
1276 type testArgsType8c struct {
1277 a, b, c, d, e, f, g, h int
1278 i [2]int
1279 j int
1280 }`,
1281 true,
1282 allPlatforms,
1283 Stack{
1284 Calls: []Call{
1285 newCallSrc(
1286 "main.testTracebackArgs8c",
1287 Args{
1288 Values: []Arg{{IsAggregate: true, Fields: Args{
1289 Values: []Arg{
1290 {Value: 1}, {Value: 2}, {Value: 3}, {Value: 4},
1291 {Value: 5}, {Value: 6}, {Value: 7}, {Value: 8},
1292 {IsAggregate: true, Fields: Args{
1293 Values: []Arg{{Value: 9}, {Value: 10}},
1294 }},
1295 },
1296 Elided: true,
1297 }}},
1298 Processed: []string{"testArgsType8c{0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, ...}"},
1299 },
1300 "/root/main.go",
1301 6),
1302 newCallSrc("main.main", Args{}, "/root/main.go", 3),
1303 },
1304 },
1305 },
1306 {
1307 "testTracebackArgs8d",
1308 `func main() {
1309 testTracebackArgs8d(testArgsType8d{1, 2, 3, 4, 5, 6, 7, 8, [3]int{9, 10, 11}, 12})
1310 }
1311 func testTracebackArgs8d(a testArgsType8d) int {
1312 panic("ooh")
1313 }
1314 type testArgsType8d struct {
1315 a, b, c, d, e, f, g, h int
1316 i [3]int
1317 j int
1318 }`,
1319 true,
1320 allPlatforms,
1321 Stack{
1322 Calls: []Call{
1323 newCallSrc(
1324 "main.testTracebackArgs8d",
1325 Args{
1326 Values: []Arg{{IsAggregate: true, Fields: Args{
1327 Values: []Arg{
1328 {Value: 1}, {Value: 2}, {Value: 3}, {Value: 4},
1329 {Value: 5}, {Value: 6}, {Value: 7}, {Value: 8},
1330 {IsAggregate: true, Fields: Args{
1331 Values: []Arg{{Value: 9}, {Value: 10}},
1332 Elided: true,
1333 }},
1334 },
1335 Elided: true,
1336 }}},
1337 Processed: []string{"testArgsType8d{0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, ...}"},
1338 },
1339 "/root/main.go",
1340 6),
1341 newCallSrc("main.main", Args{}, "/root/main.go", 3),
1342 },
1343 },
1344 },
1345 }
1346
1347 for i, line := range data {
1348 i := i
1349 line := line
1350 t.Run(fmt.Sprintf("%d-%s", i, line.name), func(t *testing.T) {
1351 t.Parallel()
1352 for _, arch := range line.archBlock {
1353 if runtime.GOARCH == arch {
1354 t.Skipf("skipping test on platform %s", arch)
1355 }
1356 }
1357 // Marshal the code a bit to make it nicer. Inject 'package main'.
1358 lines := append([]string{"package main"}, strings.Split(line.input, "\n")...)
1359 for j := 2; j < len(lines); j++ {
1360 // Strip the 3 first tab characters. It's very adhoc but good enough here
1361 // and makes test failure much more readable.
1362 if lines[j][:3] != "\t\t\t" {
1363 t.Fatal("expected line to start with 3 tab characters")
1364 }
1365 lines[j] = lines[j][3:]
1366 }
1367 input := strings.Join(lines, "\n")
1368
1369 // Create one temporary directory by subtest.
1370 root, err := os.MkdirTemp("", "stack")
1371 if err != nil {
1372 t.Fatalf("failed to create temporary directory: %v", err)
1373 }
1374 defer func() {
1375 if err2 := os.RemoveAll(root); err2 != nil {
1376 t.Fatalf("failed to remove temporary directory %q: %v", root, err2)
1377 }
1378 }()
1379 main := filepath.Join(root, "main.go")
1380 if err := os.WriteFile(main, []byte(input), 0500); err != nil {
1381 t.Fatalf("failed to write %q: %v", main, err)
1382 }
1383
1384 if runtime.GOOS == "windows" {
1385 root = strings.Replace(root, pathSeparator, "/", -1)
1386 }
1387 const prefix = "/root"
1388 for j, c := range line.want.Calls {
1389 if strings.HasPrefix(c.RemoteSrcPath, prefix) {
1390 line.want.Calls[j].RemoteSrcPath = root + c.RemoteSrcPath[len(prefix):]
1391 }
1392 if strings.HasPrefix(c.LocalSrcPath, prefix) {
1393 line.want.Calls[j].LocalSrcPath = root + c.LocalSrcPath[len(prefix):]
1394 }
1395 if strings.HasPrefix(c.DirSrc, "root") {
1396 line.want.Calls[j].DirSrc = path.Base(root) + c.DirSrc[4:]
1397 }
1398 }
1399
1400 // Run the command up to twice.
1401
1402 // Only disable inlining if necessary.
1403 disableInline := line.mayBeInlined
1404 content := getCrash(t, main, disableInline)
1405 t.Log("First")
1406 // Warning: this function modifies want.
1407 testAugmentCommon(t, content, false, line.want)
1408
1409 // If inlining was disabled, try a second time but zap things out.
1410 if disableInline {
1411 for j := range line.want.Calls {
1412 line.want.Calls[j].Args.Processed = nil
1413 }
1414 content := getCrash(t, main, false)
1415 t.Log("Second")
1416 testAugmentCommon(t, content, line.mayBeInlined, line.want)
1417 }
1418 })
1419 }
1420 }
1421
1422 func testAugmentCommon(t *testing.T, content []byte, mayBeInlined bool, want Stack) {
1423 // Analyze it.
1424 prefix := bytes.Buffer{}
1425 s, suffix, err := ScanSnapshot(bytes.NewBuffer(content), &prefix, defaultOpts())
1426 if err != nil {
1427 t.Fatalf("failed to parse input: %v", err)
1428 }
1429 if got := prefix.String(); got != "panic: ooh\n\n" {
1430 t.Fatalf("Unexpected panic output:\n%#v", got)
1431 }
1432 compareString(t, "exit status 2\n", string(suffix))
1433 if !s.guessPaths() {
1434 t.Error("expected success")
1435 }
1436
1437 if err := s.augment(); err != nil {
1438 t.Errorf("augment() returned %v", err)
1439 }
1440 got := s.Goroutines[0].Signature.Stack
1441 zapPointers(t, &want, &got)
1442
1443 // With a non-pointer method, elided argument can be shown. It's only for
1444 // test case "non-pointer method".
1445 if mayBeInlined {
1446 for j := range got.Calls {
1447 if !want.Calls[j].Args.Elided {
1448 got.Calls[j].Args.Elided = false
1449 }
1450 if got.Calls[j].Args.Values == nil {
1451 want.Calls[j].Args.Values = nil
1452 }
1453 }
1454 }
1455
1456 if diff := cmp.Diff(want, got); diff != "" {
1457 t.Logf("Different (-want +got):\n%s", diff)
1458 t.Logf("Output:\n%s", content)
1459 t.FailNow()
1460 }
1461 }
1462
1463 func TestAugmentErr(t *testing.T) {
1464 t.Parallel()
1465 root, err := os.MkdirTemp("", "stack")
1466 if err != nil {
1467 t.Fatalf("failed to create temporary directory: %v", err)
1468 }
1469 defer func() {
1470 if err2 := os.RemoveAll(root); err2 != nil {
1471 t.Fatalf("failed to remove temporary directory %q: %v", root, err2)
1472 }
1473 }()
1474
1475 tree := map[string]string{
1476 "bad.go": "bad content",
1477 "foo.asm": "; good but ignored",
1478 "good.go": "package main",
1479 "no_access.go": "package main",
1480 }
1481 createTree(t, root, tree)
1482
1483 type dataLine struct {
1484 name string
1485 src string
1486 line int
1487 args Args
1488 errRe string
1489 }
1490 // Note: these tests assumes an OS running in English-US locale. That should
1491 // eventually be fixed, maybe by using regexes?
1492 msgRe := ".+"
1493 if runtime.GOOS != "windows" {
1494 msgRe = "no such file or directory"
1495 }
1496 data := []dataLine{
1497 {
1498 name: "assembly is skipped",
1499 src: "foo.asm",
1500 args: Args{Values: []Arg{{}}},
1501 errRe: regexp.QuoteMeta(fmt.Sprintf("cannot load non-go file %q", filepath.Join(root, "foo.asm"))),
1502 },
1503 {
1504 name: "assembly is skipped (no arg)",
1505 src: "foo.asm",
1506 },
1507 {
1508 name: "invalid line number",
1509 src: "good.go",
1510 line: 2,
1511 args: Args{Values: []Arg{{}}},
1512 errRe: "line 2 is over line count of 1",
1513 },
1514 {
1515 name: "invalid line number (no arg)",
1516 src: "good.go",
1517 line: 2,
1518 },
1519 {
1520 name: "missing file",
1521 src: "missing.go",
1522 args: Args{Values: []Arg{{}}},
1523 errRe: regexp.QuoteMeta(fmt.Sprintf("open %s: ", filepath.Join(root, "missing.go"))) + msgRe,
1524 },
1525 {
1526 name: "missing file (no arg)",
1527 src: "missing.go",
1528 },
1529 {
1530 name: "invalid go code (no arg)",
1531 src: "bad.go",
1532 },
1533 {
1534 name: "no I/O access (no arg)",
1535 src: "no_access.go",
1536 },
1537 }
1538 if internaltest.GetGoMinorVersion() > 11 {
1539 // The format changed between 1.9 and 1.12.
1540 data = append(data, dataLine{
1541 name: "invalid go code",
1542 src: "bad.go",
1543 args: Args{Values: []Arg{{}}},
1544 errRe: regexp.QuoteMeta(fmt.Sprintf("failed to parse %s:1:1: expected 'package', found bad", filepath.Join(root, "bad.go"))),
1545 })
1546 }
1547 if runtime.GOOS != "windows" {
1548 // Chmod has no effect on Windows.
1549 compareErr(t, nil, os.Chmod(filepath.Join(root, "no_access.go"), 0))
1550 data = append(data, dataLine{
1551 name: "no I/O access",
1552 src: "no_access.go",
1553 args: Args{Values: []Arg{{}}},
1554 errRe: regexp.QuoteMeta(fmt.Sprintf("open %s: permission denied", filepath.Join(root, "no_access.go"))),
1555 })
1556 }
1557
1558 for i, line := range data {
1559 line := line
1560 t.Run(fmt.Sprintf("%d-%s", i, line.name), func(t *testing.T) {
1561 l := line.line
1562 if l == 0 {
1563 l = 1
1564 }
1565 s := Snapshot{
1566 Goroutines: []*Goroutine{
1567 {Signature: Signature{Stack: Stack{Calls: []Call{
1568 {LocalSrcPath: filepath.Join(root, line.src), Args: line.args, Line: l},
1569 }}}},
1570 }}
1571 if err := s.augment(); (line.errRe == "") != (err == nil) {
1572 t.Fatalf("want: %q; got: %q", line.errRe, err)
1573 } else if err != nil {
1574 if m, err2 := regexp.MatchString(line.errRe, err.Error()); err2 != nil {
1575 t.Fatal(err2)
1576 } else if !m {
1577 t.Fatalf("want: %q; got: %q", line.errRe, err)
1578 }
1579 }
1580 })
1581 }
1582 }
1583
1584 func TestLineToByteOffsets(t *testing.T) {
1585 src := "\n\n\n"
1586 want := []int{0, 0, 1, 2, 3}
1587 if diff := cmp.Diff(want, lineToByteOffsets([]byte(src))); diff != "" {
1588 t.Error(diff)
1589 }
1590 src = "hello"
1591 want = []int{0, 0}
1592 if diff := cmp.Diff(want, lineToByteOffsets([]byte(src))); diff != "" {
1593 t.Error(diff)
1594 }
1595 src = "this\nis\na\ntest"
1596 want = []int{0, 0, 5, 8, 10}
1597 if diff := cmp.Diff(want, lineToByteOffsets([]byte(src))); diff != "" {
1598 t.Error(diff)
1599 }
1600 }
1601
1602 //
1603
1604 const pointer = uint64(0x2fffffff)
1605 const pointerStr = "0x2fffffff"
1606 const is64Bit = uint64(^uintptr(0)) == ^uint64(0)
1607
1608 func overrideEnv(env []string, key, value string) []string {
1609 prefix := key + "="
1610 for i, e := range env {
1611 if strings.HasPrefix(e, prefix) {
1612 env[i] = prefix + value
1613 return env
1614 }
1615 }
1616 return append(env, prefix+value)
1617 }
1618
1619 func getCrash(t *testing.T, main string, disableInline bool) []byte {
1620 args := []string{"run"}
1621 if disableInline {
1622 // Disable both optimization (-N) and inlining (-l).
1623 args = append(args, "-gcflags", "-N -l")
1624 }
1625 cmd := exec.Command("go", append(args, main)...)
1626 // Use the Go 1.4 compatible format.
1627 cmd.Env = overrideEnv(os.Environ(), "GOTRACEBACK", "1")
1628 out, err := cmd.CombinedOutput()
1629 if err == nil {
1630 t.Helper()
1631 t.Fatal("expected error since this is supposed to crash")
1632 }
1633 return out
1634 }
1635
1636 // zapPointers zaps out pointers in got.
1637 func zapPointers(t *testing.T, want, got *Stack) {
1638 for i := range got.Calls {
1639 if i >= len(want.Calls) {
1640 // When using GOTRACEBACK=2, it'll include runtime.main() and
1641 // runtime.goexit(). Ignore these since they could be changed in a future
1642 // version.
1643 got.Calls = got.Calls[:len(want.Calls)]
1644 break
1645 }
1646 gotArgs := got.Calls[i].Args
1647 ptrsToReplace := zapPointersInArgs(t, want.Calls[i].Args, gotArgs)
1648 for _, ptr := range ptrsToReplace {
1649 for k := range gotArgs.Processed {
1650 gotArgs.Processed[k] = strings.Replace(gotArgs.Processed[k], ptr, pointerStr, -1)
1651 }
1652 }
1653 }
1654 }
1655
1656 func zapPointersInArgs(t *testing.T, want, got Args) (ptrs []string) {
1657 for j := range got.Values {
1658 if j >= len(want.Values) {
1659 break
1660 }
1661 if want.Values[j].IsAggregate && got.Values[j].IsAggregate {
1662 ptrs = append(ptrs, zapPointersInArgs(t, want.Values[j].Fields, got.Values[j].Fields)...)
1663 } else if want.Values[j].IsPtr && got.Values[j].IsPtr {
1664 // Record the existing pointer value and then replace.
1665 ptrs = append(ptrs, fmt.Sprintf("0x%x", got.Values[j].Value))
1666 got.Values[j].Value = want.Values[j].Value
1667 }
1668 }
1669 return ptrs
1670 }