ssa

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2019 License: Apache-2.0 Imports: 18 Imported by: 2

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

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrNoTestMainPkgs = errors.New("no main packages in tests")
	ErrNoMainPkgs     = errors.New("no main packages")
)

Functions

func MainPkgs

func MainPkgs(prog *ssa.Program, tests bool) ([]*ssa.Package, error)

MainPkgs returns the main packages in the program.

Types

type CallGraph

type CallGraph struct {
	// contains filtered or unexported fields
}

CallGraph is a representation of CallGraph, wrapped with metadata.

func (*CallGraph) AllFunctions

func (g *CallGraph) AllFunctions() ([]*ssa.Function, error)

AllFunctions return all ssa.Functions defined in the current Program.

func (*CallGraph) UsedFunctions

func (g *CallGraph) UsedFunctions() ([]*ssa.Function, error)

UsedFunctions return a slice of ssa.Function actually used by the current Program, rooted at main.init() and main.main().

func (*CallGraph) WriteGraphviz

func (g *CallGraph) WriteGraphviz(w io.Writer) error

WriteGraphviz writes callgraph to w in graphviz dot format.

Example
package main

import (
	"bytes"
	"fmt"
	"log"
	"strings"

	"github.com/nickng/gospal/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

func (info *Info) BuildCallGraph(algo string, tests bool) (*CallGraph, error)

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

func (info *Info) FindFunc(path string) (*ssa.Function, error)

FindFunc parses path (e.g. "github.com/nickng/gospal/ssa".MainPkgs) and returns Function body in SSA IR.

func (*Info) PtrAnlysCfg

func (info *Info) PtrAnlysCfg(tests bool) (*pointer.Config, error)

PtrAnlysCfg returns a default pointer analysis config from Info.

func (*Info) RunPtrAnlys

func (info *Info) RunPtrAnlys(config *pointer.Config) (*pointer.Result, error)

RunPtrAnlys runs pointer analysis and returns the analysis result.

func (*Info) WriteAll

func (info *Info) WriteAll(w io.Writer) (int64, error)

WriteAll writes all Functions found in the Program to w in human readable SSA IR instruction format.

func (*Info) WriteFunc

func (info *Info) WriteFunc(w io.Writer, funcPath string) (int64, error)

WriteFunc writes Functions specified by funcPath to w in human readable SSA IR instruction format.

func (*Info) WriteTo

func (info *Info) WriteTo(w io.Writer) (int64, error)

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/nickng/gospal/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

Directories

Path Synopsis
Package build is a helper package for building SSA IR in the parent directory.
Package build is a helper package for building SSA IR in the parent directory.

Jump to

Keyboard shortcuts

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