exec

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2022 License: GPL-3.0, MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultCallStackSize is the default call stack size.
	DefaultCallStackSize = 512

	// DefaultPageSize is the linear memory page size.  65536
	DefaultPageSize = 65536

	// JITCodeSizeThreshold is the lower-bound code size threshold for the JIT compiler.
	JITCodeSizeThreshold = 30

	DefaultMemoryPages = 16
	DynamicMemoryPages = 16

	DefaultMemPoolCount   = 5
	DefaultMemBlockSize   = 5
	DefaultMemTreeMaxPage = 8
)

Variables

View Source
var (
	EmptyPage = make([]byte, DefaultPageSize)
)
View Source
var GasTable = [256]Instruction{}/* 161 elements not displayed */

LE is a simple alias to `binary.LittleEndian`.

Functions

func ImportGasFunc

func ImportGasFunc(vm *VirtualMachine, frame *Frame) (uint64, error)

func ParseModuleAndFunc

func ParseModuleAndFunc(code []byte, gasPolicy compiler.GasPolicy) (*compiler.Module, []compiler.InterpreterCode, error)

Types

type DynamicModule

type DynamicModule struct {
}

func CompileDynamicModule

func CompileDynamicModule(source string) *DynamicModule

func (*DynamicModule) Run

func (m *DynamicModule) Run(vm *VirtualMachine, ret *int64) int32

type Execute

type Execute func(vm *VirtualMachine) int64

type Frame

type Frame struct {
	FunctionID   int
	Code         []byte
	JITInfo      interface{}
	Regs         []int64
	Locals       []int64
	IP           int
	ReturnReg    int
	Continuation int32
}

Frame represents a call frame.

func (*Frame) Destroy

func (f *Frame) Destroy(vm *VirtualMachine)

Destroy destroys a frame. Must be called on return.

func (*Frame) Init

func (f *Frame) Init(vm *VirtualMachine, functionID int, code compiler.InterpreterCode)

Init initializes a frame. Must be called on `call` and `call_indirect`.

type FunctionImport

type FunctionImport struct {
	Execute Execute
	GasCost GasCost
}

FunctionImport represents the function import type. If len(sig.ReturnTypes) == 0, the return value will be ignored.

type GasCost

type GasCost func(vm *VirtualMachine) (uint64, error)

type ImportResolver

type ImportResolver interface {
	ResolveFunc(module, field string) *FunctionImport
	ResolveGlobal(module, field string) int64
}

ImportResolver is an interface for allowing one to define imports to WebAssembly modules ran under a single VirtualMachine instance.

type Instruction

type Instruction struct {
	Execute executionFunc
	GasCost gasFunc
}

type MemBlock

type MemBlock struct {
	FreeMem [][]byte
	// contains filtered or unexported fields
}

func NewMemBlock

func NewMemBlock(size, pages int) *MemBlock

func (*MemBlock) Get

func (mb *MemBlock) Get() []byte

func (*MemBlock) Put

func (mb *MemBlock) Put(mem []byte)

type MemPool

type MemPool struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewMemPool

func NewMemPool(count int, size int) *MemPool

func (*MemPool) Get

func (mp *MemPool) Get(pages int) []byte

func (*MemPool) Put

func (mp *MemPool) Put(mem []byte)

type Memory

type Memory struct {
	Memory []byte
	Start  int //start position for malloc
	Size   int //memory size for malloc
	// contains filtered or unexported fields
}

func (*Memory) Free

func (m *Memory) Free(offset int) error

func (*Memory) Malloc

func (m *Memory) Malloc(size int) int

func (*Memory) Realloc

func (m *Memory) Realloc(offset int, size int) int

type NopResolver

type NopResolver struct{}

NopResolver is a nil WebAssembly module import resolver.

func (*NopResolver) ResolveFunc

func (r *NopResolver) ResolveFunc(module, field string) *FunctionImport

func (*NopResolver) ResolveGlobal

func (r *NopResolver) ResolveGlobal(module, field string) int64

type StateDB

type StateDB interface {
	GasPrice() int64
	BlockHash(num uint64) common.Hash
	BlockNumber() *big.Int
	GasLimimt() uint64
	Time() *big.Int
	Coinbase() common.Address
	GetBalance(addr common.Address) *big.Int
	Origin() common.Address
	Caller() common.Address
	GetCode(addr common.Address) []byte
	Address() common.Address
	CallValue() *big.Int
	IsOwner(contractAddress common.Address, accountAddress common.Address) int64
	AddLog(address common.Address, topics []common.Hash, data []byte, bn uint64)
	SetState(key []byte, value []byte)
	GetState(key []byte) []byte

	GetCallerNonce() int64
	Transfer(addr common.Address, value *big.Int) (ret []byte, leftOverGas uint64, err error)
	DelegateCall(addr, params []byte) ([]byte, error)
	Call(addr, params []byte) ([]byte, error)
}

