gossa

package module
v0.3.25 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2022 License: Apache-2.0 Imports: 31 Imported by: 0

README

gossa - Golang SSA interpreter

Go1.14 Go1.15 Go1.16 Go1.17 Go1.18

ABI

support ABI0 and ABIInternal

unsupport features
  • Go1.18 type parameters
  • Go1.18 fuzzing
gossa command line
go get -u github.com/goplus/gossa/cmd/gossa

Commands

gossa run         # interpret package
gossa test        # test package
gossa repl mode
gossa                       # run repl mode, support Go/Go+
gossa repl                  # run repl mode, support Go/Go+
gossa repl -gop=false       # run repl mode, disable Go+ syntax
gossa package

run go source

package main

import (
	"github.com/goplus/gossa"
	_ "github.com/goplus/gossa/pkg/fmt"
)

var source = `
package main

import "fmt"

func main() {
	fmt.Println("hello")
}
`

func main() {
	_, err := gossa.RunFile("main.go", source, nil, 0)
	if err != nil {
		panic(err)
	}
}

run gop source

package main

import (
	"github.com/goplus/gossa"
	_ "github.com/goplus/gossa/gopbuild"
	_ "github.com/goplus/gossa/pkg/fmt"
)

var source = `
println "Hello, Go+"
`

func main() {
	_, err := gossa.RunFile("main.gop", source, nil, 0)
	if err != nil {
		panic(err)
	}
}

Documentation

Overview

Package ssa/interp defines an interpreter for the SSA representation of Go programs.

This interpreter is provided as an adjunct for testing the SSA construction algorithm. Its purpose is to provide a minimal metacircular implementation of the dynamic semantics of each SSA instruction. It is not, and will never be, a production-quality Go interpreter.

The following is a partial list of Go features that are currently unsupported or incomplete in the interpreter.

* Unsafe operations, including all uses of unsafe.Pointer, are impossible to support given the "boxed" value representation we have chosen.

* The reflect package is only partially implemented.

* The "testing" package is no longer supported because it depends on low-level details that change too often.

* "sync/atomic" operations are not atomic due to the "boxed" value representation: it is not possible to read, modify and write an interface value atomically. As a consequence, Mutexes are currently broken.

* recover is only partially implemented. Also, the interpreter makes no attempt to distinguish target panics from interpreter crashes.

* the sizes of the int, uint and uintptr types in the target program are assumed to be the same as those of the interpreter itself.

* all values occupy space, even those of types defined by the spec to have zero size, e.g. struct{}. This can cause asymptotic performance degradation.

* os.Exit is implemented using panic, causing deferred functions to run.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoPackage        = errors.New("no package")
	ErrPackage          = errors.New("package contain errors")
	ErrNotFoundMain     = errors.New("not found main package")
	ErrTestFailed       = errors.New("test failed")
	ErrNotFoundPackage  = errors.New("not found package")
	ErrNotFoundImporter = errors.New("not found provider for types.Importer")
	ErrGoexitDeadlock   = errors.New("fatal error: no goroutines (main called runtime.Goexit) - deadlock!")
	ErrNoFunction       = errors.New("no function")
)

Functions

func AllMethod added in v0.2.0

func AllMethod(typ reflect.Type, enableUnexport bool) []reflect.Method

func CreateTestMainPackage deprecated

func CreateTestMainPackage(pkg *ssa.Package) (*ssa.Package, error)

CreateTestMainPackage creates and returns a synthetic "testmain" package for the specified package if it defines tests, benchmarks or executable examples, or nil otherwise. The new package is named "main" and provides a function named "main" that runs the tests, similar to the one that would be created by the 'go test' tool.

Subsequent calls to prog.AllPackages include the new package. The package pkg must belong to the program prog.

Deprecated: Use golang.org/x/tools/go/packages to access synthetic testmain packages.

func Field added in v0.2.0

func Field(v interface{}, index int) (interface{}, error)

func FieldAddr added in v0.2.0

func FieldAddr(v interface{}, index int) (interface{}, error)

func FindTests deprecated added in v0.2.6

func FindTests(pkg *ssa.Package) (tests, benchmarks, examples []*ssa.Function, main *ssa.Function)

FindTests returns the Test, Benchmark, and Example functions (as defined by "go test") defined in the specified package, and its TestMain function, if any.

Deprecated: Use golang.org/x/tools/go/packages to access synthetic testmain packages.

func IntuitiveMethodSet added in v0.2.0

func IntuitiveMethodSet(T types.Type) []*types.Selection

golang.org/x/tools/go/types/typeutil.IntuitiveMethodSet

