From 7b455eea68df4fad0d78f08d7811f818426afc30 Mon Sep 17 00:00:00 2001 From: Zvonimir Pavlinovic Date: Tue, 14 May 2024 14:19:43 +0000 Subject: [PATCH] internal/vulncheck: load source code for scan symbol mode only 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 Reviewed-by: Ian Cottrell Run-TryBot: Zvonimir Pavlinovic TryBot-Result: Gopher Robot --- internal/scan/source.go | 2 +- internal/vulncheck/packages.go | 23 ++++++++++++++--------- internal/vulncheck/slicing_test.go | 2 +- internal/vulncheck/source_test.go | 10 +++++----- internal/vulncheck/utils_test.go | 2 +- internal/vulncheck/witness_test.go | 2 +- 6 files changed, 23 insertions(+), 18 deletions(-) diff --git a/internal/scan/source.go b/internal/scan/source.go index 89285f1..764e732 100644 --- a/internal/scan/source.go +++ b/internal/scan/source.go @@ -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) } diff --git a/internal/vulncheck/packages.go b/internal/vulncheck/packages.go index f68bd2a..feb6e35 100644 --- a/internal/vulncheck/packages.go +++ b/internal/vulncheck/packages.go @@ -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 { @@ -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 diff --git a/internal/vulncheck/slicing_test.go b/internal/vulncheck/slicing_test.go index 6ad5968..df8fd28 100644 --- a/internal/vulncheck/slicing_test.go +++ b/internal/vulncheck/slicing_test.go @@ -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) } diff --git a/internal/vulncheck/source_test.go b/internal/vulncheck/source_test.go index 44bd7e0..2d94cee 100644 --- a/internal/vulncheck/source_test.go +++ b/internal/vulncheck/source_test.go @@ -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) } @@ -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) } @@ -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) } @@ -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) } @@ -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) } diff --git a/internal/vulncheck/utils_test.go b/internal/vulncheck/utils_test.go index d9c103d..20905fe 100644 --- a/internal/vulncheck/utils_test.go +++ b/internal/vulncheck/utils_test.go @@ -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) } diff --git a/internal/vulncheck/witness_test.go b/internal/vulncheck/witness_test.go index d6f740f..526aa69 100644 --- a/internal/vulncheck/witness_test.go +++ b/internal/vulncheck/witness_test.go @@ -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) }