type TreePool

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

func NewTreePool

func NewTreePool(poolSize int, cacheSize int) *TreePool

func (*TreePool) GetTree

func (tp *TreePool) GetTree(pages int) tree

func (*TreePool) PutTree

func (tp *TreePool) PutTree(tree tree)

type VMConfig

type VMConfig struct {
	EnableJIT          bool
	DynamicMemoryPages int
	MaxMemoryPages     int
	MaxTableSize       int
	MaxValueSlots      int
	MaxCallStackDepth  int
	DefaultMemoryPages int
	DefaultTableSize   int
	GasLimit           uint64
	DisableFree        bool
}

VMConfig denotes a set of options passed to a single VirtualMachine insta.ce

type VMContext

type VMContext struct {
	Config   VMConfig
	Addr     [20]byte
	GasUsed  uint64
	GasLimit uint64

	StateDB StateDB
	Log     log.Logger
}

type VMMemory

type VMMemory struct {
	Memory    []byte
	Start     int
	Current   int
	MemPoints map[int]int
}

type VirtualMachine

type VirtualMachine struct {
	Context         *VMContext
	Module          *compiler.Module
	FunctionCode    []compiler.InterpreterCode
	FunctionImports []*FunctionImport
	JumpTable       [256]Instruction
	CallStack       []Frame
	CurrentFrame    int
	Table           []uint32
	Globals         []int64
	//Memory          *VMMemory
	Memory         *Memory
	NumValueSlots  int
	Yielded        int64
	InsideExecute  bool
	Delegate       func()
	Exited         bool
	ExitError      interface{}
	ReturnValue    int64
	Gas            uint64
	ExternalParams []int64
	InitEntryID    int
}

VirtualMachine is a WebAssembly execution environment.

func NewVirtualMachine

func NewVirtualMachine(code []byte, context *VMContext, impResolver ImportResolver, gasPolicy compiler.GasPolicy) (_retVM *VirtualMachine, retErr error)

NewVirtualMachine instantiates a virtual machine for a given WebAssembly module, with specific execution options specified under a VMConfig, and a WebAssembly module import resolver.

func NewVirtualMachineWithModule

func NewVirtualMachineWithModule(m *compiler.Module, functionCode []compiler.InterpreterCode, context *VMContext, impResolver ImportResolver, gasPolicy compiler.GasPolicy) (_retVM *VirtualMachine, retErr error)

func (*VirtualMachine) AddAndCheckGas

func (vm *VirtualMachine) AddAndCheckGas(delta uint64)

func (*VirtualMachine) Execute

func (vm *VirtualMachine) Execute()

Execute starts the virtual machines main instruction processing loop. This function may return at any point and is guaranteed to return at least once every 10000 instructions. Caller is responsible for detecting VM status in a loop.

func (*VirtualMachine) GenerateCodeForFunction

func (vm *VirtualMachine) GenerateCodeForFunction(functionID int) bool

GenerateCodeForFunction generates C code for the given function. Returns true if codegen succeeds, or false if the current function cannot be code-generated.

func (*VirtualMachine) GetCurrentFrame

func (vm *VirtualMachine) GetCurrentFrame() *Frame

GetCurrentFrame returns the current frame.

func (*VirtualMachine) GetFunctionExport

func (vm *VirtualMachine) GetFunctionExport(key string) (int, bool)

GetFunctionExport returns the function export with the given name.

func (*VirtualMachine) GetGlobalExport

func (vm *VirtualMachine) GetGlobalExport(key string) (int, bool)

GetGlobalExport returns the global export with the given name.

func (*VirtualMachine) Ignite

func (vm *VirtualMachine) Ignite(functionID int, params ...int64)

Ignite initializes the first call frame.

func (*VirtualMachine) PrintStackTrace

func (vm *VirtualMachine) PrintStackTrace()

PrintStackTrace prints the entire VM stack trace for debugging.

func (*VirtualMachine) Run

func (vm *VirtualMachine) Run(entryID int, params ...int64) (int64, error)

Run runs a WebAssembly modules function denoted by its ID with a specified set of parameters. Panics on logical errors.

func (*VirtualMachine) RunWithGasLimit

func (vm *VirtualMachine) RunWithGasLimit(entryID, limit int, params ...int64) (int64, error)

RunWithGasLimit runs a WebAssembly modules function denoted by its ID with a specified set of parameters for a specified amount of instructions (also known as gas) denoted by `limit`. Panics on logical errors.

func (*VirtualMachine) Stop

func (vm *VirtualMachine) Stop() (err error)

Jump to

Keyboard shortcuts

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