Skip to content

Commit

Permalink
go-fuzz-build: add -race
Browse files Browse the repository at this point in the history
I'm not particularly happy about all the hard-coding of paths
required to make this work. One of the nice things about migrating
to go/packages was that we almost entirely stopped hard-coding
lists of packages. But unfortunately, I don't see an alternative here.

Updates #231
  • Loading branch information
josharian authored and dvyukov committed Apr 2, 2019
1 parent 7f2a178 commit 9cfa592
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion go-fuzz-build/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var (
flagOut = flag.String("o", "", "output file")
flagFunc = flag.String("func", "", "preferred entry function")
flagWork = flag.Bool("work", false, "don't remove working directory")
flagRace = flag.Bool("race", false, "enable race detector")
flagCPU = flag.Bool("cpuprofile", false, "generate cpu profile in cpu.pprof")
flagLibFuzzer = flag.Bool("libfuzzer", false, "output static archive for use with libFuzzer")
)
Expand All @@ -44,6 +45,9 @@ func makeTags() string {
if *flagLibFuzzer {
tags += " gofuzz_libfuzzer"
}
if *flagRace {
tags += " race"
}
if len(*flagTag) > 0 {
tags += " " + *flagTag
}
Expand All @@ -67,6 +71,9 @@ func main() {
if *flagFunc != "" && !isFuzzFuncName(*flagFunc) {
c.failf("provided -func=%v, but %v is not a fuzz function name", *flagFunc, *flagFunc)
}
if *flagLibFuzzer && *flagRace {
c.failf("-race and -libfuzzer are incompatible")
}

c.startProfiling() // start pprof as requested
c.loadPkg(pkg) // load and typecheck pkg
Expand Down Expand Up @@ -401,9 +408,13 @@ func (c *Context) populateWorkdir() {

// TODO: See if we can avoid making toolchain copies,
// using some combination of env vars and toolexec.
if *flagLibFuzzer {
if *flagLibFuzzer || *flagRace {
c.copyDir(filepath.Join(c.GOROOT, "src", "runtime", "cgo"), filepath.Join(c.workdir, "goroot", "src", "runtime", "cgo"))
}
if *flagRace {
c.copyDir(filepath.Join(c.GOROOT, "src", "runtime", "race"), filepath.Join(c.workdir, "goroot", "src", "runtime", "race"))
c.copyDir(filepath.Join(c.GOROOT, "src", "sync", "atomic"), filepath.Join(c.workdir, "goroot", "src", "sync", "atomic"))
}
c.copyDir(filepath.Join(c.GOROOT, "pkg", "tool"), filepath.Join(c.workdir, "goroot", "pkg", "tool"))
if _, err := os.Stat(filepath.Join(c.GOROOT, "pkg", "include")); err == nil {
c.copyDir(filepath.Join(c.GOROOT, "pkg", "include"), filepath.Join(c.workdir, "goroot", "pkg", "include"))
Expand Down Expand Up @@ -443,6 +454,9 @@ func (c *Context) buildInstrumentedBinary(blocks *[]CoverBlock, sonar *[]CoverBl
mainPkg := c.createFuzzMain()
outf := c.tempFile()
args := []string{"build", "-tags", makeTags()}
if *flagRace {
args = append(args, "-race")
}
if *flagLibFuzzer {
args = append(args, "-buildmode=c-archive")
}
Expand Down

0 comments on commit 9cfa592

Please sign in to comment.