This repository has been archived by the owner on Sep 2, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
xdb_fuzz.go
72 lines (60 loc) · 1.52 KB
/
xdb_fuzz.go
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
// +build gofuzz
package xdb
import (
"github.com/spf13/afero"
"github.com/xqueries/xdb/internal/compiler"
"github.com/xqueries/xdb/internal/engine"
"github.com/xqueries/xdb/internal/engine/dbfs"
"github.com/xqueries/xdb/internal/parser"
)
const (
// DataNotInteresting indicates, that the input was not interesting, meaning
// that the input was not valid and the parser handled detected and returned
// an error. The input will still be added to the corpus.
DataNotInteresting int = 0
// DataInteresting indicates a valid parser input. The fuzzer should keep it
// and modify it further.
DataInteresting int = 1
// Skip indicates, that the data must not be added to the corpus. You
// probably shouldn't use it.
Skip int = -1
)
func Fuzz(data []byte) int {
statement := string(data)
// try to parse the input
p, err := parser.New(statement)
if err != nil {
return DataNotInteresting
}
stmt, errs, ok := p.Next()
if !ok {
return DataNotInteresting
}
if len(errs) != 0 {
return DataNotInteresting
}
// compile the statement
c := compiler.New()
cmd, err := c.Compile(stmt)
if err != nil {
return DataNotInteresting
}
// create a new im-memory db file if none is set
fs := afero.NewMemMapFs()
dbFile, err := dbfs.CreateNew(fs)
if err != nil {
panic(err)
}
defer func() { _ = dbFile.Close() }()
// fire up the engine
e, err := engine.New(dbFile)
if err != nil {
panic(err)
}
result, err := e.Evaluate(cmd)
if err != nil {
return DataNotInteresting
}
_ = result
return DataInteresting
}