land/cmd/land/process.go

140 lines
2.7 KiB
Go
Raw Normal View History

2018-06-07 22:31:01 +00:00
package main
import (
gcontext "context"
2018-06-08 00:08:00 +00:00
"errors"
"io"
2018-06-07 22:31:01 +00:00
"os"
"reflect"
"github.com/Xe/ln"
"github.com/go-interpreter/wagon/exec"
"github.com/go-interpreter/wagon/validate"
"github.com/go-interpreter/wagon/wasm"
)
2018-06-08 00:08:00 +00:00
// Process is a larger level wrapper around a webassembly VM that gives it
// system call access.
2018-06-07 22:31:01 +00:00
type Process struct {
2018-06-08 00:08:00 +00:00
id int32
vm *exec.VM
mod *wasm.Module
2018-06-07 22:31:01 +00:00
}
2018-06-08 00:08:00 +00:00
// NewProcess constructs a new webassembly process based on the input webassembly module as a reader.
func NewProcess(fin io.Reader) (*Process, error) {
p := &Process{}
mod, err := wasm.ReadModule(fin, p.importer)
if err != nil {
return nil, err
}
if mod.Memory == nil {
return nil, errors.New("must declare a memory, sorry :(")
}
vm, err := exec.NewVM(mod)
if err != nil {
return nil, err
2018-06-07 22:31:01 +00:00
}
2018-06-08 00:08:00 +00:00
p.mod = mod
p.vm = vm
return p, nil
2018-06-07 22:31:01 +00:00
}
2018-06-08 00:08:00 +00:00
func (p *Process) importer(name string) (*wasm.Module, error) {
2018-06-07 22:31:01 +00:00
switch name {
case "env":
m := wasm.NewModule()
m.Types = &wasm.SectionTypes{
Entries: []wasm.FunctionSig{
{
Form: 0,
ParamTypes: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI32},
ReturnTypes: []wasm.ValueType{wasm.ValueTypeI32},
},
},
}
m.FunctionIndexSpace = []wasm.Function{
{
Sig: &m.Types.Entries[0],
Host: reflect.ValueOf(p.log),
Body: &wasm.FunctionBody{},
},
}
m.Export = &wasm.SectionExports{
Entries: map[string]wasm.ExportEntry{
"log": {
FieldStr: "log",
Kind: wasm.ExternalFunction,
Index: 0,
},
},
}
return m, nil
default:
f, err := os.Open(name + ".wasm")
if err != nil {
return nil, err
}
defer f.Close()
m, err := wasm.ReadModule(f, nil)
if err != nil {
return nil, err
}
err = validate.VerifyModule(m)
if err != nil {
return nil, err
}
return m, nil
}
}
2018-06-08 00:08:00 +00:00
// ID returns the process ID.
func (p Process) ID() int32 { return p.id }
2018-06-07 22:31:01 +00:00
// VM returns the webassembly VM used to execute code. This isn't thread-safe.
func (p Process) VM() *exec.VM { return p.vm }
func (p *Process) log(ptr int32, len int32) int32 {
2018-06-08 00:08:00 +00:00
mem := p.vm.Memory()
if mem == nil {
panic("memory is nil, wtf")
}
data := mem[ptr : ptr+len]
2018-06-07 22:31:01 +00:00
ln.Log(gcontext.Background(), ln.F{"data": data, "data_string": string(data), "pid": p.id})
return 0
}
2018-06-08 02:02:12 +00:00
func (p *Process) Main() (uint32, error) {
foundMain := false
mainID := uint32(0)
for name, entry := range p.mod.Export.Entries {
if name == "main" && entry.FieldStr == "main" {
mainID = entry.Index
foundMain = true
break
}
}
if !foundMain {
return 1, errors.New("no main function found")
}
returnCode, err := p.vm.ExecCode(int64(mainID))
if err != nil {
return 1, err
}
return returnCode.(uint32), nil
}