Categorygithub.com/aykevl/go-wasm
modulepackage
0.0.1
Repository: https://github.com/aykevl/go-wasm.git
Documentation: pkg.go.dev

# README

CircleCI godoc

go-wasm

A WebAssembly binary file parser in go.

The parser takes an io.Reader and parses a WebAssembly module from it, which allows the user to see into the binary file. All data is read, future version may allow to write it out too, which would allow modifying the binary.

For example:

package main

import (
	"flag"
	"fmt"
	"os"
	"text/tabwriter"

	wasm "github.com/akupila/go-wasm"
)

func main() {
	file := flag.String("file", "", "file to parse (.wasm)")
	flag.Parse()

	if *file == "" {
		flag.Usage()
		os.Exit(2)
	}

	f, err := os.Open(*file)
	if err != nil {
		fmt.Fprintf(os.Stderr, "open file: %v", err)
		os.Exit(1)
	}
	defer f.Close()

	mod, err := wasm.Parse(f)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}

	w := tabwriter.NewWriter(os.Stdout, 0, 0, 4, ' ', 0)
	fmt.Fprintf(w, "Index\tName\tSize (bytes)\n")
	for i, s := range mod.Sections {
		fmt.Fprintf(w, "%d\t%s\t%d\n", i, s.Name(), s.Size())
	}
	w.Flush()
}

when passed in a .wasm file compiled with go1.11:

Index    Name        Size (bytes)
0        Custom      103
1        Type        58
2        Import      363
3        Function    1588
4        Table       5
5        Memory      5
6        Global      51
7        Export      14
8        Element     3066
9        Code        1174891
10       Data        1169054
11       Custom      45428

Much more information is available by type asserting on the items in .Sections, for example:

for i, s := range mod.Sections {
    switch section := s.(type) {
        case *wasm.SectionCode:
            // can now read function bytecode from section.
    }
}

Installation

go get github.com/akupila/go-wasm/...

Notes

This is a experimental, early and definitely not properly tested. There are probably bugs. If you find one, please open an issue!

# Packages

No description provided by the author

# Functions

Parse parses the input to a WASM module.

# Constants

ExtKindFunction is an imported function.
ExtKindGlobal is an imported global.
ExtKindMemory is imported memory.
ExtKindTable is an imported table.

# Structs

A DataSegment is a segment of data in the Data section that is loaded into linear memory.
An ElemSegment is an element segment.
ExportEntry specifies an individual export from the module.
A FunctionBody is the body of a function.
FunctionType the type for a function import.
A FuncType is the description of a function signature.
GlobalType is the type for a global import.
A GlobalVariable is a global variable defined by the module.
ImportEntry describes an individual import to the module.
LocalEntry is a local variable in a function.
LocalName a name mapping for a local function name.
Locals assigns name maps to a subset of functions in the function index space (imports and module-defined).
MemoryType is the type for a memory import.
A Module represents a parsed WASM module.
A NameMap is a map that maps an index to a name.
Naming is a single function naming.
ResizableLimits describes the limits of a table or memory.
SectionCode contains a function body for every function in the module.
SectionCustom is a custom or name section added by the compiler that generated the WASM file.
SectionData declares the initialized data that is loaded into the linear memory.
SectionElement defines element segments that initialize elements of imported or internally-defined tables with any other definition in the module.
SectionExport declares exports from the WASM module.
SectionFunction declares the signatures of all functions in the modules.
SectionGlobal provides an internal definition of global variables.
SectionImport declares all imports defined by the module.
SectionMemory declares a memory section.
SectionName is a custom section that provides debugging information, by matching indices to human readable names.
SectionStart defines the start node, if the module has a start node defined.
SectionTable declares a table section.
SectionType declares all function type definitions used in the module.
TableType is the type for a table import.

# Interfaces

A Section contains all the information for a single section in the WASM file.

# Type aliases

ExternalKind is set as the Kind for an import entry.