callgraphutil

package
v0.0.0-...-ca0546c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 19, 2024 License: MPL-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package callgraphutil implements utilities for golang.org/x/tools/go/callgraph including path searching, graph construction, printing, and more.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddFunction

func AddFunction(cg *callgraph.Graph, target *ssa.Function, allFns map[*ssa.Function]bool) error

AddFunction analyzes the given target SSA function, adding information to the call graph.

Based on the implementation of golang.org/x/tools/cmd/guru/callers.go: https://cs.opensource.google/go/x/tools/+/master:cmd/guru/callers.go;drc=3e0d083b858b3fdb7d095b5a3deb184aa0a5d35e;bpv=1;bpt=1;l=90

func GraphString

func GraphString(g *callgraph.Graph) string

GraphString returns a string representation of the call graph, which is a sequence of nodes separated by newlines, with the callees of each node indented by a tab.

func InstructionsFor

func InstructionsFor(root *callgraph.Node, v ssa.Value) (si ssa.Instruction)

InstructionsFor returns the ssa.Instruction for the given ssa.Value using the given node as the root of the call graph that is searched.

func NewGraph

func NewGraph(root *ssa.Function, srcFns ...*ssa.Function) (*callgraph.Graph, error)

NewGraph returns a new Graph with the specified root node.

Typically, the root node is the main function of the program, and the srcFns are the source functions that are of interest to the caller. But, the root node can be any function, and the srcFns can be any set of functions.

This algorithm attempts to add all source functions reachable from the root node by traversing the SSA IR and adding edges to the graph; it handles calls to functions, methods, closures, and interfaces. It may miss some complex edges today, such as stucts containing function fields accessed via slice or map indexing. This is a known limitation, but something we hope to improve in the near future. https://github.com/picatz/taint/issues/23

func NewVulncheckCallGraph

func NewVulncheckCallGraph(ctx context.Context, prog *ssa.Program, entries []*ssa.Function) (*callgraph.Graph, error)

NewVulncheckCallGraph builds a call graph of prog based on VTA analysis, straight from the govulncheck project. This is used to demonstrate the difference between the call graph built by this package's algorithm and govulncheck's algorithm (based on CHA and VTA analysis).

This method is based on the following: https://github.com/golang/vuln/blob/7335627909c99e391cf911fcd214badcb8aa6d7d/internal/vulncheck/utils.go#L63

func WriteCSV

func WriteCSV(w io.Writer, g *callgraph.Graph) error

WriteCSV writes the given callgraph.Graph to the given io.Writer in CSV format. This format can be used to generate a visual representation of the call graph using many different tools.

func WriteCosmograph

func WriteCosmograph(graph, metadata io.Writer, g *callgraph.Graph) error

WriteComsmograph writes the given callgraph.Graph to the given io.Writer in CSV format, which can be used to generate a visual representation of the call graph using Comsmograph.

https://cosmograph.app/run/

func WriteDOT

func WriteDOT(w io.Writer, g *callgraph.Graph) error

WriteDOT writes the given callgraph.Graph to the given io.Writer in the DOT format, which can be used to generate a visual representation of the call graph using Graphviz.

Types

type Edges

type Edges = []*callgraph.Edge

Edges is a handy alias for a slice of callgraph.Edges.

type Nodes

type Nodes = []*callgraph.Node

Nodes is a handy alias for a slice of callgraph.Nodes.

func CalleesOf

func CalleesOf(caller *callgraph.Node) Nodes

CalleesOf returns nodes that are called by the caller node.

func CallersOf

func CallersOf(callee *callgraph.Node) Nodes

CallersOf returns nodes that call the callee node.

type Path

type Path []*callgraph.Edge

Path is a sequence of callgraph.Edges, where each edge represents a call from a caller to a callee, making up a "chain" of calls, e.g.: main → foo → bar → baz.

func PathSearch

func PathSearch(start *callgraph.Node, isMatch func(*callgraph.Node) bool) Path

PathSearch returns the first path found from the start node to a node that matches the isMatch function. This is a depth first search, so it will return the first path found, which may not be the shortest path.

To find all paths, use PathsSearch, which returns a collection of paths.

func PathSearchCallTo

func PathSearchCallTo(start *callgraph.Node, fn string) Path

PathSearchCallTo returns the first path found from the start node to a node that matches the function name.

func (Path) Empty

func (p Path) Empty() bool

Empty returns true if the path is empty, false otherwise.

func (Path) First

func (p Path) First() *callgraph.Edge

First returns the first edge in the path, or nil if the path is empty.

func (Path) Last

func (p Path) Last() *callgraph.Edge

Last returns the last edge in the path, or nil if the path is empty.

func (Path) String

func (p Path) String() string

String returns a string representation of the path which is a sequence of edges separated by " → ".

Intended to be used while debugging.

type Paths

type Paths []Path

Paths is a collection of paths, which may be logically grouped together, e.g.: all paths from main to foo, or all paths from main to bar.

func PathsSearch

func PathsSearch(start *callgraph.Node, isMatch func(*callgraph.Node) bool) Paths

PathsSearch returns all paths found from the start node to a node that matches the isMatch function. Under the hood, this is a depth first search.

To find the first path (which may not be the shortest), use PathSearch.

func PathsSearchCallTo

func PathsSearchCallTo(start *callgraph.Node, fn string) Paths

PathsSearchCallTo returns the paths that call the given function name, which uses SSA function name syntax, e.g.: "(*database/sql.DB).Query".

func (Paths) Longest

func (p Paths) Longest() Path

Longest returns the longest path in the collection of paths.

If there are no paths, this returns nil. If there are multiple paths of the same length, the first path found is returned.

func (Paths) Shortest

func (p Paths) Shortest() Path

Shortest returns the shortest path in the collection of paths.

If there are no paths, this returns nil. If there are multiple paths of the same length, this returns the first path found.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL