Git Product home page Git Product logo

go-wasm's Introduction

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!

go-wasm's People

Contributors

akupila avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

go-wasm's Issues

Parse error - data corrupted

Hi,

I've tried using this library on a couple of wasm files I generated using Tinygo. On one of the files I got this error:

[0x000345] parse section: data corrupted; section id 0x73656374696f6e494428323029 not valid
exit status 1

On a second I received this error:

[0x30d413] parse section: entry 43793: read data section offset initializer: EOF
exit status 1

I used the suggested main on the readme file and it is throwing these errors after the wasm.Parse(f) call. Any ideas on how to troubleshoot this?

Thanks

Parsing a hello-world wasm causes an error

I prepared this Go file

package main

func main() {
        println("Hello, World!")
}

and compiled it as Wasm (GOOS=js GOARCH=wasm go build -o helloworld.wasm main.go). I found the error when passing this wasm file to Parse.

[0x1321e3] parse section: entry 16715: read data section offset initializer: EOF

My Go version is 1.14.

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.