-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
264 lines (229 loc) · 6.12 KB
/
main.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package main
import (
"flag"
"go/token"
"io/ioutil"
"log"
"os"
"os/exec"
"strings"
"text/template"
"golang.org/x/tools/go/packages"
)
var (
flagFunc = flag.String("func", "Fuzz", "fuzzer entry point")
flagO = flag.String("o", "", "output file")
flagPath = flag.String("abs_path", "", "absolute path to fuzzer")
flagRace = flag.Bool("race", false, "enable data race detection")
flagTags = flag.String("tags", "", "a comma-separated list of build tags to consider satisfied during the build")
flagV = flag.Bool("v", false, "print the names of packages as they are compiled")
flagWork = flag.Bool("work", false, "print the name of the temporary work directory and do not remove it when exiting")
flagX = flag.Bool("x", false, "print the commands")
flagOverlay = flag.String("overlay", "", "JSON config file that provides an overlay for build operations")
flagInclude = flag.String("include", "*", "a comma-separated list of import paths to instrument")
flagPreserve = flag.String("preserve", "", "a comma-separated list of import paths not to instrument")
LoadMode = packages.NeedName |
packages.NeedFiles |
packages.NeedCompiledGoFiles |
packages.NeedImports |
packages.NeedDeps
)
var include, ignore []string
func main() {
flag.Parse()
if !token.IsIdentifier(*flagFunc) || !token.IsExported(*flagFunc) {
log.Fatal("-func must be an exported identifier")
}
tags := "gofuzz_libfuzzer,libfuzzer"
if *flagTags != "" {
tags += "," + *flagTags
}
buildFlags := []string{
"-buildmode", "c-archive",
"-tags", tags,
"-trimpath",
}
if *flagRace {
buildFlags = append(buildFlags, "-race")
}
if *flagV {
buildFlags = append(buildFlags, "-v")
}
if *flagWork {
buildFlags = append(buildFlags, "-work")
}
if *flagX {
buildFlags = append(buildFlags, "-x")
}
if len(flag.Args()) != 1 {
log.Fatal("must specify exactly one package path")
}
path := flag.Args()[0]
if strings.Contains(path, "...") {
log.Fatal("package path must not contain ... wildcards")
}
include = strings.Split(*flagInclude, ",")
ignore = []string{
"runtime/cgo", // No reason to instrument these.
"runtime/pprof", // No reason to instrument these.
"runtime/race", // No reason to instrument these.
"syscall", // https://github.com/google/oss-fuzz/issues/3639
}
if *flagPreserve != "" {
ignore = append(ignore, strings.Split(*flagPreserve, ",")...)
}
buildFlags = append(buildFlags, "-gcflags", "all=-d=libfuzzer")
//fset := token.NewFileSet()
pkgs, err := packages.Load(&packages.Config{
Mode: LoadMode,
BuildFlags: buildFlags,
Tests: true,
}, "pattern="+path)
if err != nil {
log.Fatal("failed to load packages:", err)
}
visit := func(pkg *packages.Package) {
if !shouldInstrument(pkg.PkgPath) {
buildFlags = append(buildFlags, "-gcflags", pkg.PkgPath+"=-d=libfuzzer=0")
}
}
packages.Visit(pkgs, nil, visit)
if packages.PrintErrors(pkgs) != 0 {
os.Exit(1)
}
/*if len(pkgs) != 1 {
log.Fatal("package path matched multiple packages")
}*/
fuzzerFile, originalFuzzContents, err := rewriteTestingImports(pkgs, *flagFunc)
if err != nil {
panic(err)
}
os.Remove(fuzzerFile)
pkg := pkgs[0]
importPath := pkg.PkgPath
if strings.HasPrefix(importPath, "_/") {
importPath = path
}
mainFile, err := ioutil.TempFile(".", "main.*.go")
if err != nil {
log.Fatal("failed to create temporary file:", err)
}
defer os.Remove(mainFile.Name())
type Data struct {
PkgPath string
Func string
Declarations string
FuzzerParams string
}
/*err = mainTmpl.Execute(os.Stdout, &Data{
PkgPath: importPath,
Func: *flagFunc,
})*/
//return
err = mainTmpl.Execute(mainFile, &Data{
PkgPath: importPath,
Func: *flagFunc,
})
if err != nil {
log.Fatal("failed to execute template:", err)
}
if err := mainFile.Close(); err != nil {
log.Fatal(err)
}
out := *flagO
if out == "" {
out = pkg.Name + "-fuzz.a"
}
args := []string{"build", "-o", out}
if *flagOverlay != "" {
buildFlags = append(buildFlags, "-overlay", *flagOverlay)
}
args = append(args, buildFlags...)
args = append(args, mainFile.Name())
cmd := exec.Command("go", args...)
//cmd := exec.Command("gotip", args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Fatal("failed to build packages:", err)
}
newFile, err := os.Create(fuzzerFile)
if err != nil {
panic(err)
}
defer newFile.Close()
_, err = newFile.Write(originalFuzzContents)
if err != nil {
panic(err)
}
os.Remove(fuzzerFile + "_fuzz.go")
}
// Packages that match one of the include patterns (default is include all packages)
// and none of the exclude patterns (default is none) will be instrumented.
func shouldInstrument(pkgPath string) bool {
for _, incPath := range include {
if matchPattern(incPath, pkgPath) {
for _, excPath := range ignore {
if matchPattern(excPath, pkgPath) {
return false
}
}
return true
}
}
return false
}
func matchPattern(pattern, path string) bool {
if strings.HasSuffix(pattern, "*") {
return strings.HasPrefix(path, strings.TrimSuffix(pattern, "*"))
}
return strings.EqualFold(path, pattern)
}
var mainTmpl = template.Must(template.New("main").Parse(`
// Code generated by go-118-fuzz-build; DO NOT EDIT.
// +build ignore
package main
import (
"runtime"
"strings"
"unsafe"
target {{printf "%q" .PkgPath}}
"github.com/AdamKorcz/go-118-fuzz-build/testing"
)
// #include <stdint.h>
import "C"
//export LLVMFuzzerTestOneInput
func LLVMFuzzerTestOneInput(data *C.char, size C.size_t) C.int {
s := (*[1<<30]byte)(unsafe.Pointer(data))[:size:size]
//target.{{.Func}}(s)
defer catchPanics()
LibFuzzer{{.Func}}(s)
return 0
}
func LibFuzzer{{.Func}}(data []byte) int {
fuzzer := &testing.F{Data:data, T:testing.NewT()}
defer fuzzer.CleanupTempDirs()
target.{{.Func}}(fuzzer)
return 1
}
func catchPanics() {
if r := recover(); r != nil {
var err string
switch r.(type) {
case string:
err = r.(string)
case runtime.Error:
err = r.(runtime.Error).Error()
case error:
err = r.(error).Error()
}
if strings.Contains(err, "GO-FUZZ-BUILD-PANIC") {
return
} else {
panic(err)
}
}
}
func main() {
}
`))