Open thesilentg opened 4 months ago
@adonovan Given your closeness to the likely similar prior issue
Thanks. I expect a patch along these lines should be effective:
case "vta":
- cg = vta.CallGraph(ssautil.AllFunctions(prog), cha.CallGraph(prog))
+ // Gather all source-level functions as entry points.
+ sourceFuncs := make(map[*ssa.Function]bool)
+ packages.Visit(initial, nil, func(p *packages.Package) {
+ for _, file := range p.Syntax {
+ for _, decl := range file.Decls {
+ if decl, ok := decl.(*ast.FuncDecl); ok {
+ obj := p.TypesInfo.Defs[decl.Name].(*types.Func)
+ fn := prog.FuncValue(obj)
+ sourceFuncs[fn] = true
+ }
+ }
+ }
+ })
+
+ cg = vta.CallGraph(sourceFuncs, cha.CallGraph(prog))
Agree with respect to rta
, although based on my understanding both static
and cha
will require separate fixes as those don't take in sourceFuncs.
Based on my early testing, I believe you'll also need to filter out nil
results for fn
, as prog.FuncValue
returns nil
for interface methods and those nils cause panics within vta.CallGraph
Go version
go version go1.22.0 darwin/amd64
Output of
go env
in your module/workspace:What did you do?
go.mod
example2/main.go
Ran
callgraph -algo={algo} ./example2
foralgo
in[static, cha, rta, vta]
What did you see happen?
callgraph -algo=static ./example2
callgraph -algo=cha ./example2
callgraph -algo=rta ./example2
callgraph -algo=vta ./example2
What did you expect to see?
callgraph -algo=static ./example2
callgraph -algo=cha ./example2
callgraph -algo=rta ./example2
This is because rta only includes reachable funcs by design
callgraph -algo=vta ./example2
Note that the link from
example2.unexported).Func1
tocalled
is present whenexample2.unexported).Func1
is forced to be reachable. For example:example2/main.go
callgraph -algo=static ./example2
callgraph -algo=cha ./example2
callgraph -algo=rta ./example2
This is because rta only includes reachable funcs by design
callgraph -algo=vta ./example2
This likely has to do with the same usage of
ssautil.AllFunctions
(at least for vta) that resulted in this bug for thedeadcode
command.