func IsConstNil

func IsConstNil(v ssa.Value) bool

func IsNil

func IsNil(v reflect.Value) bool

func RegisterCustomBuiltin added in v0.3.25

func RegisterCustomBuiltin(key string, fn interface{}) error

func RegisterExternal

func RegisterExternal(key string, i interface{})

register external function for no function body

func RegisterFileProcess added in v0.2.10

func RegisterFileProcess(ext string, fn SourceProcessFunc)

func RegisterPackage

func RegisterPackage(pkg *Package)

RegisterPackage register pkg

func Run

func Run(path string, args []string, mode Mode) (exitCode int, err error)

func RunFile

func RunFile(filename string, src interface{}, args []string, mode Mode) (exitCode int, err error)

func RunTest

func RunTest(path string, args []string, mode Mode) error

func SetValue

func SetValue(v reflect.Value, x reflect.Value)

Types

type Context added in v0.2.0

type Context struct {
	Loader      Loader          // types loader
	Mode        Mode            // mode
	ParserMode  parser.Mode     // parser mode
	BuilderMode ssa.BuilderMode // ssa builder mode
	External    types.Importer  // external import
	Sizes       types.Sizes     // types size for package unsafe
	// contains filtered or unexported fields
}

Context ssa context

func NewContext added in v0.2.0

func NewContext(mode Mode) *Context

NewContext create a new Context

func (*Context) BuildPackage added in v0.2.0

func (ctx *Context) BuildPackage(fset *token.FileSet, pkg *types.Package, files []*ast.File) (*ssa.Package, *types.Info, error)

func (*Context) ClearOverrideFunction added in v0.3.17

func (c *Context) ClearOverrideFunction(key string)

ClearOverrideFunction reset override function

func (*Context) IsEvalMode added in v0.3.25

func (c *Context) IsEvalMode() bool

func (*Context) LoadAstFile added in v0.2.0

func (c *Context) LoadAstFile(fset *token.FileSet, file *ast.File) (*ssa.Package, error)

func (*Context) LoadAstPackage added in v0.2.0

func (c *Context) LoadAstPackage(fset *token.FileSet, apkg *ast.Package) (*ssa.Package, error)

func (*Context) LoadDir added in v0.2.0

func (c *Context) LoadDir(fset *token.FileSet, path string) (pkgs []*ssa.Package, first error)

func (*Context) LoadFile added in v0.2.0

func (c *Context) LoadFile(fset *token.FileSet, filename string, src interface{}) (*ssa.Package, error)

func (*Context) NewInterp added in v0.2.9

func (c *Context) NewInterp(mainPkg *ssa.Package) (*Interp, error)

func (*Context) ParseFile added in v0.3.25

func (c *Context) ParseFile(fset *token.FileSet, filename string, src interface{}) (*ast.File, error)

func (*Context) Run added in v0.2.0

func (c *Context) Run(path string, args []string) (exitCode int, err error)

func (*Context) RunFile added in v0.2.0

func (c *Context) RunFile(filename string, src interface{}, args []string) (exitCode int, err error)

func (*Context) RunFunc added in v0.2.9

func (c *Context) RunFunc(mainPkg *ssa.Package, fnname string, args ...Value) (ret Value, err error)

func (*Context) RunPkg added in v0.2.0

func (c *Context) RunPkg(mainPkg *ssa.Package, input string, args []string) (exitCode int, err error)

func (*Context) RunTest added in v0.2.0

func (c *Context) RunTest(path string, args []string) error

func (*Context) SetDebug added in v0.2.15

func (c *Context) SetDebug(fn func(*DebugInfo))

func (*Context) SetLeastCallForEnablePool added in v0.3.18

func (c *Context) SetLeastCallForEnablePool(count int)

SetLeastCallForEnablePool set least call count for enable function pool, default 64

func (*Context) SetOverrideFunction added in v0.3.15

func (c *Context) SetOverrideFunction(key string, fn interface{})

SetOverrideFunction register external function to override function. match func fullname and signature

func (*Context) SetPrintOutput added in v0.3.17

func (c *Context) SetPrintOutput(output *bytes.Buffer)

set builtin print/println captured output

func (*Context) TestPkg added in v0.2.0

func (c *Context) TestPkg(pkgs []*ssa.Package, input string, args []string) error

type DebugInfo added in v0.2.15

type DebugInfo struct {
	*ssa.DebugRef
	// contains filtered or unexported fields
}

func (*DebugInfo) AsFunc added in v0.2.15

