From c135dfbf1842993aa2fd4c293b2476ce4733daf7 Mon Sep 17 00:00:00 2001 From: Than McIntosh Date: Fri, 18 Jan 2019 15:16:11 -0500 Subject: [PATCH] debug/dwarf: more graceful handling of unsupported types Enhance the type decoder to do a better job handling unknown type tags. DWARF has a number of type DIEs that this package doesn't handle (things like "pointer to member" types in C++); avoid crashing for such types, but instead return a placeholder "UnsupportedType" object (this idea suggested by Austin). This provides a compromise between implementing the entire kitchen sink and simply returning an error outright on any unknown type DIE. Fixes #29601. Change-Id: I2eeffa094c86ef3a2c358ee42e8e629d74cec2ed Reviewed-on: https://go-review.googlesource.com/c/go/+/158797 Reviewed-by: Brad Fitzpatrick Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- src/debug/dwarf/testdata/cppunsuptypes.cc | 34 ++++++++++++ src/debug/dwarf/testdata/cppunsuptypes.elf | Bin 0 -> 3920 bytes src/debug/dwarf/type.go | 24 +++++++++ src/debug/dwarf/type_test.go | 60 +++++++++++++++++++++ 4 files changed, 118 insertions(+) create mode 100644 src/debug/dwarf/testdata/cppunsuptypes.cc create mode 100644 src/debug/dwarf/testdata/cppunsuptypes.elf diff --git a/src/debug/dwarf/testdata/cppunsuptypes.cc b/src/debug/dwarf/testdata/cppunsuptypes.cc new file mode 100644 index 0000000000..e9281c7dec --- /dev/null +++ b/src/debug/dwarf/testdata/cppunsuptypes.cc @@ -0,0 +1,34 @@ +// 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. + +// cppunsuptypes.elf built with g++ 7.3 +// g++ -g -c -o cppunsuptypes.elf cppunsuptypes.cc + +int i = 3; +double d = 3; + +// anonymous reference type +int &culprit = i; + +// named reference type +typedef double &dref; +dref dr = d; + +// incorporated into another type +typedef struct { + dref q; + int &r; +} hasrefs; + +hasrefs hr = { d, i }; + +// This code is intended to trigger a DWARF "pointer to member" type DIE +struct CS { int dm; }; + +int foo() +{ + int CS::* pdm = &CS::dm; + CS cs = {42}; + return cs.*pdm; +} diff --git a/src/debug/dwarf/testdata/cppunsuptypes.elf b/src/debug/dwarf/testdata/cppunsuptypes.elf new file mode 100644 index 0000000000000000000000000000000000000000..e955512ecd0227f5564ab4d1a29c054d636742d9 GIT binary patch literal 3920 zcmbVP&2Jl35TCc3CU%{;PTZ!ALZ}-RK`B|c5Zbh;txFo30OC;814SWS+nd-B+hKQI zQUv0F0HK1EUigrT1P7#w8#j(X1%+ELNWhIl{{W;Of;hm;?u`BXvZWFu?Rzu7`FykQ zy|v$(d-f?yDG*X{61tM002}EpUy9fg?1oV|KmY#i`I~c_1l^pw^Ds{HSLQb7KRkbD zUfsF^i#H-;u;YoxRH;Y&58*Tx(--MeQCTssIZSH2RRlF!Ph{CCaQp1@(5J?!f$3@k)OZOwfD8?4g2oaJOtM;a?HIOWaE(0n5^G(p z4ORkBg+I{Jx>|)HVoLa^Nl?S;fowiwXUDU{YGfd{8%?IcP94uYky*%}8ngBQ*!v-q zIhnCj*l%XD0NcI9#`g>26&I~D6mn{m{s zu{dfoyO5`fN_#$ja*T9?-Lj~tp}jDXU&ud=EIfjbg%9;qCuxa}5q}0B3m(K#slT&B zI7ZtXd!?YVw$^S2?X~donin{g3fW`Xad_nH3H8j|?Vl|Rz0N*&zA$_6;Nj`QQRgw| zP+_VOwwvCG6|d>}^$LovU#Xo~pE)vhWV$f50!M3Z;Cssf6oa7pXmO=g4E##5>Xq9o z#p-LWzg!HxAgsEf3stm(62> zcbjo#Q;#2r8CuIu8j#fM=RmFJ)cD{)Y6I7PA7b|dJ``&PO5Khx$JwF#3zP)x4GhV;z!tOz3GVy)&owZ)o6J5OT=lmLeIgE%2W`# zl~*{BS6f>4-G=N2UhMyv#?gm*Cw-R?C1xfC;F)Z7+J^#Ro!=*{tnsYxaf0--NiuzJ z2{ZlINtr-q{=;aKlwbVsm7jhk&@EZQN2eKXh@1zyXUR_Lr(YOE3-~1E--xu`eaqsO zJMIiBlJeIQ@)Km{e+euG8-Y)1UsP dCM`9^`+o}cI+^RIZ*P+Sx18vf?u@+Y|1bX320;J- literal 0 HcmV?d00001 diff --git a/src/debug/dwarf/type.go b/src/debug/dwarf/type.go index 4352092ed0..316db258f6 100644 --- a/src/debug/dwarf/type.go +++ b/src/debug/dwarf/type.go @@ -261,6 +261,20 @@ func (t *TypedefType) String() string { return t.Name } func (t *TypedefType) Size() int64 { return t.Type.Size() } +// An UnsupportedType is a placeholder returned in situations where we +// encounter a type that isn't supported. +type UnsupportedType struct { + CommonType + Tag Tag +} + +func (t *UnsupportedType) String() string { + if t.Name != "" { + return t.Name + } + return t.Name + "(unsupported type " + t.Tag.String() + ")" +} + // typeReader is used to read from either the info section or the // types section. type typeReader interface { @@ -680,6 +694,16 @@ func (d *Data) readType(name string, r typeReader, off Offset, typeCache map[Off typ = t typeCache[off] = t t.Name, _ = e.Val(AttrName).(string) + + default: + // This is some other type DIE that we're currently not + // equipped to handle. Return an abstract "unsupported type" + // object in such cases. + t := new(UnsupportedType) + typ = t + typeCache[off] = t + t.Tag = e.Tag + t.Name, _ = e.Val(AttrName).(string) } if err != nil { diff --git a/src/debug/dwarf/type_test.go b/src/debug/dwarf/type_test.go index 6c06731ea1..aa2fbeca0b 100644 --- a/src/debug/dwarf/type_test.go +++ b/src/debug/dwarf/type_test.go @@ -9,6 +9,8 @@ import ( "debug/elf" "debug/macho" "debug/pe" + "fmt" + "strconv" "testing" ) @@ -168,3 +170,61 @@ func TestTypedefCycle(t *testing.T) { } } } + +var unsupportedTypeTests = []string{ + // varname:typename:string:size + "culprit::(unsupported type ReferenceType):8", + "pdm::(unsupported type PtrToMemberType):-1", +} + +func TestUnsupportedTypes(t *testing.T) { + // Issue 29601: + // When reading DWARF from C++ load modules, we can encounter + // oddball type DIEs. These will be returned as "UnsupportedType" + // objects; check to make sure this works properly. + d := elfData(t, "testdata/cppunsuptypes.elf") + r := d.Reader() + seen := make(map[string]bool) + for { + e, err := r.Next() + if err != nil { + t.Fatal("r.Next:", err) + } + if e == nil { + break + } + if e.Tag == TagVariable { + vname, _ := e.Val(AttrName).(string) + tAttr := e.Val(AttrType) + typOff, ok := tAttr.(Offset) + if !ok { + t.Errorf("variable at offset %v has no type", e.Offset) + continue + } + typ, err := d.Type(typOff) + if err != nil { + t.Errorf("err in type decode: %v\n", err) + continue + } + unsup, isok := typ.(*UnsupportedType) + if !isok { + continue + } + tag := vname + ":" + unsup.Name + ":" + unsup.String() + + ":" + strconv.FormatInt(unsup.Size(), 10) + seen[tag] = true + } + } + dumpseen := false + for _, v := range unsupportedTypeTests { + if !seen[v] { + t.Errorf("missing %s", v) + dumpseen = true + } + } + if dumpseen { + for k, _ := range seen { + fmt.Printf("seen: %s\n", k) + } + } +} -- 2.48.1