Skip to content

Commit

Permalink
feat: add strict type check for dwarf struct field
Browse files Browse the repository at this point in the history
Signed-off-by: Zxilly <[email protected]>
  • Loading branch information
Zxilly committed Jun 16, 2024
1 parent d8c0187 commit 2f29c5b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion internal/dwarf/dwarf.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func SizeForDWARFVar(
}}, uint64(typ.Size()), nil
} else if structTyp.StructName == "[]uint8" {
// check byte slice, normally it comes from embed
dataAddr, size, err := readSlice(structTyp, readMemory)
dataAddr, size, err := readSlice(structTyp, readMemory, "*uint8")
if err != nil || size == 0 {
return nil, uint64(typ.Size()), err
}
Expand Down
10 changes: 5 additions & 5 deletions internal/dwarf/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func readIntTo64(data []byte) int64 {
type MemoryReader func(addr, size uint64) ([]byte, error)

func readString(structTyp *dwarf.StructType, readMemory MemoryReader) (addr uint64, size uint64, err error) {
err = checkField(structTyp, "str", "len")
err = checkField(structTyp, fieldPattern{"str", "*uint8"}, fieldPattern{"len", "int"})
if err != nil {
return 0, 0, err
}
Expand All @@ -63,8 +63,8 @@ func readString(structTyp *dwarf.StructType, readMemory MemoryReader) (addr uint
return ptr, uint64(strLen), nil
}

func readSlice(typ *dwarf.StructType, readMemory MemoryReader) (addr uint64, size uint64, err error) {
err = checkField(typ, "array", "len", "cap")
func readSlice(typ *dwarf.StructType, readMemory MemoryReader, memberTyp string) (addr uint64, size uint64, err error) {
err = checkField(typ, fieldPattern{"array", memberTyp}, fieldPattern{"len", "int"}, fieldPattern{"cap", "int"})
if err != nil {
return 0, 0, err
}
Expand Down Expand Up @@ -100,7 +100,7 @@ func readSlice(typ *dwarf.StructType, readMemory MemoryReader) (addr uint64, siz
}

func readEmbedFS(typ *dwarf.StructType, readMemory MemoryReader) ([]Content, error) {
err := checkField(typ, "files")
err := checkField(typ, fieldPattern{"files", "*struct []embed.file"})
if err != nil {
return nil, err
}
Expand All @@ -127,7 +127,7 @@ func readEmbedFS(typ *dwarf.StructType, readMemory MemoryReader) ([]Content, err
addr = ptr
}
return readMemory(addr, size)
})
}, "*embed.file")

if err != nil {
return nil, err
Expand Down
15 changes: 12 additions & 3 deletions internal/dwarf/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,23 @@ import (
"fmt"
)

func checkField(typ *dwarf.StructType, fields ...string) error {
type fieldPattern struct {
name string
typ string
}

func checkField(typ *dwarf.StructType, fields ...fieldPattern) error {
if len(typ.Field) != len(fields) {
return fmt.Errorf("%s struct has %d fields", typ.StructName, len(typ.Field))
}

for i, field := range fields {
if typ.Field[i].Name != field {
return fmt.Errorf("%s struct has wrong field name", typ.StructName)
if typ.Field[i].Name != field.name {
return fmt.Errorf("field %d name is %s, expect %s", i, typ.Field[i].Name, field.name)
}

if typ.Field[i].Type.String() != field.typ {
return fmt.Errorf("field %d type is %s, expect %s", i, typ.Field[i].Type.String(), field.typ)
}
}

Expand Down

0 comments on commit 2f29c5b

Please sign in to comment.