-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
interna/vulncheck: call updateInitPositions
The call was likely lost somewhere during the refactoring. We now call it. Moreover, we call it where it makes the most sense, in CallStacks computation. This is why this CL also moves some code. Change-Id: I6665444e81d83fcb5436509f845983c6ba35d5a2 Reviewed-on: https://go-review.googlesource.com/c/vuln/+/506998 Run-TryBot: Zvonimir Pavlinovic <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Ian Cottrell <[email protected]>
- Loading branch information
1 parent
2f47882
commit 40b2f5d
Showing
4 changed files
with
246 additions
and
248 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,19 +5,10 @@ | |
package scan | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"path" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"golang.org/x/tools/go/packages/packagestest" | ||
"golang.org/x/vuln/internal/client" | ||
"golang.org/x/vuln/internal/govulncheck" | ||
"golang.org/x/vuln/internal/osv" | ||
"golang.org/x/vuln/internal/vulncheck" | ||
) | ||
|
||
func TestSummarizeCallStack(t *testing.T) { | ||
|
@@ -83,142 +74,3 @@ func stringToFinding(s string) *govulncheck.Finding { | |
} | ||
return f | ||
} | ||
|
||
// TestInits checks for correct positions of init functions | ||
// and their respective calls (see #51575). | ||
func TestInits(t *testing.T) { | ||
testClient, err := client.NewInMemoryClient( | ||
[]*osv.Entry{ | ||
{ | ||
ID: "A", Affected: []osv.Affected{{Module: osv.Module{Path: "golang.org/amod"}, Ranges: []osv.Range{{Type: osv.RangeTypeSemver}}, | ||
EcosystemSpecific: osv.EcosystemSpecific{Packages: []osv.Package{{ | ||
Path: "golang.org/amod/avuln", Symbols: []string{"A"}}, | ||
}}, | ||
}}, | ||
}, | ||
{ | ||
ID: "C", Affected: []osv.Affected{{Module: osv.Module{Path: "golang.org/cmod"}, Ranges: []osv.Range{{Type: osv.RangeTypeSemver}}, | ||
EcosystemSpecific: osv.EcosystemSpecific{Packages: []osv.Package{{ | ||
Path: "golang.org/cmod/cvuln", Symbols: []string{"C"}}, | ||
}}, | ||
}}, | ||
}, | ||
}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
e := packagestest.Export(t, packagestest.Modules, []packagestest.Module{ | ||
{ | ||
Name: "golang.org/entry", | ||
Files: map[string]interface{}{ | ||
"x/x.go": ` | ||
package x | ||
import ( | ||
_ "golang.org/amod/avuln" | ||
_ "golang.org/bmod/b" | ||
) | ||
`, | ||
}, | ||
}, | ||
{ | ||
Name: "golang.org/[email protected]", | ||
Files: map[string]interface{}{"avuln/avuln.go": ` | ||
package avuln | ||
func init() { | ||
A() | ||
} | ||
func A() {} | ||
`}, | ||
}, | ||
{ | ||
Name: "golang.org/[email protected]", | ||
Files: map[string]interface{}{"b/b.go": ` | ||
package b | ||
import _ "golang.org/cmod/cvuln" | ||
`}, | ||
}, | ||
{ | ||
Name: "golang.org/[email protected]", | ||
Files: map[string]interface{}{"cvuln/cvuln.go": ` | ||
package cvuln | ||
var x int = C() | ||
func C() int { | ||
return 0 | ||
} | ||
`}, | ||
}, | ||
}) | ||
defer e.Cleanup() | ||
|
||
// Load x as entry package. | ||
graph := vulncheck.NewPackageGraph("go1.18") | ||
pkgs, err := graph.LoadPackages(e.Config, nil, []string{path.Join(e.Temp(), "entry/x")}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if len(pkgs) != 1 { | ||
t.Fatal("failed to load x test package") | ||
} | ||
cfg := &govulncheck.Config{ScanLevel: "symbol"} | ||
result, err := vulncheck.Source(context.Background(), pkgs, cfg, testClient, graph) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
cs := vulncheck.CallStacks(result) | ||
updateInitPositions(cs) | ||
|
||
want := map[string][]string{ | ||
"A": { | ||
// Entry init's position is the package statement. | ||
// It calls avuln.init at avuln import statement. | ||
"N:golang.org/entry/x.init F:x.go:2:4 C:x.go:5:5", | ||
// implicit avuln.init is calls explicit init at the avuln | ||
// package statement. | ||
"N:golang.org/amod/avuln.init F:avuln.go:2:4 C:avuln.go:2:4", | ||
"N:golang.org/amod/avuln.init#1 F:avuln.go:4:9 C:avuln.go:5:6", | ||
"N:golang.org/amod/avuln.A F:avuln.go:8:9 C:", | ||
}, | ||
"C": { | ||
"N:golang.org/entry/x.init F:x.go:2:4 C:x.go:6:5", | ||
"N:golang.org/bmod/b.init F:b.go:2:4 C:b.go:4:11", | ||
"N:golang.org/cmod/cvuln.init F:cvuln.go:2:4 C:cvuln.go:4:17", | ||
"N:golang.org/cmod/cvuln.C F:cvuln.go:6:9 C:", | ||
}, | ||
} | ||
if diff := cmp.Diff(want, strStacks(cs)); diff != "" { | ||
t.Errorf("modules mismatch (-want, +got):\n%s", diff) | ||
} | ||
} | ||
|
||
// strStacks creates a string representation of a call stacks map where | ||
// vulnerability is represented with its ID and stack entry is a string | ||
// "N:<package path.function name> F:<function position> C:< call position>" | ||
// File paths in positions consists of only file names. | ||
func strStacks(callStacks map[*vulncheck.Vuln]vulncheck.CallStack) map[string][]string { | ||
m := make(map[string][]string) | ||
for v, cs := range callStacks { | ||
var scs []string | ||
for _, se := range cs { | ||
fPos := se.Function.Pos | ||
fp := fmt.Sprintf("%s:%d:%d", filepath.Base(fPos.Filename), fPos.Line, fPos.Column) | ||
|
||
var cp string | ||
if se.Call != nil && se.Call.Pos.IsValid() { | ||
cPos := se.Call.Pos | ||
cp = fmt.Sprintf("%s:%d:%d", filepath.Base(cPos.Filename), cPos.Line, cPos.Column) | ||
} | ||
|
||
sse := fmt.Sprintf("N:%s.%s\tF:%v\tC:%v", se.Function.Package.PkgPath, se.Function.Name, fp, cp) | ||
scs = append(scs, sse) | ||
} | ||
m[v.OSV.ID] = scs | ||
} | ||
return m | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.