-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuzz.go
83 lines (65 loc) · 1.35 KB
/
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
73
74
75
76
77
78
79
80
81
82
83
// +build gofuzz
package plua
import (
"io/ioutil"
"os"
"github.com/hirochachacha/plua/compiler"
"github.com/hirochachacha/plua/compiler/ast/printer"
"github.com/hirochachacha/plua/compiler/parser"
"github.com/hirochachacha/plua/object"
"github.com/hirochachacha/plua/runtime"
"github.com/hirochachacha/plua/stdlib"
)
func Fuzz(data []byte) int {
f, err := ioutil.TempFile("", "plua")
if err != nil {
return 0
}
defer os.Remove(f.Name())
defer f.Close()
_, err = f.Write(data)
if err != nil {
return 0
}
var text bool
if ast, err := parser.ParseFile(f.Name(), parser.ParseComments); err == nil {
tmp, err := ioutil.TempFile("", "plua")
if err != nil {
return 0
}
defer os.Remove(tmp.Name())
defer tmp.Close()
err = printer.Fprint(tmp, ast)
if err != nil {
panic(err)
}
_, err = parser.ParseFile(tmp.Name(), parser.ParseComments)
if err != nil {
panic(err)
}
text = true
}
c := compiler.NewCompiler()
proto, err := c.CompileFile(f.Name(), compiler.Either)
if err != nil {
if text {
panic(err)
}
return 0
}
err = object.FprintProto(ioutil.Discard, proto)
if err != nil {
panic(err)
}
p := runtime.NewProcess()
p.Require("", stdlib.Open)
_, err = p.Exec(proto)
if err != nil {
err = object.FprintError(ioutil.Discard, err)
if err != nil {
panic(err)
}
return 0
}
return 1
}