func (i *DebugInfo) AsFunc() (*types.Func, bool)

func (*DebugInfo) AsVar added in v0.2.15

func (i *DebugInfo) AsVar() (*types.Var, interface{}, bool)

func (*DebugInfo) Position added in v0.2.15

func (i *DebugInfo) Position() token.Position

type FindMethod added in v0.2.0

type FindMethod interface {
	FindMethod(mtyp reflect.Type, fn *types.Func) func([]reflect.Value) []reflect.Value
}

type Importer added in v0.2.0

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

func NewImporter added in v0.2.0

func NewImporter(loader Loader, importer types.Importer) *Importer

func (*Importer) Import added in v0.2.0

func (i *Importer) Import(path string) (*types.Package, error)

type Interp added in v0.2.0

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

State shared between all interpreted goroutines.

func NewInterp added in v0.2.0

func NewInterp(ctx *Context, mainpkg *ssa.Package) (*Interp, error)

func (*Interp) ExitCode added in v0.3.24

func (i *Interp) ExitCode() int

func (*Interp) FindMethod added in v0.2.0

func (i *Interp) FindMethod(mtyp reflect.Type, fn *types.Func) func([]reflect.Value) []reflect.Value

func (*Interp) GetConst added in v0.2.11

func (i *Interp) GetConst(key string) (constant.Value, bool)

func (*Interp) GetFunc added in v0.2.11

func (i *Interp) GetFunc(key string) (interface{}, bool)

func (*Interp) GetType added in v0.2.11

func (i *Interp) GetType(key string) (reflect.Type, bool)

func (*Interp) GetVarAddr added in v0.2.11

func (i *Interp) GetVarAddr(key string) (interface{}, bool)

func (*Interp) RunFunc added in v0.2.9

func (i *Interp) RunFunc(name string, args ...Value) (r Value, err error)

func (*Interp) RunInit added in v0.3.24

func (i *Interp) RunInit() (err error)

func (*Interp) RunMain added in v0.3.24

func (i *Interp) RunMain() (exitCode int, err error)

type Loader added in v0.2.0

type Loader interface {
	Import(path string) (*types.Package, error)
	Installed(path string) (*Package, bool)
	Packages() []*types.Package
	LookupReflect(typ types.Type) (reflect.Type, bool)
	LookupTypes(typ reflect.Type) (types.Type, bool)
}

Loader types loader interface

func NewTypesLoader added in v0.2.0

func NewTypesLoader(mode Mode) Loader

NewTypesLoader install package and readonly

type Mode

type Mode uint

Mode is a bitmask of options affecting the interpreter.

const (
	DisableRecover         Mode = 1 << iota // Disable recover() in target programs; show interpreter crash instead.
	DisableCustomBuiltin                    // Disable load custom builtin func
	DisableUnexportMethods                  // Disable unexport methods
	EnableTracing                           // Print a trace of all instructions as they are interpreted.
	EnableDumpInstr                         // Print packages & SSA instruction code
	EnablePrintAny                          // Enable builtin print for any type ( struct/array )
)

type NamedType added in v0.2.0

type NamedType struct {
	Typ        reflect.Type
	Methods    string
	PtrMethods string
}

type Package added in v0.2.0

type Package struct {
	Name          string
	Path          string
	Interfaces    map[string]reflect.Type
	NamedTypes    map[string]NamedType
	AliasTypes    map[string]reflect.Type
	Vars          map[string]reflect.Value
	Funcs         map[string]reflect.Value
	TypedConsts   map[string]TypedConst
	UntypedConsts map[string]UntypedConst
	Deps          map[string]string
	// contains filtered or unexported fields
}

func LookupPackage added in v0.2.0

func LookupPackage(name string) (pkg *Package, ok bool)

LookupPackage lookup register pkgs

type Repl added in v0.3.25

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

func NewRepl added in v0.3.25

func NewRepl(ctx *Context) *Repl

func (*Repl) Eval added in v0.3.25

func (r *Repl) Eval(expr string) (tok token.Token, dump []string, err error)

func (*Repl) SetFileName added in v0.3.25

func (r *Repl) SetFileName(filename string)

func (*Repl) Source added in v0.3.25

func (r *Repl) Source() string

type SourceProcessFunc added in v0.2.10

type SourceProcessFunc func(ctx *Context, filename string, src interface{}) ([]byte, error)

type Tuple added in v0.2.9

type Tuple = tuple

type TypedConst added in v0.2.0

type TypedConst struct {
	Typ   reflect.Type
	Value constant.Value
}

type TypesLoader added in v0.2.0

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

