-
Notifications
You must be signed in to change notification settings - Fork 0
/
flags.go
111 lines (90 loc) · 2.93 KB
/
flags.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
package main
import (
"flag"
"fmt"
"os"
"strings"
"unicode"
)
var (
funcName = flag.String("func", "Fuzz", "name of the Fuzz function")
corpusDir = flag.String("corpus", "corpus", "corpus directory (optional)")
keepFile = flag.Bool("keep", false, "keep generated fuzz file (always true for native)")
printCmd = flag.Bool("x", false, "print the commands")
outputFile = flag.String("o", "", "output file")
verbose = flag.Bool("v", false, "show verbose output")
runFuzzer = flag.Bool("run", false, "run fuzzer after building")
native = flag.Bool("native", false, "generate native Go fuzzing test to run with go test -fuzz")
libfuzzer = flag.Bool("libfuzzer", false, "build for libFuzzer")
libfuzzerFlags = flag.String("libfuzzer.flags", "", "additional go-libfuzz-build flags")
libfuzzerList = flag.Bool("libfuzzer.list", false, "list go-libfuzz-build flags")
clangFlags = flag.String("libfuzzer.clangflags", "-g -O1 -fsanitize=fuzzer", "clang build flags")
gofuzz = flag.Bool("gofuzz", false, "build for go-fuzz")
gofuzzFlags = flag.String("gofuzz.flags", "", "additional golang-fuzz flags")
gofuzzList = flag.Bool("gofuzz.list", false, "list golang-fuzz flags")
afl = flag.Bool("afl", false, "build for AFL++")
aflFlags = flag.String("afl.flags", "", "additional go-afl-build flags")
aflList = flag.Bool("afl.list", false, "list go-afl-build flags")
all = flag.Bool("all", false, "build for all supported fuzzing engines")
)
func parseArgs() []string {
flag.Parse()
if *libfuzzerList || *gofuzzList || *aflList {
listBuildFlags()
os.Exit(0)
}
checkOneFuzzerFlag(`-run`, *runFuzzer)
checkOneFuzzerFlag(`-o`, *outputFile != ``)
if *funcName == "" {
fmt.Println("Usage: golang-fuzz [options] PACKAGE_PATH [run_args]")
flag.PrintDefaults()
os.Exit(1)
}
// Check if Fuzz function is formatted FuzzXxx
if !strings.HasPrefix(*funcName, `Fuzz`) || (*funcName != `Fuzz` && !unicode.IsUpper(rune((*funcName)[4]))) {
fmt.Printf("Fuzz function %s must be formatted as FuzzXxx\n", *funcName)
os.Exit(1)
}
if flag.NArg() == 0 {
return []string{`.`}
}
return flag.Args()
}
func listBuildFlags() {
*verbose = true
if *libfuzzerList {
command(`go-libfuzz-build`, `-help`)
}
if *gofuzzList {
command(`golang-fuzz`, `-help`)
}
if *aflList {
command(`go-afl-build`, `-help`)
}
}
func checkOneFuzzerFlag(flagName string, condition bool) {
if *all && condition {
fmt.Println(`Must specify a fuzzer when using ` + flagName)
os.Exit(1)
}
}
func getBuildArgs(flags *string, args ...string) []string {
additionalFlags := strings.Fields(*flags)
if len(additionalFlags) == 0 {
return args
}
return append(additionalFlags, args...)
}
func getOutputFile(defaultFile string) string {
if *outputFile != `` {
return *outputFile
}
return defaultFile
}
func isValidCorpusDir() (bool, os.FileInfo) {
f, err := os.Stat(*corpusDir)
if err == nil && f.IsDir() {
return true, f
}
return false, nil
}