Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go-fuzz-build: added support for '-tags' #151

Merged
merged 1 commit into from
Feb 14, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions go-fuzz-build/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
)

var (
flagTag = flag.String("tags", "", "a space-separated list of build tags to consider satisfied during the build")
flagOut = flag.String("o", "", "output file")
flagFunc = flag.String("func", "Fuzz", "entry function")
flagWork = flag.Bool("work", false, "don't remove working directory")
Expand All @@ -32,6 +33,14 @@ var (
GOROOT string
)

func makeTags() string {
tags := "gofuzz"
if len(*flagTag) > 0 {
tags += " " + *flagTag
}
return tags
Copy link
Contributor

@Kubuxu Kubuxu Feb 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Joined tags should have form -tags="tagA tagB" try it out for yourself.

See also: golang/go#8772 (comment)

Thins get a bit tricky when you have multiple tags in play.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, indeed. This works though, it should be a space separated list of tags.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you mean that they need to be quoted, that's handled by the exec.Command

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, rigth, sorry.

}

// Copies the package with all dependent packages into a temp dir,
// instruments Go source files there and builds setting GOROOT to the temp dir.
func main() {
Expand Down Expand Up @@ -136,7 +145,7 @@ func testNormalBuild(pkg string) {
}()
copyFuzzDep(workdir, false)
mainPkg := createFuzzMain(pkg)
cmd := exec.Command("go", "build", "-tags", "gofuzz", "-o", filepath.Join(workdir, "bin"), mainPkg)
cmd := exec.Command("go", "build", "-tags", makeTags(), "-o", filepath.Join(workdir, "bin"), mainPkg)
for _, v := range os.Environ() {
if strings.HasPrefix(v, "GOPATH") {
continue
Expand Down Expand Up @@ -202,7 +211,7 @@ func buildInstrumentedBinary(pkg string, deps map[string]bool, lits map[Literal]
outf := tempFile()
os.Remove(outf)
outf += ".exe"
cmd := exec.Command("go", "build", "-tags", "gofuzz", "-o", outf, mainPkg)
cmd := exec.Command("go", "build", "-tags", makeTags(), "-o", outf, mainPkg)
for _, v := range os.Environ() {
if strings.HasPrefix(v, "GOROOT") || strings.HasPrefix(v, "GOPATH") {
continue
Expand Down Expand Up @@ -435,7 +444,7 @@ func copyDir(dir, newDir string, rec bool, pred func(string) bool) {

func goListList(pkg, what string) []string {
templ := fmt.Sprintf("{{range .%v}}{{.}}|{{end}}", what)
out, err := exec.Command("go", "list", "-tags", "gofuzz", "-f", templ, pkg).CombinedOutput()
out, err := exec.Command("go", "list", "-tags", makeTags(), "-f", templ, pkg).CombinedOutput()
if err != nil {
failf("failed to execute 'go list -f \"%v\" %v': %v\n%v", templ, pkg, err, string(out))
}
Expand All @@ -451,7 +460,7 @@ func goListProps(pkg string, props ...string) []string {
for _, p := range props {
templ += fmt.Sprintf("{{.%v}}|", p)
}
out, err := exec.Command("go", "list", "-tags", "gofuzz", "-f", templ, pkg).CombinedOutput()
out, err := exec.Command("go", "list", "-tags", makeTags(), "-f", templ, pkg).CombinedOutput()
if err != nil {
failf("failed to execute 'go list -f \"%v\" %v': %v\n%v", templ, pkg, err, string(out))
}
Expand All @@ -464,7 +473,7 @@ func goListProps(pkg string, props ...string) []string {

func goListBool(pkg, what string) bool {
templ := fmt.Sprintf("{{.%v}}", what)
out, err := exec.Command("go", "list", "-tags", "gofuzz", "-f", templ, pkg).CombinedOutput()
out, err := exec.Command("go", "list", "-tags", makeTags(), "-f", templ, pkg).CombinedOutput()
if err != nil {
failf("failed to execute 'go list -f \"%v\" %v': %v\n%v", templ, pkg, err, string(out))
}
Expand Down