summaryrefslogtreecommitdiff
path: root/vendor/github.com/cilium/ebpf/internal/btf/fuzz.go
blob: 37e043fd378c7fab4574a2e4c58ddacc6bb87689 (plain)
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
// +build gofuzz

// Use with https://github.com/dvyukov/go-fuzz

package btf

import (
	"bytes"
	"encoding/binary"

	"github.com/cilium/ebpf/internal"
)

func FuzzSpec(data []byte) int {
	if len(data) < binary.Size(btfHeader{}) {
		return -1
	}

	spec, err := loadNakedSpec(bytes.NewReader(data), internal.NativeEndian, nil, nil)
	if err != nil {
		if spec != nil {
			panic("spec is not nil")
		}
		return 0
	}
	if spec == nil {
		panic("spec is nil")
	}
	return 1
}

func FuzzExtInfo(data []byte) int {
	if len(data) < binary.Size(btfExtHeader{}) {
		return -1
	}

	table := stringTable("\x00foo\x00barfoo\x00")
	info, err := parseExtInfo(bytes.NewReader(data), internal.NativeEndian, table)
	if err != nil {
		if info != nil {
			panic("info is not nil")
		}
		return 0
	}
	if info == nil {
		panic("info is nil")
	}
	return 1
}