-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.go
157 lines (132 loc) · 3.38 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
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")
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")
)
func main() {
flag.Parse()
if !token.IsIdentifier(*flagFunc) || !token.IsExported(*flagFunc) {
log.Fatal("-func must be an exported identifier")
}
tags := "gofuzz,gofuzz_libfuzzer,libfuzzer"
if *flagTags != "" {
tags += "," + *flagTags
}
buildFlags := []string{
"-buildmode", "c-archive",
"-gcflags", "all=-d=libfuzzer",
"-tags", tags,
"-trimpath",
}
suppress := []string{
"syscall", // https://github.com/google/oss-fuzz/issues/3639
}
for _, pkg := range suppress {
buildFlags = append(buildFlags, "-gcflags", pkg+"=-d=libfuzzer=0")
}
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")
}
pkgs, err := packages.Load(&packages.Config{
Mode: packages.NeedName,
BuildFlags: buildFlags,
}, "pattern="+path)
if err != nil {
log.Fatal("failed to load packages:", err)
}
if packages.PrintErrors(pkgs) != 0 {
os.Exit(1)
}
if len(pkgs) != 1 {
log.Fatal("package path matched multiple packages")
}
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
}
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}
args = append(args, buildFlags...)
args = append(args, mainFile.Name())
cmd := exec.Command("go", args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Fatal("failed to build packages:", err)
}
}
var mainTmpl = template.Must(template.New("main").Parse(`
// Code generated by go114-fuzz-build; DO NOT EDIT.
// +build ignore
package main
import (
"unsafe"
target {{printf "%q" .PkgPath}}
)
// #include <stdint.h>
import "C"
//export LLVMFuzzerTestOneInput
func LLVMFuzzerTestOneInput(data *C.char, size C.size_t) C.int {
// TODO(mdempsky): Use unsafe.Slice once golang.org/issue/19367 is accepted.
s := (*[1<<30]byte)(unsafe.Pointer(data))[:size:size]
target.{{.Func}}(s)
return 0
}
func main() {
}
`))