1// Copyright 2019 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package dwarf_test
16
17import (
18	"testing"
19
20	. "cloud.google.com/go/cmd/go-cloud-debug-agent/internal/debug/dwarf"
21	"cloud.google.com/go/cmd/go-cloud-debug-agent/internal/debug/elf"
22)
23
24var typedefTests = map[string]string{
25	"t_ptr_volatile_int":                    "*volatile int",
26	"t_ptr_const_char":                      "*const char",
27	"t_long":                                "long int",
28	"t_ushort":                              "short unsigned int",
29	"t_func_int_of_float_double":            "func(float, double) int",
30	"t_ptr_func_int_of_float_double":        "*func(float, double) int",
31	"t_ptr_func_int_of_float_complex":       "*func(complex float) int",
32	"t_ptr_func_int_of_double_complex":      "*func(complex double) int",
33	"t_ptr_func_int_of_long_double_complex": "*func(complex long double) int",
34	"t_func_ptr_int_of_char_schar_uchar":    "func(char, signed char, unsigned char) *int",
35	"t_func_void_of_char":                   "func(char) void",
36	"t_func_void_of_void":                   "func() void",
37	"t_func_void_of_ptr_char_dots":          "func(*char, ...) void",
38	"t_my_struct":                           "struct my_struct {vi volatile int@0; x char@4 : 1@7; y int@4 : 4@27; z [0]int@8; array [40]long long int@8; zz [0]int@328}",
39	"t_my_struct1":                          "struct my_struct1 {zz [1]int@0}",
40	"t_my_union":                            "union my_union {vi volatile int@0; x char@0 : 1@7; y int@0 : 4@28; array [40]long long int@0}",
41	"t_my_enum":                             "enum my_enum {e1=1; e2=2; e3=-5; e4=1000000000000000}",
42	"t_my_list":                             "struct list {val short int@0; next *t_my_list@8}",
43	"t_my_tree":                             "struct tree {left *struct tree@0; right *struct tree@8; val long long unsigned int@16}",
44}
45
46func elfData(t *testing.T, name string) *Data {
47	f, err := elf.Open(name)
48	if err != nil {
49		t.Fatal(err)
50	}
51
52	d, err := f.DWARF()
53	if err != nil {
54		t.Fatal(err)
55	}
56	return d
57}
58
59func TestTypedefsELF(t *testing.T) { testTypedefs(t, elfData(t, "testdata/typedef.elf"), "elf") }
60
61func TestTypedefsELFDwarf4(t *testing.T) { testTypedefs(t, elfData(t, "testdata/typedef.elf4"), "elf") }
62
63func testTypedefs(t *testing.T, d *Data, kind string) {
64	r := d.Reader()
65	seen := make(map[string]bool)
66	for {
67		e, err := r.Next()
68		if err != nil {
69			t.Fatal("r.Next:", err)
70		}
71		if e == nil {
72			break
73		}
74		if e.Tag == TagTypedef {
75			typ, err := d.Type(e.Offset)
76			if err != nil {
77				t.Fatal("d.Type:", err)
78			}
79			t1 := typ.(*TypedefType)
80			var typstr string
81			if ts, ok := t1.Type.(*StructType); ok {
82				typstr = ts.Defn()
83			} else {
84				typstr = t1.Type.String()
85			}
86
87			if want, ok := typedefTests[t1.Name]; ok {
88				if seen[t1.Name] {
89					t.Errorf("multiple definitions for %s", t1.Name)
90				}
91				seen[t1.Name] = true
92				if typstr != want {
93					t.Errorf("%s:\n\thave %s\n\twant %s", t1.Name, typstr, want)
94				}
95			}
96		}
97		if e.Tag != TagCompileUnit {
98			r.SkipChildren()
99		}
100	}
101
102	for k := range typedefTests {
103		if !seen[k] {
104			t.Errorf("missing %s", k)
105		}
106	}
107}
108
109func TestTypeForNonTypeEntry(t *testing.T) {
110	d := elfData(t, "testdata/typedef.elf")
111
112	// The returned entry will be a Subprogram.
113	ent, err := d.LookupFunction("main")
114	if err != nil {
115		t.Fatal("d.LookupFunction:", err)
116	}
117
118	_, err = d.Type(ent.Offset)
119	if err == nil {
120		t.Fatal("nil error for unreadable entry")
121	}
122}
123