func (*TypesLoader) GetPackage added in v0.2.0

func (r *TypesLoader) GetPackage(pkg string) *types.Package

func (*TypesLoader) Import added in v0.2.0

func (r *TypesLoader) Import(path string) (*types.Package, error)

func (*TypesLoader) Insert added in v0.2.0

func (r *TypesLoader) Insert(v reflect.Value)

func (*TypesLoader) InsertAlias added in v0.2.0

func (r *TypesLoader) InsertAlias(p *types.Package, name string, rt reflect.Type)

func (*TypesLoader) InsertConst added in v0.2.0

func (r *TypesLoader) InsertConst(p *types.Package, name string, typ types.Type, c constant.Value)

func (*TypesLoader) InsertFunc added in v0.2.0

func (r *TypesLoader) InsertFunc(p *types.Package, name string, v reflect.Value)

func (*TypesLoader) InsertInterface added in v0.2.0

func (r *TypesLoader) InsertInterface(p *types.Package, name string, rt reflect.Type)

func (*TypesLoader) InsertNamedType added in v0.2.0

func (r *TypesLoader) InsertNamedType(p *types.Package, name string, t NamedType)

func (*TypesLoader) InsertTypedConst added in v0.2.0

func (r *TypesLoader) InsertTypedConst(p *types.Package, name string, v TypedConst)

func (*TypesLoader) InsertUntypedConst added in v0.2.0

func (r *TypesLoader) InsertUntypedConst(p *types.Package, name string, v UntypedConst)

func (*TypesLoader) InsertVar added in v0.2.0

func (r *TypesLoader) InsertVar(p *types.Package, name string, v reflect.Value)

func (*TypesLoader) Installed added in v0.2.0

func (r *TypesLoader) Installed(path string) (pkg *Package, ok bool)

func (*TypesLoader) LookupPackage added in v0.2.0

func (r *TypesLoader) LookupPackage(pkgpath string) (*types.Package, bool)

func (*TypesLoader) LookupReflect added in v0.2.0

func (r *TypesLoader) LookupReflect(typ types.Type) (reflect.Type, bool)

func (*TypesLoader) LookupType added in v0.2.0

func (r *TypesLoader) LookupType(typ string) types.Type

func (*TypesLoader) LookupTypes added in v0.2.0

func (r *TypesLoader) LookupTypes(typ reflect.Type) (types.Type, bool)

func (*TypesLoader) Packages added in v0.2.0

func (r *TypesLoader) Packages() (pkgs []*types.Package)

func (*TypesLoader) ToType added in v0.2.0

func (r *TypesLoader) ToType(rt reflect.Type) types.Type

type TypesRecord added in v0.2.0

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

func NewTypesRecord added in v0.2.0

func NewTypesRecord(loader Loader, finder FindMethod) *TypesRecord

func (*TypesRecord) Load added in v0.2.0

func (r *TypesRecord) Load(pkg *ssa.Package)

func (*TypesRecord) LoadType added in v0.2.0

func (r *TypesRecord) LoadType(typ types.Type) reflect.Type

func (*TypesRecord) LookupLocalTypes added in v0.2.0

func (r *TypesRecord) LookupLocalTypes(rt reflect.Type) (typ types.Type, ok bool)

func (*TypesRecord) LookupReflect added in v0.2.0

func (r *TypesRecord) LookupReflect(typ types.Type) (rt reflect.Type, ok bool)

func (*TypesRecord) LookupTypes added in v0.2.0

func (r *TypesRecord) LookupTypes(rt reflect.Type) (typ types.Type, ok bool)

func (*TypesRecord) ToType added in v0.2.0

func (r *TypesRecord) ToType(typ types.Type) reflect.Type

func (*TypesRecord) ToTypeList added in v0.2.0

func (r *TypesRecord) ToTypeList(tuple *types.Tuple) []reflect.Type

type UntypedConst added in v0.2.0

type UntypedConst struct {
	Typ   string
	Value constant.Value
}

type Value added in v0.2.9

type Value = value

Directories

Path Synopsis
cmd
internal/base
Package base defines shared xtype pieces of the gop command, in particular logging and the Command structure.
Package base defines shared xtype pieces of the gop command, in particular logging and the Command structure.
internal/help
Package help implements the “gossa help” command.
Package help implements the “gossa help” command.
internal/run
Package run implements the “gop run” command.
Package run implements the “gop run” command.
internal/test
Package test implements the “gossa test” command.
Package test implements the “gossa test” command.
internal
pkg
fmt
io
log
net
os

Jump to

Keyboard shortcuts

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