| 1 | package btf |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/binary" |
| 6 | "fmt" |
| 7 | "io" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/go-quicktest/qt" |
| 11 | |
| 12 | "github.com/cilium/ebpf/internal" |
| 13 | ) |
| 14 | |
| 15 | func FuzzSpec(f *testing.F) { |
| 16 | f.Add(mustBTFHeader(f)) |
| 17 | |
| 18 | f.Fuzz(func(t *testing.T, data []byte) { |
| 19 | if len(data) < binary.Size(btfHeader{}) { |
| 20 | t.Skip("data is too short") |
| 21 | } |
| 22 | |
| 23 | spec, err := loadRawSpec(data, nil) |
| 24 | if err != nil { |
| 25 | if spec != nil { |
| 26 | t.Fatal("spec is not nil") |
| 27 | } |
| 28 | return |
| 29 | } |
| 30 | |
| 31 | if spec == nil { |
| 32 | t.Fatal("spec is nil") |
| 33 | } |
| 34 | |
| 35 | for typ, err := range spec.All() { |
| 36 | if err == nil { |
| 37 | fmt.Fprintf(io.Discard, "%+10v", typ) |
| 38 | } |
| 39 | } |
| 40 | }) |
| 41 | } |
| 42 | |
| 43 | func FuzzExtInfo(f *testing.F) { |
| 44 | f.Add(mustBTFHeader(f), []byte("\x00foo\x00barfoo\x00")) |
| 45 | |
| 46 | f.Fuzz(func(t *testing.T, data, strings []byte) { |
| 47 | if len(data) < binary.Size(btfExtHeader{}) { |
| 48 | t.Skip("data is too short") |
| 49 | } |
| 50 | |
| 51 | table, err := readStringTable(bytes.NewReader(strings), nil) |
| 52 | if err != nil { |
| 53 | t.Skip("invalid string table") |
| 54 | } |
| 55 | |
| 56 | emptySpec := specFromTypes(t, nil) |
| 57 | emptySpec.strings = table |
| 58 | |
| 59 | info, err := loadExtInfos(bytes.NewReader(data), internal.NativeEndian, emptySpec) |
| 60 | if err != nil { |
| 61 | if info != nil { |
| 62 | t.Fatal("info is not nil") |
| 63 | } |
| 64 | } else if info == nil { |
| 65 | t.Fatal("info is nil") |
| 66 | } |
| 67 | }) |
| 68 | } |
| 69 | |
| 70 | func mustBTFHeader(f *testing.F) []byte { |
| 71 | buf, err := binary.Append(nil, internal.NativeEndian, &btfHeader{ |
| 72 | Magic: btfMagic, |
| 73 | Version: 1, |
| 74 | HdrLen: uint32(binary.Size(btfHeader{})), |
| 75 | }) |
| 76 | qt.Assert(f, qt.IsNil(err)) |
| 77 | return buf |
| 78 | } |