Skip to content

Commit 87b63f1

Browse files
committed
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 dvyukov#223. Updates google/oss-fuzz/#2188
1 parent 24b3a97 commit 87b63f1

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

go-fuzz-build/main.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,15 @@ func (c *Context) loadPkg(pkg string) {
251251
cfg.ParseFile = func(fset *token.FileSet, filename string, src []byte) (*ast.File, error) {
252252
return parser.ParseFile(fset, filename, src, parser.ParseComments)
253253
}
254-
initial, err := packages.Load(cfg, pkg, "github.com/dvyukov/go-fuzz/go-fuzz-dep")
254+
// We need to load:
255+
// * the target package, obviously
256+
// * go-fuzz-dep, since we use it for instrumentation
257+
// * reflect, if we are using libfuzzer, since its generated main function requires it
258+
loadpkgs := []string{pkg, "github.com/dvyukov/go-fuzz/go-fuzz-dep"}
259+
if *flagLibFuzzer {
260+
loadpkgs = append(loadpkgs, "reflect")
261+
}
262+
initial, err := packages.Load(cfg, loadpkgs...)
255263
if err != nil {
256264
c.failf("could not load packages: %v", err)
257265
}

0 commit comments

Comments
 (0)