godwarf: handle unsupported types gracefully (#2106)

Backport https://go-review.googlesource.com/c/go/+/158797 from upstream.

Fixes #2101
This commit is contained in:
Alessandro Arzilli
2020-07-15 19:09:28 +02:00
committed by GitHub
parent c63ae072bc
commit 8571fddbc1
2 changed files with 40 additions and 0 deletions

View File

@ -513,6 +513,22 @@ func (t *ChanType) stringIntl(recCheck recCheck) string {
return "chan " + t.ElemType.String()
}
// An UnsupportedType is a placeholder returned in situations where we
// encounter a type that isn't supported.
type UnsupportedType struct {
CommonType
Tag dwarf.Tag
}
func (t *UnsupportedType) stringIntl(recCheck) string {
if t.Name != "" {
return t.Name
}
return fmt.Sprintf("(unsupported type %s)", t.Tag.String())
}
func (t *UnsupportedType) String() string { return t.stringIntl(nil) }
// Type reads the type at off in the DWARF ``info'' section.
func ReadType(d *dwarf.Data, index int, off dwarf.Offset, typeCache map[dwarf.Offset]Type) (Type, error) {
typ, err := readType(d, "info", d.Reader(), off, typeCache, nil)
@ -1008,6 +1024,16 @@ func readType(d *dwarf.Data, name string, r *dwarf.Reader, off dwarf.Offset, typ
typ = t
typeCache[off] = t
t.Name, _ = e.Val(dwarf.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(dwarf.AttrName).(string)
}
if err != nil {

View File

@ -323,3 +323,17 @@ func TestIssue1636_InlineWithoutOrigin(t *testing.T) {
dwb.TagClose()
fakeBinaryInfo(t, dwb)
}
func TestUnsupportedType(t *testing.T) {
// Tests that reading an unsupported type does not cause an error
dwb := dwarfbuilder.New()
dwb.AddCompileUnit("main", 0x0)
off := dwb.TagOpen(dwarf.TagReferenceType, "blah")
dwb.TagClose()
dwb.TagClose()
_, dw := fakeBinaryInfo(t, dwb)
_, err := godwarf.ReadType(dw, 0, off, make(map[dwarf.Offset]godwarf.Type))
if err != nil {
t.Errorf("unexpected error reading unsupported type: %#v", err)
}
}