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