Skip to content

Commit

Permalink
internal/vulncheck: load source code for scan symbol mode only
Browse files Browse the repository at this point in the history
For package and module scan mode, loading code is not needed and it just
takes longer. With this CL, the loading for these mode drops down by
~40%.

Change-Id: Ibdaa98c50e3e90ba1696d8b9706589ff4c279e17
Reviewed-on: https://go-review.googlesource.com/c/vuln/+/585335
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
Run-TryBot: Zvonimir Pavlinovic <zpavlinovic@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
  • Loading branch information
zpavlinovic committed May 15, 2024
1 parent 7ed0faa commit 7b455ee
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 18 deletions.
2 changes: 1 addition & 1 deletion internal/scan/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func runSource(ctx context.Context, handler govulncheck.Handler, cfg *config, cl
Tests: cfg.test,
Env: cfg.env,
}
if err := graph.LoadPackagesAndMods(pkgConfig, cfg.tags, cfg.patterns); err != nil {
if err := graph.LoadPackagesAndMods(pkgConfig, cfg.tags, cfg.patterns, cfg.ScanLevel == govulncheck.ScanLevelSymbol); err != nil {
if isGoVersionMismatchError(err) {
return fmt.Errorf("%v\n\n%v", errGoVersionMismatch, err)
}
Expand Down
23 changes: 14 additions & 9 deletions internal/vulncheck/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,18 +194,12 @@ func (g *PackageGraph) GetPackage(path string) *packages.Package {

// LoadPackages loads the packages specified by the patterns into the graph.
// See golang.org/x/tools/go/packages.Load for details of how it works.
func (g *PackageGraph) LoadPackagesAndMods(cfg *packages.Config, tags []string, patterns []string) error {
func (g *PackageGraph) LoadPackagesAndMods(cfg *packages.Config, tags []string, patterns []string, wantSymbols bool) error {
if len(tags) > 0 {
cfg.BuildFlags = []string{fmt.Sprintf("-tags=%s", strings.Join(tags, ","))}
}
cfg.Mode |=
packages.NeedDeps |
packages.NeedImports |
packages.NeedModule |
packages.NeedSyntax |
packages.NeedTypes |
packages.NeedTypesInfo |
packages.NeedName

addLoadMode(cfg, wantSymbols)

pkgs, err := packages.Load(cfg, patterns...)
if err != nil {
Expand All @@ -230,6 +224,17 @@ func (g *PackageGraph) LoadPackagesAndMods(cfg *packages.Config, tags []string,
return err
}

func addLoadMode(cfg *packages.Config, wantSymbols bool) {
cfg.Mode |=
packages.NeedModule |
packages.NeedName |
packages.NeedDeps |
packages.NeedImports
if wantSymbols {
cfg.Mode |= packages.NeedSyntax | packages.NeedTypes | packages.NeedTypesInfo
}
}

// packageError contains errors from loading a set of packages.
type packageError struct {
Errors []packages.Error
Expand Down
2 changes: 1 addition & 1 deletion internal/vulncheck/slicing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func Do(i I, input string) {
})

graph := NewPackageGraph("go1.18")
err := graph.LoadPackagesAndMods(e.Config, nil, []string{path.Join(e.Temp(), "/module/slice")})
err := graph.LoadPackagesAndMods(e.Config, nil, []string{path.Join(e.Temp(), "/module/slice")}, true)
if err != nil {
t.Fatal(err)
}
Expand Down
10 changes: 5 additions & 5 deletions internal/vulncheck/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func TestCalls(t *testing.T) {

// Load x and y as entry packages.
graph := NewPackageGraph("go1.18")
err := graph.LoadPackagesAndMods(e.Config, nil, []string{path.Join(e.Temp(), "entry/x"), path.Join(e.Temp(), "entry/y")})
err := graph.LoadPackagesAndMods(e.Config, nil, []string{path.Join(e.Temp(), "entry/x"), path.Join(e.Temp(), "entry/y")}, true)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -298,7 +298,7 @@ func TestAllSymbolsVulnerable(t *testing.T) {

// Load x as entry package.
graph := NewPackageGraph("go1.18")
err = graph.LoadPackagesAndMods(e.Config, nil, []string{path.Join(e.Temp(), "entry/x")})
err = graph.LoadPackagesAndMods(e.Config, nil, []string{path.Join(e.Temp(), "entry/x")}, true)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -361,7 +361,7 @@ func TestNoSyntheticNodes(t *testing.T) {

// Load x as entry package.
graph := NewPackageGraph("go1.18")
err := graph.LoadPackagesAndMods(e.Config, nil, []string{path.Join(e.Temp(), "entry/x")})
err := graph.LoadPackagesAndMods(e.Config, nil, []string{path.Join(e.Temp(), "entry/x")}, true)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -435,7 +435,7 @@ func TestRecursion(t *testing.T) {

// Load x as entry package.
graph := NewPackageGraph("go1.18")
err := graph.LoadPackagesAndMods(e.Config, nil, []string{path.Join(e.Temp(), "entry/x")})
err := graph.LoadPackagesAndMods(e.Config, nil, []string{path.Join(e.Temp(), "entry/x")}, true)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -500,7 +500,7 @@ func TestIssue57174(t *testing.T) {

// Load x as entry package.
graph := NewPackageGraph("go1.18")
err := graph.LoadPackagesAndMods(e.Config, nil, []string{path.Join(e.Temp(), "entry/x")})
err := graph.LoadPackagesAndMods(e.Config, nil, []string{path.Join(e.Temp(), "entry/x")}, true)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/vulncheck/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func TestDbSymbolName(t *testing.T) {
defer e.Cleanup()

graph := NewPackageGraph("go1.18")
err := graph.LoadPackagesAndMods(e.Config, nil, []string{path.Join(e.Temp(), "package/x")})
err := graph.LoadPackagesAndMods(e.Config, nil, []string{path.Join(e.Temp(), "package/x")}, true)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/vulncheck/witness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func TestInits(t *testing.T) {

// Load x as entry package.
graph := NewPackageGraph("go1.18")
err = graph.LoadPackagesAndMods(e.Config, nil, []string{path.Join(e.Temp(), "entry/x")})
err = graph.LoadPackagesAndMods(e.Config, nil, []string{path.Join(e.Temp(), "entry/x")}, true)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 7b455ee

Please sign in to comment.