diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index eeab87c0..befd8781 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -80,7 +80,7 @@ jobs: env: PROJECTGRAPH_PUBLIC_REPORT: ${{ runner.temp }}/public.txt PROJECTGRAPH_GOVULNCHECK_REPORT: ${{ runner.temp }}/govulncheck.txt - run: go test -tags projectgraph -run 'TestPublicSets|TestGovulncheckTraces' -count=1 ./test/projectgraph/ + run: go test -tags projectgraph -run 'TestPublicSets|TestGovulncheckTraces|TestVulnAgreement' -count=1 ./test/projectgraph/ - name: Check the gates run: bash scripts/check_projectgraph_gates.sh "$RUNNER_TEMP/public.txt" "$RUNNER_TEMP/govulncheck.txt" pycg-micro=0.95 diff --git a/core/language.go b/core/language.go index 854b19e6..e40ede41 100644 --- a/core/language.go +++ b/core/language.go @@ -95,3 +95,46 @@ type Language interface { type GrammarSelector interface { LanguageForExtension(ext string) Language } + +// SourceSelector is an optional interface of a Language whose files of some +// extensions hold their code in blocks among markup, as the script blocks +// of a Vue component. Source returns the source of the data of a file of +// the extension. A file of another extension gives the Language and data. +type SourceSelector interface { + Source(ext string, data []byte) Source +} + +// Source is what the grammar of a file parses. +type Source struct { + Language Language + // Code is data with each byte outside the code replaced by a space. It + // keeps the offsets and the line ends of data, so a node reports its + // line and column in the file, and its content in data. + Code []byte + // Markup is what the file holds outside the code, or nil. + Markup *Markup +} + +// Markup is the part of a file that the grammar does not parse, as the +// template of a Vue component. The framework of the file runs it. +type Markup struct { + // Offset is the offset of the first byte of the markup that is no + // space. + Offset int + // Scripts are the script elements that load a file, in file order. + Scripts []Script +} + +// Script is a script element that loads a file, as \n\n\n"), + }, + }, + { + name: "lang ts gives TypeScript", + ext: ".svelte", + parts: []part{ + markup(`\n

{n}

\n"), + }, + dialect: dialectTypescript, + }, + { + name: "lang typescript gives TypeScript and a quoted > stays in the tag", + ext: ".vue", + parts: []part{ + markup(`"), + }, + dialect: dialectTypescript, + }, + { + name: "lang tsx gives TSX over the TypeScript of another block", + ext: ".vue", + parts: []part{ + markup(`\n\n"), + }, + dialect: dialectTSX, + }, + { + name: "a block with a src attribute gives no code and no dialect", + ext: ".vue", + parts: []part{ + markup(``), markup("\n\n"), + }, + }, + { + name: "a block that does not close runs to the end of the file", + ext: ".svelte", + parts: []part{ + markup("

hi

\n"), + }, + }, + { + name: "a longer tag name is not a script", + ext: ".vue", + parts: []part{ + markup("\nnot code\n\n"), + }, + }, + { + name: "Astro reads the fence and the scripts as TypeScript", + ext: ".astro", + parts: []part{ + markup("\n---\n"), code("import pad from 'left-pad'\nconst title: string = pad('a', 2)\n"), markup("---\n

{title}

\n\n"), + }, + dialect: dialectTypescript, + }, + { + name: "an Astro fence that does not close runs to the end of the file", + ext: ".astro", + parts: []part{ + markup("---\n"), code("import pad from 'left-pad'\n\n"), + }, + dialect: dialectTypescript, + }, + { + name: "an Astro file with no fence reads its scripts", + ext: ".astro", + parts: []part{ + markup("

---

\n"), + }, + dialect: dialectTypescript, + }, + { + name: "CRLF line ends stay in the markup", + ext: ".vue", + parts: []part{ + markup("\r\n\r\n"), + }, + }, + { + name: "an Astro fence with CRLF line ends", + ext: ".astro", + parts: []part{ + markup("---\r\n"), code("import pad from 'left-pad'\r\n"), markup("---\r\n

\r\n"), + }, + dialect: dialectTypescript, + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + var data, want strings.Builder + for _, p := range tc.parts { + data.WriteString(p.text) + if p.code { + want.WriteString(p.text) + } else { + want.WriteString(blankExceptLineEnds(p.text)) + } + } + + source, dialect, _ := componentSource(tc.ext, []byte(data.String())) + assert.Equal(t, want.String(), string(source)) + assert.Equal(t, tc.dialect, dialect) + }) + } +} + +func TestComponentMarkup(t *testing.T) { + cases := []struct { + name string + ext string + data string + markup *core.Markup + }{ + { + name: "a template is markup", + ext: ".vue", + data: "\n \n\n", + markup: &core.Markup{Offset: 3}, + }, + { + name: "the tags of a script block are markup", + ext: ".vue", + data: "\n", + markup: &core.Markup{Offset: 0}, + }, + { + name: "a script with a src attribute loads its file", + ext: ".vue", + data: "\n\n\n" + cases := []struct { + file string + content string + code core.LanguageCode + markup *core.Markup + }{ + {file: "App.jsx", content: "const e =

{x}
;\n", code: core.LanguageCodeJavascript}, + {file: "App.vue", content: vue, code: core.LanguageCodeTypescript, markup: &core.Markup{Offset: 0}}, + } + for _, tc := range cases { + t.Run(tc.file, func(t *testing.T) { + p, err := NewParser([]core.Language{javascript(t)}) + require.NoError(t, err) + t.Cleanup(p.Close) + + tree, err := p.Parse(t.Context(), &trackedFile{name: tc.file, content: tc.content}) + require.NoError(t, err) + defer tree.Close() + assert.False(t, tree.Tree().RootNode().HasError()) + + treeLanguage, err := tree.Language() + require.NoError(t, err) + assert.Equal(t, tc.code, treeLanguage.Meta().Code) + + data, err := tree.Data() + require.NoError(t, err) + assert.Equal(t, tc.content, string(*data), "the tree keeps the bytes of the file") + assert.Equal(t, tc.markup, tree.(core.MarkupTree).Markup(), "the tree keeps the markup of the file") + }) + } +} diff --git a/plugin/callgraph/shapes.go b/plugin/callgraph/shapes.go index d2d87268..2a4fa2e9 100644 --- a/plugin/callgraph/shapes.go +++ b/plugin/callgraph/shapes.go @@ -8,7 +8,9 @@ import ( "unicode" "unicode/utf8" + "github.com/safedep/code/core" "github.com/safedep/code/fileindex" + "github.com/safedep/code/langcode" "github.com/safedep/code/pkg/ts" sitter "github.com/smacker/go-tree-sitter" ) @@ -189,6 +191,7 @@ const maxKeywordBytes = 16 var assignableNamePattern = regexp.MustCompile(`^[$@]*[A-Za-z_][A-Za-z0-9_]*$`) type shapedProcessors struct { + code core.LanguageCode shapes syntaxShapes separators []string } @@ -197,7 +200,7 @@ type shapedProcessors struct { // the reachable scopes. A shape replaces a processor of profile for the same // node type. func shapedProfile(profile languageProfile, shapes syntaxShapes) languageProfile { - p := &shapedProcessors{shapes: shapes, separators: profile.separators} + p := &shapedProcessors{code: profile.code, shapes: shapes, separators: profile.separators} processors := maps.Clone(profile.processors) if processors == nil { @@ -650,13 +653,21 @@ func cleanSegment(segment string) string { // call of its name makes in a language of class calls, as Parser(). func (p *shapedProcessors) instanceOf(callee string) (string, bool) { receiver, method, found := cutLast(callee, namespaceSeparator) - if found && slices.Contains(p.shapes.constructorMethods, method) { + if found && slices.Contains(p.shapes.constructorMethods, method) && !p.isPointer(lastOf(receiver)) { return receiver, true } first, _ := utf8.DecodeRuneInString(lastOf(callee)) return callee, p.shapes.classCalls && unicode.IsUpper(first) } +// isPointer reports whether a type is a smart pointer whose constructor +// method holds its argument, as Box in Box::new(Html {}). A method call on +// the value runs a method of the argument, so the value is no instance of +// the pointer. +func (p *shapedProcessors) isPointer(name string) bool { + return langcode.IsPointer(p.code, name) +} + // lastOf returns the last segment of a namespace. func lastOf(key string) string { _, last, found := cutLast(key, namespaceSeparator) diff --git a/plugin/indexer/cache.go b/plugin/indexer/cache.go index 569c6708..a48786cc 100644 --- a/plugin/indexer/cache.go +++ b/plugin/indexer/cache.go @@ -15,7 +15,8 @@ import ( // the run gives each plugin its stored artifact in place of a tree. The // run keys an artifact by the fileindex.Key of the file and the Name and // the Version of the plugin, so a new Version makes every older artifact -// miss. +// miss. The run stores no artifact that the plugin returns after ctx is +// done, because the end of ctx can have cut it. type CachedPlugin interface { core.Plugin @@ -61,7 +62,7 @@ func artifactKey(p pending, pl CachedPlugin) store.ArtifactKey { // supports reports whether a plugin runs on the language of a file. func supports(pl core.Plugin, p pending) bool { - return slices.Contains(pl.SupportedLanguages(), p.language.Meta().Code) + return slices.Contains(pl.SupportedLanguages(), p.source.Language.Meta().Code) } // needsParse reports whether a file with a stored index still needs its diff --git a/plugin/indexer/cache_test.go b/plugin/indexer/cache_test.go index b62004f3..444f767f 100644 --- a/plugin/indexer/cache_test.go +++ b/plugin/indexer/cache_test.go @@ -4,13 +4,18 @@ import ( "bytes" "context" "errors" + "fmt" + "maps" + "slices" "strconv" "strings" "testing" "github.com/safedep/code/core" "github.com/safedep/code/fileindex" + "github.com/safedep/code/fileindex/store" "github.com/safedep/code/fileindex/store/memstore" + "github.com/safedep/code/langcode" "github.com/safedep/code/parser" "github.com/safedep/code/plugin/callgraph" "github.com/stretchr/testify/assert" @@ -201,7 +206,7 @@ func TestAnalyzeKeepsTheCallGraphOnlyForAGraphPlugin(t *testing.T) { require.NoError(t, err) defer p.Close() - file := pending{file: files[0], language: byExtension[".js"], data: data} + file := pending{file: files[0], source: parser.Source(byExtension[".js"], files[0].Name(), data), data: data} for _, keep := range []bool{false, true} { o := analyze(ctx, p, file, keep) require.NoError(t, o.err) @@ -210,3 +215,163 @@ func TestAnalyzeKeepsTheCallGraphOnlyForAGraphPlugin(t *testing.T) { assert.Equal(t, keep, o.graph != nil, "a run with no GraphPlugin drops the graph after the index") } } + +// storeContents returns the index that the store holds for each file, and +// the artifact of the plugin for each file. The files are JavaScript. +func storeContents(t *testing.T, s store.Store, files map[string]string, pl CachedPlugin) (map[string]*fileindex.Index, map[string]string) { + t.Helper() + var keys []fileindex.Key + var artifactKeys []store.ArtifactKey + for name, content := range files { + key := fileindex.NewKey(name, fileindex.HashContent([]byte(content)), langcode.Javascript) + keys = append(keys, key) + artifactKeys = append(artifactKeys, store.ArtifactKey{Key: key, Plugin: pl.Name(), Version: pl.Version()}) + } + stored, err := s.Get(context.Background(), keys) + require.NoError(t, err) + indexes := map[string]*fileindex.Index{} + for key, ix := range stored { + indexes[key.Path] = ix + } + data, err := s.GetArtifacts(context.Background(), artifactKeys) + require.NoError(t, err) + artifacts := map[string]string{} + for key, d := range data { + artifacts[key.Path] = string(d) + } + return indexes, artifacts +} + +// finished returns the files that a run indexed, by name. +func finished(r *Result) map[string]*fileindex.Index { + out := map[string]*fileindex.Index{} + for _, ix := range r.Indexes { + if r.Files[ix.Path] == fileindex.FileIndexed { + out[ix.Path] = ix + } + } + return out +} + +// cuttingPlugin is a countingPlugin that ends the run when it analyzes the +// file at. After the end of ctx it returns a partial artifact with no +// error, as a plugin that stops at the deadline can. +type cuttingPlugin struct { + *countingPlugin + at string + cancel context.CancelFunc +} + +func (p *cuttingPlugin) Analyze(ctx context.Context, file core.File, tree core.ParseTree) ([]byte, error) { + if file.Name() == p.at { + p.cancel() + } + if ctx.Err() != nil { + return []byte("partial"), nil + } + return p.countingPlugin.Analyze(ctx, file, tree) +} + +func TestIndexerStoresTheFinishedFilesOfACutBatch(t *testing.T) { + files := map[string]string{} + var names []string + for i := range 10 { + name := fmt.Sprintf("f%d.js", i) + files[name] = fmt.Sprintf("function f%d() {}\n", i) + names = append(names, name) + } + dir := t.TempDir() + writeFiles(t, dir, files) + fsys := appFileSystem(t, dir) + langs := languages(t, core.LanguageCodeJavascript) + + for _, workers := range []int{1, 4} { + t.Run(fmt.Sprintf("workers=%d", workers), func(t *testing.T) { + s := memstore.New() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + pl := &cuttingPlugin{countingPlugin: newCountingPlugin("1"), at: "f4.js", cancel: cancel} + cut, err := runIndexer(t, ctx, fsys, Options{Languages: langs, Store: s, Plugins: []core.Plugin{pl}, Workers: workers}) + require.ErrorIs(t, err, context.Canceled) + + done := finished(cut) + var rest []string + for _, name := range names { + if _, ok := done[name]; !ok { + assert.Equal(t, fileindex.FileMissing, cut.Files[name], name) + rest = append(rest, name) + } + } + assert.Subset(t, slices.Collect(maps.Keys(done)), names[:5], "the files up to the cut finish") + + indexes, artifacts := storeContents(t, s, files, pl) + assert.Equal(t, done, indexes, "the store holds the index of each finished file and of no other file") + assert.Equal(t, map[string]string{"f0.js": "1", "f1.js": "1", "f2.js": "1", "f3.js": "1"}, artifacts, + "the store holds no artifact that the plugin made after the cut") + + next, err := runIndexer(t, context.Background(), fsys, Options{Languages: langs, Store: s, Workers: workers}) + require.NoError(t, err) + assert.Equal(t, rest, next.Report.Visited, "the next run parses only the files that the cut stopped") + for name := range done { + assert.Equal(t, fileindex.FileCached, next.Files[name], name) + } + }) + } +} + +// graphKeepingPlugin is a GraphPlugin that keeps the number of lines of a +// file, or "partial" for a partial call graph, as a plugin that does not +// check the graph can. +type graphKeepingPlugin struct { + *countingPlugin +} + +func (p *graphKeepingPlugin) AnalyzeGraph(_ context.Context, _ core.File, tree core.ParseTree, cg *callgraph.CallGraph) ([]byte, error) { + for _, t := range cg.Truncations() { + if t.Kind == callgraph.TruncationDeadline { + return []byte("partial"), nil + } + } + return lineCount(tree) +} + +func TestIndexerStoresNoPartialIndexOrGraphArtifact(t *testing.T) { + var big strings.Builder + for i := range 20000 { + _, err := fmt.Fprintf(&big, "f%d();\n", i) + require.NoError(t, err) + } + files := map[string]string{ + "a.js": "function a() {}\n", + "b.js": "function b() {}\n", + // The deadline cuts the build of the call graph of this file. + "c.js": big.String(), + "d.js": "function d() {}\n", + } + dir := t.TempDir() + writeFiles(t, dir, files) + fsys := appFileSystem(t, dir) + + for _, workers := range []int{1, 4} { + t.Run(fmt.Sprintf("workers=%d", workers), func(t *testing.T) { + s := memstore.New() + pl := &graphKeepingPlugin{countingPlugin: newCountingPlugin("1")} + ctx := &cutContext{Context: context.Background(), at: []string{callGraphBuild}} + result, err := runIndexer(t, ctx, fsys, Options{Languages: languages(t, core.LanguageCodeJavascript), Store: s, Plugins: []core.Plugin{pl}, Workers: workers}) + require.ErrorIs(t, err, context.Canceled) + assert.Equal(t, fileindex.FileMissing, result.Files["c.js"]) + + indexes, artifacts := storeContents(t, s, files, pl) + assert.Equal(t, finished(result), indexes, "the store holds the index of each finished file and of no other file") + assert.NotContains(t, indexes, "c.js") + for name, data := range artifacts { + assert.Equal(t, "1", data, name) + assert.Contains(t, indexes, name, "an artifact comes with the index of its file") + } + if workers == 1 { + assert.Contains(t, indexes, "a.js", "one worker finishes the files before the cut") + assert.Contains(t, indexes, "b.js", "one worker finishes the files before the cut") + } + }) + } +} diff --git a/plugin/indexer/calls.go b/plugin/indexer/calls.go index 828598e0..87eeb05c 100644 --- a/plugin/indexer/calls.go +++ b/plugin/indexer/calls.go @@ -1,6 +1,7 @@ package indexer import ( + "context" "regexp" "slices" "strings" @@ -13,24 +14,68 @@ import ( // retargetTyped gives a call on a receiver that a declared type of the // scope names the key of the name in its scope as its target, in a language -// with typedReceivers. The call graph bound the receiver to the value of an -// assignment, as dep//new_thing for t = dep.new_thing(), and a call on that -// value, as dep//new_thing//do, names no method. The declared type or the -// result type of the name does. A call that the call graph bound to a -// method of a class of the file, as Parser//render for p = Parser(), keeps -// that target. -func (p *projection) retargetTyped(call *fileindex.Call) { +// with typedReceivers, or with declaredAt at an untyped site. The call +// graph bound the receiver to the value of an assignment, as dep//new_thing +// for t = dep.new_thing(), and a call on that value, as dep//new_thing//do, +// names no method. The declared type or the result type of the name does. +// A call that the call graph bound to a method of a class of the file, as +// Parser//render for p = Parser(), keeps that target. +func (p *projection) retargetTyped(ctx context.Context, call *fileindex.Call, untyped bool) { for _, t := range call.Targets { if def, ok := p.defs[fileindex.Parent(t)]; ok && def.Kind == fileindex.DefClass { return } } - if target, ok := p.typedTarget(call.Caller, call.Receiver, call.Method); ok { + if target, ok := p.typedTarget(ctx, *call, untyped); ok { call.Targets = []string{target} } } -func (p *projection) addCalls() { +// bindsAsWritten reports whether the call graph binds the names of a call +// as the code does, in a language with declaredAt. The call graph reads +// only some binders of a name, so it can bind a receiver to a parameter or +// to the value of a let while a pattern binds the name again, as t in +// fn run(t: Local) { if let Some(t) = dep::try_thing() { t.inspect() } }. +// The target then names a method of the wrong type, and a method of the +// real type reads as not-found. So the receiver must have no binder that +// the call graph does not read, and a target through a declared type of a +// name must have its declared type at the call. +func (p *projection) bindsAsWritten(ctx context.Context, call fileindex.Call) bool { + rules := p.rules() + if rules.declaredAt == nil { + return true + } + if name, _, _ := strings.Cut(call.Receiver, "."); isIdentifier(name) && rules.rebound(ctx, p, call, name) { + return false + } + for _, target := range call.Targets { + if name, ok := p.typedName(call.Caller, target); ok && !rules.declaredAt(ctx, p, call, name) { + return false + } + } + return true +} + +// typedName returns the name of a parameter or a typed local of the scope +// chain of the caller whose key starts a target, as t for run//t//inspect. +func (p *projection) typedName(caller, target string) (string, bool) { + for scope := caller; fileindex.HasKeyPrefix(scope, p.cg.FileName); scope = fileindex.Parent(scope) { + if rest, ok := strings.CutPrefix(target, scope+fileindex.Separator); ok { + name, _, _ := strings.Cut(rest, fileindex.Separator) + if typed, ok := p.typedScope(caller, name); ok && typed == scope { + return name, true + } + } + if scope == p.cg.FileName { + break + } + } + return "", false +} + +func (p *projection) addCalls(ctx context.Context) { + untyped := p.untypedSites() + markTries := p.rules().markTries for key, node := range p.cg.Nodes { for _, ref := range node.CallsTo { kind, ok := callKind(ref.Kind) @@ -63,12 +108,35 @@ func (p *projection) addCalls() { Kind: kind, Args: p.arguments(key, ref.Site), } - p.retargetTyped(&call) + p.retargetTyped(ctx, &call, untyped[[2]uint32{ref.Site.StartLine, ref.Site.StartColumn}]) + if !p.bindsAsWritten(ctx, call) { + p.addRecord(call, fileindex.UnresolvedUnknownReceiver) + continue + } + if markTries != nil { + markTries(ctx, p, &call, ref.Site) + } p.ix.Calls = append(p.ix.Calls, call) } } } +// untypedSites returns the line and the column of each call on a receiver +// whose value the call graph does not type, as the value of a call, in a +// language with declaredAt. +func (p *projection) untypedSites() map[[2]uint32]bool { + if p.rules().declaredAt == nil { + return nil + } + sites := map[[2]uint32]bool{} + for _, u := range p.cg.Unresolved() { + if u.Reason == callgraph.UnresolvedUnknownReceiver { + sites[[2]uint32{u.Site.StartLine, u.Site.StartColumn}] = true + } + } + return sites +} + // objectMember returns the definition that a JavaScript call of a member of // an object of the file names, as api//run for api.run() after // const api = { run() {} }. The call graph keeps no value of an object @@ -226,56 +294,68 @@ func (p *projection) rebind(caller, name string) []string { } } -func (p *projection) addUnresolved() { +func (p *projection) addUnresolved(ctx context.Context) { for _, u := range p.cg.Unresolved() { if p.members[[2]uint32{u.Site.StartLine, u.Site.StartColumn}] && u.Reason == callgraph.UnresolvedUnknownReceiver { continue } - caller := p.caller(u.Caller, u.Site) - if target, ok := p.typedTarget(caller, u.Receiver, u.Method); ok && u.Reason == callgraph.UnresolvedUnknownReceiver { - p.ix.Calls = append(p.ix.Calls, fileindex.Call{ - Caller: caller, - Line: int(u.Site.StartLine), - Column: int(u.Site.StartColumn), - Callee: u.Text, - Targets: []string{target}, - Receiver: u.Receiver, - Method: u.Method, - Kind: fileindex.CallCall, - }) - continue - } - p.ix.Unresolved = append(p.ix.Unresolved, fileindex.Unresolved{ - Caller: caller, + call := fileindex.Call{ + Caller: p.caller(u.Caller, u.Site), Line: int(u.Site.StartLine), Column: int(u.Site.StartColumn), - Text: u.Text, + Callee: u.Text, Receiver: u.Receiver, Method: u.Method, - Reason: fileindex.UnresolvedReason(u.Reason), - }) + Kind: fileindex.CallCall, + } + untyped := u.Reason == callgraph.UnresolvedUnknownReceiver + if target, ok := p.typedTarget(ctx, call, untyped); ok && untyped { + call.Targets = []string{target} + p.ix.Calls = append(p.ix.Calls, call) + continue + } + p.addRecord(call, fileindex.UnresolvedReason(u.Reason)) } } +// addRecord records a call site whose target the index does not know. +func (p *projection) addRecord(call fileindex.Call, reason fileindex.UnresolvedReason) { + p.ix.Unresolved = append(p.ix.Unresolved, fileindex.Unresolved{ + Caller: call.Caller, + Line: call.Line, + Column: call.Column, + Text: call.Callee, + Receiver: call.Receiver, + Method: call.Method, + Reason: reason, + }) +} + // typedTarget returns the target of a call on a receiver whose first name a // declared type of the scope chain of the caller types, as main.py//run//t//do -// for t.do() in run(t: Thing), in a language with typedReceivers. The link -// step reads the type of the name from the scope, as it does for Go. -func (p *projection) typedTarget(caller, receiver, method string) (string, bool) { - if !p.rules().typedReceivers || receiver == "" || method == "" { +// for t.do() in run(t: Thing), in a language with typedReceivers, or with +// declaredAt at an untyped site. The link step reads the type of the name +// from the scope, as it does for Go. +func (p *projection) typedTarget(ctx context.Context, call fileindex.Call, untyped bool) (string, bool) { + if call.Receiver == "" || call.Method == "" { return "", false } - names := strings.Split(receiver, ".") + names := strings.Split(call.Receiver, ".") for _, name := range names { if !isIdentifier(name) { return "", false } } - scope, ok := p.typedScope(caller, names[0]) + rules := p.rules() + typed := rules.typedReceivers || untyped && rules.declaredAt != nil && rules.declaredAt(ctx, p, call, names[0]) + if !typed { + return "", false + } + scope, ok := p.typedScope(call.Caller, names[0]) if !ok { return "", false } - return fileindex.Join(scope, append(names, method)...), true + return fileindex.Join(scope, append(names, call.Method)...), true } func (p *projection) addTruncations() { diff --git a/plugin/indexer/ecmascript.go b/plugin/indexer/ecmascript.go index e044e8e8..110afd8e 100644 --- a/plugin/indexer/ecmascript.go +++ b/plugin/indexer/ecmascript.go @@ -61,7 +61,12 @@ func ecmascriptRules(typescript bool) languageRules { "binary_expression": "", "parenthesized_expression": "", "export_statement": "value", + "jsx_expression": "", + "jsx_opening_element": "name", + "jsx_self_closing_element": "name", }, + spreads: map[string]string{"jsx_expression": "spread_element"}, + intrinsic: jsxIntrinsic, member: "member_expression", object: "object", property: "property", @@ -86,6 +91,22 @@ func ecmascriptRules(typescript bool) languageRules { return r } +// jsxIntrinsic reports whether a JSX element names a tag of the host, as +// div in
. JSX gives such a name as a string, so the element renders +// no component. An element of a capitalized name or of a member, as +// or , renders the component that the name holds. +func jsxIntrinsic(p *projection, holder, value *sitter.Node) bool { + switch holder.Type() { + case "jsx_opening_element", "jsx_self_closing_element": + if value == nil || value.Type() != "identifier" { + return false + } + name := p.text(value) + return name != "" && 'a' <= name[0] && name[0] <= 'z' + } + return false +} + // ecmascriptBases returns the bases of a class from its class heritage, as // Base in class A extends Base, and the interfaces that a TypeScript // interface extends. An abstract class of TypeScript is its own node type diff --git a/plugin/indexer/golang.go b/plugin/indexer/golang.go index 01b7d88d..699dc5b2 100644 --- a/plugin/indexer/golang.go +++ b/plugin/indexer/golang.go @@ -1,6 +1,7 @@ package indexer import ( + "slices" "unicode" "unicode/utf8" @@ -21,7 +22,7 @@ var goRules = languageRules{ abstract: goMethodElems, signature: goSignature, exported: goExported, - definitions: goTypes, + definitions: goDefinitions, visitors: []func(p *projection) visitor{referenceVisitor}, references: &referenceSyntax{ holders: map[string]string{ @@ -31,12 +32,18 @@ var goRules = languageRules{ "var_spec": "value", "return_statement": "", "literal_value": "", + "send_statement": "value", + // A conversion, as (func())(f), or a call of a generic + // function with type arguments, as id[func()](f), which the + // grammar parses as a conversion. + "type_conversion_expression": "operand", }, member: "selector_expression", object: "operand", property: "field", identifiers: map[string]bool{"identifier": true}, lists: map[string]bool{"expression_list": true, "literal_element": true, "keyed_element": true}, + value: goReference, }, arguments: &argumentSyntax{ calls: map[string]string{"call_expression": "function"}, @@ -61,38 +68,206 @@ func goPackage(p *projection) string { return "" } -// goTypes adds each top-level named type of a Go file. An interface is an -// interface. Any other named type is a class, because it can have methods. -// The call graph has no node for a type, so the tree gives the types. -func goTypes(p *projection) { +// goDefinitions adds the top-level named types of a Go file with the fields +// of each struct, the constants and the package variables. The call graph +// has no node for them, so the tree gives them. +func goDefinitions(p *projection) { + var specs []*sitter.Node for _, statement := range ts.NamedChildren(p.tree.Tree().RootNode()) { - if statement.Type() != "type_declaration" { + switch statement.Type() { + case "type_declaration": + p.goTypes(statement) + case "const_declaration": + for _, spec := range ts.NamedChildren(statement) { + if spec.Type() == "const_spec" { + for _, name := range goNames(spec) { + p.goValue(name, spec, fileindex.DefConstant) + } + } + } + case "var_declaration": + specs = append(specs, goVarSpecs(statement)...) + } + } + vars := make([][]*fileindex.Definition, len(specs)) + for i, spec := range specs { + for _, name := range goNames(spec) { + vars[i] = append(vars[i], p.goValue(name, spec, fileindex.DefVariable)) + } + } + // A value can name a variable that the file declares after it. + for i, spec := range specs { + p.goVariableValues(spec, vars[i]) + } +} + +// goTypes adds each named type of a type declaration. An interface is an +// interface. Any other named type is a class, because it can have methods. +// A named field of a struct is a field of its class. +func (p *projection) goTypes(statement *sitter.Node) { + for _, spec := range ts.NamedChildren(statement) { + if spec.Type() != "type_spec" && spec.Type() != "type_alias" { continue } - for _, spec := range ts.NamedChildren(statement) { - if spec.Type() != "type_spec" && spec.Type() != "type_alias" { - continue - } - key := p.local(p.text(spec.ChildByFieldName("name"))) - if _, defined := p.defs[key]; defined { - continue - } - kind := fileindex.DefClass - if t := spec.ChildByFieldName("type"); t != nil && t.Type() == "interface_type" { - kind = fileindex.DefInterface + key := p.local(p.text(spec.ChildByFieldName("name"))) + if _, defined := p.defs[key]; defined { + continue + } + kind := fileindex.DefClass + t := spec.ChildByFieldName("type") + if t != nil && t.Type() == "interface_type" { + kind = fileindex.DefInterface + } + p.addDefinition(key, kind, spec) + p.addSyntax(key, spec) + if t == nil || t.Type() != "struct_type" || spec.Type() != "type_spec" { + continue + } + for _, field := range p.goStructFields(t) { + for _, name := range childrenByField(field, "name") { + p.addDefinition(fileindex.Join(key, p.text(name)), fileindex.DefField, field) } - p.defs[key] = &fileindex.Definition{ - Key: key, - Kind: kind, - Name: fileindex.Last(key), - StartLine: lineOf(spec), - EndLine: int(spec.EndPoint().Row) + 1, + } + } +} + +// goVarSpecs returns the specs of a var declaration. A declaration with +// parentheses holds them in a list. +func goVarSpecs(statement *sitter.Node) []*sitter.Node { + var specs []*sitter.Node + for _, child := range ts.NamedChildren(statement) { + switch child.Type() { + case "var_spec": + specs = append(specs, child) + case "var_spec_list": + specs = append(specs, goVarSpecs(child)...) + } + } + return specs +} + +// goNames returns the names that a const or a var spec declares. The commas +// between them have the name field too. +func goNames(spec *sitter.Node) []*sitter.Node { + return slices.DeleteFunc(childrenByField(spec, "name"), func(n *sitter.Node) bool { return n.Type() != "identifier" }) +} + +// goValue adds a constant or a package variable of a spec, and returns it. +// The blank name _ declares nothing, and a name that the file defines +// already keeps its definition. Both give nil. +func (p *projection) goValue(name, spec *sitter.Node, kind fileindex.DefKind) *fileindex.Definition { + key := p.local(p.text(name)) + if _, defined := p.defs[key]; defined || p.text(name) == "_" { + return nil + } + return p.addDefinition(key, kind, spec) +} + +// goVariableValues fills each package variable of a spec: the per-file +// target of a value that names a function or another value, as run for +// var Run = run, or the Data mark when the source shows that the value is +// no function. A variable with neither can hold a function that the index +// does not know, as the value of a function literal or of a call, so a +// call of it keeps its record. defs holds the definition that goValue +// added for each name of the spec, or nil. +func (p *projection) goVariableValues(spec *sitter.Node, defs []*fileindex.Definition) { + var values []*sitter.Node + if list := spec.ChildByFieldName("value"); list != nil { + values = ts.NamedChildren(list) + } + typed := spec.ChildByFieldName("type") + for i, def := range defs { + if def == nil { + continue + } + var value *sitter.Node + if len(values) == len(defs) { + value = values[i] + } + switch { + case typed != nil && p.goDataType(typed): + def.Data = true + case value == nil: + case p.goDataValue(value): + def.Data = true + default: + if target, ok := p.goValueTarget(value); ok { + def.Values = []string{target} } - p.addSyntax(key, spec) } } } +// goDataType reports whether a value of the type is no function: a +// predeclared type, as int or error, or a type literal that is not a +// function type, as *T or []T. A named type can be a function type, as +// http.HandlerFunc. +func (p *projection) goDataType(t *sitter.Node) bool { + switch t.Type() { + case "type_identifier": + return p.goPredeclared(p.text(t)) + case "parenthesized_type": + return t.NamedChildCount() == 1 && p.goDataType(t.NamedChild(0)) + case "pointer_type", "slice_type", "array_type", "map_type", "channel_type", "struct_type", "interface_type": + return true + } + return false +} + +// goErrorConstructors are the functions of the standard library that give +// an error value, by import path and name. +var goErrorConstructors = map[[2]string]bool{{"errors", "New"}: true, {"fmt", "Errorf"}: true} + +// goDataValue reports whether the source shows that a value is no +// function: a literal, a composite literal, an arithmetic expression, a +// conversion to a type of data, a call of a builtin, or an error that +// errors.New or fmt.Errorf gives. +func (p *projection) goDataValue(value *sitter.Node) bool { + switch value.Type() { + case "interpreted_string_literal", "raw_string_literal", "int_literal", "float_literal", "imaginary_literal", "rune_literal", + "true", "false", "iota", "composite_literal", "binary_expression": + return true + case "parenthesized_expression": + return value.NamedChildCount() == 1 && p.goDataValue(value.NamedChild(0)) + case "unary_expression": + // A dereference of a pointer to a function or a receive from a + // channel of functions gives a function. + op := p.text(value.ChildByFieldName("operator")) + return op != "*" && op != "<-" + case "call_expression": + function := value.ChildByFieldName("function") + if function == nil { + return false + } + switch function.Type() { + case "identifier": + return p.goPredeclared(p.text(function)) + case "selector_expression": + operand, field := function.ChildByFieldName("operand"), function.ChildByFieldName("field") + return operand != nil && field != nil && goErrorConstructors[[2]string{p.goImportPath(p.text(operand)), p.text(field)}] + } + return p.goDataType(function) + } + return false +} + +// goImportPath returns the import path of the import that binds the alias. +func (p *projection) goImportPath(alias string) string { + for _, imp := range p.ix.Imports { + if imp.Alias == alias { + return imp.Module + } + } + return "" +} + +// goValueTarget returns the per-file target of a value that names a +// function or another value, in each form that a reference reads. +func (p *projection) goValueTarget(value *sitter.Node) (string, bool) { + target, _ := goReference(p, p.cg.FileName, value) + return target, target != "" +} + // goBases returns the types that a Go type embeds, and the type that an // alias names. A method of such a type is a method of the type too. func goBases(p *projection, spec *sitter.Node) []string { diff --git a/plugin/indexer/golang_test.go b/plugin/indexer/golang_test.go index 0fcab12a..11851842 100644 --- a/plugin/indexer/golang_test.go +++ b/plugin/indexer/golang_test.go @@ -95,3 +95,71 @@ func TestFileIndexGo(t *testing.T) { } }) } + +func TestFileIndexGoValues(t *testing.T) { + source := strings.Join([]string{ + "package shop", + "import (", + " \"errors\"", + " \"example.com/shop/store\"", + ")", + "type Node struct { Data, kind string; *Base }", + "const ( Max, Min = 1, iota; Next )", + "var ErrNoRows = errors.New(\"no rows\")", + "var n, m int", + "var Default = &Node{}", + "var Handler = func() {}", + "var Hook = store.Hook()", + "var Run = run", + "var Open = store.Open", + "var Fn func()", + "var Conv = store.HandlerFunc(run)", + "var _ = run", + "func run() { use(ErrNoRows, Max, Run) }", + }, "\n") + "\n" + ix := indexSource(t, core.LanguageCodeGo, "shop.go", source) + + tests := []struct { + key string + kind fileindex.DefKind + data bool + values []string + }{ + {key: "shop.go//Node//Data", kind: fileindex.DefField}, + {key: "shop.go//Node//kind", kind: fileindex.DefField}, + {key: "shop.go//Max", kind: fileindex.DefConstant}, + {key: "shop.go//Min", kind: fileindex.DefConstant}, + {key: "shop.go//Next", kind: fileindex.DefConstant}, + {key: "shop.go//ErrNoRows", kind: fileindex.DefVariable, data: true}, + {key: "shop.go//n", kind: fileindex.DefVariable, data: true}, + {key: "shop.go//m", kind: fileindex.DefVariable, data: true}, + {key: "shop.go//Default", kind: fileindex.DefVariable, data: true}, + {key: "shop.go//Handler", kind: fileindex.DefVariable}, + {key: "shop.go//Hook", kind: fileindex.DefVariable}, + {key: "shop.go//Run", kind: fileindex.DefVariable, values: []string{"shop.go//run"}}, + {key: "shop.go//Open", kind: fileindex.DefVariable, values: []string{"example.com//shop//store//Open"}}, + {key: "shop.go//Fn", kind: fileindex.DefVariable}, + {key: "shop.go//Conv", kind: fileindex.DefVariable}, + } + for _, tt := range tests { + t.Run(tt.key, func(t *testing.T) { + def := definitionOf(t, ix, tt.key) + assert.Equal(t, tt.kind, def.Kind) + assert.Equal(t, tt.data, def.Data) + assert.Equal(t, tt.values, def.Values) + }) + } + t.Run("a field is no method of its class and the embedded type is a base", func(t *testing.T) { + node := classOf(t, ix, "shop.go//Node") + assert.Empty(t, node.Methods) + assert.Equal(t, []string{"Base"}, node.Bases) + assert.Equal(t, "shop.go//Node", definitionOf(t, ix, "shop.go//Node//Data").Parent) + }) + t.Run("a read of data is no reference and a read of a function value is one", func(t *testing.T) { + var texts []string + for _, ref := range ix.References { + texts = append(texts, ref.Text) + } + assert.Equal(t, []string{"run", "store.Open", "run", "run", "Run"}, texts) + }) +} diff --git a/plugin/indexer/gounit.go b/plugin/indexer/gounit.go index 562b80a6..554a1445 100644 --- a/plugin/indexer/gounit.go +++ b/plugin/indexer/gounit.go @@ -114,7 +114,10 @@ func isGeneratedTable(name string, data []byte) bool { // data values of a generated table file are what hit the limits of the // index, as a string of thousands of concatenated parts. Function bodies and // any value with a call or a function literal stay, because a generated -// file can hold code that a program runs, as a .pb.go file. +// file can hold code that a program runs, as a .pb.go file. A var value +// that names a function stays too, as a map of handlers. A call of an +// element of the map is a call of a function value, and the search finds +// the function only through the reference that the value records. func goSkeleton(tree *sitter.Tree, data []byte) []byte { out := bytes.Clone(data) clear := func(from, to uint32) { @@ -132,7 +135,7 @@ func goSkeleton(tree *sitter.Tree, data []byte) []byte { case "var_spec", "const_spec": if values := node.ChildByFieldName("value"); values != nil { for _, v := range ts.NamedChildren(values) { - if !holdsCode(v) { + if !holdsCode(v) && (node.Type() == "const_spec" || !namesValue(v)) { clear(v.StartByte(), v.EndByte()) out[v.StartByte()] = '0' } @@ -157,6 +160,25 @@ func holdsCode(node *sitter.Node) bool { return slices.ContainsFunc(ts.NamedChildren(node), holdsCode) } +// namesValue reports whether an expression holds a name that can give a +// function, as remove in map[string]func(){"rm": remove}. The key of a +// keyed element and an operand of arithmetic give no function. +func namesValue(node *sitter.Node) bool { + switch node.Type() { + case "identifier", "selector_expression": + return true + case "binary_expression": + return false + case "unary_expression": + if op := goOperator(node); op != "&" && op != "*" && op != "<-" { + return false + } + case "keyed_element": + return node.NamedChildCount() > 0 && namesValue(node.NamedChild(int(node.NamedChildCount())-1)) + } + return slices.ContainsFunc(ts.NamedChildren(node), namesValue) +} + // skeletonIndex indexes the skeleton of a generated table file of a // dependency unit whose index hit a limit. The index keeps the hash of the // bytes of the file, so the store serves it on the next run. @@ -173,6 +195,9 @@ func skeletonIndex(ctx context.Context, p contentParser, file pending, tree core if err != nil { return nil, err } + if err := complete(ctx, out); err != nil { + return nil, err + } out.ContentHash = file.key.ContentHash return out, nil } diff --git a/plugin/indexer/gounit_test.go b/plugin/indexer/gounit_test.go index 24551c7e..378fd076 100644 --- a/plugin/indexer/gounit_test.go +++ b/plugin/indexer/gounit_test.go @@ -2,6 +2,7 @@ package indexer import ( "context" + "os" "path/filepath" "regexp" "strings" @@ -11,6 +12,7 @@ import ( "github.com/safedep/code/fileindex" "github.com/safedep/code/fileindex/store/memstore" "github.com/safedep/code/fs" + "github.com/safedep/code/langcode" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -98,6 +100,39 @@ func TestGoSkeletonKeepsTheHashOfTheFile(t *testing.T) { assert.Equal(t, fileindex.FileCached, result.Dependencies[goKitUnit+"//tables.go"], "the store serves the skeleton index on the next run") } +func TestIndexerStoresNoPartialSkeletonIndex(t *testing.T) { + dir := t.TempDir() + kit := filepath.Join(dir, "kit") + writeFiles(t, kit, goKitFiles()) + fsys, err := fs.NewLocalFileSystem(fs.LocalFileSystemConfig{AppDirectories: []string{dir}, ImportDirectories: []string{kit}, ImportUnits: map[string]string{kit: goKitUnit}, ExcludePatterns: []*regexp.Regexp{fs.SkipPattern(kit)}}) + require.NoError(t, err) + s := memstore.New() + // The deadline cuts the walk of the skeleton of tables.go, after the + // skeleton parsed. + ctx := &cutContext{Context: context.Background(), at: []string{"indexer.skeletonIndex", "indexer.(*projection).walkTree"}} + result, err := runIndexer(t, ctx, fsys, Options{Languages: languages(t, core.LanguageCodeGo), Store: s, Imports: true, Workers: 1}) + require.ErrorIs(t, err, context.Canceled) + + tables := goKitUnit + "//tables.go" + assert.Equal(t, fileindex.FileMissing, result.Dependencies[tables]) + var stored []string + for _, ix := range result.Indexes { + assert.NotEqual(t, tables, ix.Path) + got, err := s.Get(context.Background(), []fileindex.Key{ix.Key()}) + require.NoError(t, err) + if _, ok := got[ix.Key()]; ok { + stored = append(stored, ix.Path) + } + } + assert.ElementsMatch(t, []string{goKitUnit + "//kit.go", goKitUnit + "//kit_linux.go", goKitUnit + "//kit_windows.go"}, stored, "the files before the cut reach the store") + + data, err := os.ReadFile(filepath.Join(kit, "tables.go")) + require.NoError(t, err) + got, err := s.Get(context.Background(), []fileindex.Key{fileindex.NewKey(tables, fileindex.HashContent(data), langcode.Go)}) + require.NoError(t, err) + assert.Empty(t, got, "the partial skeleton index does not reach the store") +} + func TestIsGeneratedTable(t *testing.T) { for _, tt := range []struct { name string diff --git a/plugin/indexer/govalues.go b/plugin/indexer/govalues.go new file mode 100644 index 00000000..1febb324 --- /dev/null +++ b/plugin/indexer/govalues.go @@ -0,0 +1,300 @@ +package indexer + +import ( + "slices" + + "github.com/safedep/code/fileindex" + "github.com/safedep/code/langcode" + "github.com/safedep/code/pkg/ts" + sitter "github.com/smacker/go-tree-sitter" +) + +// goReference returns the per-file target of a Go value that can give a +// function, or the member node of a method value whose receiver the index +// cannot name. A call of a Go function value concerns no target. That is +// sound only when the index records each function that a value can give, +// in each form that docs/projectgraph.md lists. +func goReference(p *projection, caller string, value *sitter.Node) (string, *sitter.Node) { + value = goValueName(value) + if value == nil { + return "", nil + } + switch value.Type() { + case "identifier": + if p.goTypedLocal(caller, value) { + return "", nil + } + target, _ := p.goName(caller, value) + return target, nil + case "selector_expression": + if target, ok := p.reference(*p.rules().references, caller, value); ok { + return target, nil + } + if base, ok := p.goOperand(caller, value.ChildByFieldName("operand")); ok { + return fileindex.Join(base, p.text(value.ChildByFieldName("field"))), nil + } + return "", value + case "type_instantiation_expression": + // The grammar parses F[A, B] with more than one type argument + // as a type. + target, _ := p.goTypeTarget(caller, value.NamedChild(0)) + return target, nil + } + return "", nil +} + +// goValueName returns the expression that names the function of a value: +// the value in parentheses, the operand of &, which a dereference gives +// back, and the function of a generic instantiation, as F for F[int]. An +// index expression can be an element of a slice or a map, and then its +// operand names a variable of the container. +func goValueName(node *sitter.Node) *sitter.Node { + for depth := 0; node != nil && depth < maxReferenceSegments; depth++ { + switch node.Type() { + case "parenthesized_expression": + node = node.NamedChild(0) + case "index_expression": + node = node.ChildByFieldName("operand") + case "unary_expression": + if goOperator(node) != "&" { + return node + } + node = node.ChildByFieldName("operand") + default: + return node + } + } + return node +} + +// goOperand returns the per-file target of the receiver of a method value, +// as the key that a call on the receiver walks from: the type of a +// composite literal, a conversion, a type assertion or new, the function +// whose result a call gives, or a name or a member chain. A receiver that +// the index cannot type, as an element of a slice, gives none. +func (p *projection) goOperand(caller string, node *sitter.Node) (string, bool) { + if node == nil { + return "", false + } + switch node.Type() { + case "parenthesized_expression": + return p.goOperand(caller, node.NamedChild(0)) + case "unary_expression": + if op := goOperator(node); op != "*" && op != "&" { + return "", false + } + return p.goOperand(caller, node.ChildByFieldName("operand")) + case "identifier": + return p.goName(caller, node) + case "selector_expression": + base, ok := p.goOperand(caller, node.ChildByFieldName("operand")) + return fileindex.Join(base, p.text(node.ChildByFieldName("field"))), ok + case "composite_literal", "type_assertion_expression", "type_conversion_expression": + return p.goTypeTarget(caller, node.ChildByFieldName("type")) + case "call_expression": + return p.goResultOperand(caller, node) + } + return "", false +} + +// goResultOperand returns the per-file target of the result of a call: +// the type that new or make gives, or the function that the call names. A +// call of a local function value has a result of no known type. +func (p *projection) goResultOperand(caller string, call *sitter.Node) (string, bool) { + function := call.ChildByFieldName("function") + for function != nil && function.Type() == "parenthesized_expression" { + function = function.NamedChild(0) + } + if function == nil || function.Type() != "identifier" { + return p.goOperand(caller, function) + } + name := p.text(function) + if _, typed := p.typedScope(caller, name); typed { + return "", false + } + if p.goPredeclared(name) { + args := call.ChildByFieldName("arguments") + if (name == "new" || name == "make") && args != nil && args.NamedChildCount() > 0 { + return p.goTypeTarget(caller, args.NamedChild(0)) + } + return "", false + } + return p.goName(caller, function) +} + +// goTypeTarget returns the per-file target of the named type that a type +// node names, as dep//Thing for *dep.Thing or Box for Box[int]. +func (p *projection) goTypeTarget(caller string, node *sitter.Node) (string, bool) { + for depth := 0; node != nil && depth < maxReferenceSegments; depth++ { + switch node.Type() { + case "pointer_type", "parenthesized_type": + node = node.NamedChild(0) + case "generic_type": + node = node.ChildByFieldName("type") + case "type_identifier", "identifier": + return p.goName(caller, node) + case "qualified_type": + pkg, ok := p.goName(caller, node.ChildByFieldName("package")) + return fileindex.Join(pkg, p.text(node.ChildByFieldName("name"))), ok + default: + return "", false + } + } + return "", false +} + +// goName returns the per-file target of a name: the definition that the +// scope chain binds, a typed local, or an import. A name that no local of +// the caller declares in scope keeps its text, as a function of another +// file of the package, and the link step resolves it or drops it. A typed +// local of a closed block does not bind the name. A predeclared name keeps +// its text too, because a function of another file of the package can +// shadow it, as func clear(). A local that the index does not type gives +// none. +func (p *projection) goName(caller string, node *sitter.Node) (string, bool) { + if node == nil { + return "", false + } + name := p.text(node) + inScope := p.goLocal(caller, name, node.StartByte()) + if _, typed := p.typedScope(caller, name); typed && !inScope { + return name, true + } + if target, ok := p.valueOf(*p.rules().references, caller, name); ok { + return target, true + } + if name == "_" || inScope { + return "", false + } + return name, true +} + +// goTypedLocal reports whether a name is a parameter or a local of a +// declared type that is in scope at the node. Such a name holds a value of +// the type, and a value is no function. The index records the typed locals +// of a function with no extent. So a local of a closed block can hide a +// function of the package with the same name. A call of the value is a +// function-value record that concerns no target, and the reference to the +// function is then the only edge to it. +func (p *projection) goTypedLocal(caller string, node *sitter.Node) bool { + name := p.text(node) + _, typed := p.typedScope(caller, name) + return typed && p.goLocal(caller, name, node.StartByte()) +} + +// goPredeclared reports whether a name is a predeclared name of Go, as int +// or len, that no definition of the file shadows. A declaration of another +// file of the package can still shadow it. So a reference keeps the name, +// and only two rules read this. The Data mark of a package variable hides +// no function, because its value is a reference of its file, and a search +// reaches the file with the variable. new and make take a type argument +// only as the builtins. +func (p *projection) goPredeclared(name string) bool { + return langcode.IsBuiltin(langcode.Go, name) && p.defs[p.local(name)] == nil +} + +// goScope holds the bytes of the source where a local name of a Go +// function is in scope. +type goScope struct { + from, to uint32 +} + +// goBlocks are the nodes whose extent is the scope of a name that a +// declaration in them makes. +var goBlocks = map[string]bool{ + "block": true, "if_statement": true, "for_statement": true, "expression_switch_statement": true, + "type_switch_statement": true, "select_statement": true, "expression_case": true, "type_case": true, + "default_case": true, "communication_case": true, "func_literal": true, + "function_declaration": true, "method_declaration": true, +} + +// goTypeLiterals are the type literals whose names are no locals. +var goTypeLiterals = map[string]bool{"function_type": true, "interface_type": true, "struct_type": true} + +// goLocal reports whether a local name of the caller is in scope at the +// byte: a parameter, a result, a variable, a constant or a type that the +// function or a function literal in it declares. +func (p *projection) goLocal(caller, name string, at uint32) bool { + if p.goScoped == nil { + p.goScoped = map[string]map[string][]goScope{} + } + locals, ok := p.goScoped[caller] + if !ok { + locals = p.goScopes(p.syntax[caller]) + p.goScoped[caller] = locals + } + return slices.ContainsFunc(locals[name], func(s goScope) bool { return s.from <= at && at < s.to }) +} + +// goScopes returns the scopes of each name that a function declares. The +// scope of a variable starts after its declaration, so x in x := x names +// the outer x. +func (p *projection) goScopes(function *sitter.Node) map[string][]goScope { + locals := map[string][]goScope{} + if function == nil || function.Type() != "function_declaration" && function.Type() != "method_declaration" { + return locals + } + add := func(nodes []*sitter.Node, from uint32, block *sitter.Node) { + for _, n := range nodes { + if n.Type() == "identifier" { + locals[p.text(n)] = append(locals[p.text(n)], goScope{from: from, to: block.EndByte()}) + } + } + } + var walk func(node, block *sitter.Node, depth int) + walk = func(node, block *sitter.Node, depth int) { + if depth > maxLocalDepth { + return + } + switch { + case goTypeLiterals[node.Type()]: + // The parameters of a function type and the fields of a + // struct type declare no local. + return + case goBlocks[node.Type()]: + block = node + } + switch node.Type() { + case "parameter_declaration", "variadic_parameter_declaration": + add(childrenByField(node, "name"), block.StartByte(), block) + case "short_var_declaration": + add(ts.NamedChildren(node.ChildByFieldName("left")), node.EndByte(), block) + case "var_spec", "const_spec": + add(childrenByField(node, "name"), node.EndByte(), block) + case "type_spec", "type_alias": + add(childrenByField(node, "name"), node.StartByte(), block) + case "range_clause", "receive_statement": + if goDeclares(node) { + add(ts.NamedChildren(node.ChildByFieldName("left")), node.EndByte(), block) + } + case "type_switch_statement": + if alias := node.ChildByFieldName("alias"); alias != nil { + add(ts.NamedChildren(alias), alias.EndByte(), block) + } + } + for _, child := range ts.NamedChildren(node) { + walk(child, block, depth+1) + } + } + walk(function, function, 0) + return locals +} + +// goOperator returns the operator of a unary expression, as & or <-. +func goOperator(node *sitter.Node) string { + if op := node.ChildByFieldName("operator"); op != nil { + return op.Type() + } + return "" +} + +// goDeclares reports whether a range clause or a receive declares its left +// side with :=. +func goDeclares(node *sitter.Node) bool { + for i := 0; i < int(node.ChildCount()); i++ { + if node.Child(i).Type() == ":=" { + return true + } + } + return false +} diff --git a/plugin/indexer/indexer.go b/plugin/indexer/indexer.go index 45e49c67..e3317e24 100644 --- a/plugin/indexer/indexer.go +++ b/plugin/indexer/indexer.go @@ -161,10 +161,10 @@ func New(opts Options) (*Indexer, error) { // pending is a file that a run read and has not indexed yet. type pending struct { - file core.File - language core.Language - data []byte - key fileindex.Key + file core.File + source core.Source + data []byte + key fileindex.Key // restored names the cached plugins that took their stored artifact // before another plugin rejected its own. They skip the tree. @@ -194,8 +194,10 @@ type run struct { // that the store serves come first, in path order, and then the parsed // files, in path order. // -// When ctx is done, Run stops, marks each file that it did not index as -// missing, and returns the result so far with the error of ctx. Run fails +// When ctx is done, Run stops and puts the indexes and the artifacts of the +// files that it finished in the store. It marks each file that it did not +// index as missing, and returns the result so far with the error of ctx. +// A file that ctx cut keeps no index and no artifact. Run fails // when two files of fsys have one name, as the same relative path under // two app roots, because the index keys a file by its name. func (ix *Indexer) Run(ctx context.Context, fsys core.ImportAwareFileSystem) (*Result, error) { @@ -285,9 +287,9 @@ func (r *run) index(ctx context.Context, files []core.File, byExtension map[stri r.exclude(file) continue } - r.result.Languages[file.Name()] = language.Meta().Code data, err := read(file, r.opts.MaxFileBytes) if err != nil { + r.result.Languages[file.Name()] = language.Meta().Code r.fail(ctx, file, core.StageRead, err) continue } @@ -295,12 +297,16 @@ func (r *run) index(ctx context.Context, files []core.File, byExtension map[stri r.exclude(file) continue } + // The script blocks of a component file name their language, and + // the key and the index of the file hold it. + source := parser.Source(language, file.Name(), data) + r.result.Languages[file.Name()] = source.Language.Meta().Code batch = append(batch, pending{ - file: file, - language: language, - data: data, - key: fileindex.NewKey(file.Name(), fileindex.HashContent(data), language.Meta().Code), + file: file, + source: source, + data: data, + key: fileindex.NewKey(file.Name(), fileindex.HashContent(data), source.Language.Meta().Code), }) bytes += len(data) if len(batch) == batchSize || bytes >= maxBatchBytes { @@ -325,7 +331,7 @@ func read(file core.File, limit int) ([]byte, error) { } // flush serves the files of a batch from the store, parses the rest and -// puts their indexes. +// puts their indexes and artifacts. func (r *run) flush(ctx context.Context, batch []pending) error { if len(batch) == 0 { return nil @@ -352,20 +358,28 @@ func (r *run) flush(ctx context.Context, batch []pending) error { parse = append(parse, p) } - put, made, err := r.parse(ctx, parse) - if err != nil { + put, made, parseErr := r.parse(ctx, parse) + // The files that finished before ctx was done hold whole indexes and + // artifacts. The store keeps them past the end of ctx, so the next run + // with the same deadline does not parse them again. + if err := r.save(context.WithoutCancel(ctx), put, made); err != nil { return err } + return parseErr +} + +// save stores the new indexes and artifacts of a batch. +func (r *run) save(ctx context.Context, indexes []*fileindex.Index, artifacts []store.Artifact) error { if r.opts.Store == nil { return nil } - if len(put) > 0 { - if err := r.opts.Store.Put(ctx, put); err != nil { + if len(indexes) > 0 { + if err := r.opts.Store.Put(ctx, indexes); err != nil { return fmt.Errorf("failed to store file indexes: %w", err) } } - if len(made) > 0 { - if err := r.opts.Store.PutArtifacts(ctx, made); err != nil { + if len(artifacts) > 0 { + if err := r.opts.Store.PutArtifacts(ctx, artifacts); err != nil { return fmt.Errorf("failed to store plugin artifacts: %w", err) } } @@ -420,7 +434,8 @@ func (r *run) parse(ctx context.Context, files []pending) ([]*fileindex.Index, [ // collect records the outcome of one file, runs the extra plugins on its // tree and closes the tree. It returns the index of the file, or nil when -// the file failed, and the artifacts of the cached plugins. +// the file failed, and the artifacts of the cached plugins that finished +// before ctx was done. func (r *run) collect(ctx context.Context, p pending, o outcome) (*fileindex.Index, []store.Artifact) { if o.err != nil { r.fail(ctx, p.file, core.StageParse, o.err) @@ -447,9 +462,13 @@ func (r *run) collect(ctx context.Context, p pending, o outcome) (*fileindex.Ind continue } data, err := analyzeCached(ctx, cached, p.file, o) - if err != nil { + switch { + case err != nil: report.Failed = append(report.Failed, failure(p.file, pl, err)) - } else { + case ctx.Err() == nil: + // A plugin can stop at the end of ctx and return a partial + // artifact with no error. A live ctx after the return proves + // that the plugin saw no end of ctx. artifacts = append(artifacts, store.Artifact{Key: artifactKey(p, cached), Data: data}) } continue diff --git a/plugin/indexer/indexer_pool.go b/plugin/indexer/indexer_pool.go index 86756c50..dc252f3a 100644 --- a/plugin/indexer/indexer_pool.go +++ b/plugin/indexer/indexer_pool.go @@ -12,8 +12,10 @@ import ( "github.com/safedep/code/plugin/callgraph" ) -// contentParser parses the bytes of a file that the run already read. +// contentParser parses the bytes of a file that the run already read, from +// the source that the run picked or from the bytes alone. type contentParser interface { + ParseSource(ctx context.Context, file core.File, data []byte, source core.Source) (core.ParseTree, error) ParseContent(ctx context.Context, file core.File, data []byte) (core.ParseTree, error) Close() } @@ -114,7 +116,7 @@ func analyze(ctx context.Context, p contentParser, file pending, keepGraph bool) if ctx.Err() != nil { return outcome{} } - tree, err := p.ParseContent(ctx, file.file, file.data) + tree, err := p.ParseSource(ctx, file.file, file.data, file.source) if err != nil { return outcome{err: err} } @@ -124,7 +126,7 @@ func analyze(ctx context.Context, p contentParser, file pending, keepGraph bool) index = ix return nil }} - if !slices.Contains(indexPlugin.SupportedLanguages(), file.language.Meta().Code) { + if !slices.Contains(indexPlugin.SupportedLanguages(), file.source.Language.Meta().Code) { return outcome{tree: tree} } err = plugin.Analyze(ctx, indexPlugin, file.file, tree) diff --git a/plugin/indexer/indexer_test.go b/plugin/indexer/indexer_test.go index 40c27a7c..a48ac8a6 100644 --- a/plugin/indexer/indexer_test.go +++ b/plugin/indexer/indexer_test.go @@ -471,3 +471,88 @@ func TestNewValidatesTheOptions(t *testing.T) { caller[0] = nil assert.Equal(t, languages, ix.opts.Languages, "a change of the caller's slice after New does not reach the indexer") } + +func TestIndexerReadsJSXAndTheScriptsOfComponentFiles(t *testing.T) { + type importAt struct { + Module string + Line int + } + type callAt struct { + Callee string + Line int + Column int + } + files := map[string]struct { + content string + language core.LanguageCode + imports []importAt + calls []callAt + }{ + "App.jsx": { + content: "import pad from 'left-pad'\n\nexport default function App() {\n return
{pad('a', 2)}
;\n}\n", + language: core.LanguageCodeJavascript, + imports: []importAt{{"left-pad", 1}}, + calls: []callAt{{"pad", 4, 16}}, + }, + "App.vue": { + content: "\n\n\n", + language: core.LanguageCodeJavascript, + imports: []importAt{{"left-pad", 6}}, + calls: []callAt{{"pad", 7, 13}}, + }, + "Widget.vue": { + content: "\n\r\n\r\n", + language: core.LanguageCodeTypescript, + imports: []importAt{{"vue", 2}, {"left-pad", 7}}, + calls: []callAt{{"defineComponent", 3, 16}, {"pad", 8, 19}}, + }, + "Page.astro": { + content: "---\nimport pad from 'left-pad'\nconst title: string = pad('a', 2)\n---\n

{title}

\n\n", + language: core.LanguageCodeTypescript, + imports: []importAt{{"left-pad", 2}, {"canvas-confetti", 7}}, + calls: []callAt{{"pad", 3, 23}, {"confetti", 8, 3}}, + }, + "Card.svelte": { + content: "

{label}

\n\n", + language: core.LanguageCodeJavascript, + imports: []importAt{{"left-pad", 3}}, + calls: []callAt{{"pad", 4, 15}}, + }, + } + dir := t.TempDir() + contents := map[string]string{} + for name, f := range files { + contents[name] = f.content + } + writeFiles(t, dir, contents) + fsys := appFileSystem(t, dir) + opts := Options{Languages: projectLanguages(t), Store: memstore.New()} + + cold, err := runIndexer(t, context.Background(), fsys, opts) + require.NoError(t, err) + assert.Empty(t, cold.Report.Failed) + require.Len(t, cold.Indexes, len(files)) + for _, ix := range cold.Indexes { + want := files[ix.Path] + t.Run(ix.Path, func(t *testing.T) { + assert.Equal(t, want.language, ix.Language) + assert.Equal(t, want.language, cold.Languages[ix.Path]) + assert.Equal(t, fileindex.FileIndexed, cold.Files[ix.Path]) + var imports []importAt + for _, imp := range ix.Imports { + imports = append(imports, importAt{imp.Module, imp.Line}) + } + assert.Equal(t, want.imports, imports) + var calls []callAt + for _, c := range ix.Calls { + calls = append(calls, callAt{c.Callee, c.Line, c.Column}) + } + assert.Equal(t, want.calls, calls) + }) + } + + warm, err := runIndexer(t, context.Background(), fsys, opts) + require.NoError(t, err) + assert.Empty(t, warm.Report.Visited) + assert.Equal(t, cold.Indexes, warm.Indexes) +} diff --git a/plugin/indexer/languages.go b/plugin/indexer/languages.go index 34e0dae0..3325e8e4 100644 --- a/plugin/indexer/languages.go +++ b/plugin/indexer/languages.go @@ -1,7 +1,10 @@ package indexer import ( + "context" + "github.com/safedep/code/core" + "github.com/safedep/code/core/ast" "github.com/safedep/code/fileindex" sitter "github.com/smacker/go-tree-sitter" ) @@ -29,6 +32,19 @@ type languageRules struct { // on such a receiver gets the key of the name in its scope as its // target, as main.py//run//t//do, so the link step reads the type. typedReceivers bool + // declaredAt reports whether the declared type of a name holds at a + // call: the caller binds the name once, as a parameter or a typed + // local, before the call. Rust reads it for w.write() after + // let w: Box = Box::new(Html {}), where the call graph knows + // no type of the value, and for a call that the call graph binds to a + // parameter. A value that the call graph makes, as String::new(), is an + // instance of that type alone, so a call on it keeps its target. + declaredAt func(ctx context.Context, p *projection, call fileindex.Call, name string) bool + // rebound reports whether the caller binds a name before a call by a + // syntax that the call graph does not read, as Some(t) in + // if let Some(t) = x. The call graph then binds a call on the name to + // the value of another binding. + rebound func(ctx context.Context, p *projection, call fileindex.Call, name string) bool // packageOf returns the package or the namespace of the file, which the // files of one unit share. @@ -70,6 +86,11 @@ type languageRules struct { // prune drops the definitions that hold no record, after the records. prune func(p *projection) + // markTries marks each ? in the targets of a call from the syntax of + // its callee, as the ? of Rust in dep//load//?//run. The call graph + // keeps no such mark. + markTries func(ctx context.Context, p *projection, call *fileindex.Call, callee ast.NodePosition) + // visitors read the walk of the tree, as for the references and the // flags of the language, in this order. visitors []func(p *projection) visitor diff --git a/plugin/indexer/markup.go b/plugin/indexer/markup.go new file mode 100644 index 00000000..8da16b88 --- /dev/null +++ b/plugin/indexer/markup.go @@ -0,0 +1,41 @@ +package indexer + +import ( + "bytes" + + "github.com/safedep/code/core" + "github.com/safedep/code/fileindex" + "github.com/safedep/code/plugin/callgraph" +) + +// addMarkup adds the records of the markup of a component file. Each +// script element that loads a file is an import of that file, as +// import './logic.js'. The rest of the markup is one record at the top +// level of the file, because the parse does not read it. +func (p *projection) addMarkup() { + tree, ok := p.tree.(core.MarkupTree) + if !ok || tree.Markup() == nil { + return + } + markup := tree.Markup() + for _, script := range markup.Scripts { + line, _ := p.position(script.Offset) + p.ix.Imports = append(p.ix.Imports, fileindex.Import{Line: line, Module: script.Src, Kind: fileindex.ImportStatic}) + } + line, column := p.position(markup.Offset) + text, _, _ := bytes.Cut(p.source[markup.Offset:], []byte("\n")) + p.ix.Unresolved = append(p.ix.Unresolved, fileindex.Unresolved{ + Caller: p.cg.FileName, + Line: line, + Column: column, + Text: callgraph.ClipText(string(bytes.TrimSpace(text))), + Reason: fileindex.UnresolvedMarkup, + }) +} + +// position returns the line and the column of an offset of the file, from +// 1 as the index counts them. +func (p *projection) position(offset int) (line, column int) { + before := p.source[:offset] + return bytes.Count(before, []byte("\n")) + 1, offset - bytes.LastIndexByte(before, '\n') +} diff --git a/plugin/indexer/plugin.go b/plugin/indexer/plugin.go index 50792ae8..1388abef 100644 --- a/plugin/indexer/plugin.go +++ b/plugin/indexer/plugin.go @@ -50,7 +50,12 @@ func (p *fileIndexPlugin) AnalyzeTree(ctx context.Context, tree core.ParseTree) if err := p.callback(ctx, ix); err != nil { return err } + return complete(ctx, ix) +} +// complete fails on an index that the deadline cut, so the run does not +// store it as whole. +func complete(ctx context.Context, ix *fileindex.Index) error { if partial(ix) { return fmt.Errorf("index of %s is partial: %w", ix.Path, ctx.Err()) } diff --git a/plugin/indexer/plugin_test.go b/plugin/indexer/plugin_test.go index 14cdb360..912ef2b0 100644 --- a/plugin/indexer/plugin_test.go +++ b/plugin/indexer/plugin_test.go @@ -9,8 +9,10 @@ import ( "os" "path/filepath" "runtime" + "slices" "strings" "sync" + "sync/atomic" "testing" "github.com/safedep/code/core" @@ -104,6 +106,7 @@ var goldenHashes = map[string]string{ "13": "97614d8b96782e65813ee409e4a3f33c4655157b5dfa58baf17cd3677958c619", "14": "26f22c80fd4120cd04f228a53188accb1695434d0ed1ff5654b110981f8f44dd", "15": "c4b949e72be5b2b4474e06f20ae18b0cc4e080b6c5fe20ccaf712c3714bf2a12", + "16": "235591d02eeb5e1bb771fbacf0ba6db228146a6a70c55670de6cb93392d75d81", } func TestFileIndexGoldenHashFollowsVersion(t *testing.T) { @@ -186,7 +189,7 @@ func TestFileIndexPartialAtTheDeadline(t *testing.T) { } tree := parseSource(t, core.LanguageCodeJavascript, "slow.js", source.String()) - ctx := &buildCutContext{Context: context.Background()} + ctx := &cutContext{Context: context.Background(), at: []string{callGraphBuild}} var got *fileindex.Index err := NewFileIndexPlugin(func(_ context.Context, ix *fileindex.Index) error { @@ -198,37 +201,48 @@ func TestFileIndexPartialAtTheDeadline(t *testing.T) { assert.True(t, partial(got)) } -// buildCutContext is done from the first check inside the build of the -// call graph on. The import queries before the build see it live, so the -// build starts and the deadline cuts it. It reads the stack, so it does not -// depend on the number of checks before the build. -type buildCutContext struct { +// callGraphBuild is the function that builds a call graph. +const callGraphBuild = "callgraph.(*CallGraph).build" + +// cutContext is done from the first check of it that runs inside every +// function of at. The checks before see it live, so the work starts and +// the deadline cuts it. It reads the stack, so it does not depend on the number of checks +// before the cut. The workers of a run check it at once, so the cut is +// atomic. +type cutContext struct { context.Context - cut bool + at []string + cut atomic.Bool } -func (c *buildCutContext) Err() error { - if !c.cut { - c.cut = insideBuild() +func (c *cutContext) Err() error { + if !c.cut.Load() && inside(c.at) { + c.cut.Store(true) } - if c.cut { + if c.cut.Load() { return context.Canceled } return nil } -func insideBuild() bool { +// inside reports whether the stack of the caller runs each function of fns. +func inside(fns []string) bool { pcs := make([]uintptr, 64) frames := runtime.CallersFrames(pcs[:runtime.Callers(2, pcs)]) + var stack []string for { frame, more := frames.Next() - if strings.HasSuffix(frame.Function, "callgraph.(*CallGraph).build") { - return true - } + stack = append(stack, frame.Function) if !more { + break + } + } + for _, fn := range fns { + if !slices.ContainsFunc(stack, func(f string) bool { return strings.HasSuffix(f, fn) }) { return false } } + return true } func parseSource(t *testing.T, code core.LanguageCode, name, source string) core.ParseTree { diff --git a/plugin/indexer/project.go b/plugin/indexer/project.go index 874db990..ddb18328 100644 --- a/plugin/indexer/project.go +++ b/plugin/indexer/project.go @@ -47,10 +47,18 @@ type projection struct { // members holds the line and the column of each call that names a // member of an object of the file. members map[[2]uint32]bool + // binders holds the nodes that bind each name of a function, by the + // start of the function, for a language with declaredAt. A nil map is + // a function whose walk stopped early. + binders map[uint32]map[string][]*sitter.Node // exportStatements are the export statements and the CommonJS export // assignments of the file. exportStatements []*sitter.Node + // goScoped holds the scopes of the local names of each Go function + // whose values the index reads, by the key of the function and the + // name. + goScoped map[string]map[string][]goScope } // project builds the call graph of tree and projects it into an index. It @@ -95,6 +103,7 @@ func project(ctx context.Context, tree core.ParseTree) (*fileindex.Index, *callg p.ix.Package = rules.packageOf(p) } p.addImports() + p.addMarkup() p.addDefinitions() p.flagModuleGetattr() p.run(rules.exports) @@ -103,8 +112,8 @@ func project(ctx context.Context, tree core.ParseTree) (*fileindex.Index, *callg p.run(rules.inits) p.walkTree(ctx) p.run(rules.afterWalk) - p.addCalls() - p.addUnresolved() + p.addCalls(ctx) + p.addUnresolved(ctx) p.run(rules.prune) if err := p.addClasses(ctx); err != nil { return nil, nil, err @@ -344,7 +353,7 @@ func decorators(p *projection, node *sitter.Node) []string { func (p *projection) addClasses(_ context.Context) error { methods := map[string][]string{} for key, def := range p.defs { - if def.Parent != "" { + if def.Parent != "" && !def.HoldsData() { methods[def.Parent] = append(methods[def.Parent], key) } } diff --git a/plugin/indexer/references.go b/plugin/indexer/references.go index f85a2bd5..cd20b424 100644 --- a/plugin/indexer/references.go +++ b/plugin/indexer/references.go @@ -32,10 +32,22 @@ type referenceSyntax struct { // of a, b := f, g. The last child of each list holds the value, so the // key of a keyed element is no value. lists map[string]bool + // spreads maps a holder to the node type of a spread in it, as + // {...handlers} in + bold + + ); +} diff --git a/test/projectgraph/cases/javascript/jsx-elements/project/src/Other.jsx b/test/projectgraph/cases/javascript/jsx-elements/project/src/Other.jsx new file mode 100644 index 00000000..bd3485a4 --- /dev/null +++ b/test/projectgraph/cases/javascript/jsx-elements/project/src/Other.jsx @@ -0,0 +1,6 @@ +import { odanger } from 'plug'; + +export function Other() { + odanger(); + return null; +} diff --git a/test/projectgraph/cases/javascript/jsx-elements/project/src/main.js b/test/projectgraph/cases/javascript/jsx-elements/project/src/main.js new file mode 100644 index 00000000..d9478ff0 --- /dev/null +++ b/test/projectgraph/cases/javascript/jsx-elements/project/src/main.js @@ -0,0 +1,4 @@ +import { render } from 'react-dom'; +import App from './App'; + +render(App, document.body); diff --git a/test/projectgraph/cases/javascript/jsx-spread-attributes/expect.yaml b/test/projectgraph/cases/javascript/jsx-spread-attributes/expect.yaml new file mode 100644 index 00000000..712be8a9 --- /dev/null +++ b/test/projectgraph/cases/javascript/jsx-spread-attributes/expect.yaml @@ -0,0 +1,48 @@ +source: safedep:hand +description: >- + App spreads the object handlers into a button, as ; +} diff --git a/test/projectgraph/cases/javascript/jsx-spread-attributes/project/src/main.js b/test/projectgraph/cases/javascript/jsx-spread-attributes/project/src/main.js new file mode 100644 index 00000000..eb515c7e --- /dev/null +++ b/test/projectgraph/cases/javascript/jsx-spread-attributes/project/src/main.js @@ -0,0 +1,5 @@ +import { createElement } from 'react'; +import { createRoot } from 'react-dom/client'; +import { App } from './App.jsx'; + +createRoot(document.body).render(createElement(App)); diff --git a/test/projectgraph/cases/javascript/jsx-spread-unknown-object/expect.yaml b/test/projectgraph/cases/javascript/jsx-spread-unknown-object/expect.yaml new file mode 100644 index 00000000..d59d7709 --- /dev/null +++ b/test/projectgraph/cases/javascript/jsx-spread-unknown-object/expect.yaml @@ -0,0 +1,46 @@ +source: safedep:hand +description: >- + App spreads the result of make into Button, as ; +} + +export function App() { + return diff --git a/test/projectgraph/cases/javascript/svelte-markup/project/src/main.js b/test/projectgraph/cases/javascript/svelte-markup/project/src/main.js new file mode 100644 index 00000000..a335534c --- /dev/null +++ b/test/projectgraph/cases/javascript/svelte-markup/project/src/main.js @@ -0,0 +1,4 @@ +import { mount } from 'svelte'; +import Other from './Other.svelte'; + +mount(Other, { target: document.body }); diff --git a/test/projectgraph/cases/javascript/vue-options-api/expect.yaml b/test/projectgraph/cases/javascript/vue-options-api/expect.yaml new file mode 100644 index 00000000..3ff5fd4a --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-options-api/expect.yaml @@ -0,0 +1,68 @@ +source: safedep:hand +description: Vue calls the methods and the hooks of the options object of a component. No code of the project calls go of Opt.vue or mounted of Hooks.vue. Hooks.vue has no template, and it still has a markup record, so hdanger reads uncertain and not not-found. +roots: + - key: src/main.js + kind: manifest + rule: node.package-json +exact_edges: true +edges: + - from: src/main.js + to: src/Opt.vue + kind: import + confidence: high + site: src/main.js:2 + - from: src/main.js + to: src/Hooks.vue + kind: import + confidence: high + site: src/main.js:3 + - from: src/main.js + to: vue//createApp + kind: direct + confidence: high + site: src/main.js:5 + - from: src/main.js + to: vue//createApp + kind: direct + confidence: high + site: src/main.js:6 + - from: src/Opt.vue//methods//go + to: plug//odanger + kind: direct + confidence: high + site: src/Opt.vue:11 + - from: src/Hooks.vue//mounted + to: plug//hdanger + kind: direct + confidence: high + site: src/Hooks.vue:6 +unresolved: + - caller: src/Hooks.vue + site: src/Hooks.vue:1 + reason: markup + - caller: src/Opt.vue + site: src/Opt.vue:1 + reason: markup +results: + - target: plug//odanger + status: uncertain + call_sites: + - caller: src/Opt.vue//methods//go + site: src/Opt.vue:11 + reached: false + uncertainties: + - kind: unresolved-call + site: src/Opt.vue:1 + - kind: unresolved-call + site: src/Hooks.vue:1 + - target: plug//hdanger + status: uncertain + call_sites: + - caller: src/Hooks.vue//mounted + site: src/Hooks.vue:6 + reached: false + uncertainties: + - kind: unresolved-call + site: src/Opt.vue:1 + - kind: unresolved-call + site: src/Hooks.vue:1 diff --git a/test/projectgraph/cases/javascript/vue-options-api/project/package.json b/test/projectgraph/cases/javascript/vue-options-api/project/package.json new file mode 100644 index 00000000..44fcd270 --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-options-api/project/package.json @@ -0,0 +1,4 @@ +{ + "name": "vue-options-api", + "bin": { "app": "src/main.js" } +} diff --git a/test/projectgraph/cases/javascript/vue-options-api/project/src/Hooks.vue b/test/projectgraph/cases/javascript/vue-options-api/project/src/Hooks.vue new file mode 100644 index 00000000..d9061384 --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-options-api/project/src/Hooks.vue @@ -0,0 +1,9 @@ + diff --git a/test/projectgraph/cases/javascript/vue-options-api/project/src/Opt.vue b/test/projectgraph/cases/javascript/vue-options-api/project/src/Opt.vue new file mode 100644 index 00000000..adc22260 --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-options-api/project/src/Opt.vue @@ -0,0 +1,15 @@ + + + diff --git a/test/projectgraph/cases/javascript/vue-options-api/project/src/main.js b/test/projectgraph/cases/javascript/vue-options-api/project/src/main.js new file mode 100644 index 00000000..509191f0 --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-options-api/project/src/main.js @@ -0,0 +1,6 @@ +import { createApp } from 'vue'; +import Opt from './Opt.vue'; +import Hooks from './Hooks.vue'; + +createApp(Opt); +createApp(Hooks); diff --git a/test/projectgraph/cases/javascript/vue-script-src/expect.yaml b/test/projectgraph/cases/javascript/vue-script-src/expect.yaml new file mode 100644 index 00000000..29f39ff6 --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-script-src/expect.yaml @@ -0,0 +1,53 @@ +source: safedep:hand +description: The script element of Ext.vue loads logic.js by its src attribute. The element is an import of logic.js, so the search reaches edanger through it. +roots: + - key: src/main.js + kind: manifest + rule: node.package-json +exact_edges: true +edges: + - from: src/main.js + to: src/Ext.vue + kind: import + confidence: high + site: src/main.js:2 + - from: src/main.js + to: vue//createApp + kind: direct + confidence: high + site: src/main.js:4 + - from: src/Ext.vue + to: src/logic.js + kind: import + confidence: high + site: src/Ext.vue:2 + - from: src/logic.js + to: plug//edanger + kind: direct + confidence: high + site: src/logic.js:3 +unresolved: + - caller: src/Ext.vue + site: src/Ext.vue:1 + reason: markup +results: + - target: plug//edanger + status: found + path: + - key: src/main.js + - key: src/Ext.vue + site: src/main.js:2 + kind: import + confidence: high + - key: src/logic.js + site: src/Ext.vue:2 + kind: import + confidence: high + - key: plug//edanger + site: src/logic.js:3 + kind: direct + confidence: high + call_sites: + - caller: src/logic.js + site: src/logic.js:3 + reached: true diff --git a/test/projectgraph/cases/javascript/vue-script-src/project/package.json b/test/projectgraph/cases/javascript/vue-script-src/project/package.json new file mode 100644 index 00000000..444e462b --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-script-src/project/package.json @@ -0,0 +1,4 @@ +{ + "name": "vue-script-src", + "bin": { "app": "src/main.js" } +} diff --git a/test/projectgraph/cases/javascript/vue-script-src/project/src/Ext.vue b/test/projectgraph/cases/javascript/vue-script-src/project/src/Ext.vue new file mode 100644 index 00000000..0ddee2d8 --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-script-src/project/src/Ext.vue @@ -0,0 +1,2 @@ + + diff --git a/test/projectgraph/cases/javascript/vue-script-src/project/src/logic.js b/test/projectgraph/cases/javascript/vue-script-src/project/src/logic.js new file mode 100644 index 00000000..cd251f07 --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-script-src/project/src/logic.js @@ -0,0 +1,4 @@ +import { edanger } from 'plug'; + +edanger(); +export default {}; diff --git a/test/projectgraph/cases/javascript/vue-script-src/project/src/main.js b/test/projectgraph/cases/javascript/vue-script-src/project/src/main.js new file mode 100644 index 00000000..cb71a93c --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-script-src/project/src/main.js @@ -0,0 +1,4 @@ +import { createApp } from 'vue'; +import Ext from './Ext.vue'; + +createApp(Ext); diff --git a/test/projectgraph/cases/javascript/vue-setup-handler/expect.yaml b/test/projectgraph/cases/javascript/vue-setup-handler/expect.yaml new file mode 100644 index 00000000..e0c011bc --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-setup-handler/expect.yaml @@ -0,0 +1,37 @@ +source: safedep:hand +description: The template of App.vue names onClick as a click handler. No code of the script calls onClick, so only the markup record makes danger, which onClick calls, read uncertain and not not-found. +roots: + - key: src/main.js + kind: manifest + rule: node.package-json +exact_edges: true +edges: + - from: src/main.js + to: src/App.vue + kind: import + confidence: high + site: src/main.js:2 + - from: src/main.js + to: vue//createApp + kind: direct + confidence: high + site: src/main.js:4 + - from: src/App.vue//onClick + to: plug//danger + kind: direct + confidence: high + site: src/App.vue:9 +unresolved: + - caller: src/App.vue + site: src/App.vue:1 + reason: markup +results: + - target: plug//danger + status: uncertain + call_sites: + - caller: src/App.vue//onClick + site: src/App.vue:9 + reached: false + uncertainties: + - kind: unresolved-call + site: src/App.vue:1 diff --git a/test/projectgraph/cases/javascript/vue-setup-handler/project/package.json b/test/projectgraph/cases/javascript/vue-setup-handler/project/package.json new file mode 100644 index 00000000..df4d053f --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-setup-handler/project/package.json @@ -0,0 +1,4 @@ +{ + "name": "vue-setup-handler", + "bin": { "app": "src/main.js" } +} diff --git a/test/projectgraph/cases/javascript/vue-setup-handler/project/src/App.vue b/test/projectgraph/cases/javascript/vue-setup-handler/project/src/App.vue new file mode 100644 index 00000000..7e8e227e --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-setup-handler/project/src/App.vue @@ -0,0 +1,11 @@ + + + diff --git a/test/projectgraph/cases/javascript/vue-setup-handler/project/src/main.js b/test/projectgraph/cases/javascript/vue-setup-handler/project/src/main.js new file mode 100644 index 00000000..c4744d5b --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-setup-handler/project/src/main.js @@ -0,0 +1,4 @@ +import { createApp } from 'vue'; +import App from './App.vue'; + +createApp(App); diff --git a/test/projectgraph/cases/javascript/vue-template-markup/expect.yaml b/test/projectgraph/cases/javascript/vue-template-markup/expect.yaml new file mode 100644 index 00000000..41d80d3a --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-template-markup/expect.yaml @@ -0,0 +1,54 @@ +source: safedep:hand +description: The template of App.vue calls danger and render, which its script imports. The index does not parse the template, so the file has a markup record, and each target reads uncertain, not no-such-target. setup runs in the script and is found. +roots: + - key: src/main.js + kind: manifest + rule: node.package-json +exact_edges: true +edges: + - from: src/main.js + to: src/App.vue + kind: import + confidence: high + site: src/main.js:2 + - from: src/main.js + to: vue//createApp + kind: direct + confidence: high + site: src/main.js:4 + - from: src/App.vue + to: plug//setup + kind: direct + confidence: high + site: src/App.vue:8 +unresolved: + - caller: src/App.vue + site: src/App.vue:1 + reason: markup +results: + - target: plug//setup + status: found + path: + - key: src/main.js + - key: src/App.vue + site: src/main.js:2 + kind: import + confidence: high + - key: plug//setup + site: src/App.vue:8 + kind: direct + confidence: high + call_sites: + - caller: src/App.vue + site: src/App.vue:8 + reached: true + - target: plug//danger + status: uncertain + uncertainties: + - kind: unresolved-call + site: src/App.vue:1 + - target: plug//render + status: uncertain + uncertainties: + - kind: unresolved-call + site: src/App.vue:1 diff --git a/test/projectgraph/cases/javascript/vue-template-markup/project/package.json b/test/projectgraph/cases/javascript/vue-template-markup/project/package.json new file mode 100644 index 00000000..feaf197e --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-template-markup/project/package.json @@ -0,0 +1,4 @@ +{ + "name": "vue-template-markup", + "bin": { "app": "src/main.js" } +} diff --git a/test/projectgraph/cases/javascript/vue-template-markup/project/src/App.vue b/test/projectgraph/cases/javascript/vue-template-markup/project/src/App.vue new file mode 100644 index 00000000..03c71185 --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-template-markup/project/src/App.vue @@ -0,0 +1,9 @@ + + + diff --git a/test/projectgraph/cases/javascript/vue-template-markup/project/src/main.js b/test/projectgraph/cases/javascript/vue-template-markup/project/src/main.js new file mode 100644 index 00000000..c4744d5b --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-template-markup/project/src/main.js @@ -0,0 +1,4 @@ +import { createApp } from 'vue'; +import App from './App.vue'; + +createApp(App); diff --git a/test/projectgraph/cases/python/dependency-compiled-module/summaries/pypi_speedy_1.0.0.json b/test/projectgraph/cases/python/dependency-compiled-module/summaries/pypi_speedy_1.0.0.json index 22a34a9f..78254678 100644 --- a/test/projectgraph/cases/python/dependency-compiled-module/summaries/pypi_speedy_1.0.0.json +++ b/test/projectgraph/cases/python/dependency-compiled-module/summaries/pypi_speedy_1.0.0.json @@ -1,7 +1,7 @@ { - "version": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "unit": "pypi:speedy@1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/python/dependency-dark-unit-record/expect.yaml b/test/projectgraph/cases/python/dependency-dark-unit-record/expect.yaml new file mode 100644 index 00000000..1aeae745 --- /dev/null +++ b/test/projectgraph/cases/python/dependency-dark-unit-record/expect.yaml @@ -0,0 +1,34 @@ +source: safedep:hand +description: >- + The unit PyYAML holds the package yaml, and no import of the project names it, so a record of the unit that + no node holds concerns what the code of the unit can run in every search. construct of the unit calls a + function that getattr gives, which can be any function. The scope of the unit counts the records of its + code, so os.system, which only the uncalled cleanup of the project calls, is uncertain and not not-found. +units: + venv/site-packages/yaml: pypi:PyYAML@6.0.1 +import_dirs: + - venv/site-packages +problems: + - path: pypi:PyYAML@6.0.1 + text: no import names the unit, and it gives no module for the package pyyaml +roots: + - key: main.py + kind: main + rule: python.main-guard +unresolved: + - caller: "" + site: pypi:PyYAML@6.0.1 + reason: no-definition + - caller: pypi:PyYAML@6.0.1//yaml/__init__.py//construct + site: pypi:PyYAML@6.0.1//yaml/__init__.py:9 + reason: computed-name +results: + - target: os//system + status: uncertain + call_sites: + - caller: main.py//cleanup + site: main.py:5 + reached: false + uncertainties: + - kind: unresolved-call + site: pypi:PyYAML@6.0.1 diff --git a/test/projectgraph/cases/python/dependency-dark-unit-record/project/main.py b/test/projectgraph/cases/python/dependency-dark-unit-record/project/main.py new file mode 100644 index 00000000..cfa5de8e --- /dev/null +++ b/test/projectgraph/cases/python/dependency-dark-unit-record/project/main.py @@ -0,0 +1,9 @@ +import os + + +def cleanup(): + os.system("rm -rf build") + + +if __name__ == "__main__": + print("start") diff --git a/test/projectgraph/cases/python/dependency-dark-unit-record/project/venv/site-packages/yaml/__init__.py b/test/projectgraph/cases/python/dependency-dark-unit-record/project/venv/site-packages/yaml/__init__.py new file mode 100644 index 00000000..31dadd3a --- /dev/null +++ b/test/projectgraph/cases/python/dependency-dark-unit-record/project/venv/site-packages/yaml/__init__.py @@ -0,0 +1,9 @@ +import marshal + + +def load(data): + return marshal.loads(data) + + +def construct(loader, name): + return getattr(loader, name)() diff --git a/test/projectgraph/cases/python/dependency-dark-unit-record/summaries/pypi_PyYAML_6.0.1.json b/test/projectgraph/cases/python/dependency-dark-unit-record/summaries/pypi_PyYAML_6.0.1.json new file mode 100644 index 00000000..c185c149 --- /dev/null +++ b/test/projectgraph/cases/python/dependency-dark-unit-record/summaries/pypi_PyYAML_6.0.1.json @@ -0,0 +1,52 @@ +{ + "version": "3", + "index_version": "16", + "link_version": "18", + "unit": "pypi:PyYAML@6.0.1", + "symbols": [ + { + "key": "yaml//construct", + "definitions": [ + "pypi:PyYAML@6.0.1//yaml/__init__.py//construct" + ], + "reaches": [], + "uncertainties": [ + 0, + 1 + ] + }, + { + "key": "yaml//load", + "definitions": [ + "pypi:PyYAML@6.0.1//yaml/__init__.py//load" + ], + "reaches": [ + "marshal//loads" + ] + } + ], + "targets": [ + "marshal//loads" + ], + "uncertainties": [ + { + "kind": "dynamic-flag", + "key": "pypi:PyYAML@6.0.1//yaml/__init__.py//construct", + "site": { + "path": "pypi:PyYAML@6.0.1//yaml/__init__.py", + "line": 9 + }, + "text": "reflection" + }, + { + "kind": "unresolved-call", + "key": "pypi:PyYAML@6.0.1//yaml/__init__.py//construct", + "site": { + "path": "pypi:PyYAML@6.0.1//yaml/__init__.py", + "line": 9, + "column": 12 + }, + "text": "getattr(loader, name) (computed-name)" + } + ] +} diff --git a/test/projectgraph/cases/python/dependency-dark-unit/summaries/pypi_PyYAML_6.0.1.json b/test/projectgraph/cases/python/dependency-dark-unit/summaries/pypi_PyYAML_6.0.1.json index 3f50d6a1..2fd267bc 100644 --- a/test/projectgraph/cases/python/dependency-dark-unit/summaries/pypi_PyYAML_6.0.1.json +++ b/test/projectgraph/cases/python/dependency-dark-unit/summaries/pypi_PyYAML_6.0.1.json @@ -1,7 +1,7 @@ { - "version": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "unit": "pypi:PyYAML@6.0.1", "symbols": [ { diff --git a/test/projectgraph/cases/python/dependency-failed-reached/summaries/pypi_badpkg_1.0.0.json b/test/projectgraph/cases/python/dependency-failed-reached/summaries/pypi_badpkg_1.0.0.json index c1762ee2..b82d1f86 100644 --- a/test/projectgraph/cases/python/dependency-failed-reached/summaries/pypi_badpkg_1.0.0.json +++ b/test/projectgraph/cases/python/dependency-failed-reached/summaries/pypi_badpkg_1.0.0.json @@ -1,7 +1,7 @@ { - "version": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "unit": "pypi:badpkg@1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/python/dependency-failed-reached/summaries/pypi_okpkg_1.0.0.json b/test/projectgraph/cases/python/dependency-failed-reached/summaries/pypi_okpkg_1.0.0.json index 75940477..571e973e 100644 --- a/test/projectgraph/cases/python/dependency-failed-reached/summaries/pypi_okpkg_1.0.0.json +++ b/test/projectgraph/cases/python/dependency-failed-reached/summaries/pypi_okpkg_1.0.0.json @@ -1,7 +1,7 @@ { - "version": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "unit": "pypi:okpkg@1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/python/dependency-failed-unreached/summaries/pypi_badpkg_1.0.0.json b/test/projectgraph/cases/python/dependency-failed-unreached/summaries/pypi_badpkg_1.0.0.json index c1762ee2..b82d1f86 100644 --- a/test/projectgraph/cases/python/dependency-failed-unreached/summaries/pypi_badpkg_1.0.0.json +++ b/test/projectgraph/cases/python/dependency-failed-unreached/summaries/pypi_badpkg_1.0.0.json @@ -1,7 +1,7 @@ { - "version": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "unit": "pypi:badpkg@1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/python/dependency-failed-unreached/summaries/pypi_okpkg_1.0.0.json b/test/projectgraph/cases/python/dependency-failed-unreached/summaries/pypi_okpkg_1.0.0.json index 75940477..571e973e 100644 --- a/test/projectgraph/cases/python/dependency-failed-unreached/summaries/pypi_okpkg_1.0.0.json +++ b/test/projectgraph/cases/python/dependency-failed-unreached/summaries/pypi_okpkg_1.0.0.json @@ -1,7 +1,7 @@ { - "version": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "unit": "pypi:okpkg@1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/python/dependency-namespace-two-dists/summaries/pypi_google-auth_2.0.0.json b/test/projectgraph/cases/python/dependency-namespace-two-dists/summaries/pypi_google-auth_2.0.0.json index 1e7a400f..cb2b0b6b 100644 --- a/test/projectgraph/cases/python/dependency-namespace-two-dists/summaries/pypi_google-auth_2.0.0.json +++ b/test/projectgraph/cases/python/dependency-namespace-two-dists/summaries/pypi_google-auth_2.0.0.json @@ -1,7 +1,7 @@ { - "version": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "unit": "pypi:google-auth@2.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/python/dependency-namespace-two-dists/summaries/pypi_google-cloud-storage_2.1.0.json b/test/projectgraph/cases/python/dependency-namespace-two-dists/summaries/pypi_google-cloud-storage_2.1.0.json index 6586eb5d..c7329383 100644 --- a/test/projectgraph/cases/python/dependency-namespace-two-dists/summaries/pypi_google-cloud-storage_2.1.0.json +++ b/test/projectgraph/cases/python/dependency-namespace-two-dists/summaries/pypi_google-cloud-storage_2.1.0.json @@ -1,7 +1,7 @@ { - "version": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "unit": "pypi:google-cloud-storage@2.1.0", "symbols": [ { diff --git a/test/projectgraph/cases/python/dependency-package-dir/summaries/pypi_outer_1.0.0.json b/test/projectgraph/cases/python/dependency-package-dir/summaries/pypi_outer_1.0.0.json index 4d777061..65c4753a 100644 --- a/test/projectgraph/cases/python/dependency-package-dir/summaries/pypi_outer_1.0.0.json +++ b/test/projectgraph/cases/python/dependency-package-dir/summaries/pypi_outer_1.0.0.json @@ -1,7 +1,7 @@ { - "version": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "unit": "pypi:outer@1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/python/dependency-record-import-name/summaries/pypi_PyYAML_6.0.1.json b/test/projectgraph/cases/python/dependency-record-import-name/summaries/pypi_PyYAML_6.0.1.json index 3f50d6a1..2fd267bc 100644 --- a/test/projectgraph/cases/python/dependency-record-import-name/summaries/pypi_PyYAML_6.0.1.json +++ b/test/projectgraph/cases/python/dependency-record-import-name/summaries/pypi_PyYAML_6.0.1.json @@ -1,7 +1,7 @@ { - "version": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "unit": "pypi:PyYAML@6.0.1", "symbols": [ { diff --git a/test/projectgraph/cases/python/dependency-site-packages/summaries/pypi_requests_2.31.0.json b/test/projectgraph/cases/python/dependency-site-packages/summaries/pypi_requests_2.31.0.json index 9f426c70..a22d03c2 100644 --- a/test/projectgraph/cases/python/dependency-site-packages/summaries/pypi_requests_2.31.0.json +++ b/test/projectgraph/cases/python/dependency-site-packages/summaries/pypi_requests_2.31.0.json @@ -1,7 +1,7 @@ { - "version": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "unit": "pypi:requests@2.31.0", "symbols": [ { diff --git a/test/projectgraph/cases/python/dependency-site-packages/summaries/pypi_urllib3_2.0.7.json b/test/projectgraph/cases/python/dependency-site-packages/summaries/pypi_urllib3_2.0.7.json index 620b0a77..f7671ddd 100644 --- a/test/projectgraph/cases/python/dependency-site-packages/summaries/pypi_urllib3_2.0.7.json +++ b/test/projectgraph/cases/python/dependency-site-packages/summaries/pypi_urllib3_2.0.7.json @@ -1,7 +1,7 @@ { - "version": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "unit": "pypi:urllib3@2.0.7", "symbols": [ { diff --git a/test/projectgraph/cases/python/dependency-stdlib/summaries/python_std_3.12.3.json b/test/projectgraph/cases/python/dependency-stdlib/summaries/python_std_3.12.3.json index 0c1cb890..a8ed36ca 100644 --- a/test/projectgraph/cases/python/dependency-stdlib/summaries/python_std_3.12.3.json +++ b/test/projectgraph/cases/python/dependency-stdlib/summaries/python_std_3.12.3.json @@ -1,7 +1,7 @@ { - "version": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "unit": "python:std@3.12.3", "symbols": [ { diff --git a/test/projectgraph/cases/python/dependency-typed-return/summaries/pypi_dep_1.0.0.json b/test/projectgraph/cases/python/dependency-typed-return/summaries/pypi_dep_1.0.0.json index 657312ba..33e4eca4 100644 --- a/test/projectgraph/cases/python/dependency-typed-return/summaries/pypi_dep_1.0.0.json +++ b/test/projectgraph/cases/python/dependency-typed-return/summaries/pypi_dep_1.0.0.json @@ -1,7 +1,7 @@ { - "version": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "unit": "pypi:dep@1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/python/name-ambiguous-callable/expect.yaml b/test/projectgraph/cases/python/name-ambiguous-callable/expect.yaml new file mode 100644 index 00000000..c3de824a --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-callable/expect.yaml @@ -0,0 +1,24 @@ +source: safedep:hand +description: >- + dispatch calls handle on the parameter handler. 24 classes define a method handle, so the site is over + the cap of 20 and is ambiguous. H0 holds an object of Cb in its class attribute handle. main passes an H0, so the call runs Cb.__call__, which calls os.remove. That code is no + definition of the name handle. So the site keeps no method name and concerns every target, and os.remove + is uncertain and not not-found. +roots: + - key: main.py + kind: manifest + rule: procfile +unresolved: + - caller: main.py//dispatch + site: main.py:4 + reason: ambiguous +results: + - target: os//remove + status: uncertain + call_sites: + - caller: handlers.py//Cb//__call__ + site: handlers.py:5 + reached: false + uncertainties: + - kind: unresolved-call + site: main.py:4 diff --git a/test/projectgraph/cases/python/name-ambiguous-callable/project/Procfile b/test/projectgraph/cases/python/name-ambiguous-callable/project/Procfile new file mode 100644 index 00000000..629b83ad --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-callable/project/Procfile @@ -0,0 +1 @@ +web: python main.py diff --git a/test/projectgraph/cases/python/name-ambiguous-callable/project/handlers.py b/test/projectgraph/cases/python/name-ambiguous-callable/project/handlers.py new file mode 100644 index 00000000..c465d824 --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-callable/project/handlers.py @@ -0,0 +1,105 @@ +import os + +class Cb: + def __call__(self, path): + os.remove(path) + +class H0: + handle = Cb() + +class H1: + def handle(self, text): + return text + +class H2: + def handle(self, text): + return text + +class H3: + def handle(self, text): + return text + +class H4: + def handle(self, text): + return text + +class H5: + def handle(self, text): + return text + +class H6: + def handle(self, text): + return text + +class H7: + def handle(self, text): + return text + +class H8: + def handle(self, text): + return text + +class H9: + def handle(self, text): + return text + +class H10: + def handle(self, text): + return text + +class H11: + def handle(self, text): + return text + +class H12: + def handle(self, text): + return text + +class H13: + def handle(self, text): + return text + +class H14: + def handle(self, text): + return text + +class H15: + def handle(self, text): + return text + +class H16: + def handle(self, text): + return text + +class H17: + def handle(self, text): + return text + +class H18: + def handle(self, text): + return text + +class H19: + def handle(self, text): + return text + +class H20: + def handle(self, text): + return text + +class H21: + def handle(self, text): + return text + +class H22: + def handle(self, text): + return text + +class H23: + def handle(self, text): + return text + +class H24: + def handle(self, text): + return text + diff --git a/test/projectgraph/cases/python/name-ambiguous-callable/project/main.py b/test/projectgraph/cases/python/name-ambiguous-callable/project/main.py new file mode 100644 index 00000000..4455897b --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-callable/project/main.py @@ -0,0 +1,6 @@ +from handlers import H0 + +def dispatch(handler, text): + return handler.handle(text) + +dispatch(H0(), 'a') diff --git a/test/projectgraph/cases/python/name-ambiguous-getattr/expect.yaml b/test/projectgraph/cases/python/name-ambiguous-getattr/expect.yaml new file mode 100644 index 00000000..e5f43d55 --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-getattr/expect.yaml @@ -0,0 +1,24 @@ +source: safedep:hand +description: >- + dispatch calls handle on the parameter handler. 24 classes define a method handle, so the site is over + the cap of 20 and is ambiguous. main passes an H0, which defines no handle. Its __getattr__ gives cleanup, + which calls os.remove. That code is no definition of the name handle. So the site keeps no method name + and concerns every target, and os.remove is uncertain and not not-found. +roots: + - key: main.py + kind: manifest + rule: procfile +unresolved: + - caller: main.py//dispatch + site: main.py:4 + reason: ambiguous +results: + - target: os//remove + status: uncertain + call_sites: + - caller: handlers.py//cleanup + site: handlers.py:4 + reached: false + uncertainties: + - kind: unresolved-call + site: main.py:4 diff --git a/test/projectgraph/cases/python/name-ambiguous-getattr/project/Procfile b/test/projectgraph/cases/python/name-ambiguous-getattr/project/Procfile new file mode 100644 index 00000000..629b83ad --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-getattr/project/Procfile @@ -0,0 +1 @@ +web: python main.py diff --git a/test/projectgraph/cases/python/name-ambiguous-getattr/project/handlers.py b/test/projectgraph/cases/python/name-ambiguous-getattr/project/handlers.py new file mode 100644 index 00000000..496760f4 --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-getattr/project/handlers.py @@ -0,0 +1,105 @@ +import os + +def cleanup(path): + os.remove(path) + +class H0: + def __getattr__(self, name): + return cleanup + +class H1: + def handle(self, text): + return text + +class H2: + def handle(self, text): + return text + +class H3: + def handle(self, text): + return text + +class H4: + def handle(self, text): + return text + +class H5: + def handle(self, text): + return text + +class H6: + def handle(self, text): + return text + +class H7: + def handle(self, text): + return text + +class H8: + def handle(self, text): + return text + +class H9: + def handle(self, text): + return text + +class H10: + def handle(self, text): + return text + +class H11: + def handle(self, text): + return text + +class H12: + def handle(self, text): + return text + +class H13: + def handle(self, text): + return text + +class H14: + def handle(self, text): + return text + +class H15: + def handle(self, text): + return text + +class H16: + def handle(self, text): + return text + +class H17: + def handle(self, text): + return text + +class H18: + def handle(self, text): + return text + +class H19: + def handle(self, text): + return text + +class H20: + def handle(self, text): + return text + +class H21: + def handle(self, text): + return text + +class H22: + def handle(self, text): + return text + +class H23: + def handle(self, text): + return text + +class H24: + def handle(self, text): + return text + diff --git a/test/projectgraph/cases/python/name-ambiguous-getattr/project/main.py b/test/projectgraph/cases/python/name-ambiguous-getattr/project/main.py new file mode 100644 index 00000000..4455897b --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-getattr/project/main.py @@ -0,0 +1,6 @@ +from handlers import H0 + +def dispatch(handler, text): + return handler.handle(text) + +dispatch(H0(), 'a') diff --git a/test/projectgraph/cases/python/name-ambiguous-module/expect.yaml b/test/projectgraph/cases/python/name-ambiguous-module/expect.yaml new file mode 100644 index 00000000..b7d69dee --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-module/expect.yaml @@ -0,0 +1,20 @@ +source: safedep:hand +description: >- + dispatch calls handle on the parameter handler. 24 classes define a method handle, so the site is over + the cap of 20 and is ambiguous. main passes the module plug, which binds os.remove to the name handle by + an import. No definition of the name handle runs. So the site keeps no method name and concerns every + target, and os.remove is uncertain and not no-such-target. +roots: + - key: main.py + kind: manifest + rule: procfile +unresolved: + - caller: main.py//dispatch + site: main.py:4 + reason: ambiguous +results: + - target: os//remove + status: uncertain + uncertainties: + - kind: unresolved-call + site: main.py:4 diff --git a/test/projectgraph/cases/python/name-ambiguous-module/project/Procfile b/test/projectgraph/cases/python/name-ambiguous-module/project/Procfile new file mode 100644 index 00000000..629b83ad --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-module/project/Procfile @@ -0,0 +1 @@ +web: python main.py diff --git a/test/projectgraph/cases/python/name-ambiguous-module/project/handlers.py b/test/projectgraph/cases/python/name-ambiguous-module/project/handlers.py new file mode 100644 index 00000000..4ecf9269 --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-module/project/handlers.py @@ -0,0 +1,96 @@ +class H1: + def handle(self, text): + return text + +class H2: + def handle(self, text): + return text + +class H3: + def handle(self, text): + return text + +class H4: + def handle(self, text): + return text + +class H5: + def handle(self, text): + return text + +class H6: + def handle(self, text): + return text + +class H7: + def handle(self, text): + return text + +class H8: + def handle(self, text): + return text + +class H9: + def handle(self, text): + return text + +class H10: + def handle(self, text): + return text + +class H11: + def handle(self, text): + return text + +class H12: + def handle(self, text): + return text + +class H13: + def handle(self, text): + return text + +class H14: + def handle(self, text): + return text + +class H15: + def handle(self, text): + return text + +class H16: + def handle(self, text): + return text + +class H17: + def handle(self, text): + return text + +class H18: + def handle(self, text): + return text + +class H19: + def handle(self, text): + return text + +class H20: + def handle(self, text): + return text + +class H21: + def handle(self, text): + return text + +class H22: + def handle(self, text): + return text + +class H23: + def handle(self, text): + return text + +class H24: + def handle(self, text): + return text + diff --git a/test/projectgraph/cases/python/name-ambiguous-module/project/main.py b/test/projectgraph/cases/python/name-ambiguous-module/project/main.py new file mode 100644 index 00000000..d13a1635 --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-module/project/main.py @@ -0,0 +1,6 @@ +import plug + +def dispatch(handler, text): + return handler.handle(text) + +dispatch(plug, 'a') diff --git a/test/projectgraph/cases/python/name-ambiguous-module/project/plug.py b/test/projectgraph/cases/python/name-ambiguous-module/project/plug.py new file mode 100644 index 00000000..0955f9e4 --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-module/project/plug.py @@ -0,0 +1 @@ +from os import remove as handle diff --git a/test/projectgraph/cases/python/name-ambiguous-nounit/expect.yaml b/test/projectgraph/cases/python/name-ambiguous-nounit/expect.yaml new file mode 100644 index 00000000..76d73b13 --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-nounit/expect.yaml @@ -0,0 +1,29 @@ +source: safedep:hand +description: >- + dispatch calls handle on the parameter handler. 24 classes define a method handle, so the site is over + the cap of 20 and is ambiguous. main passes an extlib.Handler, and no unit gives extlib. Its handle can + call any code of extlib, as extlib.vuln, which only the uncalled unused calls in the project. So the site + keeps no method name and concerns every target, and extlib.vuln is uncertain and not not-found. +roots: + - key: main.py + kind: manifest + rule: procfile +unresolved: + - caller: main.py//dispatch + site: main.py:5 + reason: ambiguous +results: + - target: extlib//vuln + status: uncertain + call_sites: + - caller: handlers.py//unused + site: handlers.py:4 + reached: false + uncertainties: + - kind: unresolved-call + site: main.py:5 + - target: extlib//Handler//handle + status: uncertain + uncertainties: + - kind: unresolved-call + site: main.py:5 diff --git a/test/projectgraph/cases/python/name-ambiguous-nounit/project/Procfile b/test/projectgraph/cases/python/name-ambiguous-nounit/project/Procfile new file mode 100644 index 00000000..629b83ad --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-nounit/project/Procfile @@ -0,0 +1 @@ +web: python main.py diff --git a/test/projectgraph/cases/python/name-ambiguous-nounit/project/handlers.py b/test/projectgraph/cases/python/name-ambiguous-nounit/project/handlers.py new file mode 100644 index 00000000..65b8ec72 --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-nounit/project/handlers.py @@ -0,0 +1,101 @@ +import extlib + +def unused(): + extlib.vuln() + +class H1: + def handle(self, text): + return text + +class H2: + def handle(self, text): + return text + +class H3: + def handle(self, text): + return text + +class H4: + def handle(self, text): + return text + +class H5: + def handle(self, text): + return text + +class H6: + def handle(self, text): + return text + +class H7: + def handle(self, text): + return text + +class H8: + def handle(self, text): + return text + +class H9: + def handle(self, text): + return text + +class H10: + def handle(self, text): + return text + +class H11: + def handle(self, text): + return text + +class H12: + def handle(self, text): + return text + +class H13: + def handle(self, text): + return text + +class H14: + def handle(self, text): + return text + +class H15: + def handle(self, text): + return text + +class H16: + def handle(self, text): + return text + +class H17: + def handle(self, text): + return text + +class H18: + def handle(self, text): + return text + +class H19: + def handle(self, text): + return text + +class H20: + def handle(self, text): + return text + +class H21: + def handle(self, text): + return text + +class H22: + def handle(self, text): + return text + +class H23: + def handle(self, text): + return text + +class H24: + def handle(self, text): + return text + diff --git a/test/projectgraph/cases/python/name-ambiguous-nounit/project/main.py b/test/projectgraph/cases/python/name-ambiguous-nounit/project/main.py new file mode 100644 index 00000000..50203479 --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-nounit/project/main.py @@ -0,0 +1,7 @@ +import extlib +import handlers + +def dispatch(handler, text): + return handler.handle(text) + +dispatch(extlib.Handler(), 'a') diff --git a/test/projectgraph/cases/python/name-ambiguous-record/expect.yaml b/test/projectgraph/cases/python/name-ambiguous-record/expect.yaml new file mode 100644 index 00000000..664d3254 --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-record/expect.yaml @@ -0,0 +1,38 @@ +source: safedep:hand +description: >- + dispatch calls handle on the parameter handler. 25 classes define handle, so the site is ambiguous and gets + no name edge. H0.handle calls load on its field codec, whose value the index does not know, so that site is + a record with the method load. main passes the module yaml as the codec, so the program runs yaml.load. No + edge leads from a candidate of handle to yaml.load. A Python call of a name can run code that no definition + of the name holds, so the ambiguous site keeps no method name and concerns every target. yaml.load is + uncertain and not no-such-target, and so is os.remove, which only the uncalled cleanup calls. +roots: + - key: main.py + kind: manifest + rule: procfile +no_edges: + - from: main.py//dispatch + to: handlers.py//H0//handle +unresolved: + - caller: handlers.py//H0//handle + site: handlers.py:9 + reason: no-definition + method: load + - caller: main.py//dispatch + site: main.py:6 + reason: ambiguous +results: + - target: yaml//load + status: uncertain + uncertainties: + - kind: unresolved-call + site: main.py:6 + - target: os//remove + status: uncertain + call_sites: + - caller: handlers.py//cleanup + site: handlers.py:133 + reached: false + uncertainties: + - kind: unresolved-call + site: main.py:6 diff --git a/test/projectgraph/cases/python/name-ambiguous-record/project/Procfile b/test/projectgraph/cases/python/name-ambiguous-record/project/Procfile new file mode 100644 index 00000000..629b83ad --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-record/project/Procfile @@ -0,0 +1 @@ +web: python main.py diff --git a/test/projectgraph/cases/python/name-ambiguous-record/project/handlers.py b/test/projectgraph/cases/python/name-ambiguous-record/project/handlers.py new file mode 100644 index 00000000..8c0ab89a --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-record/project/handlers.py @@ -0,0 +1,133 @@ +import os + + +class H0: + def __init__(self, codec): + self.codec = codec + + def handle(self, text): + return self.codec.load(text) + + +class H1: + def handle(self, text): + return text + + +class H2: + def handle(self, text): + return text + + +class H3: + def handle(self, text): + return text + + +class H4: + def handle(self, text): + return text + + +class H5: + def handle(self, text): + return text + + +class H6: + def handle(self, text): + return text + + +class H7: + def handle(self, text): + return text + + +class H8: + def handle(self, text): + return text + + +class H9: + def handle(self, text): + return text + + +class H10: + def handle(self, text): + return text + + +class H11: + def handle(self, text): + return text + + +class H12: + def handle(self, text): + return text + + +class H13: + def handle(self, text): + return text + + +class H14: + def handle(self, text): + return text + + +class H15: + def handle(self, text): + return text + + +class H16: + def handle(self, text): + return text + + +class H17: + def handle(self, text): + return text + + +class H18: + def handle(self, text): + return text + + +class H19: + def handle(self, text): + return text + + +class H20: + def handle(self, text): + return text + + +class H21: + def handle(self, text): + return text + + +class H22: + def handle(self, text): + return text + + +class H23: + def handle(self, text): + return text + + +class H24: + def handle(self, text): + return text + + +def cleanup(path): + os.remove(path) diff --git a/test/projectgraph/cases/python/name-ambiguous-record/project/main.py b/test/projectgraph/cases/python/name-ambiguous-record/project/main.py new file mode 100644 index 00000000..d306846d --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-record/project/main.py @@ -0,0 +1,9 @@ +import yaml +from handlers import H0 + + +def dispatch(handler, text): + return handler.handle(text) + + +dispatch(H0(yaml), "a: 1") diff --git a/test/projectgraph/cases/python/name-ambiguous/expect.yaml b/test/projectgraph/cases/python/name-ambiguous/expect.yaml index 43f10784..dd9a16fa 100644 --- a/test/projectgraph/cases/python/name-ambiguous/expect.yaml +++ b/test/projectgraph/cases/python/name-ambiguous/expect.yaml @@ -1,5 +1,10 @@ source: safedep:hand -description: dispatch calls handle on the parameter handler, whose value the index does not know. 25 classes define handle. That is over the cap of 20, so the site gets no name edge and is ambiguous. An ambiguous site can call any target, so the reach result for yaml.load is uncertain. +description: >- + dispatch calls handle on the parameter handler, whose value the index does not know. 25 classes define + handle. That is over the cap of 20, so the site gets no name edge and is ambiguous. A Python call of a + name can run code that no definition of the name holds, as a class attribute that holds an object with + __call__. So the site keeps no method name and concerns every target. yaml.load, which H0.handle calls, + and os.remove, which only the uncalled cleanup calls, are uncertain. roots: - key: main.py kind: manifest @@ -22,7 +27,16 @@ results: status: uncertain call_sites: - caller: handlers.py//H0//handle - site: handlers.py:7 + site: handlers.py:8 + reached: false + uncertainties: + - kind: unresolved-call + site: main.py:5 + - target: os//remove + status: uncertain + call_sites: + - caller: handlers.py//cleanup + site: handlers.py:132 reached: false uncertainties: - kind: unresolved-call diff --git a/test/projectgraph/cases/python/name-ambiguous/project/handlers.py b/test/projectgraph/cases/python/name-ambiguous/project/handlers.py index c48b244e..8bd7ab57 100644 --- a/test/projectgraph/cases/python/name-ambiguous/project/handlers.py +++ b/test/projectgraph/cases/python/name-ambiguous/project/handlers.py @@ -1,3 +1,4 @@ +import os import yaml @@ -125,3 +126,7 @@ def handle(self, text): class H24: def handle(self, text): return text + + +def cleanup(path): + os.remove(path) diff --git a/test/projectgraph/cases/python/source-module-load/expect.yaml b/test/projectgraph/cases/python/source-module-load/expect.yaml new file mode 100644 index 00000000..0d4171f7 --- /dev/null +++ b/test/projectgraph/cases/python/source-module-load/expect.yaml @@ -0,0 +1,100 @@ +source: safedep:hand +description: >- + The module top level of handlers.py holds os.system in the list HOOKS, and os.getcwd in the attribute hook + of DEFAULTS. run calls HOOKS[0], and run_hook calls the attribute hook of its parameter. The program runs + the module top level of handlers.py before run or run_hook can run. So a search from run or run_hook + alone enters it, and each target is found through the reference there. Without that step, the call of + o.hook concerns only the targets of the method name hook, and os.getcwd reads as not-found. +roots: + - key: main.py + kind: manifest + rule: procfile +exact_edges: true +edges: + - from: handlers.py + to: os//system + kind: reference + confidence: low + site: handlers.py:9 + - from: handlers.py + to: handlers.py//Options + kind: direct + confidence: high + site: handlers.py:10 + - from: handlers.py + to: os//getcwd + kind: reference + confidence: low + site: handlers.py:10 + - from: handlers.py//Options + to: handlers.py//Options//__init__ + kind: direct + confidence: high + site: handlers.py:5 + - from: handlers.py//run_hook + to: handlers.py//Options + kind: reference + confidence: low + site: handlers.py:17 + - from: main.py + to: handlers.py + kind: import + confidence: high + site: main.py:1 + - from: main.py + to: handlers.py//run + kind: direct + confidence: high + site: main.py:3 + - from: main.py + to: handlers.py//run_hook + kind: direct + confidence: high + site: main.py:4 +unresolved: + - caller: handlers.py//run + site: handlers.py:14 + reason: computed-name + - caller: handlers.py//run_hook + site: handlers.py:18 + reason: unknown-receiver + method: hook +results: + - target: os//system + sources: [handlers.py//run] + sources_only: true + status: found + path: + - key: handlers.py//run + root: query + - key: handlers.py + site: handlers.py + kind: import + confidence: high + - key: os//system + site: handlers.py:9 + kind: reference + confidence: low + call_sites: + - caller: handlers.py + site: handlers.py:9 + reached: true + - target: os//getcwd + sources: [handlers.py//run_hook] + sources_only: true + status: found + path: + - key: handlers.py//run_hook + root: query + - key: handlers.py + site: handlers.py + kind: import + confidence: high + - key: os//getcwd + site: handlers.py:10 + kind: reference + confidence: low + call_sites: + - caller: handlers.py + site: handlers.py:10 + reached: true diff --git a/test/projectgraph/cases/python/source-module-load/project/Procfile b/test/projectgraph/cases/python/source-module-load/project/Procfile new file mode 100644 index 00000000..629b83ad --- /dev/null +++ b/test/projectgraph/cases/python/source-module-load/project/Procfile @@ -0,0 +1 @@ +web: python main.py diff --git a/test/projectgraph/cases/python/source-module-load/project/handlers.py b/test/projectgraph/cases/python/source-module-load/project/handlers.py new file mode 100644 index 00000000..2a089bf1 --- /dev/null +++ b/test/projectgraph/cases/python/source-module-load/project/handlers.py @@ -0,0 +1,18 @@ +import os + + +class Options: + def __init__(self, hook): + self.hook = hook + + +HOOKS = [os.system] +DEFAULTS = Options(os.getcwd) + + +def run(cmd): + HOOKS[0](cmd) + + +def run_hook(o=DEFAULTS): + o.hook() diff --git a/test/projectgraph/cases/python/source-module-load/project/main.py b/test/projectgraph/cases/python/source-module-load/project/main.py new file mode 100644 index 00000000..6a7fe4a8 --- /dev/null +++ b/test/projectgraph/cases/python/source-module-load/project/main.py @@ -0,0 +1,4 @@ +from handlers import run, run_hook + +run("ls") +run_hook() diff --git a/test/projectgraph/cases/python/typed-base-subclass/summaries/pypi_dep_1.0.0.json b/test/projectgraph/cases/python/typed-base-subclass/summaries/pypi_dep_1.0.0.json index 657312ba..33e4eca4 100644 --- a/test/projectgraph/cases/python/typed-base-subclass/summaries/pypi_dep_1.0.0.json +++ b/test/projectgraph/cases/python/typed-base-subclass/summaries/pypi_dep_1.0.0.json @@ -1,7 +1,7 @@ { - "version": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "unit": "pypi:dep@1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/rust/alias-cfg-struct/expect.yaml b/test/projectgraph/cases/rust/alias-cfg-struct/expect.yaml new file mode 100644 index 00000000..0d637b1a --- /dev/null +++ b/test/projectgraph/cases/rust/alias-cfg-struct/expect.yaml @@ -0,0 +1,89 @@ +source: safedep:hand +description: >- + The file declares Client two times. With the feature native, Client is a struct, and its method send calls + std::fs::remove_file. Without the feature, Client is an alias of fallback::Client, whose new calls + std::env::set_var. main calls Client::new, and work calls send on a parameter of the type Client. The index + does not know which cfg the build sets, so the alias binds no type. Each call gets the record + unknown-receiver and a name edge to the method of each declaration. So both targets are found. An index + that lets the alias replace the struct gives only fallback::Client.send, and std::fs::remove_file reads + not-found. An index that lets the struct win gives only the struct's new, and std::env::set_var reads + not-found. +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +edges: + - from: src/main.rs//main + to: src/main.rs//work + kind: direct + confidence: high + site: src/main.rs:36 + - from: src/main.rs//main + to: src/main.rs//Client//new + kind: name + confidence: low + site: src/main.rs:36 + - from: src/main.rs//main + to: src/main.rs//fallback//Client//new + kind: name + confidence: low + site: src/main.rs:36 + - from: src/main.rs//work + to: src/main.rs//Client//send + kind: name + confidence: low + site: src/main.rs:32 + - from: src/main.rs//work + to: src/main.rs//fallback//Client//send + kind: name + confidence: low + site: src/main.rs:32 +unresolved: + - caller: src/main.rs//work + site: src/main.rs:32 + reason: unknown-receiver + method: send + - caller: src/main.rs//main + site: src/main.rs:36 + reason: unknown-receiver + method: new +results: + - target: std//fs//remove_file + status: found + path: + - key: src/main.rs//main + - key: src/main.rs//work + site: src/main.rs:36 + kind: direct + confidence: high + - key: src/main.rs//Client//send + site: src/main.rs:32 + kind: name + confidence: low + - key: std//fs//remove_file + site: src/main.rs:24 + kind: direct + confidence: high + call_sites: + - caller: src/main.rs//Client//send + site: src/main.rs:24 + reached: true + - target: std//env//set_var + status: found + path: + - key: src/main.rs//main + - key: src/main.rs//fallback//Client//new + site: src/main.rs:36 + kind: name + confidence: low + - key: std//env//set_var + site: src/main.rs:6 + kind: direct + confidence: high + call_sites: + - caller: src/main.rs//fallback//Client//new + site: src/main.rs:6 + reached: true diff --git a/test/projectgraph/cases/rust/alias-cfg-struct/project/Cargo.toml b/test/projectgraph/cases/rust/alias-cfg-struct/project/Cargo.toml new file mode 100644 index 00000000..7f3bfbc8 --- /dev/null +++ b/test/projectgraph/cases/rust/alias-cfg-struct/project/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" + +[features] +default = ["native"] +native = [] diff --git a/test/projectgraph/cases/rust/alias-cfg-struct/project/src/main.rs b/test/projectgraph/cases/rust/alias-cfg-struct/project/src/main.rs new file mode 100644 index 00000000..d4dfabfd --- /dev/null +++ b/test/projectgraph/cases/rust/alias-cfg-struct/project/src/main.rs @@ -0,0 +1,37 @@ +mod fallback { + pub struct Client; + + impl Client { + pub fn new() -> Client { + std::env::set_var("MODE", "fallback"); + Client + } + + pub fn send(&self) {} + } +} + +#[cfg(feature = "native")] +pub struct Client; + +#[cfg(feature = "native")] +impl Client { + pub fn new() -> Client { + Client + } + + pub fn send(&self) { + let _ = std::fs::remove_file("x"); + } +} + +#[cfg(not(feature = "native"))] +pub type Client = fallback::Client; + +fn work(c: &Client) { + c.send(); +} + +fn main() { + work(&Client::new()); +} diff --git a/test/projectgraph/cases/rust/alias-cfg-two/expect.yaml b/test/projectgraph/cases/rust/alias-cfg-two/expect.yaml new file mode 100644 index 00000000..60952a0b --- /dev/null +++ b/test/projectgraph/cases/rust/alias-cfg-two/expect.yaml @@ -0,0 +1,87 @@ +source: safedep:hand +description: >- + The file declares the alias Imp two times, as unix::Thing under cfg(unix) and as windows::Thing under + cfg(windows). Only the open and the close of unix::Thing call the standard library. main calls Imp::open, + and work calls close on a parameter of the type Imp. The index does not know which cfg the build sets, so + the alias binds no type. Each call gets the record unknown-receiver and a name edge to the method of each + type. So std::fs::remove_file and std::process::Command::new are found. An index that lets the last alias + win gives only the methods of windows::Thing, and both targets read not-found. +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +edges: + - from: src/main.rs//main + to: src/main.rs//work + kind: direct + confidence: high + site: src/main.rs:40 + - from: src/main.rs//main + to: src/main.rs//unix//Thing//open + kind: name + confidence: low + site: src/main.rs:39 + - from: src/main.rs//main + to: src/main.rs//windows//Thing//open + kind: name + confidence: low + site: src/main.rs:39 + - from: src/main.rs//work + to: src/main.rs//unix//Thing//close + kind: name + confidence: low + site: src/main.rs:35 + - from: src/main.rs//work + to: src/main.rs//windows//Thing//close + kind: name + confidence: low + site: src/main.rs:35 +unresolved: + - caller: src/main.rs//work + site: src/main.rs:35 + reason: unknown-receiver + method: close + - caller: src/main.rs//main + site: src/main.rs:39 + reason: unknown-receiver + method: open +results: + - target: std//fs//remove_file + status: found + path: + - key: src/main.rs//main + - key: src/main.rs//unix//Thing//open + site: src/main.rs:39 + kind: name + confidence: low + - key: std//fs//remove_file + site: src/main.rs:6 + kind: direct + confidence: high + call_sites: + - caller: src/main.rs//unix//Thing//open + site: src/main.rs:6 + reached: true + - target: std//process//Command//new + status: found + path: + - key: src/main.rs//main + - key: src/main.rs//work + site: src/main.rs:40 + kind: direct + confidence: high + - key: src/main.rs//unix//Thing//close + site: src/main.rs:35 + kind: name + confidence: low + - key: std//process//Command//new + site: src/main.rs:11 + kind: direct + confidence: high + call_sites: + - caller: src/main.rs//unix//Thing//close + site: src/main.rs:11 + reached: true diff --git a/test/projectgraph/cases/rust/alias-cfg-two/project/Cargo.toml b/test/projectgraph/cases/rust/alias-cfg-two/project/Cargo.toml new file mode 100644 index 00000000..0d44349a --- /dev/null +++ b/test/projectgraph/cases/rust/alias-cfg-two/project/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/alias-cfg-two/project/src/main.rs b/test/projectgraph/cases/rust/alias-cfg-two/project/src/main.rs new file mode 100644 index 00000000..42c22799 --- /dev/null +++ b/test/projectgraph/cases/rust/alias-cfg-two/project/src/main.rs @@ -0,0 +1,41 @@ +mod unix { + pub struct Thing; + + impl Thing { + pub fn open() -> Thing { + let _ = std::fs::remove_file("x"); + Thing + } + + pub fn close(&self) { + let _ = std::process::Command::new("sync"); + } + } +} + +mod windows { + pub struct Thing; + + impl Thing { + pub fn open() -> Thing { + Thing + } + + pub fn close(&self) {} + } +} + +#[cfg(unix)] +type Imp = unix::Thing; + +#[cfg(windows)] +type Imp = windows::Thing; + +fn work(t: &Imp) { + t.close(); +} + +fn main() { + let t = Imp::open(); + work(&t); +} diff --git a/test/projectgraph/cases/rust/crate-bindings/expect.yaml b/test/projectgraph/cases/rust/crate-bindings/expect.yaml new file mode 100644 index 00000000..05fdd1ff --- /dev/null +++ b/test/projectgraph/cases/rust/crate-bindings/expect.yaml @@ -0,0 +1,220 @@ +source: safedep:hand +description: >- + The crate kit is the unit cargo:kit@1.0.0, and no unit gives the crate aes. src/secure/mod.rs binds kit with extern + crate, so crate::secure::kit::flush() in src/secure/aes.rs calls kit//flush. src/main.rs binds kit as ffi with + pub use, and its test module binds ffi again with use crate::ffi. So ffi::reset() after use super::ffi and + crate::ffi::Store::open() in src/store.rs call kit//reset and kit//Store//open. src/defs.rs names kit::Store as the + type alias Handle, so Handle::open() calls kit//Store//open, and the declared type Handle of h gives + kit//Store//save. The alias binds the key kit//Store and makes no node of it, so no type target turns from + no-such-target into not-found. src/secure/aes.rs binds aes with extern crate, and make imports self::aes::Cipher. + The file has the name of the crate, and the crate still names no module of the project. No unit gives aes, so the + use is a no-dependency-source record with the import prefix aes, and the extern crate has no record. A target of + aes is uncertain, and kit//unused, which no code calls, stays not-found. Cipher::new() calls the external key + aes//Cipher//new, as a call of aes::Cipher::new() does. +units: + vendor/kit: cargo:kit@1.0.0 +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +exact_edges: true +edges: + - from: cargo:kit@1.0.0//src/lib.rs//Store//open + to: cargo:kit@1.0.0//src/lib.rs//Store + kind: direct + confidence: high + site: cargo:kit@1.0.0//src/lib.rs:7 + - from: cargo:kit@1.0.0//src/lib.rs//Store//save + to: cargo:kit@1.0.0//src/lib.rs//Store + kind: direct + confidence: high + site: cargo:kit@1.0.0//src/lib.rs:10 + - from: cargo:kit@1.0.0//src/lib.rs//Store//save + to: std//fs//write + kind: direct + confidence: high + site: cargo:kit@1.0.0//src/lib.rs:11 + - from: cargo:kit@1.0.0//src/lib.rs//Store//save + to: std//fs//write//unwrap + kind: direct + confidence: high + site: cargo:kit@1.0.0//src/lib.rs:11 + - from: kit//Store//open + to: cargo:kit@1.0.0//src/lib.rs//Store//open + kind: package + confidence: high + site: cargo:kit@1.0.0//src/lib.rs:6 + - from: kit//Store//save + to: cargo:kit@1.0.0//src/lib.rs//Store//save + kind: package + confidence: high + site: cargo:kit@1.0.0//src/lib.rs:10 + - from: kit//flush + to: cargo:kit@1.0.0//src/lib.rs//flush + kind: package + confidence: high + site: cargo:kit@1.0.0//src/lib.rs:15 + - from: kit//reset + to: cargo:kit@1.0.0//src/lib.rs//reset + kind: package + confidence: high + site: cargo:kit@1.0.0//src/lib.rs:17 + - from: src/defs.rs//keep + to: kit//Store//save + kind: direct + confidence: high + site: src/defs.rs:8 + - from: src/defs.rs//run + to: kit//Store//open + kind: direct + confidence: high + site: src/defs.rs:4 + - from: src/defs.rs//run + to: src/defs.rs//keep + kind: direct + confidence: high + site: src/defs.rs:4 + - from: src/main.rs//main + to: src/defs.rs//run + kind: direct + confidence: high + site: src/main.rs:10 + - from: src/main.rs//main + to: src/secure/aes.rs//make + kind: direct + confidence: high + site: src/main.rs:8 + - from: src/main.rs//main + to: src/store.rs//sync + kind: direct + confidence: high + site: src/main.rs:9 + - from: src/secure/aes.rs//make + to: aes//Cipher//new + kind: direct + confidence: high + site: src/secure/aes.rs:7 + - from: src/secure/aes.rs//make + to: kit//flush + kind: direct + confidence: high + site: src/secure/aes.rs:6 + - from: src/store.rs//sync + to: kit//Store//open + kind: direct + confidence: high + site: src/store.rs:5 + - from: src/store.rs//sync + to: kit//reset + kind: direct + confidence: high + site: src/store.rs:4 +unresolved: + - caller: cargo:kit@1.0.0//src/lib.rs//Store//save + site: cargo:kit@1.0.0//src/lib.rs:11 + reason: unknown-receiver + method: unwrap + - caller: src/secure/aes.rs//make + site: src/secure/aes.rs:4 + reason: no-dependency-source +results: + - target: kit//flush + status: found + path: + - key: src/main.rs//main + - key: src/secure/aes.rs//make + site: src/main.rs:8 + kind: direct + confidence: high + - key: kit//flush + site: src/secure/aes.rs:6 + kind: direct + confidence: high + call_sites: + - caller: src/secure/aes.rs//make + site: src/secure/aes.rs:6 + reached: true + - target: kit//reset + status: found + path: + - key: src/main.rs//main + - key: src/store.rs//sync + site: src/main.rs:9 + kind: direct + confidence: high + - key: kit//reset + site: src/store.rs:4 + kind: direct + confidence: high + call_sites: + - caller: src/store.rs//sync + site: src/store.rs:4 + reached: true + - target: kit//Store//open + status: found + path: + - key: src/main.rs//main + - key: src/store.rs//sync + site: src/main.rs:9 + kind: direct + confidence: high + - key: kit//Store//open + site: src/store.rs:5 + kind: direct + confidence: high + call_sites: + - caller: src/defs.rs//run + site: src/defs.rs:4 + reached: true + - caller: src/store.rs//sync + site: src/store.rs:5 + reached: true + - target: kit//Store//save + status: found + path: + - key: src/main.rs//main + - key: src/defs.rs//run + site: src/main.rs:10 + kind: direct + confidence: high + - key: src/defs.rs//keep + site: src/defs.rs:4 + kind: direct + confidence: high + - key: kit//Store//save + site: src/defs.rs:8 + kind: direct + confidence: high + call_sites: + - caller: src/defs.rs//keep + site: src/defs.rs:8 + reached: true + - target: aes//Cipher//new + status: found + path: + - key: src/main.rs//main + - key: src/secure/aes.rs//make + site: src/main.rs:8 + kind: direct + confidence: high + - key: aes//Cipher//new + site: src/secure/aes.rs:7 + kind: direct + confidence: high + call_sites: + - caller: src/secure/aes.rs//make + site: src/secure/aes.rs:7 + reached: true + - target: aes//Cipher//encrypt + status: uncertain + uncertainties: + - kind: unresolved-call + site: src/secure/aes.rs:4 + - target: kit//unused + status: not-found + set_aside: + - kind: unresolved-call + site: src/secure/aes.rs:4 diff --git a/test/projectgraph/cases/rust/crate-bindings/project/Cargo.toml b/test/projectgraph/cases/rust/crate-bindings/project/Cargo.toml new file mode 100644 index 00000000..2745c74d --- /dev/null +++ b/test/projectgraph/cases/rust/crate-bindings/project/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" + +[dependencies] +kit = "1.0.0" +aes = "0.8.0" diff --git a/test/projectgraph/cases/rust/crate-bindings/project/src/defs.rs b/test/projectgraph/cases/rust/crate-bindings/project/src/defs.rs new file mode 100644 index 00000000..d54f3f31 --- /dev/null +++ b/test/projectgraph/cases/rust/crate-bindings/project/src/defs.rs @@ -0,0 +1,9 @@ +pub type Handle = kit::Store; + +pub fn run() { + keep(&Handle::open()); +} + +fn keep(h: &Handle) { + h.save(); +} diff --git a/test/projectgraph/cases/rust/crate-bindings/project/src/main.rs b/test/projectgraph/cases/rust/crate-bindings/project/src/main.rs new file mode 100644 index 00000000..aff1357b --- /dev/null +++ b/test/projectgraph/cases/rust/crate-bindings/project/src/main.rs @@ -0,0 +1,16 @@ +mod defs; +mod secure; +mod store; + +pub use kit as ffi; + +fn main() { + secure::aes::make(); + store::sync(); + defs::run(); +} + +#[cfg(test)] +mod tests { + use crate::ffi; +} diff --git a/test/projectgraph/cases/rust/crate-bindings/project/src/secure/aes.rs b/test/projectgraph/cases/rust/crate-bindings/project/src/secure/aes.rs new file mode 100644 index 00000000..83dccdef --- /dev/null +++ b/test/projectgraph/cases/rust/crate-bindings/project/src/secure/aes.rs @@ -0,0 +1,8 @@ +extern crate aes; + +pub fn make() { + use self::aes::Cipher; + + crate::secure::kit::flush(); + Cipher::new(); +} diff --git a/test/projectgraph/cases/rust/crate-bindings/project/src/secure/mod.rs b/test/projectgraph/cases/rust/crate-bindings/project/src/secure/mod.rs new file mode 100644 index 00000000..ff63e5a8 --- /dev/null +++ b/test/projectgraph/cases/rust/crate-bindings/project/src/secure/mod.rs @@ -0,0 +1,3 @@ +extern crate kit; + +pub mod aes; diff --git a/test/projectgraph/cases/rust/crate-bindings/project/src/store.rs b/test/projectgraph/cases/rust/crate-bindings/project/src/store.rs new file mode 100644 index 00000000..a6a8b414 --- /dev/null +++ b/test/projectgraph/cases/rust/crate-bindings/project/src/store.rs @@ -0,0 +1,6 @@ +use super::ffi; + +pub fn sync() { + ffi::reset(); + crate::ffi::Store::open(); +} diff --git a/test/projectgraph/cases/rust/crate-bindings/project/vendor/kit/Cargo.toml b/test/projectgraph/cases/rust/crate-bindings/project/vendor/kit/Cargo.toml new file mode 100644 index 00000000..58da5f79 --- /dev/null +++ b/test/projectgraph/cases/rust/crate-bindings/project/vendor/kit/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "kit" +version = "1.0.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/crate-bindings/project/vendor/kit/src/lib.rs b/test/projectgraph/cases/rust/crate-bindings/project/vendor/kit/src/lib.rs new file mode 100644 index 00000000..d6cad052 --- /dev/null +++ b/test/projectgraph/cases/rust/crate-bindings/project/vendor/kit/src/lib.rs @@ -0,0 +1,19 @@ +pub struct Store { + path: String, +} + +impl Store { + pub fn open() -> Store { + Store { path: String::new() } + } + + pub fn save(&self) { + std::fs::write(&self.path, "data").unwrap(); + } +} + +pub fn flush() {} + +pub fn reset() {} + +pub fn unused() {} diff --git a/test/projectgraph/cases/rust/crate-bindings/summaries/cargo_kit_1.0.0.json b/test/projectgraph/cases/rust/crate-bindings/summaries/cargo_kit_1.0.0.json new file mode 100644 index 00000000..49d0517f --- /dev/null +++ b/test/projectgraph/cases/rust/crate-bindings/summaries/cargo_kit_1.0.0.json @@ -0,0 +1,67 @@ +{ + "version": "3", + "index_version": "16", + "link_version": "18", + "unit": "cargo:kit@1.0.0", + "symbols": [ + { + "key": "kit//Store", + "definitions": [ + "cargo:kit@1.0.0//src/lib.rs//Store" + ], + "reaches": [] + }, + { + "key": "kit//Store//open", + "definitions": [ + "cargo:kit@1.0.0//src/lib.rs//Store//open" + ], + "reaches": [] + }, + { + "key": "kit//Store//save", + "definitions": [ + "cargo:kit@1.0.0//src/lib.rs//Store//save" + ], + "reaches": [], + "uncertainties": [ + 0 + ] + }, + { + "key": "kit//flush", + "definitions": [ + "cargo:kit@1.0.0//src/lib.rs//flush" + ], + "reaches": [] + }, + { + "key": "kit//reset", + "definitions": [ + "cargo:kit@1.0.0//src/lib.rs//reset" + ], + "reaches": [] + }, + { + "key": "kit//unused", + "definitions": [ + "cargo:kit@1.0.0//src/lib.rs//unused" + ], + "reaches": [] + } + ], + "targets": [], + "uncertainties": [ + { + "kind": "unresolved-call", + "key": "cargo:kit@1.0.0//src/lib.rs//Store//save", + "site": { + "path": "cargo:kit@1.0.0//src/lib.rs", + "line": 11, + "column": 9 + }, + "text": "std::fs::write(\u0026self.path, \"data\").unwrap (unknown-receiver)", + "method": "unwrap" + } + ] +} diff --git a/test/projectgraph/cases/rust/dependency-crate/summaries/cargo_kit_1.0.0.json b/test/projectgraph/cases/rust/dependency-crate/summaries/cargo_kit_1.0.0.json index 4991c200..0fc6f041 100644 --- a/test/projectgraph/cases/rust/dependency-crate/summaries/cargo_kit_1.0.0.json +++ b/test/projectgraph/cases/rust/dependency-crate/summaries/cargo_kit_1.0.0.json @@ -1,7 +1,7 @@ { - "version": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "unit": "cargo:kit@1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/rust/dependency-internal-call/summaries/cargo_kit_1.0.0.json b/test/projectgraph/cases/rust/dependency-internal-call/summaries/cargo_kit_1.0.0.json index e43ee48b..8308d1c4 100644 --- a/test/projectgraph/cases/rust/dependency-internal-call/summaries/cargo_kit_1.0.0.json +++ b/test/projectgraph/cases/rust/dependency-internal-call/summaries/cargo_kit_1.0.0.json @@ -1,7 +1,7 @@ { - "version": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "unit": "cargo:kit@1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/rust/dependency-renamed/summaries/cargo_kit_1.0.0.json b/test/projectgraph/cases/rust/dependency-renamed/summaries/cargo_kit_1.0.0.json index e43ee48b..8308d1c4 100644 --- a/test/projectgraph/cases/rust/dependency-renamed/summaries/cargo_kit_1.0.0.json +++ b/test/projectgraph/cases/rust/dependency-renamed/summaries/cargo_kit_1.0.0.json @@ -1,7 +1,7 @@ { - "version": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "unit": "cargo:kit@1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/rust/glob-use-crate-module/expect.yaml b/test/projectgraph/cases/rust/glob-use-crate-module/expect.yaml new file mode 100644 index 00000000..de354513 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-crate-module/expect.yaml @@ -0,0 +1,43 @@ +source: safedep:hand +description: >- + main.rs has use crate::deep::*, which brings the module helpers of src/deep/mod.rs into the root, and main + calls helpers::cleanup. cleanup calls std::fs::remove_file, so std::fs::remove_file is found. The root + declares no module helpers. A link step that reads the name as another crate gives the edge to the key + helpers//cleanup with no record, and std::fs::remove_file reads not-found. +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +exact_edges: true +edges: + - from: src/main.rs//main + to: src/deep/helpers.rs//cleanup + kind: direct + confidence: high + site: src/main.rs:5 + - from: src/deep/helpers.rs//cleanup + to: std//fs//remove_file + kind: direct + confidence: high + site: src/deep/helpers.rs:2 +unresolved: [] +results: + - target: std//fs//remove_file + status: found + path: + - key: src/main.rs//main + - key: src/deep/helpers.rs//cleanup + site: src/main.rs:5 + kind: direct + confidence: high + - key: std//fs//remove_file + site: src/deep/helpers.rs:2 + kind: direct + confidence: high + call_sites: + - caller: src/deep/helpers.rs//cleanup + site: src/deep/helpers.rs:2 + reached: true diff --git a/test/projectgraph/cases/rust/glob-use-crate-module/project/Cargo.toml b/test/projectgraph/cases/rust/glob-use-crate-module/project/Cargo.toml new file mode 100644 index 00000000..0d44349a --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-crate-module/project/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/glob-use-crate-module/project/src/deep/helpers.rs b/test/projectgraph/cases/rust/glob-use-crate-module/project/src/deep/helpers.rs new file mode 100644 index 00000000..af9dfae2 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-crate-module/project/src/deep/helpers.rs @@ -0,0 +1,3 @@ +pub fn cleanup(p: &str) { + let _ = std::fs::remove_file(p); +} diff --git a/test/projectgraph/cases/rust/glob-use-crate-module/project/src/deep/mod.rs b/test/projectgraph/cases/rust/glob-use-crate-module/project/src/deep/mod.rs new file mode 100644 index 00000000..1630fabc --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-crate-module/project/src/deep/mod.rs @@ -0,0 +1 @@ +pub mod helpers; diff --git a/test/projectgraph/cases/rust/glob-use-crate-module/project/src/main.rs b/test/projectgraph/cases/rust/glob-use-crate-module/project/src/main.rs new file mode 100644 index 00000000..cb6298ba --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-crate-module/project/src/main.rs @@ -0,0 +1,6 @@ +mod deep; +use crate::deep::*; + +fn main() { + helpers::cleanup("x"); +} diff --git a/test/projectgraph/cases/rust/glob-use-inline-module/expect.yaml b/test/projectgraph/cases/rust/glob-use-inline-module/expect.yaml new file mode 100644 index 00000000..8aaf7ae7 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-inline-module/expect.yaml @@ -0,0 +1,53 @@ +source: safedep:hand +description: >- + The root declares the module log, and the crate depends on the crate log. worker.rs calls log::info at its + top level. log is no child module of worker, and no use of the module worker binds it, so log::info names + the crate log. The inline module tests has use super::*, which brings the names of worker into tests, and + not the names of the root into worker. A link step that reads that glob use from the module worker gives + the edge to src/log.rs//info, and the target log//info reads not-found. A path of the 2015 edition can name + the module log of the root as log, so the use of log at src/worker.rs:2 keeps its record. +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main + - key: src/worker.rs//tests//runs + kind: test + rule: rust.test.attribute +edges: + - from: src/main.rs//main + to: src/worker.rs//go + kind: direct + confidence: high + site: src/main.rs:5 + - from: src/worker.rs//go + to: log//info + kind: direct + confidence: high + site: src/worker.rs:2 +no_edges: + - from: src/worker.rs//go + to: src/log.rs//info +unresolved: + - caller: src/worker.rs//go + site: src/worker.rs:2 + reason: no-definition +results: + - target: log//info + status: found + path: + - key: src/main.rs//main + - key: src/worker.rs//go + site: src/main.rs:5 + kind: direct + confidence: high + - key: log//info + site: src/worker.rs:2 + kind: direct + confidence: high + call_sites: + - caller: src/worker.rs//go + site: src/worker.rs:2 + reached: true diff --git a/test/projectgraph/cases/rust/glob-use-inline-module/project/Cargo.toml b/test/projectgraph/cases/rust/glob-use-inline-module/project/Cargo.toml new file mode 100644 index 00000000..51980c45 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-inline-module/project/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" + +[dependencies] +log = "0.4" diff --git a/test/projectgraph/cases/rust/glob-use-inline-module/project/src/log.rs b/test/projectgraph/cases/rust/glob-use-inline-module/project/src/log.rs new file mode 100644 index 00000000..bfec7c60 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-inline-module/project/src/log.rs @@ -0,0 +1,3 @@ +pub fn info(m: &str) { + let _ = m; +} diff --git a/test/projectgraph/cases/rust/glob-use-inline-module/project/src/main.rs b/test/projectgraph/cases/rust/glob-use-inline-module/project/src/main.rs new file mode 100644 index 00000000..5a7ee4d5 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-inline-module/project/src/main.rs @@ -0,0 +1,6 @@ +mod log; +mod worker; + +fn main() { + worker::go(); +} diff --git a/test/projectgraph/cases/rust/glob-use-inline-module/project/src/worker.rs b/test/projectgraph/cases/rust/glob-use-inline-module/project/src/worker.rs new file mode 100644 index 00000000..c33ec77c --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-inline-module/project/src/worker.rs @@ -0,0 +1,13 @@ +pub fn go() { + log::info("x"); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn runs() { + go(); + } +} diff --git a/test/projectgraph/cases/rust/glob-use-module/expect.yaml b/test/projectgraph/cases/rust/glob-use-module/expect.yaml new file mode 100644 index 00000000..4d09432f --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-module/expect.yaml @@ -0,0 +1,53 @@ +source: safedep:hand +description: >- + The module deep declares the modules helpers and worker. worker.rs has use super::*, which brings the + module helpers into scope, and go calls helpers::cleanup. cleanup calls std::fs::remove_file, so + std::fs::remove_file is found. The name helpers is no child module of worker and no module of the root. + A link step that reads such a name as another crate gives the edge to the key helpers//cleanup with no + record, and std::fs::remove_file reads not-found. Rust resolves a glob use exactly, so the edge is direct. +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +exact_edges: true +edges: + - from: src/main.rs//main + to: src/deep/worker.rs//go + kind: direct + confidence: high + site: src/main.rs:4 + - from: src/deep/worker.rs//go + to: src/deep/helpers.rs//cleanup + kind: direct + confidence: high + site: src/deep/worker.rs:4 + - from: src/deep/helpers.rs//cleanup + to: std//fs//remove_file + kind: direct + confidence: high + site: src/deep/helpers.rs:2 +unresolved: [] +results: + - target: std//fs//remove_file + status: found + path: + - key: src/main.rs//main + - key: src/deep/worker.rs//go + site: src/main.rs:4 + kind: direct + confidence: high + - key: src/deep/helpers.rs//cleanup + site: src/deep/worker.rs:4 + kind: direct + confidence: high + - key: std//fs//remove_file + site: src/deep/helpers.rs:2 + kind: direct + confidence: high + call_sites: + - caller: src/deep/helpers.rs//cleanup + site: src/deep/helpers.rs:2 + reached: true diff --git a/test/projectgraph/cases/rust/glob-use-module/project/Cargo.toml b/test/projectgraph/cases/rust/glob-use-module/project/Cargo.toml new file mode 100644 index 00000000..0d44349a --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-module/project/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/glob-use-module/project/src/deep/helpers.rs b/test/projectgraph/cases/rust/glob-use-module/project/src/deep/helpers.rs new file mode 100644 index 00000000..af9dfae2 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-module/project/src/deep/helpers.rs @@ -0,0 +1,3 @@ +pub fn cleanup(p: &str) { + let _ = std::fs::remove_file(p); +} diff --git a/test/projectgraph/cases/rust/glob-use-module/project/src/deep/mod.rs b/test/projectgraph/cases/rust/glob-use-module/project/src/deep/mod.rs new file mode 100644 index 00000000..ee3caa76 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-module/project/src/deep/mod.rs @@ -0,0 +1,2 @@ +pub mod helpers; +pub mod worker; diff --git a/test/projectgraph/cases/rust/glob-use-module/project/src/deep/worker.rs b/test/projectgraph/cases/rust/glob-use-module/project/src/deep/worker.rs new file mode 100644 index 00000000..bba92cff --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-module/project/src/deep/worker.rs @@ -0,0 +1,5 @@ +use super::*; + +pub fn go() { + helpers::cleanup("x"); +} diff --git a/test/projectgraph/cases/rust/glob-use-module/project/src/main.rs b/test/projectgraph/cases/rust/glob-use-module/project/src/main.rs new file mode 100644 index 00000000..2ab48314 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-module/project/src/main.rs @@ -0,0 +1,5 @@ +mod deep; + +fn main() { + deep::worker::go(); +} diff --git a/test/projectgraph/cases/rust/glob-use/expect.yaml b/test/projectgraph/cases/rust/glob-use/expect.yaml new file mode 100644 index 00000000..843d7711 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use/expect.yaml @@ -0,0 +1,29 @@ +source: safedep:hand +description: >- + The case of the vet reach corpus rust/glob-use. main brings in every item of the module dep::html with a glob use + and calls render. The code of the crate dep is the unit cargo:dep@1.2.0. The link step does not resolve a name + that a glob use of a module of a unit brings in, so the call stays a record of the method render. The record makes + render uncertain, and not not-found. No record names escape, so escape is not-found. +units: + vendor/dep: cargo:dep@1.2.0 +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +exact_edges: true +unresolved: + - caller: src/main.rs//main + site: src/main.rs:4 + reason: unknown-receiver + method: render +results: + - target: dep//html//render + status: uncertain + uncertainties: + - kind: unresolved-call + site: src/main.rs:4 + - target: dep//html//escape + status: not-found diff --git a/test/projectgraph/cases/rust/glob-use/project/Cargo.toml b/test/projectgraph/cases/rust/glob-use/project/Cargo.toml new file mode 100644 index 00000000..56e246be --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use/project/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" + +[dependencies] +dep = "1.2.0" diff --git a/test/projectgraph/cases/rust/glob-use/project/src/main.rs b/test/projectgraph/cases/rust/glob-use/project/src/main.rs new file mode 100644 index 00000000..e648a30d --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use/project/src/main.rs @@ -0,0 +1,5 @@ +use dep::html::*; + +fn main() { + render(); +} diff --git a/test/projectgraph/cases/rust/glob-use/project/vendor/dep/Cargo.toml b/test/projectgraph/cases/rust/glob-use/project/vendor/dep/Cargo.toml new file mode 100644 index 00000000..5352efc0 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use/project/vendor/dep/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "dep" +version = "1.2.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/glob-use/project/vendor/dep/src/html.rs b/test/projectgraph/cases/rust/glob-use/project/vendor/dep/src/html.rs new file mode 100644 index 00000000..38c45e42 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use/project/vendor/dep/src/html.rs @@ -0,0 +1,3 @@ +pub fn render() {} + +pub fn escape() {} diff --git a/test/projectgraph/cases/rust/glob-use/project/vendor/dep/src/lib.rs b/test/projectgraph/cases/rust/glob-use/project/vendor/dep/src/lib.rs new file mode 100644 index 00000000..3d4613dd --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use/project/vendor/dep/src/lib.rs @@ -0,0 +1 @@ +pub mod html; diff --git a/test/projectgraph/cases/rust/glob-use/summaries/cargo_dep_1.2.0.json b/test/projectgraph/cases/rust/glob-use/summaries/cargo_dep_1.2.0.json new file mode 100644 index 00000000..a88279ec --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use/summaries/cargo_dep_1.2.0.json @@ -0,0 +1,23 @@ +{ + "version": "3", + "index_version": "16", + "link_version": "18", + "unit": "cargo:dep@1.2.0", + "symbols": [ + { + "key": "dep//html//escape", + "definitions": [ + "cargo:dep@1.2.0//src/html.rs//escape" + ], + "reaches": [] + }, + { + "key": "dep//html//render", + "definitions": [ + "cargo:dep@1.2.0//src/html.rs//render" + ], + "reaches": [] + } + ], + "targets": [] +} diff --git a/test/projectgraph/cases/rust/into-ambiguous/expect.yaml b/test/projectgraph/cases/rust/into-ambiguous/expect.yaml new file mode 100644 index 00000000..be7c4d21 --- /dev/null +++ b/test/projectgraph/cases/rust/into-ambiguous/expect.yaml @@ -0,0 +1,36 @@ +source: safedep:hand +description: >- + dispatch calls into on an element of a vector of a generic type, which the link step does not know. A call + of into runs the from of an impl of From, so the method from gives the candidates too. 22 types implement + From. That is over the cap of 20, so the site gets no name edge and is ambiguous, and it keeps the + method name into. The from of D0 calls std::env::var. The record concerns what a method of the name or of + an alias of the name reaches, so std::env::var is uncertain and not not-found. std::fs::remove_file, which + only the uncalled cleanup calls, is not found. +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +unresolved: + - caller: src/main.rs//dispatch + site: src/main.rs:10 + reason: ambiguous + method: into +results: + - target: std//env//var + status: uncertain + call_sites: + - caller: src/convert.rs//D0//from + site: src/convert.rs:7 + reached: false + uncertainties: + - kind: unresolved-call + site: src/main.rs:10 + - target: std//fs//remove_file + status: not-found + call_sites: + - caller: src/main.rs//cleanup + site: src/main.rs:4 + reached: false diff --git a/test/projectgraph/cases/rust/into-ambiguous/project/Cargo.toml b/test/projectgraph/cases/rust/into-ambiguous/project/Cargo.toml new file mode 100644 index 00000000..0d44349a --- /dev/null +++ b/test/projectgraph/cases/rust/into-ambiguous/project/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/into-ambiguous/project/src/convert.rs b/test/projectgraph/cases/rust/into-ambiguous/project/src/convert.rs new file mode 100644 index 00000000..55d28122 --- /dev/null +++ b/test/projectgraph/cases/rust/into-ambiguous/project/src/convert.rs @@ -0,0 +1,178 @@ +pub struct In; + +pub struct D0; + +impl From for D0 { + fn from(_s: In) -> D0 { + let _ = std::env::var("HOME"); + D0 + } +} + +pub struct D1; + +impl From for D1 { + fn from(_s: In) -> D1 { + D1 + } +} + +pub struct D2; + +impl From for D2 { + fn from(_s: In) -> D2 { + D2 + } +} + +pub struct D3; + +impl From for D3 { + fn from(_s: In) -> D3 { + D3 + } +} + +pub struct D4; + +impl From for D4 { + fn from(_s: In) -> D4 { + D4 + } +} + +pub struct D5; + +impl From for D5 { + fn from(_s: In) -> D5 { + D5 + } +} + +pub struct D6; + +impl From for D6 { + fn from(_s: In) -> D6 { + D6 + } +} + +pub struct D7; + +impl From for D7 { + fn from(_s: In) -> D7 { + D7 + } +} + +pub struct D8; + +impl From for D8 { + fn from(_s: In) -> D8 { + D8 + } +} + +pub struct D9; + +impl From for D9 { + fn from(_s: In) -> D9 { + D9 + } +} + +pub struct D10; + +impl From for D10 { + fn from(_s: In) -> D10 { + D10 + } +} + +pub struct D11; + +impl From for D11 { + fn from(_s: In) -> D11 { + D11 + } +} + +pub struct D12; + +impl From for D12 { + fn from(_s: In) -> D12 { + D12 + } +} + +pub struct D13; + +impl From for D13 { + fn from(_s: In) -> D13 { + D13 + } +} + +pub struct D14; + +impl From for D14 { + fn from(_s: In) -> D14 { + D14 + } +} + +pub struct D15; + +impl From for D15 { + fn from(_s: In) -> D15 { + D15 + } +} + +pub struct D16; + +impl From for D16 { + fn from(_s: In) -> D16 { + D16 + } +} + +pub struct D17; + +impl From for D17 { + fn from(_s: In) -> D17 { + D17 + } +} + +pub struct D18; + +impl From for D18 { + fn from(_s: In) -> D18 { + D18 + } +} + +pub struct D19; + +impl From for D19 { + fn from(_s: In) -> D19 { + D19 + } +} + +pub struct D20; + +impl From for D20 { + fn from(_s: In) -> D20 { + D20 + } +} + +pub struct D21; + +impl From for D21 { + fn from(_s: In) -> D21 { + D21 + } +} diff --git a/test/projectgraph/cases/rust/into-ambiguous/project/src/main.rs b/test/projectgraph/cases/rust/into-ambiguous/project/src/main.rs new file mode 100644 index 00000000..4e7bcddc --- /dev/null +++ b/test/projectgraph/cases/rust/into-ambiguous/project/src/main.rs @@ -0,0 +1,17 @@ +mod convert; + +fn cleanup(p: &str) { + let _ = std::fs::remove_file(p); +} + +fn dispatch>(items: Vec) -> Vec { + let mut out = Vec::new(); + for h in items { + out.push(h.into()); + } + out +} + +fn main() { + let _d: Vec = dispatch(vec![convert::In]); +} diff --git a/test/projectgraph/cases/rust/into-dependency/expect.yaml b/test/projectgraph/cases/rust/into-dependency/expect.yaml new file mode 100644 index 00000000..6060e883 --- /dev/null +++ b/test/projectgraph/cases/rust/into-dependency/expect.yaml @@ -0,0 +1,40 @@ +source: safedep:hand +description: >- + dispatch calls into on an element of a vector of a generic type, which the link step does not know. The + crate kit implements From for Store, and that from calls std::fs::remove_file. The site gets name + edges only to the candidates of the project, and the project has none, so the site is a record with the + method into. A call of into runs the from of an impl of From. So the record concerns what a method from + reaches, and kit//Store//from and std::fs::remove_file are uncertain and not not-found. +units: + vendor/kit: cargo:kit@1.0.0 +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +unresolved: + - caller: src/main.rs//dispatch + site: src/main.rs:4 + reason: unknown-receiver + method: into +results: + - target: kit//Store//from + status: uncertain + call_sites: + - caller: cargo:kit@1.0.0//src/lib.rs//Store + site: cargo:kit@1.0.0//src/lib.rs:6 + reached: false + uncertainties: + - kind: unresolved-call + site: src/main.rs:4 + - target: std//fs//remove_file + status: uncertain + call_sites: + - caller: cargo:kit@1.0.0//src/lib.rs//Store//from + site: cargo:kit@1.0.0//src/lib.rs:7 + reached: false + uncertainties: + - kind: unresolved-call + site: src/main.rs:4 diff --git a/test/projectgraph/cases/rust/into-dependency/project/Cargo.toml b/test/projectgraph/cases/rust/into-dependency/project/Cargo.toml new file mode 100644 index 00000000..492d638f --- /dev/null +++ b/test/projectgraph/cases/rust/into-dependency/project/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" + +[dependencies] +kit = "1.0.0" diff --git a/test/projectgraph/cases/rust/into-dependency/project/src/main.rs b/test/projectgraph/cases/rust/into-dependency/project/src/main.rs new file mode 100644 index 00000000..289f9bd6 --- /dev/null +++ b/test/projectgraph/cases/rust/into-dependency/project/src/main.rs @@ -0,0 +1,11 @@ +fn dispatch>(items: Vec) -> Vec { + let mut out = Vec::new(); + for h in items { + out.push(h.into()); + } + out +} + +fn main() { + let _d: Vec = dispatch(vec![kit::raw()]); +} diff --git a/test/projectgraph/cases/rust/into-dependency/project/vendor/kit/Cargo.toml b/test/projectgraph/cases/rust/into-dependency/project/vendor/kit/Cargo.toml new file mode 100644 index 00000000..58da5f79 --- /dev/null +++ b/test/projectgraph/cases/rust/into-dependency/project/vendor/kit/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "kit" +version = "1.0.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/into-dependency/project/vendor/kit/src/lib.rs b/test/projectgraph/cases/rust/into-dependency/project/vendor/kit/src/lib.rs new file mode 100644 index 00000000..4b2f4304 --- /dev/null +++ b/test/projectgraph/cases/rust/into-dependency/project/vendor/kit/src/lib.rs @@ -0,0 +1,14 @@ +pub struct Raw; + +pub struct Store; + +impl From for Store { + fn from(_r: Raw) -> Store { + let _ = std::fs::remove_file("x"); + Store + } +} + +pub fn raw() -> Raw { + Raw +} diff --git a/test/projectgraph/cases/rust/into-dependency/summaries/cargo_kit_1.0.0.json b/test/projectgraph/cases/rust/into-dependency/summaries/cargo_kit_1.0.0.json new file mode 100644 index 00000000..6fac9200 --- /dev/null +++ b/test/projectgraph/cases/rust/into-dependency/summaries/cargo_kit_1.0.0.json @@ -0,0 +1,37 @@ +{ + "version": "3", + "index_version": "16", + "link_version": "18", + "unit": "cargo:kit@1.0.0", + "symbols": [ + { + "key": "kit//Raw", + "definitions": [ + "cargo:kit@1.0.0//src/lib.rs//Raw" + ], + "reaches": [] + }, + { + "key": "kit//Store", + "definitions": [ + "cargo:kit@1.0.0//src/lib.rs//Store" + ], + "reaches": [] + }, + { + "key": "kit//Store//from", + "definitions": [ + "cargo:kit@1.0.0//src/lib.rs//Store//from" + ], + "reaches": [] + }, + { + "key": "kit//raw", + "definitions": [ + "cargo:kit@1.0.0//src/lib.rs//raw" + ], + "reaches": [] + } + ], + "targets": [] +} diff --git a/test/projectgraph/cases/rust/library-build-script/summaries/cargo_kit_1.0.0.json b/test/projectgraph/cases/rust/library-build-script/summaries/cargo_kit_1.0.0.json index 1f075b50..1109a3ed 100644 --- a/test/projectgraph/cases/rust/library-build-script/summaries/cargo_kit_1.0.0.json +++ b/test/projectgraph/cases/rust/library-build-script/summaries/cargo_kit_1.0.0.json @@ -1,7 +1,7 @@ { - "version": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "unit": "cargo:kit@1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/rust/macro-import/expect.yaml b/test/projectgraph/cases/rust/macro-import/expect.yaml new file mode 100644 index 00000000..97d9d859 --- /dev/null +++ b/test/projectgraph/cases/rust/macro-import/expect.yaml @@ -0,0 +1,37 @@ +source: safedep:hand +description: >- + The case of the vet reach corpus rust/macro-import. main uses the macro render_now of the crate dep, which + #[macro_use] extern crate dep brings in. The macro expands to a call of dep::render. The code of the crate dep is + the unit cargo:dep@1.2.0. The link step does not resolve a macro that #[macro_export] gives at the root of a crate + of a unit, so the call of render_now stays a record. The record makes render uncertain, and not not-found. +units: + vendor/dep: cargo:dep@1.2.0 +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +exact_edges: true +edges: + - from: cargo:dep@1.2.0//src/lib.rs//render_now + to: cargo:dep@1.2.0//src/lib.rs//render + kind: direct + confidence: high + site: cargo:dep@1.2.0//src/lib.rs:6 +unresolved: + - caller: src/main.rs//main + site: src/main.rs:5 + reason: unknown-receiver + method: render_now +results: + - target: dep//render + status: uncertain + call_sites: + - caller: cargo:dep@1.2.0//src/lib.rs//render_now + site: cargo:dep@1.2.0//src/lib.rs:6 + reached: false + uncertainties: + - kind: unresolved-call + site: src/main.rs:5 diff --git a/test/projectgraph/cases/rust/macro-import/project/Cargo.toml b/test/projectgraph/cases/rust/macro-import/project/Cargo.toml new file mode 100644 index 00000000..56e246be --- /dev/null +++ b/test/projectgraph/cases/rust/macro-import/project/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" + +[dependencies] +dep = "1.2.0" diff --git a/test/projectgraph/cases/rust/macro-import/project/src/main.rs b/test/projectgraph/cases/rust/macro-import/project/src/main.rs new file mode 100644 index 00000000..9214cf3f --- /dev/null +++ b/test/projectgraph/cases/rust/macro-import/project/src/main.rs @@ -0,0 +1,6 @@ +#[macro_use] +extern crate dep; + +fn main() { + render_now!(); +} diff --git a/test/projectgraph/cases/rust/macro-import/project/vendor/dep/Cargo.toml b/test/projectgraph/cases/rust/macro-import/project/vendor/dep/Cargo.toml new file mode 100644 index 00000000..5352efc0 --- /dev/null +++ b/test/projectgraph/cases/rust/macro-import/project/vendor/dep/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "dep" +version = "1.2.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/macro-import/project/vendor/dep/src/lib.rs b/test/projectgraph/cases/rust/macro-import/project/vendor/dep/src/lib.rs new file mode 100644 index 00000000..f2ae6e99 --- /dev/null +++ b/test/projectgraph/cases/rust/macro-import/project/vendor/dep/src/lib.rs @@ -0,0 +1,8 @@ +pub fn render() {} + +#[macro_export] +macro_rules! render_now { + () => { + $crate::render() + }; +} diff --git a/test/projectgraph/cases/rust/macro-import/summaries/cargo_dep_1.2.0.json b/test/projectgraph/cases/rust/macro-import/summaries/cargo_dep_1.2.0.json new file mode 100644 index 00000000..48a89996 --- /dev/null +++ b/test/projectgraph/cases/rust/macro-import/summaries/cargo_dep_1.2.0.json @@ -0,0 +1,16 @@ +{ + "version": "3", + "index_version": "16", + "link_version": "18", + "unit": "cargo:dep@1.2.0", + "symbols": [ + { + "key": "dep//render", + "definitions": [ + "cargo:dep@1.2.0//src/lib.rs//render" + ], + "reaches": [] + } + ], + "targets": [] +} diff --git a/test/projectgraph/cases/rust/name-ambiguous-method/expect.yaml b/test/projectgraph/cases/rust/name-ambiguous-method/expect.yaml new file mode 100644 index 00000000..ef76045c --- /dev/null +++ b/test/projectgraph/cases/rust/name-ambiguous-method/expect.yaml @@ -0,0 +1,47 @@ +source: safedep:hand +description: >- + dispatch calls handle on an element of a vector of a generic type, which the link step does not know. 25 + types define handle. That is over the cap of 20, so the site gets no name edge and is ambiguous. A Rust + call on a receiver runs only a method of the name, because a field that holds a function needs the form + (h.f)(). So the site keeps the method name handle. H0.handle calls std::env::var, so std::env::var is + uncertain. H1.handle calls decode on a trait object of Codec, which no type of the graph implements, so + that site is a record with the method decode. The ambiguous site concerns what the code of each candidate + can run, through its records too, so the Engine::decode of base64 is uncertain and not no-such-target. + std::fs::remove_file, which only the uncalled cleanup calls, is not found. +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +unresolved: + - caller: src/handlers.rs//H1//handle + site: src/handlers.rs:19 + reason: trait-receiver + method: decode + - caller: src/main.rs//dispatch + site: src/main.rs:9 + reason: ambiguous + method: handle +results: + - target: std//env//var + status: uncertain + call_sites: + - caller: src/handlers.rs//H0//handle + site: src/handlers.rs:5 + reached: false + uncertainties: + - kind: unresolved-call + site: src/main.rs:9 + - target: base64//engine//Engine//decode + status: uncertain + uncertainties: + - kind: unresolved-call + site: src/main.rs:9 + - target: std//fs//remove_file + status: not-found + call_sites: + - caller: src/main.rs//cleanup + site: src/main.rs:4 + reached: false diff --git a/test/projectgraph/cases/rust/name-ambiguous-method/project/Cargo.toml b/test/projectgraph/cases/rust/name-ambiguous-method/project/Cargo.toml new file mode 100644 index 00000000..0d44349a --- /dev/null +++ b/test/projectgraph/cases/rust/name-ambiguous-method/project/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/name-ambiguous-method/project/src/handlers.rs b/test/projectgraph/cases/rust/name-ambiguous-method/project/src/handlers.rs new file mode 100644 index 00000000..d32c474f --- /dev/null +++ b/test/projectgraph/cases/rust/name-ambiguous-method/project/src/handlers.rs @@ -0,0 +1,159 @@ +pub struct H0; + +impl H0 { + pub fn handle(&self, s: &str) { + let _ = std::env::var(s); + } +} + +pub trait Codec { + fn decode(&self, s: &str) -> Vec; +} + +pub struct H1 { + pub codec: Box, +} + +impl H1 { + pub fn handle(&self, s: &str) { + self.codec.decode(s); + } +} + +pub struct H2; + +impl H2 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H3; + +impl H3 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H4; + +impl H4 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H5; + +impl H5 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H6; + +impl H6 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H7; + +impl H7 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H8; + +impl H8 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H9; + +impl H9 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H10; + +impl H10 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H11; + +impl H11 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H12; + +impl H12 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H13; + +impl H13 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H14; + +impl H14 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H15; + +impl H15 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H16; + +impl H16 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H17; + +impl H17 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H18; + +impl H18 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H19; + +impl H19 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H20; + +impl H20 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H21; + +impl H21 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H22; + +impl H22 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H23; + +impl H23 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H24; + +impl H24 { + pub fn handle(&self, _s: &str) {} +} diff --git a/test/projectgraph/cases/rust/name-ambiguous-method/project/src/main.rs b/test/projectgraph/cases/rust/name-ambiguous-method/project/src/main.rs new file mode 100644 index 00000000..19a82879 --- /dev/null +++ b/test/projectgraph/cases/rust/name-ambiguous-method/project/src/main.rs @@ -0,0 +1,15 @@ +mod handlers; + +fn cleanup(p: &str) { + let _ = std::fs::remove_file(p); +} + +fn dispatch(items: Vec, s: &str) { + for h in items { + h.handle(s); + } +} + +fn main() { + dispatch(vec![handlers::H0], "a"); +} diff --git a/test/projectgraph/cases/rust/name-ambiguous-param/expect.yaml b/test/projectgraph/cases/rust/name-ambiguous-param/expect.yaml new file mode 100644 index 00000000..0b5dc406 --- /dev/null +++ b/test/projectgraph/cases/rust/name-ambiguous-param/expect.yaml @@ -0,0 +1,38 @@ +source: safedep:hand +description: >- + dispatch calls its parameter handle, which holds the function that pick returns. 25 types define a + method handle. That is over the cap of 20, so the site gets no name edge and is ambiguous. A Rust call + of a name with no receiver can run any function value, and the index records no reference for the tail + expression cleanup of pick. So the site keeps no method name and concerns every target, and + std::fs::remove_file, which cleanup calls, is uncertain and not not-found. So is std::env::var, which + H0.handle calls. +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +unresolved: + - caller: src/main.rs//dispatch + site: src/main.rs:12 + reason: ambiguous +results: + - target: std//fs//remove_file + status: uncertain + call_sites: + - caller: src/main.rs//cleanup + site: src/main.rs:4 + reached: false + uncertainties: + - kind: unresolved-call + site: src/main.rs:12 + - target: std//env//var + status: uncertain + call_sites: + - caller: src/handlers.rs//H0//handle + site: src/handlers.rs:5 + reached: false + uncertainties: + - kind: unresolved-call + site: src/main.rs:12 diff --git a/test/projectgraph/cases/rust/name-ambiguous-param/project/Cargo.toml b/test/projectgraph/cases/rust/name-ambiguous-param/project/Cargo.toml new file mode 100644 index 00000000..0d44349a --- /dev/null +++ b/test/projectgraph/cases/rust/name-ambiguous-param/project/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/name-ambiguous-param/project/src/handlers.rs b/test/projectgraph/cases/rust/name-ambiguous-param/project/src/handlers.rs new file mode 100644 index 00000000..34d67801 --- /dev/null +++ b/test/projectgraph/cases/rust/name-ambiguous-param/project/src/handlers.rs @@ -0,0 +1,151 @@ +pub struct H0; + +impl H0 { + pub fn handle(&self, s: &str) { + let _ = std::env::var(s); + } +} + +pub struct H1; + +impl H1 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H2; + +impl H2 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H3; + +impl H3 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H4; + +impl H4 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H5; + +impl H5 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H6; + +impl H6 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H7; + +impl H7 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H8; + +impl H8 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H9; + +impl H9 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H10; + +impl H10 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H11; + +impl H11 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H12; + +impl H12 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H13; + +impl H13 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H14; + +impl H14 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H15; + +impl H15 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H16; + +impl H16 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H17; + +impl H17 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H18; + +impl H18 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H19; + +impl H19 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H20; + +impl H20 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H21; + +impl H21 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H22; + +impl H22 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H23; + +impl H23 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H24; + +impl H24 { + pub fn handle(&self, _s: &str) {} +} diff --git a/test/projectgraph/cases/rust/name-ambiguous-param/project/src/main.rs b/test/projectgraph/cases/rust/name-ambiguous-param/project/src/main.rs new file mode 100644 index 00000000..51768849 --- /dev/null +++ b/test/projectgraph/cases/rust/name-ambiguous-param/project/src/main.rs @@ -0,0 +1,17 @@ +mod handlers; + +fn cleanup(p: &str) { + let _ = std::fs::remove_file(p); +} + +fn pick() -> fn(&str) { + cleanup +} + +fn dispatch(handle: fn(&str), s: &str) { + handle(s) +} + +fn main() { + dispatch(pick(), "a"); +} diff --git a/test/projectgraph/cases/rust/trait-object-shadowed/expect.yaml b/test/projectgraph/cases/rust/trait-object-shadowed/expect.yaml new file mode 100644 index 00000000..ed003dbf --- /dev/null +++ b/test/projectgraph/cases/rust/trait-object-shadowed/expect.yaml @@ -0,0 +1,93 @@ +source: safedep:hand +description: >- + print takes a Box, then a let binds the name w again to the dep::Plain that dep::plain returns, and + calls write on it. The code of the crate dep is the unit cargo:dep@1.2.0. The declared type of the parameter does + not hold after the let, so the call keeps the result type of dep::plain and goes to dep//Plain//write. If the + declared type typed the call, it would get the default write of the trait and the cha edge to Html::write, and + dep//Plain//write would read as not-found. Nothing calls write on an Html, so dep//Html//write is not-found. +units: + vendor/dep: cargo:dep@1.2.0 +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +exact_edges: true +edges: + - from: cargo:dep@1.2.0//src/lib.rs//Html//write + to: cargo:dep@1.2.0//src/lib.rs//Html + kind: direct + confidence: high + site: cargo:dep@1.2.0//src/lib.rs:8 + - from: cargo:dep@1.2.0//src/lib.rs//Plain//write + to: cargo:dep@1.2.0//src/lib.rs//Plain + kind: direct + confidence: high + site: cargo:dep@1.2.0//src/lib.rs:14 + - from: cargo:dep@1.2.0//src/lib.rs//plain + to: cargo:dep@1.2.0//src/lib.rs//Plain + kind: direct + confidence: high + site: cargo:dep@1.2.0//src/lib.rs:18 + - from: dep//Html + to: cargo:dep@1.2.0//src/lib.rs//Html + kind: package + confidence: high + site: cargo:dep@1.2.0//src/lib.rs:7 + - from: dep//Plain//write + to: cargo:dep@1.2.0//src/lib.rs//Plain//write + kind: package + confidence: high + site: cargo:dep@1.2.0//src/lib.rs:14 + - from: dep//plain + to: cargo:dep@1.2.0//src/lib.rs//plain + kind: package + confidence: high + site: cargo:dep@1.2.0//src/lib.rs:17 + - from: src/main.rs//main + to: src/main.rs//print + kind: direct + confidence: high + site: src/main.rs:4 + - from: src/main.rs//main + to: Box//new + kind: direct + confidence: high + site: src/main.rs:4 + - from: src/main.rs//main + to: dep//Html + kind: direct + confidence: high + site: src/main.rs:4 + - from: src/main.rs//print + to: dep//plain + kind: direct + confidence: high + site: src/main.rs:8 + - from: src/main.rs//print + to: dep//Plain//write + kind: direct + confidence: high + site: src/main.rs:9 +unresolved: [] +results: + - target: dep//Plain//write + status: found + path: + - key: src/main.rs//main + - key: src/main.rs//print + site: src/main.rs:4 + kind: direct + confidence: high + - key: dep//Plain//write + site: src/main.rs:9 + kind: direct + confidence: high + call_sites: + - caller: src/main.rs//print + site: src/main.rs:9 + reached: true + - target: dep//Html//write + status: not-found diff --git a/test/projectgraph/cases/rust/trait-object-shadowed/project/Cargo.toml b/test/projectgraph/cases/rust/trait-object-shadowed/project/Cargo.toml new file mode 100644 index 00000000..56e246be --- /dev/null +++ b/test/projectgraph/cases/rust/trait-object-shadowed/project/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" + +[dependencies] +dep = "1.2.0" diff --git a/test/projectgraph/cases/rust/trait-object-shadowed/project/src/main.rs b/test/projectgraph/cases/rust/trait-object-shadowed/project/src/main.rs new file mode 100644 index 00000000..74ece660 --- /dev/null +++ b/test/projectgraph/cases/rust/trait-object-shadowed/project/src/main.rs @@ -0,0 +1,10 @@ +use dep::{Html, Write}; + +fn main() { + print(Box::new(Html {})); +} + +fn print(w: Box) { + let w = dep::plain(); + w.write(); +} diff --git a/test/projectgraph/cases/rust/trait-object-shadowed/project/vendor/dep/Cargo.toml b/test/projectgraph/cases/rust/trait-object-shadowed/project/vendor/dep/Cargo.toml new file mode 100644 index 00000000..5352efc0 --- /dev/null +++ b/test/projectgraph/cases/rust/trait-object-shadowed/project/vendor/dep/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "dep" +version = "1.2.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/trait-object-shadowed/project/vendor/dep/src/lib.rs b/test/projectgraph/cases/rust/trait-object-shadowed/project/vendor/dep/src/lib.rs new file mode 100644 index 00000000..29825dfa --- /dev/null +++ b/test/projectgraph/cases/rust/trait-object-shadowed/project/vendor/dep/src/lib.rs @@ -0,0 +1,19 @@ +pub trait Write { + fn write(&self) {} +} + +pub struct Html {} + +impl Write for Html { + fn write(&self) {} +} + +pub struct Plain {} + +impl Plain { + pub fn write(&self) {} +} + +pub fn plain() -> Plain { + Plain {} +} diff --git a/test/projectgraph/cases/rust/trait-object-shadowed/summaries/cargo_dep_1.2.0.json b/test/projectgraph/cases/rust/trait-object-shadowed/summaries/cargo_dep_1.2.0.json new file mode 100644 index 00000000..7eca2165 --- /dev/null +++ b/test/projectgraph/cases/rust/trait-object-shadowed/summaries/cargo_dep_1.2.0.json @@ -0,0 +1,58 @@ +{ + "version": "3", + "index_version": "16", + "link_version": "18", + "unit": "cargo:dep@1.2.0", + "symbols": [ + { + "key": "dep//Html", + "definitions": [ + "cargo:dep@1.2.0//src/lib.rs//Html" + ], + "reaches": [] + }, + { + "key": "dep//Html//write", + "definitions": [ + "cargo:dep@1.2.0//src/lib.rs//Html//write" + ], + "reaches": [] + }, + { + "key": "dep//Plain", + "definitions": [ + "cargo:dep@1.2.0//src/lib.rs//Plain" + ], + "reaches": [] + }, + { + "key": "dep//Plain//write", + "definitions": [ + "cargo:dep@1.2.0//src/lib.rs//Plain//write" + ], + "reaches": [] + }, + { + "key": "dep//Write", + "definitions": [ + "cargo:dep@1.2.0//src/lib.rs//Write" + ], + "reaches": [] + }, + { + "key": "dep//Write//write", + "definitions": [ + "cargo:dep@1.2.0//src/lib.rs//Write//write" + ], + "reaches": [] + }, + { + "key": "dep//plain", + "definitions": [ + "cargo:dep@1.2.0//src/lib.rs//plain" + ], + "reaches": [] + } + ], + "targets": [] +} diff --git a/test/projectgraph/cases/rust/trait-object/expect.yaml b/test/projectgraph/cases/rust/trait-object/expect.yaml new file mode 100644 index 00000000..d7fd8ee6 --- /dev/null +++ b/test/projectgraph/cases/rust/trait-object/expect.yaml @@ -0,0 +1,75 @@ +source: safedep:hand +description: >- + The case of the vet reach corpus rust/trait-object. main calls write on a Box that holds a dep::Html. The + code of the crate dep is the unit cargo:dep@1.2.0. Box::new holds its argument, so the value of the call is no + instance of Box, and the declared type of w gives the trait Write of the unit. Html implements Write, so the call + gets a cha edge to dep//Html//write. A type of another crate can implement Write too, so the site keeps its record. +units: + vendor/dep: cargo:dep@1.2.0 +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +exact_edges: true +edges: + - from: cargo:dep@1.2.0//src/lib.rs//Html//write + to: cargo:dep@1.2.0//src/lib.rs//Html + kind: direct + confidence: high + site: cargo:dep@1.2.0//src/lib.rs:8 + - from: dep//Html + to: cargo:dep@1.2.0//src/lib.rs//Html + kind: package + confidence: high + site: cargo:dep@1.2.0//src/lib.rs:7 + - from: dep//Write + to: cargo:dep@1.2.0//src/lib.rs//Write + kind: package + confidence: high + site: cargo:dep@1.2.0//src/lib.rs:1 + - from: dep//Html//write + to: cargo:dep@1.2.0//src/lib.rs//Html//write + kind: package + confidence: high + site: cargo:dep@1.2.0//src/lib.rs:8 + - from: src/main.rs//main + to: dep//Write + kind: reference + confidence: low + site: src/main.rs:4 + - from: src/main.rs//main + to: Box//new + kind: direct + confidence: high + site: src/main.rs:4 + - from: src/main.rs//main + to: dep//Html + kind: direct + confidence: high + site: src/main.rs:4 + - from: src/main.rs//main + to: dep//Html//write + kind: cha + confidence: medium + site: src/main.rs:5 +unresolved: + - caller: src/main.rs//main + site: src/main.rs:5 + reason: no-definition + method: write +results: + - target: dep//Html//write + status: found + path: + - key: src/main.rs//main + - key: dep//Html//write + site: src/main.rs:5 + kind: cha + confidence: medium + call_sites: + - caller: src/main.rs//main + site: src/main.rs:5 + reached: true diff --git a/test/projectgraph/cases/rust/trait-object/project/Cargo.toml b/test/projectgraph/cases/rust/trait-object/project/Cargo.toml new file mode 100644 index 00000000..56e246be --- /dev/null +++ b/test/projectgraph/cases/rust/trait-object/project/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" + +[dependencies] +dep = "1.2.0" diff --git a/test/projectgraph/cases/rust/trait-object/project/src/main.rs b/test/projectgraph/cases/rust/trait-object/project/src/main.rs new file mode 100644 index 00000000..82fb8f1f --- /dev/null +++ b/test/projectgraph/cases/rust/trait-object/project/src/main.rs @@ -0,0 +1,6 @@ +use dep::{Html, Write}; + +fn main() { + let w: Box = Box::new(Html {}); + w.write(); +} diff --git a/test/projectgraph/cases/rust/trait-object/project/vendor/dep/Cargo.toml b/test/projectgraph/cases/rust/trait-object/project/vendor/dep/Cargo.toml new file mode 100644 index 00000000..5352efc0 --- /dev/null +++ b/test/projectgraph/cases/rust/trait-object/project/vendor/dep/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "dep" +version = "1.2.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/trait-object/project/vendor/dep/src/lib.rs b/test/projectgraph/cases/rust/trait-object/project/vendor/dep/src/lib.rs new file mode 100644 index 00000000..fa315abd --- /dev/null +++ b/test/projectgraph/cases/rust/trait-object/project/vendor/dep/src/lib.rs @@ -0,0 +1,9 @@ +pub trait Write { + fn write(&self); +} + +pub struct Html {} + +impl Write for Html { + fn write(&self) {} +} diff --git a/test/projectgraph/cases/rust/trait-object/summaries/cargo_dep_1.2.0.json b/test/projectgraph/cases/rust/trait-object/summaries/cargo_dep_1.2.0.json new file mode 100644 index 00000000..a74e9419 --- /dev/null +++ b/test/projectgraph/cases/rust/trait-object/summaries/cargo_dep_1.2.0.json @@ -0,0 +1,30 @@ +{ + "version": "3", + "index_version": "16", + "link_version": "18", + "unit": "cargo:dep@1.2.0", + "symbols": [ + { + "key": "dep//Html", + "definitions": [ + "cargo:dep@1.2.0//src/lib.rs//Html" + ], + "reaches": [] + }, + { + "key": "dep//Html//write", + "definitions": [ + "cargo:dep@1.2.0//src/lib.rs//Html//write" + ], + "reaches": [] + }, + { + "key": "dep//Write", + "definitions": [ + "cargo:dep@1.2.0//src/lib.rs//Write" + ], + "reaches": [] + } + ], + "targets": [] +} diff --git a/test/projectgraph/cases/rust/use-binding-path/expect.yaml b/test/projectgraph/cases/rust/use-binding-path/expect.yaml new file mode 100644 index 00000000..5a9276e0 --- /dev/null +++ b/test/projectgraph/cases/rust/use-binding-path/expect.yaml @@ -0,0 +1,132 @@ +source: safedep:hand +description: >- + A use path starts with a name that another use of the module binds. In src/a.rs, use crate::deep::helpers binds + helpers to the module src/deep/helpers.rs, so helpers::run and helpers::Mode name its items. In src/b.rs, use + dep::sub binds sub to the module sub of the crate dep, the unit cargo:dep@1.0.0, so sub::f is dep::sub::f. A + crate named helpers or sub does not exist, and the code of each item calls a target. In src/c.rs, use + crate::ffi::flush as fl names dep::flush through pub use dep as ffi of src/main.rs, and a call of fl reaches the + external node dep//flush. +units: + vendor/dep: cargo:dep@1.0.0 +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +edges: + - from: src/a.rs//go + to: src/deep/helpers.rs//run + kind: direct + confidence: high + site: src/a.rs:6 + - from: src/a.rs//go + to: src/deep/helpers.rs//Mode//start + kind: member + confidence: high + site: src/a.rs:7 + - from: src/b.rs//go + to: dep//sub//f + kind: direct + confidence: high + site: src/b.rs:5 + - from: dep//sub//f + to: cargo:dep@1.0.0//src/sub.rs//f + kind: package + confidence: high + site: cargo:dep@1.0.0//src/sub.rs:1 + - from: src/c.rs//go + to: dep//flush + kind: direct + confidence: high + site: src/c.rs:4 +no_edges: + - from: src/a.rs//go + to: helpers//run + - from: src/a.rs//go + to: helpers//Mode//start + - from: src/b.rs//go + to: sub//f +unresolved: [] +results: + - target: dep//new_thing + status: found + path: + - key: src/main.rs//main + - key: src/a.rs//go + site: src/main.rs:9 + kind: direct + confidence: high + - key: src/deep/helpers.rs//run + site: src/a.rs:6 + kind: direct + confidence: high + - key: dep//new_thing + site: src/deep/helpers.rs:2 + kind: direct + confidence: high + call_sites: + - caller: src/deep/helpers.rs//run + site: src/deep/helpers.rs:2 + reached: true + - target: dep//load_thing + status: found + path: + - key: src/main.rs//main + - key: src/a.rs//go + site: src/main.rs:9 + kind: direct + confidence: high + - key: src/deep/helpers.rs//Mode//start + site: src/a.rs:7 + kind: member + confidence: high + - key: dep//load_thing + site: src/deep/helpers.rs:11 + kind: direct + confidence: high + call_sites: + - caller: src/deep/helpers.rs//Mode//start + site: src/deep/helpers.rs:11 + reached: true + - target: dep//sink + status: found + path: + - key: src/main.rs//main + - key: src/b.rs//go + site: src/main.rs:10 + kind: direct + confidence: high + - key: dep//sub//f + site: src/b.rs:5 + kind: direct + confidence: high + - key: cargo:dep@1.0.0//src/sub.rs//f + site: cargo:dep@1.0.0//src/sub.rs:1 + kind: package + confidence: high + - key: cargo:dep@1.0.0//src/lib.rs//sink + site: cargo:dep@1.0.0//src/sub.rs:2 + kind: direct + confidence: high + call_sites: + - caller: cargo:dep@1.0.0//src/sub.rs//f + site: cargo:dep@1.0.0//src/sub.rs:2 + reached: true + - target: dep//flush + status: found + path: + - key: src/main.rs//main + - key: src/c.rs//go + site: src/main.rs:11 + kind: direct + confidence: high + - key: dep//flush + site: src/c.rs:4 + kind: direct + confidence: high + call_sites: + - caller: src/c.rs//go + site: src/c.rs:4 + reached: true diff --git a/test/projectgraph/cases/rust/use-binding-path/project/Cargo.toml b/test/projectgraph/cases/rust/use-binding-path/project/Cargo.toml new file mode 100644 index 00000000..730014fa --- /dev/null +++ b/test/projectgraph/cases/rust/use-binding-path/project/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" + +[dependencies] +dep = "1.0.0" diff --git a/test/projectgraph/cases/rust/use-binding-path/project/src/a.rs b/test/projectgraph/cases/rust/use-binding-path/project/src/a.rs new file mode 100644 index 00000000..864d8c4d --- /dev/null +++ b/test/projectgraph/cases/rust/use-binding-path/project/src/a.rs @@ -0,0 +1,8 @@ +use crate::deep::helpers; +use helpers::run as go_run; +use helpers::Mode; + +pub fn go() { + go_run(); + Mode::start(); +} diff --git a/test/projectgraph/cases/rust/use-binding-path/project/src/b.rs b/test/projectgraph/cases/rust/use-binding-path/project/src/b.rs new file mode 100644 index 00000000..517981dd --- /dev/null +++ b/test/projectgraph/cases/rust/use-binding-path/project/src/b.rs @@ -0,0 +1,6 @@ +use dep::sub; +use sub::f as g; + +pub fn go() { + g(); +} diff --git a/test/projectgraph/cases/rust/use-binding-path/project/src/c.rs b/test/projectgraph/cases/rust/use-binding-path/project/src/c.rs new file mode 100644 index 00000000..f3406f34 --- /dev/null +++ b/test/projectgraph/cases/rust/use-binding-path/project/src/c.rs @@ -0,0 +1,5 @@ +use crate::ffi::flush as fl; + +pub fn go() { + fl(); +} diff --git a/test/projectgraph/cases/rust/use-binding-path/project/src/deep/helpers.rs b/test/projectgraph/cases/rust/use-binding-path/project/src/deep/helpers.rs new file mode 100644 index 00000000..45382687 --- /dev/null +++ b/test/projectgraph/cases/rust/use-binding-path/project/src/deep/helpers.rs @@ -0,0 +1,13 @@ +pub fn run() { + dep::new_thing(); +} + +pub enum Mode { + Fast, +} + +impl Mode { + pub fn start() { + dep::load_thing(); + } +} diff --git a/test/projectgraph/cases/rust/use-binding-path/project/src/deep/mod.rs b/test/projectgraph/cases/rust/use-binding-path/project/src/deep/mod.rs new file mode 100644 index 00000000..1630fabc --- /dev/null +++ b/test/projectgraph/cases/rust/use-binding-path/project/src/deep/mod.rs @@ -0,0 +1 @@ +pub mod helpers; diff --git a/test/projectgraph/cases/rust/use-binding-path/project/src/main.rs b/test/projectgraph/cases/rust/use-binding-path/project/src/main.rs new file mode 100644 index 00000000..ec7f9a04 --- /dev/null +++ b/test/projectgraph/cases/rust/use-binding-path/project/src/main.rs @@ -0,0 +1,12 @@ +mod a; +mod b; +mod c; +mod deep; + +pub use dep as ffi; + +fn main() { + a::go(); + b::go(); + c::go(); +} diff --git a/test/projectgraph/cases/rust/use-binding-path/project/vendor/dep/Cargo.toml b/test/projectgraph/cases/rust/use-binding-path/project/vendor/dep/Cargo.toml new file mode 100644 index 00000000..537b4e47 --- /dev/null +++ b/test/projectgraph/cases/rust/use-binding-path/project/vendor/dep/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "dep" +version = "1.0.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/use-binding-path/project/vendor/dep/src/lib.rs b/test/projectgraph/cases/rust/use-binding-path/project/vendor/dep/src/lib.rs new file mode 100644 index 00000000..0ccac26c --- /dev/null +++ b/test/projectgraph/cases/rust/use-binding-path/project/vendor/dep/src/lib.rs @@ -0,0 +1,9 @@ +pub mod sub; + +pub fn new_thing() {} + +pub fn load_thing() {} + +pub fn sink() {} + +pub fn flush() {} diff --git a/test/projectgraph/cases/rust/use-binding-path/project/vendor/dep/src/sub.rs b/test/projectgraph/cases/rust/use-binding-path/project/vendor/dep/src/sub.rs new file mode 100644 index 00000000..7f6fcc5d --- /dev/null +++ b/test/projectgraph/cases/rust/use-binding-path/project/vendor/dep/src/sub.rs @@ -0,0 +1,3 @@ +pub fn f() { + crate::sink(); +} diff --git a/test/projectgraph/cases/rust/use-binding-path/summaries/cargo_dep_1.0.0.json b/test/projectgraph/cases/rust/use-binding-path/summaries/cargo_dep_1.0.0.json new file mode 100644 index 00000000..beaba435 --- /dev/null +++ b/test/projectgraph/cases/rust/use-binding-path/summaries/cargo_dep_1.0.0.json @@ -0,0 +1,44 @@ +{ + "version": "3", + "index_version": "16", + "link_version": "18", + "unit": "cargo:dep@1.0.0", + "symbols": [ + { + "key": "dep//flush", + "definitions": [ + "cargo:dep@1.0.0//src/lib.rs//flush" + ], + "reaches": [] + }, + { + "key": "dep//load_thing", + "definitions": [ + "cargo:dep@1.0.0//src/lib.rs//load_thing" + ], + "reaches": [] + }, + { + "key": "dep//new_thing", + "definitions": [ + "cargo:dep@1.0.0//src/lib.rs//new_thing" + ], + "reaches": [] + }, + { + "key": "dep//sink", + "definitions": [ + "cargo:dep@1.0.0//src/lib.rs//sink" + ], + "reaches": [] + }, + { + "key": "dep//sub//f", + "definitions": [ + "cargo:dep@1.0.0//src/sub.rs//f" + ], + "reaches": [] + } + ], + "targets": [] +} diff --git a/test/projectgraph/cases/rust/use-dependency-item/expect.yaml b/test/projectgraph/cases/rust/use-dependency-item/expect.yaml new file mode 100644 index 00000000..d07d598c --- /dev/null +++ b/test/projectgraph/cases/rust/use-dependency-item/expect.yaml @@ -0,0 +1,302 @@ +source: safedep:hand +description: >- + main.rs binds items of the crate dep with use and calls each one by its bound name: a function (top), a renamed + function (renamed as r), two names of one use list (other, Type), a function of a nested module (sub::nested), a + function that lib.rs gives on with pub use (reexported), and a use in a function body (inner). The code of the + crate is the unit cargo:dep@1.0.0. A use path names a module or an item, so the link step reads dep::top as the + module of the origin, and the unit gives the function top itself. Before the link step linked an item that way, + the external key had no package edge, and each target but run_sink was not-found. main does not call unused, so + unused_sink stays not-found. +units: + vendor/dep: cargo:dep@1.0.0 +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +exact_edges: true +edges: + - from: cargo:dep@1.0.0//src/hidden.rs//reexported + to: cargo:dep@1.0.0//src/hidden.rs//hidden_sink + kind: direct + confidence: high + site: cargo:dep@1.0.0//src/hidden.rs:2 + - from: cargo:dep@1.0.0//src/lib.rs//Type//run + to: cargo:dep@1.0.0//src/lib.rs//Type + kind: direct + confidence: high + site: cargo:dep@1.0.0//src/lib.rs:43 + - from: cargo:dep@1.0.0//src/lib.rs//Type//run + to: cargo:dep@1.0.0//src/lib.rs//run_sink + kind: direct + confidence: high + site: cargo:dep@1.0.0//src/lib.rs:44 + - from: cargo:dep@1.0.0//src/lib.rs//inner + to: cargo:dep@1.0.0//src/lib.rs//inner_sink + kind: direct + confidence: high + site: cargo:dep@1.0.0//src/lib.rs:25 + - from: cargo:dep@1.0.0//src/lib.rs//other + to: cargo:dep@1.0.0//src/lib.rs//other_sink + kind: direct + confidence: high + site: cargo:dep@1.0.0//src/lib.rs:19 + - from: cargo:dep@1.0.0//src/lib.rs//renamed + to: cargo:dep@1.0.0//src/lib.rs//renamed_sink + kind: direct + confidence: high + site: cargo:dep@1.0.0//src/lib.rs:13 + - from: cargo:dep@1.0.0//src/lib.rs//top + to: cargo:dep@1.0.0//src/lib.rs//sink + kind: direct + confidence: high + site: cargo:dep@1.0.0//src/lib.rs:7 + - from: cargo:dep@1.0.0//src/lib.rs//unused + to: cargo:dep@1.0.0//src/lib.rs//unused_sink + kind: direct + confidence: high + site: cargo:dep@1.0.0//src/lib.rs:31 + - from: cargo:dep@1.0.0//src/sub.rs//nested + to: cargo:dep@1.0.0//src/sub.rs//nested_sink + kind: direct + confidence: high + site: cargo:dep@1.0.0//src/sub.rs:2 + - from: dep//Type//new + to: cargo:dep@1.0.0//src/lib.rs//Type//new + kind: package + confidence: high + site: cargo:dep@1.0.0//src/lib.rs:39 + - from: dep//Type//run + to: cargo:dep@1.0.0//src/lib.rs//Type//run + kind: package + confidence: high + site: cargo:dep@1.0.0//src/lib.rs:43 + - from: dep//inner + to: cargo:dep@1.0.0//src/lib.rs//inner + kind: package + confidence: high + site: cargo:dep@1.0.0//src/lib.rs:24 + - from: dep//other + to: cargo:dep@1.0.0//src/lib.rs//other + kind: package + confidence: high + site: cargo:dep@1.0.0//src/lib.rs:18 + - from: dep//reexported + to: cargo:dep@1.0.0//src/hidden.rs//reexported + kind: package + confidence: high + site: cargo:dep@1.0.0//src/hidden.rs:1 + - from: dep//renamed + to: cargo:dep@1.0.0//src/lib.rs//renamed + kind: package + confidence: high + site: cargo:dep@1.0.0//src/lib.rs:12 + - from: dep//sub//nested + to: cargo:dep@1.0.0//src/sub.rs//nested + kind: package + confidence: high + site: cargo:dep@1.0.0//src/sub.rs:1 + - from: dep//top + to: cargo:dep@1.0.0//src/lib.rs//top + kind: package + confidence: high + site: cargo:dep@1.0.0//src/lib.rs:6 + - from: src/main.rs//local + to: dep//inner + kind: direct + confidence: high + site: src/main.rs:21 + - from: src/main.rs//main + to: dep//Type//new + kind: direct + confidence: high + site: src/main.rs:14 + - from: src/main.rs//main + to: dep//Type//run + kind: direct + confidence: high + site: src/main.rs:14 + - from: src/main.rs//main + to: dep//other + kind: direct + confidence: high + site: src/main.rs:12 + - from: src/main.rs//main + to: dep//reexported + kind: direct + confidence: high + site: src/main.rs:15 + - from: src/main.rs//main + to: dep//renamed + kind: direct + confidence: high + site: src/main.rs:11 + - from: src/main.rs//main + to: dep//sub//nested + kind: direct + confidence: high + site: src/main.rs:13 + - from: src/main.rs//main + to: dep//top + kind: direct + confidence: high + site: src/main.rs:10 + - from: src/main.rs//main + to: src/main.rs//local + kind: direct + confidence: high + site: src/main.rs:16 +unresolved: [] +results: + - target: dep//sink + status: found + path: + - key: src/main.rs//main + - key: dep//top + site: src/main.rs:10 + kind: direct + confidence: high + - key: cargo:dep@1.0.0//src/lib.rs//top + site: cargo:dep@1.0.0//src/lib.rs:6 + kind: package + confidence: high + - key: cargo:dep@1.0.0//src/lib.rs//sink + site: cargo:dep@1.0.0//src/lib.rs:7 + kind: direct + confidence: high + call_sites: + - caller: cargo:dep@1.0.0//src/lib.rs//top + site: cargo:dep@1.0.0//src/lib.rs:7 + reached: true + - target: dep//renamed_sink + status: found + path: + - key: src/main.rs//main + - key: dep//renamed + site: src/main.rs:11 + kind: direct + confidence: high + - key: cargo:dep@1.0.0//src/lib.rs//renamed + site: cargo:dep@1.0.0//src/lib.rs:12 + kind: package + confidence: high + - key: cargo:dep@1.0.0//src/lib.rs//renamed_sink + site: cargo:dep@1.0.0//src/lib.rs:13 + kind: direct + confidence: high + call_sites: + - caller: cargo:dep@1.0.0//src/lib.rs//renamed + site: cargo:dep@1.0.0//src/lib.rs:13 + reached: true + - target: dep//other_sink + status: found + path: + - key: src/main.rs//main + - key: dep//other + site: src/main.rs:12 + kind: direct + confidence: high + - key: cargo:dep@1.0.0//src/lib.rs//other + site: cargo:dep@1.0.0//src/lib.rs:18 + kind: package + confidence: high + - key: cargo:dep@1.0.0//src/lib.rs//other_sink + site: cargo:dep@1.0.0//src/lib.rs:19 + kind: direct + confidence: high + call_sites: + - caller: cargo:dep@1.0.0//src/lib.rs//other + site: cargo:dep@1.0.0//src/lib.rs:19 + reached: true + - target: dep//sub//nested_sink + status: found + path: + - key: src/main.rs//main + - key: dep//sub//nested + site: src/main.rs:13 + kind: direct + confidence: high + - key: cargo:dep@1.0.0//src/sub.rs//nested + site: cargo:dep@1.0.0//src/sub.rs:1 + kind: package + confidence: high + - key: cargo:dep@1.0.0//src/sub.rs//nested_sink + site: cargo:dep@1.0.0//src/sub.rs:2 + kind: direct + confidence: high + call_sites: + - caller: cargo:dep@1.0.0//src/sub.rs//nested + site: cargo:dep@1.0.0//src/sub.rs:2 + reached: true + - target: dep//run_sink + status: found + path: + - key: src/main.rs//main + - key: dep//Type//run + site: src/main.rs:14 + kind: direct + confidence: high + - key: cargo:dep@1.0.0//src/lib.rs//Type//run + site: cargo:dep@1.0.0//src/lib.rs:43 + kind: package + confidence: high + - key: cargo:dep@1.0.0//src/lib.rs//run_sink + site: cargo:dep@1.0.0//src/lib.rs:44 + kind: direct + confidence: high + call_sites: + - caller: cargo:dep@1.0.0//src/lib.rs//Type//run + site: cargo:dep@1.0.0//src/lib.rs:44 + reached: true + - target: dep//hidden//hidden_sink + status: found + path: + - key: src/main.rs//main + - key: dep//reexported + site: src/main.rs:15 + kind: direct + confidence: high + - key: cargo:dep@1.0.0//src/hidden.rs//reexported + site: cargo:dep@1.0.0//src/hidden.rs:1 + kind: package + confidence: high + - key: cargo:dep@1.0.0//src/hidden.rs//hidden_sink + site: cargo:dep@1.0.0//src/hidden.rs:2 + kind: direct + confidence: high + call_sites: + - caller: cargo:dep@1.0.0//src/hidden.rs//reexported + site: cargo:dep@1.0.0//src/hidden.rs:2 + reached: true + - target: dep//inner_sink + status: found + path: + - key: src/main.rs//main + - key: src/main.rs//local + site: src/main.rs:16 + kind: direct + confidence: high + - key: dep//inner + site: src/main.rs:21 + kind: direct + confidence: high + - key: cargo:dep@1.0.0//src/lib.rs//inner + site: cargo:dep@1.0.0//src/lib.rs:24 + kind: package + confidence: high + - key: cargo:dep@1.0.0//src/lib.rs//inner_sink + site: cargo:dep@1.0.0//src/lib.rs:25 + kind: direct + confidence: high + call_sites: + - caller: cargo:dep@1.0.0//src/lib.rs//inner + site: cargo:dep@1.0.0//src/lib.rs:25 + reached: true + - target: dep//unused_sink + status: not-found + call_sites: + - caller: cargo:dep@1.0.0//src/lib.rs//unused + site: cargo:dep@1.0.0//src/lib.rs:31 + reached: false diff --git a/test/projectgraph/cases/rust/use-dependency-item/project/Cargo.toml b/test/projectgraph/cases/rust/use-dependency-item/project/Cargo.toml new file mode 100644 index 00000000..730014fa --- /dev/null +++ b/test/projectgraph/cases/rust/use-dependency-item/project/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" + +[dependencies] +dep = "1.0.0" diff --git a/test/projectgraph/cases/rust/use-dependency-item/project/src/main.rs b/test/projectgraph/cases/rust/use-dependency-item/project/src/main.rs new file mode 100644 index 00000000..9ff577bc --- /dev/null +++ b/test/projectgraph/cases/rust/use-dependency-item/project/src/main.rs @@ -0,0 +1,22 @@ +use dep::top; +use dep::renamed as r; +use dep::{other, Type}; +use dep::sub::nested; +use dep::reexported; +#[allow(unused_imports)] +use dep::unused; + +fn main() { + top(); + r(); + other(); + nested(); + Type::new().run(); + reexported(); + local(); +} + +fn local() { + use dep::inner; + inner(); +} diff --git a/test/projectgraph/cases/rust/use-dependency-item/project/vendor/dep/Cargo.toml b/test/projectgraph/cases/rust/use-dependency-item/project/vendor/dep/Cargo.toml new file mode 100644 index 00000000..537b4e47 --- /dev/null +++ b/test/projectgraph/cases/rust/use-dependency-item/project/vendor/dep/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "dep" +version = "1.0.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/use-dependency-item/project/vendor/dep/src/hidden.rs b/test/projectgraph/cases/rust/use-dependency-item/project/vendor/dep/src/hidden.rs new file mode 100644 index 00000000..c094c095 --- /dev/null +++ b/test/projectgraph/cases/rust/use-dependency-item/project/vendor/dep/src/hidden.rs @@ -0,0 +1,5 @@ +pub fn reexported() { + hidden_sink(); +} + +fn hidden_sink() {} diff --git a/test/projectgraph/cases/rust/use-dependency-item/project/vendor/dep/src/lib.rs b/test/projectgraph/cases/rust/use-dependency-item/project/vendor/dep/src/lib.rs new file mode 100644 index 00000000..5bd6dc92 --- /dev/null +++ b/test/projectgraph/cases/rust/use-dependency-item/project/vendor/dep/src/lib.rs @@ -0,0 +1,48 @@ +pub mod sub; +mod hidden; + +pub use hidden::reexported; + +pub fn top() { + sink(); +} + +pub fn sink() {} + +pub fn renamed() { + renamed_sink(); +} + +fn renamed_sink() {} + +pub fn other() { + other_sink(); +} + +fn other_sink() {} + +pub fn inner() { + inner_sink(); +} + +fn inner_sink() {} + +pub fn unused() { + unused_sink(); +} + +fn unused_sink() {} + +pub struct Type; + +impl Type { + pub fn new() -> Type { + Type + } + + pub fn run(&self) { + run_sink(); + } +} + +fn run_sink() {} diff --git a/test/projectgraph/cases/rust/use-dependency-item/project/vendor/dep/src/sub.rs b/test/projectgraph/cases/rust/use-dependency-item/project/vendor/dep/src/sub.rs new file mode 100644 index 00000000..92d365b9 --- /dev/null +++ b/test/projectgraph/cases/rust/use-dependency-item/project/vendor/dep/src/sub.rs @@ -0,0 +1,5 @@ +pub fn nested() { + nested_sink(); +} + +fn nested_sink() {} diff --git a/test/projectgraph/cases/rust/use-dependency-item/summaries/cargo_dep_1.0.0.json b/test/projectgraph/cases/rust/use-dependency-item/summaries/cargo_dep_1.0.0.json new file mode 100644 index 00000000..f209d93e --- /dev/null +++ b/test/projectgraph/cases/rust/use-dependency-item/summaries/cargo_dep_1.0.0.json @@ -0,0 +1,86 @@ +{ + "version": "3", + "index_version": "16", + "link_version": "18", + "unit": "cargo:dep@1.0.0", + "symbols": [ + { + "key": "dep//Type", + "definitions": [ + "cargo:dep@1.0.0//src/lib.rs//Type" + ], + "reaches": [] + }, + { + "key": "dep//Type//new", + "definitions": [ + "cargo:dep@1.0.0//src/lib.rs//Type//new" + ], + "reaches": [] + }, + { + "key": "dep//Type//run", + "definitions": [ + "cargo:dep@1.0.0//src/lib.rs//Type//run" + ], + "reaches": [] + }, + { + "key": "dep//inner", + "definitions": [ + "cargo:dep@1.0.0//src/lib.rs//inner" + ], + "reaches": [] + }, + { + "key": "dep//other", + "definitions": [ + "cargo:dep@1.0.0//src/lib.rs//other" + ], + "reaches": [] + }, + { + "key": "dep//reexported", + "definitions": [ + "cargo:dep@1.0.0//src/hidden.rs//reexported" + ], + "reaches": [] + }, + { + "key": "dep//renamed", + "definitions": [ + "cargo:dep@1.0.0//src/lib.rs//renamed" + ], + "reaches": [] + }, + { + "key": "dep//sink", + "definitions": [ + "cargo:dep@1.0.0//src/lib.rs//sink" + ], + "reaches": [] + }, + { + "key": "dep//sub//nested", + "definitions": [ + "cargo:dep@1.0.0//src/sub.rs//nested" + ], + "reaches": [] + }, + { + "key": "dep//top", + "definitions": [ + "cargo:dep@1.0.0//src/lib.rs//top" + ], + "reaches": [] + }, + { + "key": "dep//unused", + "definitions": [ + "cargo:dep@1.0.0//src/lib.rs//unused" + ], + "reaches": [] + } + ], + "targets": [] +} diff --git a/test/projectgraph/cases/typescript/tsx-elements/expect.yaml b/test/projectgraph/cases/typescript/tsx-elements/expect.yaml new file mode 100644 index 00000000..19b63a6b --- /dev/null +++ b/test/projectgraph/cases/typescript/tsx-elements/expect.yaml @@ -0,0 +1,119 @@ +source: safedep:hand +description: The same JSX forms as javascript/jsx-elements, in .tsx files. App renders Child, the imported Other and the member table.Row as JSX elements, and passes press to a button. Each element name and the prop give a reference, so the search reaches what each component calls. The lower case element b is a tag of the host, a string, so it gives no reference to the imported b. +roots: + - key: src/main.ts + kind: manifest + rule: node.package-json +edges: + - from: src/main.ts + to: src/App.tsx//App + kind: reference + confidence: low + site: src/main.ts:4 + - from: src/App.tsx//App + to: src/App.tsx//Child + kind: reference + confidence: low + site: src/App.tsx:18 + - from: src/App.tsx//App + to: src/Other.tsx//Other + kind: reference + confidence: low + site: src/App.tsx:19 + - from: src/App.tsx//App + to: src/App.tsx//table//Row + kind: reference + confidence: low + site: src/App.tsx:20 + - from: src/App.tsx//App + to: src/App.tsx//press + kind: reference + confidence: low + site: src/App.tsx:21 +no_edges: + - from: src/App.tsx//App + to: plug//b +unresolved: [] +results: + - target: plug//danger + status: found + path: + - key: src/main.ts + - key: src/App.tsx//App + site: src/main.ts:4 + kind: reference + confidence: low + - key: src/App.tsx//Child + site: src/App.tsx:18 + kind: reference + confidence: low + - key: plug//danger + site: src/App.tsx:5 + kind: direct + confidence: high + call_sites: + - caller: src/App.tsx//Child + site: src/App.tsx:5 + reached: true + - target: plug//odanger + status: found + path: + - key: src/main.ts + - key: src/App.tsx//App + site: src/main.ts:4 + kind: reference + confidence: low + - key: src/Other.tsx//Other + site: src/App.tsx:19 + kind: reference + confidence: low + - key: plug//odanger + site: src/Other.tsx:4 + kind: direct + confidence: high + call_sites: + - caller: src/Other.tsx//Other + site: src/Other.tsx:4 + reached: true + - target: plug//viaprop + status: found + path: + - key: src/main.ts + - key: src/App.tsx//App + site: src/main.ts:4 + kind: reference + confidence: low + - key: src/App.tsx//table//Row + site: src/App.tsx:20 + kind: reference + confidence: low + - key: plug//viaprop + site: src/App.tsx:9 + kind: direct + confidence: high + call_sites: + - caller: src/App.tsx//table//Row + site: src/App.tsx:9 + reached: true + - target: plug//onpress + status: found + path: + - key: src/main.ts + - key: src/App.tsx//App + site: src/main.ts:4 + kind: reference + confidence: low + - key: src/App.tsx//press + site: src/App.tsx:21 + kind: reference + confidence: low + - key: plug//onpress + site: src/App.tsx:12 + kind: direct + confidence: high + call_sites: + - caller: src/App.tsx//press + site: src/App.tsx:12 + reached: true + - target: plug//b + status: no-such-target diff --git a/test/projectgraph/cases/typescript/tsx-elements/project/package.json b/test/projectgraph/cases/typescript/tsx-elements/project/package.json new file mode 100644 index 00000000..e6af785c --- /dev/null +++ b/test/projectgraph/cases/typescript/tsx-elements/project/package.json @@ -0,0 +1,4 @@ +{ + "name": "tsx-elements", + "bin": { "app": "src/main.ts" } +} diff --git a/test/projectgraph/cases/typescript/tsx-elements/project/src/App.tsx b/test/projectgraph/cases/typescript/tsx-elements/project/src/App.tsx new file mode 100644 index 00000000..2da84acc --- /dev/null +++ b/test/projectgraph/cases/typescript/tsx-elements/project/src/App.tsx @@ -0,0 +1,25 @@ +import { danger, viaprop, onpress, b } from 'plug'; +import { Other } from './Other'; + +function Child(): JSX.Element { + danger(); + return
; +} + +const table = { Row: () => {viaprop()} }; + +function press() { + onpress(); +} + +export default function App() { + return ( +
+ + + + + bold +
+ ); +} diff --git a/test/projectgraph/cases/typescript/tsx-elements/project/src/Other.tsx b/test/projectgraph/cases/typescript/tsx-elements/project/src/Other.tsx new file mode 100644 index 00000000..bd3485a4 --- /dev/null +++ b/test/projectgraph/cases/typescript/tsx-elements/project/src/Other.tsx @@ -0,0 +1,6 @@ +import { odanger } from 'plug'; + +export function Other() { + odanger(); + return null; +} diff --git a/test/projectgraph/cases/typescript/tsx-elements/project/src/main.ts b/test/projectgraph/cases/typescript/tsx-elements/project/src/main.ts new file mode 100644 index 00000000..d9478ff0 --- /dev/null +++ b/test/projectgraph/cases/typescript/tsx-elements/project/src/main.ts @@ -0,0 +1,4 @@ +import { render } from 'react-dom'; +import App from './App'; + +render(App, document.body); diff --git a/test/projectgraph/cases_test.go b/test/projectgraph/cases_test.go index 2ece6fec..2f97d46f 100644 --- a/test/projectgraph/cases_test.go +++ b/test/projectgraph/cases_test.go @@ -103,10 +103,11 @@ type edge struct { } type unresolved struct { - Caller string `yaml:"caller"` - Site string `yaml:"site"` - Reason string `yaml:"reason"` - Method string `yaml:"method,omitempty"` + Caller string `yaml:"caller"` + Site string `yaml:"site"` + Reason string `yaml:"reason"` + Method string `yaml:"method,omitempty"` + Callback bool `yaml:"callback,omitempty"` } // result is one reach query of a case and its result. Sources are the @@ -363,7 +364,7 @@ func edgesOf(g *projectgraph.Graph) []edge { func unresolvedOf(g *projectgraph.Graph) []unresolved { out := []unresolved{} for _, u := range g.Unresolved { - out = append(out, unresolved{Caller: u.Caller, Site: site(u.Site), Reason: string(u.Reason), Method: u.Method}) + out = append(out, unresolved{Caller: u.Caller, Site: site(u.Site), Reason: string(u.Reason), Method: u.Method, Callback: u.Callback}) } return out } diff --git a/test/projectgraph/methods_govulncheck_test.go b/test/projectgraph/methods_govulncheck_test.go new file mode 100644 index 00000000..92cedccf --- /dev/null +++ b/test/projectgraph/methods_govulncheck_test.go @@ -0,0 +1,290 @@ +//go:build projectgraph + +package projectgraph + +import ( + "bytes" + "encoding/json" + "errors" + "io" + "maps" + "os" + "os/exec" + "path/filepath" + "slices" + "strconv" + "strings" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// govulncheckVersion is the version of govulncheck that the method measure +// compares with. Install it with +// go install golang.org/x/vuln/cmd/govulncheck@v1.9.0. +const govulncheckVersion = "v1.9.0" + +// vulnCheck runs govulncheck on each Go project of the method measure and +// compares its findings with the answers of reach. A zero vulnCheck +// compares nothing. +type vulnCheck struct { + bin, db, gomod string + // listed holds the import paths of each advisory that list symbols. + // govulncheck reports any called symbol of a module or a package that + // an advisory names with no symbol list, and the measure has no target + // for such an advisory. + listed map[string]map[string]bool + agreement vulnAgreement +} + +// newVulnCheck finds govulncheck on the PATH and writes a vulnerability +// database of the Go advisories of the measure, so govulncheck checks +// exactly these advisories and needs no network. +func newVulnCheck(t *testing.T, corpus string, advisories map[string][]advisory, measureGo bool) *vulnCheck { + if !measureGo { + return &vulnCheck{} + } + bin, err := exec.LookPath("govulncheck") + if err != nil { + t.Logf("govulncheck is not on the PATH, so the measure does not compare with it: install govulncheck@%s", govulncheckVersion) + return &vulnCheck{} + } + db := t.TempDir() + writeVulnDB(t, db, advisories) + listed := map[string]map[string]bool{} + for _, list := range advisories { + for _, a := range list { + listed[a.ID] = map[string]bool{} + for _, imp := range a.Imports { + listed[a.ID][imp.Path] = len(imp.Symbols) > 0 + } + } + } + return &vulnCheck{bin: bin, db: db, gomod: filepath.Join(corpus, "gomod"), listed: listed} +} + +// compare runs govulncheck on the project in dir and adds the answers of +// reach for the project to the agreement. +func (v *vulnCheck) compare(t *testing.T, project, dir string, answers []methodAnswer) { + if v.bin == "" { + return + } + v.agreement.add(project, answers, v.reached(t, dir)) +} + +func (v *vulnCheck) rows() []string { + if v.bin == "" { + return nil + } + return v.agreement.rows() +} + +// reached returns the keys of the vulnerable symbols that govulncheck +// finds a call stack to in the project in dir. The module cache of the +// corpus holds the build list of the project, so the go command runs with +// no proxy. It may add go.sum lines, so it runs on a copy of the project. +func (v *vulnCheck) reached(t *testing.T, dir string) map[string]bool { + work := t.TempDir() + require.NoError(t, os.CopyFS(work, os.DirFS(dir))) + cmd := exec.Command(v.bin, "-mode", "source", "-json", "-db", "file://"+v.db, "./...") + cmd.Dir = work + cmd.Env = append(os.Environ(), "GOPROXY=off", "GOSUMDB=off", "GOFLAGS=-mod=mod", "GOMODCACHE="+v.gomod, "GOTOOLCHAIN=local", "GOWORK=off") + var stderr bytes.Buffer + cmd.Stderr = &stderr + out, err := cmd.Output() + require.NoError(t, err, stderr.String()) + return v.reachedIn(t, out) +} + +// reachedIn returns the keys of the vulnerable symbols that the findings +// of a govulncheck -json output reach with a call stack, for the advisories +// that list symbols. It fails when the output comes from another version +// of govulncheck, because the call graph of another version can reach +// other symbols. +func (v *vulnCheck) reachedIn(t *testing.T, out []byte) map[string]bool { + reached := map[string]bool{} + dec := json.NewDecoder(bytes.NewReader(out)) + for { + var m struct { + vulnMessage + Config *struct { + ScannerVersion string `json:"scanner_version"` + } `json:"config"` + } + err := dec.Decode(&m) + if errors.Is(err, io.EOF) { + return reached + } + require.NoError(t, err) + if m.Config != nil { + require.Equal(t, govulncheckVersion, m.Config.ScannerVersion, "install govulncheck@%s", govulncheckVersion) + } + if f := m.Finding; f != nil && len(f.Trace) > 0 && f.Trace[0].Function != "" && v.listed[f.OSV][f.Trace[0].Package] { + reached[frameKey(f.Trace[0])] = true + } + } +} + +// writeVulnDB writes the Go advisories of the measure as a vulnerability +// database in the layout of vuln.go.dev, which govulncheck -db file:// +// reads: index/db.json, index/modules.json and ID/.json. +func writeVulnDB(t *testing.T, dir string, advisories map[string][]advisory) { + modified := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC) + type vuln struct { + ID string `json:"id"` + Modified time.Time `json:"modified"` + } + type module struct { + Path string `json:"path"` + Vulns []vuln `json:"vulns"` + } + var index []module + entries := map[string][]any{} + for _, path := range slices.Sorted(maps.Keys(advisories)) { + m := module{Path: path} + for _, a := range advisories[path] { + m.Vulns = append(m.Vulns, vuln{ID: a.ID, Modified: modified}) + entries[a.ID] = append(entries[a.ID], map[string]any{ + "package": map[string]string{"name": path, "ecosystem": "Go"}, + "ranges": []any{map[string]any{"type": "SEMVER", "events": a.Ranges}}, + "ecosystem_specific": map[string]any{"imports": a.Imports}, + }) + } + index = append(index, m) + } + writeJSON(t, filepath.Join(dir, "index", "db.json"), map[string]time.Time{"modified": modified}) + writeJSON(t, filepath.Join(dir, "index", "modules.json"), index) + for id, affected := range entries { + writeJSON(t, filepath.Join(dir, "ID", id+".json"), map[string]any{ + "schema_version": "1.3.1", + "id": id, + "modified": modified, + "details": "", + "affected": affected, + }) + } +} + +func writeJSON(t *testing.T, file string, v any) { + data, err := json.Marshal(v) + require.NoError(t, err) + require.NoError(t, os.MkdirAll(filepath.Dir(file), 0o755)) + require.NoError(t, os.WriteFile(file, data, 0o644)) +} + +// The classes of a reach answer for the comparison with govulncheck. +const ( + answerFound = "found" + answerMissing = "not-found" + answerUncertain = "uncertain" +) + +// answerClass returns the class of an outcome of the measure. found/low +// is found, and no-such-target says that no path exists, as not-found does. +func answerClass(outcome string) string { + switch status, _, _ := strings.Cut(outcome, "/"); status { + case "found": + return answerFound + case "not-found", "no-such-target": + return answerMissing + } + return answerUncertain +} + +// vulnAgreement counts the answers of reach for the targets that +// govulncheck reaches and for the targets that it does not reach, by the +// class of the answer. A missing answer for a target that govulncheck +// reaches is a false negative. A found answer for a target that +// govulncheck does not reach is a row to review, because reach also starts +// from the library surface of the project. A symbol that govulncheck +// reaches and that is no target of the project shows a key that the two +// tools write in two ways, which would hide a false negative. +type vulnAgreement struct { + reached, quiet map[string]int + falseNegatives, review, unmatched []string +} + +func (a *vulnAgreement) add(project string, answers []methodAnswer, reached map[string]bool) { + if a.reached == nil { + a.reached, a.quiet = map[string]int{}, map[string]int{} + } + targets := map[string]bool{} + for _, ans := range answers { + targets[ans.key] = true + class := answerClass(ans.outcome) + switch { + case !reached[ans.key]: + a.quiet[class]++ + if class == answerFound { + a.review = append(a.review, ans.detail) + } + case class == answerMissing: + a.reached[class]++ + a.falseNegatives = append(a.falseNegatives, ans.detail) + default: + a.reached[class]++ + } + } + for _, key := range slices.Sorted(maps.Keys(reached)) { + if !targets[key] { + a.unmatched = append(a.unmatched, "project="+project+" symbol="+key) + } + } +} + +// rows writes the counts of each direction, the share of each direction +// on which the two tools agree, and one row per false negative, per +// answer to review and per unmatched symbol. +func (a vulnAgreement) rows() []string { + total := func(m map[string]int) int { + n := 0 + for _, c := range m { + n += c + } + return n + } + reached, quiet := total(a.reached), total(a.quiet) + rows := []string{ + "set=govulncheck-agreement language=go version=" + govulncheckVersion, + " govulncheck-reached=" + strconv.Itoa(reached) + " found=" + strconv.Itoa(a.reached[answerFound]) + " uncertain=" + strconv.Itoa(a.reached[answerUncertain]) + " false-negative=" + strconv.Itoa(len(a.falseNegatives)), + " govulncheck-not-reached=" + strconv.Itoa(quiet) + " not-found=" + strconv.Itoa(a.quiet[answerMissing]) + " uncertain=" + strconv.Itoa(a.quiet[answerUncertain]) + " found=" + strconv.Itoa(a.quiet[answerFound]), + " agree-reached=" + ratio{hit: a.reached[answerFound], all: reached}.String() + " agree-not-reached=" + ratio{hit: a.quiet[answerMissing], all: quiet}.String(), + " unmatched=" + strconv.Itoa(len(a.unmatched)), + } + for _, group := range []struct { + name string + lines []string + }{{"false-negative", a.falseNegatives}, {"review", a.review}, {"unmatched", a.unmatched}} { + for _, line := range group.lines { + rows = append(rows, " "+group.name+" "+line) + } + } + return rows +} + +func TestVulnAgreement(t *testing.T) { + answer := func(key, outcome string) methodAnswer { + return methodAnswer{key: key, outcome: outcome, detail: "target=" + key + " outcome=" + outcome} + } + answers := []methodAnswer{ + answer("x//Missed", "not-found"), + answer("x//Absent", "no-such-target"), + answer("x//Called", "found/low"), + answer("x//Unsure", "uncertain/ambiguous"), + answer("x//Extra", "found"), + answer("x//Quiet", "not-found"), + } + reached := map[string]bool{"x//Missed": true, "x//Absent": true, "x//Called": true, "x//Unsure": true, "x//Other": true} + + var a vulnAgreement + a.add("p", answers, reached) + + assert.Equal(t, []string{"target=x//Missed outcome=not-found", "target=x//Absent outcome=no-such-target"}, a.falseNegatives) + assert.Equal(t, []string{"target=x//Extra outcome=found"}, a.review) + assert.Equal(t, []string{"project=p symbol=x//Other"}, a.unmatched) + assert.Equal(t, map[string]int{answerMissing: 2, answerFound: 1, answerUncertain: 1}, a.reached) + assert.Equal(t, map[string]int{answerFound: 1, answerMissing: 1}, a.quiet) + assert.Contains(t, a.rows(), " govulncheck-reached=4 found=1 uncertain=1 false-negative=2") +} diff --git a/test/projectgraph/methods_test.go b/test/projectgraph/methods_test.go index 4163f7ff..ffbe958e 100644 --- a/test/projectgraph/methods_test.go +++ b/test/projectgraph/methods_test.go @@ -24,7 +24,9 @@ import ( "github.com/safedep/code/langcode" "github.com/safedep/code/plugin/indexer" "github.com/safedep/code/projectgraph" + "github.com/safedep/code/projectgraph/modules" "github.com/safedep/code/projectgraph/reach" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "golang.org/x/mod/module" "golang.org/x/mod/semver" @@ -60,8 +62,8 @@ type advisory struct { ID string `json:"id"` Source string `json:"source,omitempty"` Ranges []struct { - Introduced string `json:"introduced"` - Fixed string `json:"fixed"` + Introduced string `json:"introduced,omitempty"` + Fixed string `json:"fixed,omitempty"` } `json:"ranges"` Imports []struct { Path string `json:"path"` @@ -193,14 +195,38 @@ func ecosystemOf(language string) (string, langcode.Code) { return "cargo", langcode.Rust } +// methodNoStdEnv names a switch that leaves out the unit of the Go +// standard library, so a run shows what the unit changes. +const methodNoStdEnv = "PROJECTGRAPH_METHOD_NO_STD" + +// goReleases returns the Go release of each Go project of the corpus, as +// scripts/fetch_method_corpus.sh records it from the go line of the +// go.mod of the project. +func goReleases(t *testing.T, corpus string) map[string]string { + data, err := os.ReadFile(filepath.Join(corpus, "gostd", "releases.txt")) + require.NoError(t, err, "run scripts/fetch_method_corpus.sh again to fetch the Go standard library") + releases := map[string]string{} + for _, line := range strings.Split(strings.TrimSpace(string(data)), "\n") { + project, release, ok := strings.Cut(line, " ") + require.True(t, ok, line) + releases[project] = release + } + return releases +} + // methodDir returns the directory of a project or a dependency in the // corpus, and its unit. A PyPI dependency is an unpacked wheel, so its // directory has the layout of site-packages, and the unit is rooted there. +// The Go standard library is the dependency std@, as std@go1.16, +// and its directory is the src tree of the release. func methodDir(t *testing.T, corpus, language, item string) (string, string) { name, version, _ := strings.Cut(item, "@") eco, _ := ecosystemOf(language) switch eco { case "go": + if name == modules.StdModule { + return filepath.Join(corpus, "gostd", version, "src"), "go:" + item + } escaped, err := module.EscapePath(name) require.NoError(t, err) return filepath.Join(corpus, "gomod", filepath.FromSlash(escaped)+"@"+version), "go:" + item @@ -276,6 +302,12 @@ func TestMethodTargets(t *testing.T) { }) } projects, advisories := readMethodCorpus(t) + measureGo := slices.ContainsFunc(languages, func(l core.Language) bool { return l.Meta().Code == langcode.Go }) + var releases map[string]string + if measureGo && os.Getenv(methodNoStdEnv) == "" { + releases = goReleases(t, corpus) + } + vulncheck := newVulnCheck(t, corpus, advisories["go"], measureGo) counts := methodCounts{} var lines, details []string @@ -284,7 +316,13 @@ func TestMethodTargets(t *testing.T) { cfg := codefs.LocalFileSystemConfig{AppDirectories: []string{dir}, ImportUnits: map[string]string{}, ExcludePatterns: codefs.DefaultSkipPatterns(dir)} units := map[string]iofs.FS{} var targets []methodTarget - for _, dep := range p.deps { + deps := p.deps + if p.language == "go" && releases != nil { + release, ok := releases[p.source] + require.True(t, ok, "gostd/releases.txt has no Go release of %s", p.source) + deps = append(slices.Clone(deps), modules.StdModule+"@"+release) + } + for _, dep := range deps { depDir, unit := methodDir(t, corpus, p.language, dep) cfg.ImportDirectories = append(cfg.ImportDirectories, depDir) cfg.ImportUnits[depDir] = unit @@ -307,11 +345,16 @@ func TestMethodTargets(t *testing.T) { require.NoError(t, err) methods := receiverReasons(t, g, nonTestRoots(g)) project := methodCounts{} + answers := make([]methodAnswer, len(results)) for i, r := range results { o := outcome(r, methods) counts.add(p.language, targets[i].kind, o) project.add(p.language, targets[i].kind, o) - details = append(details, methodDetail(p.name, targets[i].key, o, r)) + answers[i] = methodAnswer{key: targets[i].key, outcome: o, detail: methodDetail(p.name, targets[i].key, o, r)} + details = append(details, answers[i].detail) + } + if p.language == "go" { + vulncheck.compare(t, p.name, dir, answers) } lines = append(lines, "project="+p.name+" files="+strconv.Itoa(len(g.Files))+" truncated="+strconv.Itoa(len(g.Truncated))+" problems="+strconv.Itoa(len(g.Problems))) for _, row := range project.rows() { @@ -320,6 +363,7 @@ func TestMethodTargets(t *testing.T) { } rows := append([]string{"set=methods projects=" + strconv.Itoa(len(projects))}, counts.rows()...) rows = append(rows, lines...) + rows = append(rows, vulncheck.rows()...) t.Log("\n" + strings.Join(rows, "\n")) if out := os.Getenv("PROJECTGRAPH_METHOD_REPORT"); out != "" { require.NoError(t, os.WriteFile(out, []byte(strings.Join(rows, "\n")+"\n"), 0o644)) @@ -327,6 +371,13 @@ func TestMethodTargets(t *testing.T) { if out := os.Getenv(methodDetailEnv); out != "" { require.NoError(t, os.WriteFile(out, []byte(strings.Join(details, "\n")+"\n"), 0o644)) } + assert.Empty(t, vulncheck.agreement.falseNegatives, "govulncheck reaches these targets, and reach says that no path exists") +} + +// methodAnswer is the answer of reach for one target of a project, and +// the detail line of the target. +type methodAnswer struct { + key, outcome, detail string } // methodDetailEnv names the file that gets one line per target: the diff --git a/test/projectgraph/receivers_test.go b/test/projectgraph/receivers_test.go index 4c5fc597..47be09f3 100644 --- a/test/projectgraph/receivers_test.go +++ b/test/projectgraph/receivers_test.go @@ -117,6 +117,12 @@ var goReceiverRows = []receiverRow{ want: "uncertain/no-dependency-source", noDeps: true, }, + { + name: "method of a first-party type on new", + source: "type thing struct{}\n\nfunc (t *thing) vuln() {\n\tdep.NewRunner()\n}\n\nfunc run() {\n\tnew(thing).vuln()\n}", + target: "example.com//dep//NewRunner", + want: "found/low", + }, } var rustReceiverRows = []receiverRow{ @@ -168,6 +174,30 @@ var rustReceiverRows = []receiverRow{ target: "dep//Thing//insert", want: "found", }, + { + name: "anyhow result with ?", + source: "fn run() -> anyhow::Result<()> {\n let mut t = dep::parse_thing()?;\n t.insert(1);\n Ok(())\n}", + target: "dep//Thing//insert", + want: "found", + }, + { + name: "chained call after ? on a std::io::Result", + source: "fn run() -> std::io::Result<()> {\n dep::read_thing()?.insert(1);\n Ok(())\n}", + target: "dep//Thing//insert", + want: "found", + }, + { + name: "chained call after ? with a name of an Option method", + source: "fn run() -> Option<()> {\n dep::try_thing()?.insert(1);\n Some(())\n}", + target: "dep//Thing//insert", + want: "found", + }, + { + name: "option method with the name of a method of the value inside", + source: "fn run() {\n dep::try_thing().inspect(|t| {});\n}", + target: "dep//Thing//inspect", + want: "uncertain/unknown-receiver", + }, { name: "chained call", source: "fn run() {\n dep::new_thing().insert(1);\n}", @@ -198,6 +228,24 @@ var rustReceiverRows = []receiverRow{ target: "dep//Thing//run", want: "found", }, + { + name: "boxed trait object", + source: "fn run() {\n let d: Box = Box::new(Thing::::new());\n d.run();\n}", + target: "dep//Thing//run", + want: "found", + }, + { + name: "reference-counted trait object", + source: "use std::rc::Rc;\n\nfn run() {\n let d: Rc = Rc::new(Thing::::new());\n d.run();\n}", + target: "dep//Thing//run", + want: "found", + }, + { + name: "boxed value with no declared type", + source: "fn run() {\n let d = Box::new(Thing::::new());\n d.run();\n}", + target: "dep//Thing//run", + want: "uncertain/unknown-receiver", + }, { name: "generic bound", source: "fn run(d: &T) {\n d.run();\n}", @@ -210,6 +258,120 @@ var rustReceiverRows = []receiverRow{ target: "dep//Job//start", want: "uncertain/trait-receiver", }, + { + name: "parameter that a let shadows", + source: "struct Local;\nimpl Local {\n fn inspect(&self) {}\n}\n\nfn run(t: Local) {\n let t = dep::new_thing();\n t.inspect();\n}", + target: "dep//Thing//inspect", + want: "found", + }, + { + name: "parameter that a macro of std reads", + source: "fn run(t: Thing) {\n println!(\"{}\", t.len());\n t.inspect();\n}", + target: "dep//Thing//inspect", + want: "found", + }, + { + name: "parameter that an if let shadows", + source: "struct Local;\nimpl Local {\n fn inspect(&self) {}\n}\n\nfn run(t: Local) {\n if let Some(t) = dep::try_thing() {\n t.inspect();\n }\n}", + target: "dep//Thing//inspect", + want: "uncertain/unknown-receiver", + }, + { + name: "parameter that a match arm shadows", + source: "struct Local;\nimpl Local {\n fn inspect(&self) {}\n}\n\nfn run(t: Local) {\n match dep::try_thing() {\n Some(t) => t.inspect(),\n None => {}\n }\n}", + target: "dep//Thing//inspect", + want: "uncertain/unknown-receiver", + }, + { + name: "parameter that a closure parameter shadows", + source: "struct Local;\nimpl Local {\n fn inspect(&self) {}\n}\n\nfn run(t: Local) {\n let f = |t: Thing| t.inspect();\n f(dep::new_thing());\n}", + target: "dep//Thing//inspect", + want: "uncertain/unknown-receiver", + }, + { + name: "parameter that a let of a macro shadows", + source: "struct Local;\nimpl Local {\n fn inspect(&self) {}\n}\n\nmacro_rules! bind {\n ($n:ident) => {\n let $n = dep::new_thing();\n };\n}\n\nfn run(t: Local) {\n bind!(t);\n t.inspect();\n}", + target: "dep//Thing//inspect", + want: "uncertain/unknown-receiver", + }, + { + name: "typed let that a static shadows", + source: "struct Local;\nimpl Local {\n fn inspect(&self) {}\n}\n\nfn run(l: Local) {\n let t: Local = l;\n {\n static t: Thing = Thing::new();\n t.inspect();\n }\n}", + target: "dep//Thing//inspect", + want: "uncertain/unknown-receiver", + }, + { + name: "let that a static after the call shadows", + source: "struct Local;\nimpl Local {\n fn inspect(&self) {}\n}\n\nfn mk() -> Local {\n Local\n}\n\nfn run() {\n let t = mk();\n {\n t.inspect();\n static t: Thing = Thing::new();\n }\n}", + target: "dep//Thing//inspect", + want: "uncertain/unknown-receiver", + }, + { + name: "let that an if let shadows", + source: "struct Local;\nimpl Local {\n fn inspect(&self) {}\n}\n\nfn mk() -> Local {\n Local\n}\n\nfn run() {\n let t = mk();\n if let Some(t) = dep::try_thing() {\n t.inspect();\n }\n}", + target: "dep//Thing//inspect", + want: "uncertain/unknown-receiver", + }, + { + name: "std function that returns its argument", + source: "fn run(t: Thing) {\n std::convert::identity(t).inspect();\n}", + target: "dep//Thing//inspect", + want: "uncertain/unknown-receiver", + }, + { + name: "typed let of a type alias of the function", + source: "struct Local;\nimpl Local {\n fn inspect(&self) {}\n}\n\nfn run() {\n type Local = dep::Thing;\n let t: Local = dep::new_thing();\n t.inspect();\n}", + target: "dep//Thing//inspect", + want: "found", + }, + { + name: "typed let of a use of the function", + source: "struct Local;\nimpl Local {\n fn inspect(&self) {}\n}\n\nfn run() {\n use dep::Thing as Local;\n let t: Local = dep::new_thing();\n t.inspect();\n}", + target: "dep//Thing//inspect", + want: "found", + }, + { + name: "typed let of a type alias of an inline module", + source: "struct Local;\nimpl Local {\n fn inspect(&self) {}\n}\n\nmod inner {\n pub type Local = dep::Thing;\n pub fn go() {\n let t: Local = dep::new_thing();\n t.inspect();\n }\n}\n\nfn run() {\n inner::go();\n}", + target: "dep//Thing//inspect", + want: "found", + }, + { + name: "typed let of a glob use of a block", + source: "struct Local;\nimpl Local {\n fn inspect(&self) {}\n}\n\nfn run() {\n mod m {\n pub use dep::Thing as Local;\n }\n use m::*;\n let t: Local = dep::new_thing();\n t.inspect();\n}", + target: "dep//Thing//inspect", + want: "found", + }, + { + name: "parameter of a use of an inline module", + source: "struct Local;\nimpl Local {\n fn inspect(&self) {}\n}\n\nmod inner {\n use dep::Thing as Local;\n pub fn go(t: Local) {\n t.inspect();\n }\n}\n\nfn run() {\n inner::go(dep::new_thing());\n}", + target: "dep//Thing//inspect", + want: "uncertain/unknown-receiver", + }, + { + name: "parameter of a type alias of the outer function", + source: "struct Local;\nimpl Local {\n fn inspect(&self) {}\n}\n\nfn run() {\n type Local = dep::Thing;\n fn inner(t: Local) {\n t.inspect();\n }\n inner(dep::new_thing());\n}", + target: "dep//Thing//inspect", + want: "uncertain/unknown-receiver", + }, + { + name: "parameter of a type parameter of the impl", + source: "struct Local;\nimpl Local {\n fn run(&self) {}\n}\n\nstruct Holder(T);\nimpl Holder {\n fn go(t: Local) {\n t.run();\n }\n}\n\nfn run() {\n Holder::>::go(dep::new_thing());\n}", + target: "dep//Thing//run", + want: "uncertain/unknown-receiver", + }, + { + name: "typed let of a type parameter of the impl", + source: "struct Local;\nimpl Local {\n fn run(&self) {}\n}\n\nstruct Holder(T);\nimpl Holder {\n fn go(t: Local) {\n let u: Local = t;\n u.run();\n }\n}\n\nfn run() {\n Holder::>::go(dep::new_thing());\n}", + target: "dep//Thing//run", + want: "uncertain/unknown-receiver", + }, + { + name: "boxed trait object in a module that reads the root", + source: "mod inner {\n use super::*;\n pub fn go() {\n let d: Box = Box::new(Thing::::new());\n d.run();\n }\n}\n\nfn run() {\n inner::go();\n}", + target: "dep//Thing//run", + want: "found", + }, } // pythonReceiverRows are the forms of a Python receiver. A type hint is @@ -324,6 +486,18 @@ var pythonReceiverRows = []receiverRow{ want: "uncertain/no-dependency-source", noDeps: true, }, + { + name: "builtin that returns an argument", + source: "def run(t):\n max(t, t).do()", + target: "dep//Thing//do", + want: "uncertain/no-dependency-source", + }, + { + name: "builtin that returns an element of an argument", + source: "def run(ts):\n next(iter(ts)).do()", + target: "dep//Thing//do", + want: "uncertain/no-dependency-source", + }, } func TestReceiverForms(t *testing.T) { diff --git a/test/projectgraph/report.md b/test/projectgraph/report.md index f2d08968..e9e4941f 100644 --- a/test/projectgraph/report.md +++ b/test/projectgraph/report.md @@ -1360,3 +1360,160 @@ the standard library to the project graph. The case that goes on to `os.execvp`. The reach result still reports the shorter path through the reference edge of the override rule, because the search returns a shortest path. + +### The Go standard library unit + +FOU-561 adds the `src` tree of the Go release of each Go project to the +corpus. The release comes from the `go` line of the go.mod of the +project: go1.12 for colly, go1.13 for goquery and hey, go1.14 for +go-grpc-middleware, go1.15 for sftp, go1.16 for bluemonday, go1.18 for +scany, go1.19 for melody and fiber, and go1.20 for go-rabbitmq. The +measure links the tree as the unit `go:std@`, as vet links +GOROOT. The advisories of the measure name no symbol of the standard +library, so the unit adds no target. It adds the code of the standard +library to each graph, and the types of its receivers. + +The rows come from two runs of the branch of FOU-561 and FOU-563, on +2026-11-14, on the machine above. The run "without" sets +`PROJECTGRAPH_METHOD_NO_STD=1`. The Python and Rust rows do not change. + +| Language | Kind | Run | Targets | found | not-found | no-such-target | unknown-receiver | no-definition | interface-receiver | no-dependency-source | budget | ambiguous | computed-name | +| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | +| Go | function | without | 256 | 51 | 0 | 0 | 3 | 5 | 0 | 2 | 39 | 106 | 50 | +| Go | function | with | 256 | 56 | 0 | 0 | 14 | 5 | 0 | 0 | 0 | 154 | 27 | +| Go | method | without | 1298 | 331 | 0 | 0 | 154 | 71 | 2 | 2 | 109 | 416 | 213 | +| Go | method | with | 1298 | 366 | 0 | 0 | 256 | 132 | 5 | 0 | 0 | 466 | 73 | + +| Run | Go method targets that end as unknown-receiver | Files of the 10 Go graphs | +| --- | --- | --- | +| without | 0.119 (154/1298) | 5,286 | +| with | 0.197 (256/1298) | 23,991 | + +- `found` moves from 331 to 366 Go method targets and from 51 to 56 Go + function targets. A call on a receiver of a standard library type now + has a type, and the graph holds the paths through the standard + library, as a handler that `net/http` calls. +- `unknown-receiver` grows from 154 to 256 Go method targets. The cause + counts a target when the search reaches any call site with an unknown + receiver and the method name of the target. The standard library adds + code that the search reaches, and more sites with the names `String`, + `RoundTrip`, `Close`, `Error` and `Write`. So the share rises, and it is + not a count of the receiver forms that the unit fixes. +- `budget` and `computed-name` fall, and `ambiguous` grows. The budget + records of scany and the computed names of hey are still in the graph. + The first uncertainty of these results is now an ambiguous call of the + standard library, as `regexp/regexp.go` of go1.18 for scany. +- The graphs hold 4.5 times the files. The run of all three languages + takes 356 seconds in place of 124 on this machine. + +These are the sites with an unknown receiver that the most +`unknown-receiver` targets reach. A site concerns each target whose +method name it has. + +| Site | Targets | Call | Receiver form | +| --- | --- | --- | --- | +| `net/http/server.go:3372` of go1.13 | 16 | `h.c.RemoteAddr().String()` | the result of a method of an interface, `net.Addr` | +| x/net `http2/frame.go:1553` | 14 | `hdec.Close()` | a local that a field of the receiver gives, `*hpack.Decoder` | +| x/net `websocket/hybi.go:318` and `:330` | 14 | `w.Close()` | the result of a method of an interface, `io.WriteCloser` | +| x/net `webdav/internal/xml/xml.go:1916` and `:1919` | 11 | `w.Write(...)` | an `io.Writer` parameter | +| x/net `webdav/internal/xml/marshal.go:220` to `:247` | 11 | `p.Write(t)` | a method of the embedded field `*bufio.Writer` | +| hey `requester/report.go:134` | 10 | `buf.String()` | a local of `&bytes.Buffer{}` | +| hey `requester/print.go:92` | 10 | `res.String()` | a local of `new(bytes.Buffer)` | +| `net/http/transport.go:533` of go1.13 | 10 | `pconn.alt.RoundTrip(req)` | an interface field, `http.RoundTripper` | +| `net/http/httputil/reverseproxy.go:249` of go1.13 | 10 | `transport.RoundTrip(outreq)` | an interface local | +| go-grpc-middleware `validator/validator.go:33` | 8 | `err.Error()` | the `error` interface | +| go-grpc-middleware `tracing/opentracing/server_interceptors.go:95` | 8 | `err.Error()` | the `error` interface | + +Of the 1,810 pairs of a target and such a site, 771 sites are in the +code of a module, 457 in the standard library and 582 in a project. +`Error` on the `error` interface and a method of an interface field or +parameter stay records until the graph has the implementations of the +method. A method on the result of an interface method, as +`RemoteAddr().String()`, needs the result type of the interface method. +The link step does not bind three other forms yet, also with the types +of the standard library: the two `bytes.Buffer` locals of hey, a method +of an embedded field, and a local that a field of the receiver gives. +Each one is a candidate for an issue. + +### Agreement with govulncheck + +FOU-563 runs govulncheck v1.9.0 on each Go project of the corpus, with no +network, against the Go advisories of `advisories.json`, and compares +each Go target. `README.md` says how. The rows come from the same runs +as the table above. + +| Run | govulncheck reaches | reach found | reach uncertain | false negative | govulncheck does not reach | reach not-found | reach uncertain | reach found (review) | unmatched | +| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | +| without | 115 | 60 | 55 | 0 | 1439 | 0 | 1117 | 322 | 1 | +| with | 115 | 62 | 53 | 0 | 1439 | 0 | 1079 | 360 | 1 | + +| Run | Agreement, targets that govulncheck reaches | Agreement, targets that govulncheck does not reach | +| --- | --- | --- | +| without | 0.522 (60/115) | 0.000 (0/1439) | +| with | 0.539 (62/115) | 0.000 (0/1439) | + +- No target is a false negative. Reach gives no Go target as `not-found` + or `no-such-target` yet, so the check has no negative answer to test. + The fixes of FOU-557, FOU-558, FOU-559, FOU-560 and FOU-562 turn + uncertain answers into definite ones, and the check then tests each of + them. +- 360 targets that reach finds are rows to review in the output of the + measure: 75 with a path of `high` and `medium` edges, and 285 with a + `low` hop. Reach starts from each exported symbol of a library project. + govulncheck starts from the packages of the project and follows the + calls that its call graph proves. +- The unmatched symbol is `utf16Decoder.Transform` of golang.org/x/text, + of GO-2020-0015. The build list of colly holds x/text at an affected + version, and `corpus.txt` does not list x/text for colly, so the measure + has no target for it. +- govulncheck reaches symbols of the module-wide advisory GO-2026-6372 of + amqp091-go in go-rabbitmq, as `Channel.NotifyClose`. The advisory has + no symbol list and gives no target, so the comparison leaves out its + findings. + +### The reach MVP branch + +The rows come from two runs of the branch of the reach MVP (FOU-556, +FOU-557, FOU-558, FOU-560, FOU-561, FOU-562, FOU-563, FOU-575, FOU-576 and +FOU-578, with the fixes of two reviews), on 2026-11-14, on the machine +above. The run "without" sets `PROJECTGRAPH_METHOD_NO_STD=1`, as vet runs +today. `found` counts `found` and `found/low`. The main rows come from the +same corpus on origin/main at 39cf1e2, without the unit. + +| Language | Kind | Run | Targets | found | not-found | no-such-target | no-dependency-source | unknown-receiver | no-definition | interface-receiver | ambiguous | budget | computed-name | +| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | +| Go | function | main | 256 | 51 | 0 | 0 | 2 | 3 | 5 | 0 | 106 | 39 | 50 | +| Go | function | without | 256 | 58 | 0 | 0 | 193 | 5 | 0 | 0 | 0 | 0 | 0 | +| Go | function | with | 256 | 59 | 0 | 0 | 187 | 8 | 2 | 0 | 0 | 0 | 0 | +| Go | method | main | 1298 | 331 | 0 | 0 | 2 | 154 | 71 | 2 | 416 | 109 | 213 | +| Go | method | without | 1298 | 343 | 0 | 7 | 776 | 156 | 15 | 1 | 0 | 0 | 0 | +| Go | method | with | 1298 | 384 | 0 | 0 | 442 | 253 | 205 | 14 | 0 | 0 | 0 | +| Rust | function | main | 13 | 4 | 0 | 0 | 0 | 0 | 9 | 0 | 0 | 0 | 0 | +| Rust | function | branch | 13 | 5 | 0 | 0 | 0 | 5 | 1 | 0 | 2 | 0 | 0 | +| Rust | method | main | 38 | 16 | 0 | 0 | 0 | 0 | 13 | 0 | 0 | 9 | 0 | +| Rust | method | branch | 38 | 16 | 0 | 0 | 0 | 8 | 1 | 0 | 4 | 9 | 0 | + +| Run | govulncheck reaches | reach found | reach uncertain | false negative | govulncheck does not reach | reach not-found or no-such-target | reach found (review) | +| --- | --- | --- | --- | --- | --- | --- | --- | +| without | 115 | 71 | 44 | 0 | 1439 | 7 | 330 | +| with | 115 | 71 | 44 | 0 | 1439 | 0 | 372 | + +- No target is a false negative in either run. A measure run fails on one. +- Reach finds 71 of the 115 targets that govulncheck reaches. Main found 60. +- The global causes of main are gone for Go: `ambiguous`, `budget` and + `computed-name`. Most of their targets now read `no-dependency-source`. + That record marks code outside the graph that can call back into it: a + module with no unit, or the standard library with no unit. The corpus + gives a unit only to the modules that an advisory names, so most + modules of a build have no unit. A consumer that gives a unit to each + module with source on the disk, and to GOROOT, removes most of these + records. +- The answers of Python do not change. An ambiguous site of Python, + JavaScript, TypeScript, Ruby and PHP concerns every target, as on main, + because a call by name there can run a callable object, a + `__getattr__` value or a module attribute. Go keeps the method name of an + ambiguous site in a search from the roots, and Rust keeps it for a call + on a receiver. +- The Rust `no-definition` records of main fall from 22 to 2. The rest are + `unknown-receiver` and `ambiguous` records of calls that the link step + does not type yet, as a turbofish or a `::m` call. diff --git a/test/projectgraph/testdata/receivers/go/dep/values.go b/test/projectgraph/testdata/receivers/go/dep/values.go new file mode 100644 index 00000000..1470897c --- /dev/null +++ b/test/projectgraph/testdata/receivers/go/dep/values.go @@ -0,0 +1,19 @@ +package dep + +// ValueFunc is the function that a function value of a value row holds. +func ValueFunc() {} + +// ValueGeneric is a generic function that a value row instantiates. +func ValueGeneric[T any]() {} + +// ValuePair is a generic function with two type parameters. +func ValuePair[A, B any]() {} + +// ValueHook holds a function. +var ValueHook = ValueHooked + +// ValueHooked is the function that ValueHook holds. +func ValueHooked() {} + +// ValueMethod is the method that a method value of a value row names. +func (t *Thing) ValueMethod() {} diff --git a/test/projectgraph/testdata/receivers/rust/dep/src/lib.rs b/test/projectgraph/testdata/receivers/rust/dep/src/lib.rs index 33158560..c3af43f8 100644 --- a/test/projectgraph/testdata/receivers/rust/dep/src/lib.rs +++ b/test/projectgraph/testdata/receivers/rust/dep/src/lib.rs @@ -18,6 +18,8 @@ impl Thing { pub fn child(&self) -> &Thing { self } + + pub fn inspect(&self) {} } impl Clone for Thing { @@ -38,6 +40,14 @@ pub fn load_thing() -> Result, String> { Ok(Thing::new()) } +pub fn read_thing() -> std::io::Result> { + Ok(Thing::new()) +} + +pub fn parse_thing() -> anyhow::Result> { + Ok(Thing::new()) +} + pub trait Doer { fn run(&self); } diff --git a/test/projectgraph/values_test.go b/test/projectgraph/values_test.go new file mode 100644 index 00000000..b4ec9f32 --- /dev/null +++ b/test/projectgraph/values_test.go @@ -0,0 +1,175 @@ +package projectgraph + +import ( + "testing" + + "github.com/safedep/code/core" + "github.com/stretchr/testify/assert" +) + +// goValueRows are the forms of a Go expression that gives a function +// value. fill stores the value in fs, and run calls fill and then fs[0]. A +// call of a function value concerns no target. So the search finds the +// function only through the reference that fill records, or through a +// record with the method name of a method value whose receiver the index +// cannot name. +var goValueRows = []receiverRow{ + { + name: "method expression", + source: goValueSource("func(*dep.Thing)", "fs[0] = (*dep.Thing).ValueMethod", "fs[0](nil)"), + target: "example.com//dep//Thing//ValueMethod", + want: "found/low", + }, + { + name: "parenthesized function", + source: goValueSource("func()", "fs[0] = (dep.ValueFunc)", "fs[0]()"), + target: "example.com//dep//ValueFunc", + want: "found/low", + }, + { + name: "generic instantiation", + source: goValueSource("func()", "fs[0] = dep.ValueGeneric[int]", "fs[0]()"), + target: "example.com//dep//ValueGeneric", + want: "found/low", + }, + { + name: "generic instantiation with two type arguments", + source: goValueSource("func()", "fs[0] = dep.ValuePair[int, string]", "fs[0]()"), + target: "example.com//dep//ValuePair", + want: "found/low", + }, + { + name: "method value on a call result", + source: goValueSource("func()", "fs[0] = dep.NewThing().ValueMethod", "fs[0]()"), + target: "example.com//dep//Thing//ValueMethod", + want: "found/low", + }, + { + name: "method value on a type assertion", + source: goValueSource("func()", "var x any = dep.NewThing()\n\tfs[0] = x.(*dep.Thing).ValueMethod", "fs[0]()"), + target: "example.com//dep//Thing//ValueMethod", + want: "found/low", + }, + { + name: "method value on a composite literal", + source: goValueSource("func()", "fs[0] = (&dep.Thing{}).ValueMethod", "fs[0]()"), + target: "example.com//dep//Thing//ValueMethod", + want: "found/low", + }, + { + name: "method value on new", + source: goValueSource("func()", "fs[0] = new(dep.Thing).ValueMethod", "fs[0]()"), + target: "example.com//dep//Thing//ValueMethod", + want: "found/low", + }, + { + name: "method value on a dereference", + source: goValueSource("func()", "t := dep.NewThing()\n\tfs[0] = (*t).ValueMethod", "fs[0]()"), + target: "example.com//dep//Thing//ValueMethod", + want: "found/low", + }, + { + name: "method value on an element of a slice", + source: goValueSource("func()", "ts := []*dep.Thing{dep.NewThing()}\n\tfs[0] = ts[0].ValueMethod", "fs[0]()"), + target: "example.com//dep//Thing//ValueMethod", + want: "uncertain/unknown-receiver", + }, + { + name: "method value on a local of no known type", + source: goValueSource("func()", "ts := []*dep.Thing{dep.NewThing()}\n\tt := ts[0]\n\tfs[0] = t.ValueMethod", "fs[0]()"), + target: "example.com//dep//Thing//ValueMethod", + want: "uncertain/unknown-receiver", + }, + { + name: "method value on a range variable", + source: goValueSource("func()", "for _, t := range []*dep.Thing{dep.NewThing()} {\n\t\tfs[0] = t.ValueMethod\n\t}", "fs[0]()"), + target: "example.com//dep//Thing//ValueMethod", + want: "uncertain/unknown-receiver", + }, + { + name: "method value on a receive", + source: goValueSource("func()", "ch := make(chan *dep.Thing, 1)\n\tch <- dep.NewThing()\n\tfs[0] = (<-ch).ValueMethod", "fs[0]()"), + target: "example.com//dep//Thing//ValueMethod", + want: "uncertain/unknown-receiver", + }, + { + name: "method value on a local that shadows a function of another file", + source: goValueSource("func()", "shadowed := []*dep.Thing{dep.NewThing()}[0]\n\tfs[0] = shadowed.ValueMethod", "fs[0]()"), + target: "example.com//dep//Thing//ValueMethod", + want: "uncertain/unknown-receiver", + }, + { + name: "argument of a generic call with type arguments", + source: "func id[T any](x T) T { return x }\n\n" + goValueSource("func()", "fs[0] = id[func()](dep.ValueFunc)", "fs[0]()"), + target: "example.com//dep//ValueFunc", + want: "found/low", + }, + { + name: "conversion to a function type", + source: goValueSource("func()", "fs[0] = (func())(dep.ValueFunc)", "fs[0]()"), + target: "example.com//dep//ValueFunc", + want: "found/low", + }, + { + name: "dereference of the address of a variable", + source: goValueSource("func()", "p := &dep.ValueHook\n\tfs[0] = *p", "fs[0]()"), + target: "example.com//dep//ValueHooked", + want: "found/low", + }, + { + name: "function of another file", + source: goValueSource("func()", "fs[0] = helper", "fs[0]()"), + target: "example.com//dep//ValueFunc", + want: "found/low", + }, + { + name: "function of another file after a local of its name in a closed block", + source: goValueSource("func()", "{\n\t\thelper := 1\n\t\t_ = helper\n\t}\n\tfs[0] = helper", "fs[0]()"), + target: "example.com//dep//ValueFunc", + want: "found/low", + }, + { + name: "package variable that holds a function in parentheses", + source: "var h = (dep.ValueFunc)\n\nfunc run() {\n\th()\n}", + target: "example.com//dep//ValueFunc", + want: "found", + }, + { + name: "package variable that holds a generic instantiation", + source: "var h = dep.ValueGeneric[int]\n\nfunc run() {\n\th()\n}", + target: "example.com//dep//ValueGeneric", + want: "found", + }, + { + name: "package variable that holds a method value", + source: "var h = dep.NewThing().ValueMethod\n\nfunc run() {\n\th()\n}", + target: "example.com//dep//Thing//ValueMethod", + want: "found", + }, +} + +// goValueSource is a main.go whose fill stores a value in fs, of the +// element type, with the statements, and whose run calls fill and then the +// element of fs with the call. +func goValueSource(element, statements, call string) string { + return "var fs = make([]" + element + ", 1)\n\nfunc fill() {\n\t" + statements + "\n}\n\nfunc run() {\n\tfill()\n\t" + call + "\n}" +} + +// goValueProject is the receiver project of the source with other.go. Its +// helper calls dep.ValueFunc, and the result of its shadowed has a method of +// the name of a method of dep.Thing. +func goValueProject(source string) map[string]string { + files := goReceiverProject(source) + files["other.go"] = "package main\n\nimport \"example.com/dep\"\n\nfunc helper() {\n\tdep.ValueFunc()\n}\n\n" + + "type local struct{}\n\nfunc (local) ValueMethod() {}\n\nfunc shadowed() local { return local{} }\n" + return files +} + +func TestGoFunctionValues(t *testing.T) { + languages := caseLanguages(t) + for _, row := range goValueRows { + t.Run(row.name, func(t *testing.T) { + assert.Equal(t, row.want, receiverAnswer(t, languages, core.LanguageCodeGo, goValueProject(row.source), row)) + }) + } +} diff --git a/test/projectgraph/version_test.go b/test/projectgraph/version_test.go index a07e8df3..78446241 100644 --- a/test/projectgraph/version_test.go +++ b/test/projectgraph/version_test.go @@ -32,6 +32,7 @@ var graphHashes = map[string]string{ "14/15": "bbc708aba767ccf554c75a7e06816abda3fc1804ad6d0876ad8ce9575f4c3a88", "14/16": "823b1f82c4aed8ba27d19275505c8d1dee755086be48fcda64d323f55b4c62a5", "15/17": "d823ce8ba221c534ab062f7e48abfe91d1c90d50b2a2fdb00d41183dc0e9da09", + "16/18": "bb92620bf9903e8d19b52b62198a0bfd69d67856f9db226112b2cedf537b312b", } // resultHashes records the hash of the reach results of every case for @@ -48,6 +49,7 @@ var resultHashes = map[string]string{ "14/15/3": "77dfd0ea8ab71869ef612d41b8c2e46852d4fb67a49458cb645321692af77ea0", "14/16/3": "13b6eecb686ddec3c2ba470fc9752ea66cff445d5e18e0772a71b15ae7fe1c9c", "15/17/4": "1b32d7c1dbb0a4637853eb7a00b0db91a0a4e1a31f221b37ed0d0654a66ed13e", + "16/18/5": "e0c6093df3a85353ee15c8a90ef7c2a441f23348ce80c9d49793c256bddeffc5", } // summaryHashes records the hash of the golden summaries of the cases for @@ -65,6 +67,7 @@ var summaryHashes = map[string]string{ "14/15/2": "5969f3cda15b666cbd902bbc2db953cbdef8c0ce48d5e77446cf243cdd0bb0e7", "14/16/2": "fb678202978212aae932e50a56a0d3dc7f0f794ce908988bf605ce9a6e04bc51", "15/17/2": "00a6bd5871f70459a617079f431939ef7c92e4b3f360013c1dedd017fe1146be", + "16/18/3": "694efb5f72075356359031d449d7b4ecbdca2ddd5099dacbcd2adad745ac6e19", } func TestGraphHashFollowsVersion(t *testing.T) {