wasm

package
v3.8.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2020 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Overview

Package wasm provides functions for reading and parsing WebAssembly modules.

Index

Constants

View Source
const (
	// Magic magic number
	Magic uint32 = 0x6d736100
	// Version version number
	Version uint32 = 0x1
)
View Source
const TypeFunc int = -0x20

TypeFunc represents the value type of a function

Variables

View Source
var (
	// ErrImportMutGlobal import global mutable variable
	ErrImportMutGlobal = errors.New("wasm: cannot import global mutable variable")
	// ErrNoExportsInImportedModule imported module has no exportsß
	ErrNoExportsInImportedModule = errors.New("wasm: imported module has no exports")
)
View Source
var ErrEmptyInitExpr = errors.New("wasm: Initializer expression produces no value")

ErrEmptyInitExpr new empty init error

View Source
var ErrFunctionNoEnd = errors.New("Function body does not end with 0x0b (end)")

ErrFunctionNoEnd function no end error

View Source
var ErrInvalidMagic = errors.New("wasm: Invalid magic number")

ErrInvalidMagic invalid magic

View Source
var ErrUnsupportedSection = errors.New("wasm: unsupported section")

ErrUnsupportedSection unsupported section

Functions

This section is empty.

Types

type BlockType

type BlockType ValueType // varint7

BlockType represents the signature of a structured block

const BlockTypeEmpty BlockType = -0x40

BlockTypeEmpty block type empty

func (BlockType) String

func (b BlockType) String() string

type DataSegment

type DataSegment struct {
	Index  uint32 // The index into the global linear memory space, should always be 0 in the MVP.
	Offset []byte // initializer expression for computing the offset for placing elements, should return an i32 value
	Data   []byte
}

DataSegment describes a group of repeated elements that begin at a specified offset in the linear memory

type DuplicateExportError

type DuplicateExportError string

DuplicateExportError duplicated export

func (DuplicateExportError) Error

func (e DuplicateExportError) Error() string

type ElemType

type ElemType int // varint7

ElemType describes the type of a table's elements

const ElemTypeAnyFunc ElemType = -0x10

ElemTypeAnyFunc descibres an any_func value

func (ElemType) String

func (t ElemType) String() string

type ElementSegment

type ElementSegment struct {
	Index  uint32 // The index into the global table space, should always be 0 in the MVP.
	Offset []byte // initializer expression for computing the offset for placing elements, should return an i32 value
	Elems  []uint32
}

ElementSegment describes a group of repeated elements that begin at a specified offset

type EnvGlobal

type EnvGlobal struct {
	Env bool
	Val uint64
}

EnvGlobal global environment

func NewEnvGlobal

func NewEnvGlobal(env bool, val uint64) *EnvGlobal

NewEnvGlobal new global environment

type ExportEntry

type ExportEntry struct {
	FieldStr string
	Kind     External
	Index    uint32
}

ExportEntry represents an exported entry by the module

type ExportNotFoundError

type ExportNotFoundError struct {
	ModuleName string
	FieldName  string
}

ExportNotFoundError defind export not found error

func (ExportNotFoundError) Error

func (e ExportNotFoundError) Error() string

type External

type External uint8

External describes the kind of the entry being imported or exported.

const (
	// ExternalFunction external function type
	ExternalFunction External = 0
	// ExternalTable external table type
	ExternalTable External = 1
	// ExternalMemory external memory type
	ExternalMemory External = 2
	// ExternalGlobal external global type
	ExternalGlobal External = 3
)

func (External) String

func (e External) String() string

type FuncImport

type FuncImport struct {
	Type uint32
}

FuncImport define import type

type Function

type Function struct {
	Sig     *FunctionSig
	Body    *FunctionBody
	EnvFunc bool
	Method  string
}

Function represents an entry in the function index space of a module.

type FunctionBody

type FunctionBody struct {
	Module *Module // The parent module containing this function body, for execution purposes
	Locals []LocalEntry
	Code   []byte
}

FunctionBody define the function body

type FunctionSig

type FunctionSig struct {
	// value for the 'func` type constructor
	Form int8
	// The parameter types of the function
	ParamTypes  []ValueType
	ReturnTypes []ValueType
}

FunctionSig describes the signature of a declared function in a WASM module

func (FunctionSig) String

func (f FunctionSig) String() string

type GlobalEntry

type GlobalEntry struct {
	Type *GlobalVar // Type holds information about the value type and mutability of the variable
	Init []byte     // Init is an initializer expression that computes the initial value of the variable
	// contains filtered or unexported fields
}

GlobalEntry declares a global variable.

type GlobalVar

type GlobalVar struct {
	Type    ValueType // Type of the value stored by the variable
	Mutable bool      // Whether the value of the variable can be changed by the set_global operator
}

GlobalVar describes the type and mutability of a declared global variable

type GlobalVarImport

type GlobalVarImport struct {
	Type GlobalVar
}

GlobalVarImport define global var

type Import

type Import interface {
	// contains filtered or unexported methods
}

Import is an intreface implemented by types that can be imported by a WebAssembly module.

