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: set GO111MODULE=off for all go/packages calls #240

Merged
merged 2 commits into from
May 4, 2019
Merged
Show file tree
Hide file tree
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
32 changes: 20 additions & 12 deletions go-fuzz-build/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ func makeTags() string {
return tags
}

// basePackagesConfig returns a base golang.org/x/tools/go/packages.Config
// that clients can then modify and use for calls to go/packages.
func basePackagesConfig() *packages.Config {
cfg := new(packages.Config)
cfg.Env = append(os.Environ(), "GO111MODULE=off")
return cfg
}

// main 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 @@ -219,10 +227,9 @@ func (c *Context) startProfiling() {
func (c *Context) loadPkg(pkg string) {
// Resolve pkg.
// See https://golang.org/issue/30826 and https://golang.org/issue/30828.
rescfg := &packages.Config{
Mode: packages.NeedName,
BuildFlags: []string{"-tags", makeTags()},
}
rescfg := basePackagesConfig()
rescfg.Mode = packages.NeedName
rescfg.BuildFlags = []string{"-tags", makeTags()}
respkgs, err := packages.Load(rescfg, pkg)
if err != nil {
c.failf("could not resolve package %q: %v", pkg, err)
Expand All @@ -240,13 +247,12 @@ func (c *Context) loadPkg(pkg string) {
// We'll use the type information later.
// This also provides better error messages in the case
// of invalid code than trying to compile instrumented code.
cfg := &packages.Config{
Mode: packages.LoadAllSyntax,
BuildFlags: []string{"-tags", makeTags()},
// use custom ParseFile in order to get comments
ParseFile: func(fset *token.FileSet, filename string, src []byte) (*ast.File, error) {
return parser.ParseFile(fset, filename, src, parser.ParseComments)
},
cfg := basePackagesConfig()
cfg.Mode = packages.LoadAllSyntax
cfg.BuildFlags = []string{"-tags", makeTags()}
// use custom ParseFile in order to get comments
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")
if err != nil {
Expand Down Expand Up @@ -362,7 +368,9 @@ func isTest(name, prefix string) bool {
// loadStd finds the set of standard library package paths.
func (c *Context) loadStd() {
// Find out what packages are in the standard library.
stdpkgs, err := packages.Load(&packages.Config{Mode: packages.NeedName}, "std")
cfg := basePackagesConfig()
cfg.Mode = packages.NeedName
stdpkgs, err := packages.Load(cfg, "std")
if err != nil {
c.failf("could not load standard library: %v", err)
}
Expand Down
4 changes: 3 additions & 1 deletion go-fuzz/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ func main() {
if *flagBin == "" {
// Try the default. Best effort only.
var bin string
pkgs, err := packages.Load(nil, ".")
cfg := new(packages.Config)
cfg.Env = append(os.Environ(), "GO111MODULE=off")
pkgs, err := packages.Load(cfg, ".")
if err == nil && len(pkgs) == 1 {
bin = pkgs[0].Name + "-fuzz.zip"
_, err := os.Stat(bin)
Expand Down