aboutsummaryrefslogtreecommitdiffhomepage
path: root/projects/cel-go/fuzz_eval.go
blob: 8b2764c6095bab1efc0f848ccc32fcb463ac53f8 (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
package cel

import (
	"github.com/golang/protobuf/proto"

	"github.com/google/cel-go/checker/decls"
	exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
)

func FuzzEval(data []byte) int {
	gen := &FuzzVariables{}
	err := proto.Unmarshal(data, gen)
	if err != nil {
		panic("Failed to unmarshal LPM generated variables")
	}

	declares := make([]*exprpb.Decl, 0, len(gen.Inputs))
	for k, _ := range gen.Inputs {
		declares = append(declares, decls.NewVar(k, decls.String))
	}
	env, err := NewEnv(Declarations(declares...))
	if err != nil {
		panic("impossible to create env")
	}

	ast, issues := env.Compile(gen.Expr)
	if issues != nil && issues.Err() != nil {
		return 0
	}
	prg, err := env.Program(ast)
	if err != nil {
		return 0
	}
	//fmt.Printf("loltry %#+v\n", gen)

	_, _, err = prg.Eval(gen.Inputs)

	return 1
}