type ImportEntry

type ImportEntry struct {
	ModuleName string // module name string
	FieldName  string // field name string
	Kind       External

	// If Kind is Function, Type is a FuncImport containing the type index of the function signature
	// If Kind is Table, Type is a TableImport containing the type of the imported table
	// If Kind is Memory, Type is a MemoryImport containing the type of the imported memory
	// If the Kind is Global, Type is a GlobalVarImport
	Type Import
}

ImportEntry describes an import statement in a Wasm module.

type InvalidCodeIndexError

type InvalidCodeIndexError int

InvalidCodeIndexError invalid code index

func (InvalidCodeIndexError) Error

func (e InvalidCodeIndexError) Error() string

type InvalidExternalError

type InvalidExternalError uint8

InvalidExternalError define invalid external type

func (InvalidExternalError) Error

func (e InvalidExternalError) Error() string

Error define invalid external error

type InvalidFunctionIndexError

type InvalidFunctionIndexError uint32

InvalidFunctionIndexError define invalid function index

func (InvalidFunctionIndexError) Error

type InvalidGlobalIndexError

type InvalidGlobalIndexError uint32

InvalidGlobalIndexError invalid global index

func (InvalidGlobalIndexError) Error

func (e InvalidGlobalIndexError) Error() string

type InvalidInitExprOpError

type InvalidInitExprOpError byte

InvalidInitExprOpError invalid init exprOpError

func (InvalidInitExprOpError) Error

func (e InvalidInitExprOpError) Error() string

type InvalidLinearMemoryIndexError

type InvalidLinearMemoryIndexError uint32

InvalidLinearMemoryIndexError invalid linear memory index

func (InvalidLinearMemoryIndexError) Error

type InvalidSectionIDError

type InvalidSectionIDError SectionID

InvalidSectionIDError invalid section id

func (InvalidSectionIDError) Error

func (e InvalidSectionIDError) Error() string

type InvalidTableIndexError

type InvalidTableIndexError uint32

InvalidTableIndexError invalid table index type

func (InvalidTableIndexError) Error

func (e InvalidTableIndexError) Error() string

type InvalidTypeConstructorError

type InvalidTypeConstructorError struct {
	Wanted int
	Got    int
}

InvalidTypeConstructorError invalid type contructor

func (InvalidTypeConstructorError) Error

type InvalidValueTypeInitExprError

type InvalidValueTypeInitExprError struct {
	Wanted reflect.Kind
	Got    reflect.Kind
}

InvalidValueTypeInitExprError invalid value type init expire

func (InvalidValueTypeInitExprError) Error

type KindMismatchError

type KindMismatchError struct {
	ModuleName string
	FieldName  string
	Import     External
	Export     External
}

KindMismatchError define mismatch type error

func (KindMismatchError) Error

func (e KindMismatchError) Error() string

type LocalEntry

type LocalEntry struct {
	Count uint32    // The total number of local variables of the given Type used in the function body
	Type  ValueType // The type of value stored by the variable
}

LocalEntry define the local entry format

type Memory

type Memory struct {
	Limits ResizableLimits
}

Memory defind memory type

type MemoryImport

type MemoryImport struct {
	Type Memory
}

MemoryImport define memory type

type MissingSectionError

type MissingSectionError SectionID

MissingSectionError missing section error

func (MissingSectionError) Error

func (e MissingSectionError) Error() string

type Module

type Module struct {
	Version uint32

	Types    *SectionTypes
	Import   *SectionImports
	Function *SectionFunctions
	Table    *SectionTables
	Memory   *SectionMemories
	Global   *SectionGlobals
	Export   *SectionExports
	Start    *SectionStartFunction
	Elements *SectionElements
	Code     *SectionCode
	Data     *SectionData

	// The function index space of the module
	FunctionIndexSpace []Function
	GlobalIndexSpace   []GlobalEntry
	// function indices into the global function space
	// the limit of each table is its capacity (cap)
	TableIndexSpace        [][]uint32
	LinearMemoryIndexSpace [][]byte

	Other []Section // Other holds the custom sections if any
	// contains filtered or unexported fields
}

Module represents a parsed WebAssembly module: http://webassembly.org/docs/modules/

func ReadModule

func ReadModule(r io.Reader, resolvePath ResolveFunc) (*Module, error)

ReadModule reads a module from the reader r. resolvePath must take a string and a return a reader to the module pointed to by the string.

func (*Module) ExecInitExpr

func (m *Module) ExecInitExpr(expr []byte) (interface{}, error)

ExecInitExpr executes an initializer expression and returns an interface{} value which can either be int32, int64, float32 or float64. It returns an error if the expression is invalid, and nil when the expression yields no value.

func (*Module) GetFunction

func (m *Module) GetFunction(i int) *Function

GetFunction returns a *Function, based on the function's index in the function index space. Returns nil when the index is invalid

func (*Module) GetGlobal

func (m *Module) GetGlobal(i int) *GlobalEntry

GetGlobal returns a *GlobalEntry, based on the global index space. Returns nil when the index is invalid

