Skip to content
This repository has been archived by the owner on May 11, 2020. It is now read-only.

Warn when modules are missing before attempting disassembly #90

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion cmd/wasm-dump/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,42 @@ func process(w io.Writer, fname string) {
}
defer f.Close()

m, err := wasm.ReadModule(f, nil)
// Decode the module in order to fake its imports as exports
mod, err := wasm.DecodeModule(f)
if err != nil {
log.Fatalf("Could not read module: %v", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/Could/could/

}

resolver := func(name string) (*wasm.Module, error) {
m := wasm.NewModule()
m.Types = mod.Types
m.Export = &wasm.SectionExports{
Entries: make(map[string]wasm.ExportEntry),
}
m.FunctionIndexSpace = []wasm.Function{}
for index, imp := range mod.Import.Entries {
fmt.Println(imp.ModuleName, imp.FieldName, imp.Type)
if imp.ModuleName == name {
m.Export.Entries[imp.FieldName] = wasm.ExportEntry{
FieldStr: name,
Kind: imp.Type.Kind(),
Index: uint32(index),
}
if imp.Type.Kind() == wasm.ExternalFunction {
m.FunctionIndexSpace = append(m.FunctionIndexSpace, wasm.Function{
Body: &wasm.FunctionBody{},
})
}
}
}
return m, nil
}

// We already read the file without trying to resolve imports,
// start over in order to do that work now.
f.Seek(0, os.SEEK_SET)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/os.SEEK_SET/io.SeekStart/


m, err := wasm.ReadModule(f, resolver)
if err != nil {
log.Fatalf("could not read module: %v", err)
}
Expand Down
9 changes: 9 additions & 0 deletions disasm/disasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"math"

Expand Down Expand Up @@ -97,6 +98,14 @@ func NewDisassembly(fn wasm.Function, module *wasm.Module) (*Disassembly, error)
}
disas := &Disassembly{}

if module.Import != nil {
ninternal, _ := leb128.ReadVarint32(bytes.NewReader(module.Code.Bytes))
nexternal := len(module.FunctionIndexSpace) - int(ninternal)
if nexternal != len(module.Import.Entries) {
panic(fmt.Sprintf("Attempting to disassemble a module that is missing imports %d != %d", nexternal, len(module.Import.Entries)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/fmt.Sprintf/fmt.Errorf/

}
}

// A stack of int arrays holding indices to instructions that make the stack
// polymorphic. Each block has its corresponding array. We start with one
// array for the root stack
Expand Down