FuzzEval evaluates a CEL expression with variable inputs generated by libprotobuf-mutator.
(data []byte)
| 24 | |
| 25 | // FuzzEval evaluates a CEL expression with variable inputs generated by libprotobuf-mutator. |
| 26 | func FuzzEval(data []byte) int { |
| 27 | gen := &FuzzVariables{} |
| 28 | err := proto.Unmarshal(data, gen) |
| 29 | if err != nil { |
| 30 | panic("Failed to unmarshal LPM generated variables") |
| 31 | } |
| 32 | |
| 33 | declares := make([]*exprpb.Decl, 0, len(gen.Inputs)) |
| 34 | for k := range gen.Inputs { |
| 35 | declares = append(declares, decls.NewVar(k, decls.String)) |
| 36 | } |
| 37 | env, err := getCELFuzzEnv() |
| 38 | if err != nil { |
| 39 | panic("impossible to create env") |
| 40 | } |
| 41 | |
| 42 | env, err = env.Extend(cel.Declarations(declares...)) |
| 43 | if err != nil { |
| 44 | panic("impossible to create env") |
| 45 | } |
| 46 | |
| 47 | ast, issues := env.Compile(gen.Expr) |
| 48 | if issues != nil && issues.Err() != nil { |
| 49 | return 0 |
| 50 | } |
| 51 | prg, err := env.Program(ast) |
| 52 | if err != nil { |
| 53 | return 0 |
| 54 | } |
| 55 | |
| 56 | _, _, _ = prg.Eval(gen.Inputs) |
| 57 | |
| 58 | return 1 |
| 59 | } |
| 60 |