func (*Module) GetLinearMemoryData

func (m *Module) GetLinearMemoryData(index int) (byte, error)

GetLinearMemoryData function to get linear memory data

func (*Module) GetTableElement

func (m *Module) GetTableElement(index int) (uint32, error)

GetTableElement returns an element from the tableindex space indexed by the integer index. It returns an error if index is invalid.

type ResizableLimits

type ResizableLimits struct {
	Flags   uint32 // 1 if the Maximum field is valid
	Initial uint32 // initial length (in units of table elements or wasm pages)
	Maximum uint32 // If flags is 1, it describes the maximum size of the table or memory
}

ResizableLimits describe the limit of a table or linear memory.

type ResolveFunc

type ResolveFunc func(name string) (*Module, error)

ResolveFunc is a function that takes a module name and returns a valid resolved module.

type Section

type Section struct {
	Start int64
	End   int64

	ID SectionID
	// Size of this section in bytes
	PayloadLen uint32
	// Section name, empty if id != 0
	Name  string
	Bytes []byte
}

Section is a declared section in a WASM module.

type SectionCode

type SectionCode struct {
	Section
	Bodies []FunctionBody
}

SectionCode describes the body for every function declared inside a module.

type SectionData

type SectionData struct {
	Section
	Entries []DataSegment
}

SectionData describes the initial values of a module's linear memory

type SectionElements

type SectionElements struct {
	Section
	Entries []ElementSegment
}

SectionElements describes the initial contents of a table's elements.

type SectionExports

type SectionExports struct {
	Section
	Entries map[string]ExportEntry
}

SectionExports declares the export section of a module

type SectionFunctions

type SectionFunctions struct {
	Section
	// Sequences of indices into (FunctionSignatues).Entries
	Types []uint32
}

SectionFunctions declares the signature of all functions defined in the module (in the code section)

type SectionGlobals

type SectionGlobals struct {
	Section
	Globals []GlobalEntry
}

SectionGlobals defines the value of all global variables declared in a module.

type SectionID

type SectionID uint8

SectionID is a 1-byte code that encodes the section code of both known and custom sections.

const (
	// SectionIDCustom customed section id
	SectionIDCustom SectionID = 0
	// SectionIDType type id
	SectionIDType SectionID = 1
	// SectionIDImport import id
	SectionIDImport SectionID = 2
	// SectionIDFunction function id
	SectionIDFunction SectionID = 3
	// SectionIDTable table id
	SectionIDTable SectionID = 4
	// SectionIDMemory memory id
	SectionIDMemory SectionID = 5
	// SectionIDGlobal global id
	SectionIDGlobal SectionID = 6
	// SectionIDExport export id
	SectionIDExport SectionID = 7
	// SectionIDStart  start id
	SectionIDStart SectionID = 8
	// SectionIDElement element id
	SectionIDElement SectionID = 9
	// SectionIDCode code id
	SectionIDCode SectionID = 10
	// SectionIDData data id
	SectionIDData SectionID = 11
)

func (SectionID) String

func (s SectionID) String() string

type SectionImports

type SectionImports struct {
	Section
	Entries []ImportEntry
}

SectionImports declares all imports that will be used in the module.

type SectionMemories

type SectionMemories struct {
	Section
	Entries []Memory
}

SectionMemories describes all linaer memories used by a module.

type SectionStartFunction

type SectionStartFunction struct {
	Section
	Index uint32 // The index of the start function into the global index space.
}

SectionStartFunction represents the start function section.

type SectionTables

type SectionTables struct {
	Section
	Entries []Table
}

SectionTables describes all tables declared by a module.

type SectionTypes

type SectionTypes struct {
	Section
	Entries []FunctionSig
}

SectionTypes declares all function signatures that will be used in a module.

type Table

type Table struct {
	// The type of elements
	ElementType ElemType
	Limits      ResizableLimits
}

Table describes a table in a Wasm module.

type TableImport

type TableImport struct {
	Type Table
}

TableImport define table type

type ValueType

type ValueType int8

ValueType represents the type of a valid value in Wasm

const (
	// ValueTypeI32 int32 type
	ValueTypeI32 ValueType = -0x01
	// ValueTypeI64 int64 type
	ValueTypeI64 ValueType = -0x02
	// ValueTypeF32 float32 type
	ValueTypeF32 ValueType = -0x03
	// ValueTypeF64 float64 type
	ValueTypeF64 ValueType = -0x04
)

func (ValueType) String

func (t ValueType) String() string

Directories

Path Synopsis
internal
Package leb128 provides functions for reading integer values encoded in the Little Endian Base 128 (LEB128) format: https://en.wikipedia.org/wiki/LEB128
Package leb128 provides functions for reading integer values encoded in the Little Endian Base 128 (LEB128) format: https://en.wikipedia.org/wiki/LEB128
Package operators provides all operators used by WebAssembly bytecode, together with their parameter and return type(s).
Package operators provides all operators used by WebAssembly bytecode, together with their parameter and return type(s).

Jump to

Keyboard shortcuts

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