-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdwarf_test.go
2044 lines (1756 loc) · 48 KB
/
dwarf_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2017 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.
package ld
import (
"debug/dwarf"
"debug/pe"
"fmt"
"internal/platform"
"internal/testenv"
"io"
"os"
"path/filepath"
"reflect"
"runtime"
"sort"
"strconv"
"strings"
"testing"
intdwarf "cmd/internal/dwarf"
objfilepkg "cmd/internal/objfile" // renamed to avoid conflict with objfile function
"cmd/link/internal/dwtest"
)
func mustHaveDWARF(t testing.TB) {
if !platform.ExecutableHasDWARF(runtime.GOOS, runtime.GOARCH) {
t.Helper()
t.Skipf("skipping on %s/%s: no DWARF symbol table in executables", runtime.GOOS, runtime.GOARCH)
}
}
const (
DefaultOpt = "-gcflags="
NoOpt = "-gcflags=-l -N"
OptInl4 = "-gcflags=-l=4"
OptAllInl4 = "-gcflags=all=-l=4"
)
func TestRuntimeTypesPresent(t *testing.T) {
t.Parallel()
testenv.MustHaveGoBuild(t)
mustHaveDWARF(t)
dir := t.TempDir()
f := gobuild(t, dir, `package main; func main() { }`, NoOpt)
defer f.Close()
dwarf, err := f.DWARF()
if err != nil {
t.Fatalf("error reading DWARF: %v", err)
}
want := map[string]bool{
"internal/abi.Type": true,
"internal/abi.ArrayType": true,
"internal/abi.ChanType": true,
"internal/abi.FuncType": true,
"internal/abi.PtrType": true,
"internal/abi.SliceType": true,
"internal/abi.StructType": true,
"internal/abi.InterfaceType": true,
"internal/abi.ITab": true,
}
found := findTypes(t, dwarf, want)
if len(found) != len(want) {
t.Errorf("found %v, want %v", found, want)
}
// Must have one of OldMapType or SwissMapType.
want = map[string]bool{
"internal/abi.OldMapType": true,
"internal/abi.SwissMapType": true,
}
found = findTypes(t, dwarf, want)
if len(found) != 1 {
t.Errorf("map type want one of %v found %v", want, found)
}
}
func findTypes(t *testing.T, dw *dwarf.Data, want map[string]bool) (found map[string]bool) {
found = make(map[string]bool)
rdr := dw.Reader()
for entry, err := rdr.Next(); entry != nil; entry, err = rdr.Next() {
if err != nil {
t.Fatalf("error reading DWARF: %v", err)
}
switch entry.Tag {
case dwarf.TagTypedef:
if name, ok := entry.Val(dwarf.AttrName).(string); ok && want[name] {
found[name] = true
}
}
}
return
}
type builtFile struct {
*objfilepkg.File
path string
}
func gobuild(t *testing.T, dir string, testfile string, gcflags string) *builtFile {
src := filepath.Join(dir, "test.go")
dst := filepath.Join(dir, "out.exe")
if err := os.WriteFile(src, []byte(testfile), 0666); err != nil {
t.Fatal(err)
}
cmd := testenv.Command(t, testenv.GoToolPath(t), "build", gcflags, "-o", dst, src)
b, err := cmd.CombinedOutput()
if len(b) != 0 {
t.Logf("## build output:\n%s", b)
}
if err != nil {
t.Fatalf("build error: %v", err)
}
f, err := objfilepkg.Open(dst)
if err != nil {
t.Fatal(err)
}
return &builtFile{f, dst}
}
// Similar to gobuild() above, but uses a main package instead of a test.go file.
func gobuildTestdata(t *testing.T, pkgDir string, gcflags string) *builtFile {
dst := filepath.Join(t.TempDir(), "out.exe")
// Run a build with an updated GOPATH
cmd := testenv.Command(t, testenv.GoToolPath(t), "build", gcflags, "-o", dst)
cmd.Dir = pkgDir
if b, err := cmd.CombinedOutput(); err != nil {
t.Logf("build: %s\n", b)
t.Fatalf("build error: %v", err)
}
f, err := objfilepkg.Open(dst)
if err != nil {
t.Fatal(err)
}
return &builtFile{f, dst}
}
// Helper to build a snippet of source for examination with dwtest.Examiner.
func gobuildAndExamine(t *testing.T, source string, gcflags string) (*dwarf.Data, *dwtest.Examiner) {
dir := t.TempDir()
f := gobuild(t, dir, source, gcflags)
defer f.Close()
d, err := f.DWARF()
if err != nil {
t.Fatalf("error reading DWARF in program %q: %v", source, err)
}
rdr := d.Reader()
ex := &dwtest.Examiner{}
if err := ex.Populate(rdr); err != nil {
t.Fatalf("error populating DWARF examiner for program %q: %v", source, err)
}
return d, ex
}
func findSubprogramDIE(t *testing.T, ex *dwtest.Examiner, sym string) *dwarf.Entry {
dies := ex.Named(sym)
if len(dies) == 0 {
t.Fatalf("unable to locate DIE for %s", sym)
}
if len(dies) != 1 {
t.Fatalf("more than one %s DIE: %+v", sym, dies)
}
die := dies[0]
// Vet the DIE.
if die.Tag != dwarf.TagSubprogram {
t.Fatalf("unexpected tag %v on %s DIE", die.Tag, sym)
}
return die
}
func TestEmbeddedStructMarker(t *testing.T) {
t.Parallel()
testenv.MustHaveGoBuild(t)
mustHaveDWARF(t)
const prog = `
package main
import "fmt"
type Foo struct { v int }
type Bar struct {
Foo
name string
}
type Baz struct {
*Foo
name string
}
func main() {
bar := Bar{ Foo: Foo{v: 123}, name: "onetwothree"}
baz := Baz{ Foo: &bar.Foo, name: "123" }
fmt.Println(bar, baz)
}`
want := map[string]map[string]bool{
"main.Foo": {"v": false},
"main.Bar": {"Foo": true, "name": false},
"main.Baz": {"Foo": true, "name": false},
}
dir := t.TempDir()
f := gobuild(t, dir, prog, NoOpt)
defer f.Close()
d, err := f.DWARF()
if err != nil {
t.Fatalf("error reading DWARF: %v", err)
}
rdr := d.Reader()
for entry, err := rdr.Next(); entry != nil; entry, err = rdr.Next() {
if err != nil {
t.Fatalf("error reading DWARF: %v", err)
}
switch entry.Tag {
case dwarf.TagStructType:
name, ok := entry.Val(dwarf.AttrName).(string)
if !ok {
continue
}
wantMembers := want[name]
if wantMembers == nil {
continue
}
gotMembers, err := findMembers(rdr)
if err != nil {
t.Fatalf("error reading DWARF: %v", err)
}
if !reflect.DeepEqual(gotMembers, wantMembers) {
t.Errorf("type %v: got map[member]embedded = %+v, want %+v", name, wantMembers, gotMembers)
}
delete(want, name)
}
}
if len(want) != 0 {
t.Errorf("failed to check all expected types: missing types = %+v", want)
}
}
func findMembers(rdr *dwarf.Reader) (map[string]bool, error) {
memberEmbedded := map[string]bool{}
// TODO(hyangah): define in debug/dwarf package
const goEmbeddedStruct = dwarf.Attr(intdwarf.DW_AT_go_embedded_field)
for entry, err := rdr.Next(); entry != nil; entry, err = rdr.Next() {
if err != nil {
return nil, err
}
switch entry.Tag {
case dwarf.TagMember:
name := entry.Val(dwarf.AttrName).(string)
embedded := entry.Val(goEmbeddedStruct).(bool)
memberEmbedded[name] = embedded
case 0:
return memberEmbedded, nil
}
}
return memberEmbedded, nil
}
func TestSizes(t *testing.T) {
mustHaveDWARF(t)
// External linking may bring in C symbols with unknown size. Skip.
testenv.MustInternalLink(t, false)
t.Parallel()
// DWARF sizes should never be -1.
// See issue #21097
const prog = `
package main
var x func()
var y [4]func()
func main() {
x = nil
y[0] = nil
}
`
dir := t.TempDir()
f := gobuild(t, dir, prog, NoOpt)
defer f.Close()
d, err := f.DWARF()
if err != nil {
t.Fatalf("error reading DWARF: %v", err)
}
rdr := d.Reader()
for entry, err := rdr.Next(); entry != nil; entry, err = rdr.Next() {
if err != nil {
t.Fatalf("error reading DWARF: %v", err)
}
switch entry.Tag {
case dwarf.TagArrayType, dwarf.TagPointerType, dwarf.TagStructType, dwarf.TagBaseType, dwarf.TagSubroutineType, dwarf.TagTypedef:
default:
continue
}
typ, err := d.Type(entry.Offset)
if err != nil {
t.Fatalf("can't read type: %v", err)
}
if typ.Size() < 0 {
t.Errorf("subzero size %s %s %T", typ, entry.Tag, typ)
}
}
}
func TestFieldOverlap(t *testing.T) {
mustHaveDWARF(t)
t.Parallel()
// This test grew out of issue 21094, where specific sudog<T> DWARF types
// had elem fields set to values instead of pointers.
const prog = `
package main
var c chan string
func main() {
c <- "foo"
}
`
dir := t.TempDir()
f := gobuild(t, dir, prog, NoOpt)
defer f.Close()
d, err := f.DWARF()
if err != nil {
t.Fatalf("error reading DWARF: %v", err)
}
rdr := d.Reader()
for entry, err := rdr.Next(); entry != nil; entry, err = rdr.Next() {
if err != nil {
t.Fatalf("error reading DWARF: %v", err)
}
if entry.Tag != dwarf.TagStructType {
continue
}
typ, err := d.Type(entry.Offset)
if err != nil {
t.Fatalf("can't read type: %v", err)
}
s := typ.(*dwarf.StructType)
for i := 0; i < len(s.Field); i++ {
end := s.Field[i].ByteOffset + s.Field[i].Type.Size()
var limit int64
if i == len(s.Field)-1 {
limit = s.Size()
} else {
limit = s.Field[i+1].ByteOffset
}
if end > limit {
name := entry.Val(dwarf.AttrName).(string)
t.Fatalf("field %s.%s overlaps next field", name, s.Field[i].Name)
}
}
}
}
func TestSubprogramDeclFileLine(t *testing.T) {
testenv.MustHaveGoBuild(t)
t.Parallel()
mustHaveDWARF(t)
const prog = `package main
%s
func main() {}
`
tests := []struct {
name string
prog string
file string
line int64
}{
{
name: "normal",
prog: fmt.Sprintf(prog, ""),
file: "test.go",
line: 3,
},
{
name: "line-directive",
prog: fmt.Sprintf(prog, "//line /foobar.go:200"),
file: "foobar.go",
line: 200,
},
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
d, ex := gobuildAndExamine(t, tc.prog, NoOpt)
maindie := findSubprogramDIE(t, ex, "main.main")
mainIdx := ex.IdxFromOffset(maindie.Offset)
fileIdx, fileIdxOK := maindie.Val(dwarf.AttrDeclFile).(int64)
if !fileIdxOK {
t.Errorf("missing or invalid DW_AT_decl_file for main")
}
file, err := ex.FileRef(d, mainIdx, fileIdx)
if err != nil {
t.Fatalf("FileRef: %v", err)
}
base := filepath.Base(file)
if base != tc.file {
t.Errorf("DW_AT_decl_file for main is %v, want %v", base, tc.file)
}
line, lineOK := maindie.Val(dwarf.AttrDeclLine).(int64)
if !lineOK {
t.Errorf("missing or invalid DW_AT_decl_line for main")
}
if line != tc.line {
t.Errorf("DW_AT_decl_line for main is %v, want %d", line, tc.line)
}
})
}
}
func TestVarDeclLine(t *testing.T) {
testenv.MustHaveGoBuild(t)
t.Parallel()
mustHaveDWARF(t)
const prog = `package main
%s
func main() {
var i int
i = i
}
`
tests := []struct {
name string
prog string
line int64
}{
{
name: "normal",
prog: fmt.Sprintf(prog, ""),
line: 5,
},
{
name: "line-directive",
prog: fmt.Sprintf(prog, "//line /foobar.go:200"),
line: 202,
},
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
_, ex := gobuildAndExamine(t, tc.prog, NoOpt)
maindie := findSubprogramDIE(t, ex, "main.main")
mainIdx := ex.IdxFromOffset(maindie.Offset)
childDies := ex.Children(mainIdx)
var iEntry *dwarf.Entry
for _, child := range childDies {
if child.Tag == dwarf.TagVariable && child.Val(dwarf.AttrName).(string) == "i" {
iEntry = child
break
}
}
if iEntry == nil {
t.Fatalf("didn't find DW_TAG_variable for i in main.main")
}
// Verify line/file attributes.
line, lineOK := iEntry.Val(dwarf.AttrDeclLine).(int64)
if !lineOK {
t.Errorf("missing or invalid DW_AT_decl_line for i")
}
if line != tc.line {
t.Errorf("DW_AT_decl_line for i is %v, want %d", line, tc.line)
}
})
}
}
// TestInlinedRoutineCallFileLine tests the call file and line records for an
// inlined subroutine.
func TestInlinedRoutineCallFileLine(t *testing.T) {
testenv.MustHaveGoBuild(t)
mustHaveDWARF(t)
t.Parallel()
const prog = `
package main
var G int
//go:noinline
func notinlined() int {
return 42
}
func inlined() int {
return notinlined()
}
%s
func main() {
x := inlined()
G = x
}
`
tests := []struct {
name string
prog string
file string // basename
line int64
}{
{
name: "normal",
prog: fmt.Sprintf(prog, ""),
file: "test.go",
line: 17,
},
{
name: "line-directive",
prog: fmt.Sprintf(prog, "//line /foobar.go:200"),
file: "foobar.go",
line: 201,
},
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
// Note: this is a build with "-l=4", as opposed to "-l -N". The
// test is intended to verify DWARF that is only generated when
// the inliner is active. We're only going to look at the DWARF for
// main.main, however, hence we build with "-gcflags=-l=4" as opposed
// to "-gcflags=all=-l=4".
d, ex := gobuildAndExamine(t, tc.prog, OptInl4)
maindie := findSubprogramDIE(t, ex, "main.main")
// Walk main's children and pick out the inlined subroutines
mainIdx := ex.IdxFromOffset(maindie.Offset)
childDies := ex.Children(mainIdx)
found := false
for _, child := range childDies {
if child.Tag != dwarf.TagInlinedSubroutine {
continue
}
// Found an inlined subroutine.
if found {
t.Fatalf("Found multiple inlined subroutines, expect only one")
}
found = true
// Locate abstract origin.
ooff, originOK := child.Val(dwarf.AttrAbstractOrigin).(dwarf.Offset)
if !originOK {
t.Fatalf("no abstract origin attr for inlined subroutine at offset %v", child.Offset)
}
originDIE := ex.EntryFromOffset(ooff)
if originDIE == nil {
t.Fatalf("can't locate origin DIE at off %v", ooff)
}
// Name should check out.
name, ok := originDIE.Val(dwarf.AttrName).(string)
if !ok {
t.Fatalf("no name attr for inlined subroutine at offset %v", child.Offset)
}
if name != "main.inlined" {
t.Fatalf("expected inlined routine %s got %s", "main.cand", name)
}
// Verify that the call_file attribute for the inlined
// instance is ok. In this case it should match the file
// for the main routine. To do this we need to locate the
// compilation unit DIE that encloses what we're looking
// at; this can be done with the examiner.
cf, cfOK := child.Val(dwarf.AttrCallFile).(int64)
if !cfOK {
t.Fatalf("no call_file attr for inlined subroutine at offset %v", child.Offset)
}
file, err := ex.FileRef(d, mainIdx, cf)
if err != nil {
t.Errorf("FileRef: %v", err)
continue
}
base := filepath.Base(file)
if base != tc.file {
t.Errorf("bad call_file attribute, found '%s', want '%s'",
file, tc.file)
}
// Verify that the call_line attribute for the inlined
// instance is ok.
cl, clOK := child.Val(dwarf.AttrCallLine).(int64)
if !clOK {
t.Fatalf("no call_line attr for inlined subroutine at offset %v", child.Offset)
}
if cl != tc.line {
t.Errorf("bad call_line attribute, found %d, want %d", cl, tc.line)
}
}
if !found {
t.Fatalf("not enough inlined subroutines found in main.main")
}
})
}
}
// TestInlinedRoutineArgsVars tests the argument and variable records for an inlined subroutine.
func TestInlinedRoutineArgsVars(t *testing.T) {
testenv.MustHaveGoBuild(t)
mustHaveDWARF(t)
t.Parallel()
const prog = `
package main
var G int
func noinline(x int) int {
defer func() { G += x }()
return x
}
func cand(x, y int) int {
return noinline(x+y) ^ (y - x)
}
func main() {
x := cand(G*G,G|7%G)
G = x
}
`
// Note: this is a build with "-l=4", as opposed to "-l -N". The
// test is intended to verify DWARF that is only generated when
// the inliner is active. We're only going to look at the DWARF for
// main.main, however, hence we build with "-gcflags=-l=4" as opposed
// to "-gcflags=all=-l=4".
_, ex := gobuildAndExamine(t, prog, OptInl4)
maindie := findSubprogramDIE(t, ex, "main.main")
// Walk main's children and pick out the inlined subroutines
mainIdx := ex.IdxFromOffset(maindie.Offset)
childDies := ex.Children(mainIdx)
found := false
for _, child := range childDies {
if child.Tag != dwarf.TagInlinedSubroutine {
continue
}
// Found an inlined subroutine.
if found {
t.Fatalf("Found multiple inlined subroutines, expect only one")
}
found = true
// Locate abstract origin.
ooff, originOK := child.Val(dwarf.AttrAbstractOrigin).(dwarf.Offset)
if !originOK {
t.Fatalf("no abstract origin attr for inlined subroutine at offset %v", child.Offset)
}
originDIE := ex.EntryFromOffset(ooff)
if originDIE == nil {
t.Fatalf("can't locate origin DIE at off %v", ooff)
}
// Name should check out.
name, ok := originDIE.Val(dwarf.AttrName).(string)
if !ok {
t.Fatalf("no name attr for inlined subroutine at offset %v", child.Offset)
}
if name != "main.cand" {
t.Fatalf("expected inlined routine %s got %s", "main.cand", name)
}
// Walk the children of the abstract subroutine. We expect
// to see child variables there, even if (perhaps due to
// optimization) there are no references to them from the
// inlined subroutine DIE.
absFcnIdx := ex.IdxFromOffset(ooff)
absFcnChildDies := ex.Children(absFcnIdx)
if len(absFcnChildDies) != 2 {
t.Fatalf("expected abstract function: expected 2 children, got %d children", len(absFcnChildDies))
}
formalCount := 0
for _, absChild := range absFcnChildDies {
if absChild.Tag == dwarf.TagFormalParameter {
formalCount += 1
continue
}
t.Fatalf("abstract function child DIE: expected formal, got %v", absChild.Tag)
}
if formalCount != 2 {
t.Fatalf("abstract function DIE: expected 2 formals, got %d", formalCount)
}
omap := make(map[dwarf.Offset]bool)
// Walk the child variables of the inlined routine. Each
// of them should have a distinct abstract origin-- if two
// vars point to the same origin things are definitely broken.
inlIdx := ex.IdxFromOffset(child.Offset)
inlChildDies := ex.Children(inlIdx)
for _, k := range inlChildDies {
ooff, originOK := k.Val(dwarf.AttrAbstractOrigin).(dwarf.Offset)
if !originOK {
t.Fatalf("no abstract origin attr for child of inlined subroutine at offset %v", k.Offset)
}
if _, found := omap[ooff]; found {
t.Fatalf("duplicate abstract origin at child of inlined subroutine at offset %v", k.Offset)
}
omap[ooff] = true
}
}
if !found {
t.Fatalf("not enough inlined subroutines found in main.main")
}
}
func abstractOriginSanity(t *testing.T, pkgDir string, flags string) {
t.Parallel()
// Build with inlining, to exercise DWARF inlining support.
f := gobuildTestdata(t, filepath.Join(pkgDir, "main"), flags)
defer f.Close()
d, err := f.DWARF()
if err != nil {
t.Fatalf("error reading DWARF: %v", err)
}
rdr := d.Reader()
ex := dwtest.Examiner{}
if err := ex.Populate(rdr); err != nil {
t.Fatalf("error reading DWARF: %v", err)
}
// Make a pass through all DIEs looking for abstract origin
// references.
abscount := 0
for i, die := range ex.DIEs() {
// Does it have an abstract origin?
ooff, originOK := die.Val(dwarf.AttrAbstractOrigin).(dwarf.Offset)
if !originOK {
continue
}
// All abstract origin references should be resolvable.
abscount += 1
originDIE := ex.EntryFromOffset(ooff)
if originDIE == nil {
ex.DumpEntry(i, false, 0)
t.Fatalf("unresolved abstract origin ref in DIE at offset 0x%x\n", die.Offset)
}
// Suppose that DIE X has parameter/variable children {K1,
// K2, ... KN}. If X has an abstract origin of A, then for
// each KJ, the abstract origin of KJ should be a child of A.
// Note that this same rule doesn't hold for non-variable DIEs.
pidx := ex.IdxFromOffset(die.Offset)
if pidx < 0 {
t.Fatalf("can't locate DIE id")
}
kids := ex.Children(pidx)
for _, kid := range kids {
if kid.Tag != dwarf.TagVariable &&
kid.Tag != dwarf.TagFormalParameter {
continue
}
kooff, originOK := kid.Val(dwarf.AttrAbstractOrigin).(dwarf.Offset)
if !originOK {
continue
}
childOriginDIE := ex.EntryFromOffset(kooff)
if childOriginDIE == nil {
ex.DumpEntry(i, false, 0)
t.Fatalf("unresolved abstract origin ref in DIE at offset %x", kid.Offset)
}
coidx := ex.IdxFromOffset(childOriginDIE.Offset)
childOriginParent := ex.Parent(coidx)
if childOriginParent != originDIE {
ex.DumpEntry(i, false, 0)
t.Fatalf("unexpected parent of abstract origin DIE at offset %v", childOriginDIE.Offset)
}
}
}
if abscount == 0 {
t.Fatalf("no abstract origin refs found, something is wrong")
}
}
func TestAbstractOriginSanity(t *testing.T) {
testenv.MustHaveGoBuild(t)
if testing.Short() {
t.Skip("skipping test in short mode.")
}
mustHaveDWARF(t)
abstractOriginSanity(t, "testdata/httptest", OptAllInl4)
}
func TestAbstractOriginSanityIssue25459(t *testing.T) {
testenv.MustHaveGoBuild(t)
mustHaveDWARF(t)
if runtime.GOARCH != "amd64" && runtime.GOARCH != "386" {
t.Skip("skipping on not-amd64 not-386; location lists not supported")
}
abstractOriginSanity(t, "testdata/issue25459", DefaultOpt)
}
func TestAbstractOriginSanityIssue26237(t *testing.T) {
testenv.MustHaveGoBuild(t)
mustHaveDWARF(t)
abstractOriginSanity(t, "testdata/issue26237", DefaultOpt)
}
func TestRuntimeTypeAttrInternal(t *testing.T) {
testenv.MustHaveGoBuild(t)
testenv.MustInternalLink(t, false)
mustHaveDWARF(t)
testRuntimeTypeAttr(t, "-ldflags=-linkmode=internal")
}
// External linking requires a host linker (https://golang.org/src/cmd/cgo/doc.go l.732)
func TestRuntimeTypeAttrExternal(t *testing.T) {
testenv.MustHaveGoBuild(t)
testenv.MustHaveCGO(t)
mustHaveDWARF(t)
// Explicitly test external linking, for dsymutil compatibility on Darwin.
if runtime.GOARCH == "ppc64" {
t.Skip("-linkmode=external not supported on ppc64")
}
testRuntimeTypeAttr(t, "-ldflags=-linkmode=external")
}
func testRuntimeTypeAttr(t *testing.T, flags string) {
t.Parallel()
const prog = `
package main
import "unsafe"
type X struct{ _ int }
func main() {
var x interface{} = &X{}
p := *(*uintptr)(unsafe.Pointer(&x))
print(p)
}
`
dir := t.TempDir()
f := gobuild(t, dir, prog, flags)
defer f.Close()
out, err := testenv.Command(t, f.path).CombinedOutput()
if err != nil {
t.Fatalf("could not run test program: %v", err)
}
addr, err := strconv.ParseUint(string(out), 10, 64)
if err != nil {
t.Fatalf("could not parse type address from program output %q: %v", out, err)
}
symbols, err := f.Symbols()
if err != nil {
t.Fatalf("error reading symbols: %v", err)
}
var types *objfilepkg.Sym
for _, sym := range symbols {
if sym.Name == "runtime.types" {
types = &sym
break
}
}
if types == nil {
t.Fatal("couldn't find runtime.types in symbols")
}
d, err := f.DWARF()
if err != nil {
t.Fatalf("error reading DWARF: %v", err)
}
rdr := d.Reader()
ex := dwtest.Examiner{}
if err := ex.Populate(rdr); err != nil {
t.Fatalf("error reading DWARF: %v", err)
}
dies := ex.Named("*main.X")
if len(dies) != 1 {
t.Fatalf("wanted 1 DIE named *main.X, found %v", len(dies))
}
rtAttr := dies[0].Val(intdwarf.DW_AT_go_runtime_type)
if rtAttr == nil {
t.Fatalf("*main.X DIE had no runtime type attr. DIE: %v", dies[0])
}
if platform.DefaultPIE(runtime.GOOS, runtime.GOARCH, false) {
return // everything is PIE, addresses are relocated
}
if rtAttr.(uint64)+types.Addr != addr {
t.Errorf("DWARF type offset was %#x+%#x, but test program said %#x", rtAttr.(uint64), types.Addr, addr)
}
}
func TestIssue27614(t *testing.T) {
// Type references in debug_info should always use the DW_TAG_typedef_type
// for the type, when that's generated.
testenv.MustHaveGoBuild(t)
mustHaveDWARF(t)
t.Parallel()
dir := t.TempDir()
const prog = `package main
import "fmt"
type astruct struct {
X int
}
type bstruct struct {
X float32
}
var globalptr *astruct
var globalvar astruct
var bvar0, bvar1, bvar2 bstruct
func main() {
fmt.Println(globalptr, globalvar, bvar0, bvar1, bvar2)
}
`
f := gobuild(t, dir, prog, NoOpt)
defer f.Close()
data, err := f.DWARF()
if err != nil {
t.Fatal(err)
}
rdr := data.Reader()