From 87b63f1b2a958c1a02a468bd4c76cd897573035d Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Thu, 9 May 2019 12:09:43 -0700 Subject: [PATCH] go-fuzz-build: load reflect when using libfuzzer libfuzzer's generated main function uses package reflect. When attempting to build a package that doesn't depend on reflect, such as github.com/dvyukuv/go-fuzz-corpus/{bzip2,gif,url}, package reflect wasn't getting copied to GOROOT, and the build failed. Fix that. We may need something similar in the future for fuzz.F; see #223. Updates google/oss-fuzz/#2188 --- go-fuzz-build/main.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/go-fuzz-build/main.go b/go-fuzz-build/main.go index 6ac0cbffe..c73a61d56 100644 --- a/go-fuzz-build/main.go +++ b/go-fuzz-build/main.go @@ -251,7 +251,15 @@ func (c *Context) loadPkg(pkg string) { cfg.ParseFile = func(fset *token.FileSet, filename string, src []byte) (*ast.File, error) { return parser.ParseFile(fset, filename, src, parser.ParseComments) } - initial, err := packages.Load(cfg, pkg, "github.com/dvyukov/go-fuzz/go-fuzz-dep") + // We need to load: + // * the target package, obviously + // * go-fuzz-dep, since we use it for instrumentation + // * reflect, if we are using libfuzzer, since its generated main function requires it + loadpkgs := []string{pkg, "github.com/dvyukov/go-fuzz/go-fuzz-dep"} + if *flagLibFuzzer { + loadpkgs = append(loadpkgs, "reflect") + } + initial, err := packages.Load(cfg, loadpkgs...) if err != nil { c.failf("could not load packages: %v", err) }