Documentation ¶
Overview ¶
Package ssa is a library to build and work with SSA. For most part the package contains helper or wrapper functions to use the packages in Go project's extra tools.
In particular, the SSA IR is from golang.org/x/tools/go/ssa, and reuses many of the packages in the static analysis stack built on top of it.
Index ¶
- Variables
- func MainPkgs(prog *ssa.Program) ([]*ssa.Package, error)
- type CallGraph
- type Info
- func (info *Info) BuildCallGraph(algo string, tests bool) (*CallGraph, error)
- func (info *Info) FindFunc(path string) (*ssa.Function, error)
- func (info *Info) PtrAnlysCfg(tests bool) (*pointer.Config, error)
- func (info *Info) RunPtrAnlys(config *pointer.Config) (*pointer.Result, error)
- func (info *Info) WriteAll(w io.Writer) (int64, error)
- func (info *Info) WriteFunc(w io.Writer, funcPath string) (int64, error)
- func (info *Info) WriteTo(w io.Writer) (int64, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrNoTestMainPkgs = errors.New("no main packages in tests") ErrNoMainPkgs = errors.New("no main packages") )
Functions ¶
Types ¶
type CallGraph ¶
type CallGraph struct {
// contains filtered or unexported fields
}
CallGraph is a representation of CallGraph, wrapped with metadata.
func (*CallGraph) AllFunctions ¶
AllFunctions return all ssa.Functions defined in the current Program.
func (*CallGraph) UsedFunctions ¶
UsedFunctions return a slice of ssa.Function actually used by the current Program, rooted at main.init() and main.main().
func (*CallGraph) WriteGraphviz ¶
WriteGraphviz writes callgraph to w in graphviz dot format.
Example ¶
package main import ( "bytes" "fmt" "log" "strings" "github.com/JorgeGCoelho/gospal/v2/ssa/build" ) func main() { s := `package main func main() { }` conf := build.FromReader(strings.NewReader(s)) info, err := conf.Build() if err != nil { log.Fatalf("SSA build failed: %v", err) } var buf bytes.Buffer cg, err := info.BuildCallGraph("pta", false) // Pointer analysis, no tests. if err != nil { log.Fatalf("Cannot build callgraph: %v", err) } cg.WriteGraphviz(&buf) fmt.Println(buf.String()) }
Output: digraph callgraph { "<root>" -> "main.init" "<root>" -> "main.main" }
type Info ¶
type Info struct { IgnoredPkgs []string // Record of ignored package during the build process. FSet *token.FileSet // FileSet for parsed source files. Prog *ssa.Program // SSA IR for whole program. LProg *loader.Program // Loaded program from go/loader. BldLog io.Writer // Build log. PtaLog io.Writer // Pointer analysis log. Logger *log.Logger // Build logger. }
Info holds the results of a SSA build for analysis. To populate this structure, the 'build' subpackage should be used.
func (*Info) BuildCallGraph ¶
BuildCallGraph constructs a callgraph from ssa.Info. algo is algorithm available in golang.org/x/tools/go/callgraph, which includes:
- static static calls only (unsound)
- cha Class Hierarchy Analysis
- rta Rapid Type Analysis
- pta inclusion-based Points-To Analysis
func (*Info) FindFunc ¶
FindFunc parses path (e.g. "github.com/nickng/gospal/ssa".MainPkgs) and returns Function body in SSA IR.
func (*Info) PtrAnlysCfg ¶
PtrAnlysCfg returns a default pointer analysis config from Info.
func (*Info) RunPtrAnlys ¶
RunPtrAnlys runs pointer analysis and returns the analysis result.
func (*Info) WriteAll ¶
WriteAll writes all Functions found in the Program to w in human readable SSA IR instruction format.
func (*Info) WriteFunc ¶
WriteFunc writes Functions specified by funcPath to w in human readable SSA IR instruction format.
func (*Info) WriteTo ¶
WriteTo writes Functions used by the Program to w in human readable SSA IR instruction format.
Example ¶
package main import ( "bytes" "fmt" "log" "strings" "github.com/JorgeGCoelho/gospal/v2/ssa/build" ) func main() { s := `package main func main() { }` conf := build.FromReader(strings.NewReader(s)) info, err := conf.Build() if err != nil { log.Fatalf("SSA build failed: %v", err) } var buf bytes.Buffer info.WriteTo(&buf) fmt.Println(buf.String()) }
Output: # Name: main.init # Package: main # Synthetic: package initializer func init(): 0: entry P:0 S:0 return # Name: main.main # Package: main # Location: tmp:2:7 func main(): 0: entry P:0 S:0 return